Commit f9be5dd3 authored by Mathijs den Burger's avatar Mathijs den Burger Committed by Jeroen Hoffman

CMS-11137 [Back port to 11.2] CMS-10840 Simplify FieldPluginHelper#getCaptionModel

- use the 'config' field directly

- use the 'field' field directly instead of using the getter just for
  the sake of being able to override it in a unit test. That change does
  require the use of Powermock and Whitebox to be able to set a dummy
  field in unit tests, but makes the actual code more readable.

- use StringUtils.isEmpty to simplify the check in the else-branch,
  and reuse the captionKey variable.

(cherry picked from commit c79f4534)
parent a399adb0
......@@ -91,6 +91,18 @@
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.onehippo.cms7</groupId>
<artifactId>hippo-cms-test</artifactId>
......
......@@ -164,9 +164,9 @@ public class FieldPluginHelper implements IDetachable {
public IModel<String> getCaptionModel(final Component component, final String defaultCaption) {
String caption = (defaultCaption != null) ? defaultCaption : getPluginConfig().getString("caption");
String caption = (defaultCaption != null) ? defaultCaption : config.getString("caption");
String captionKey;
final IFieldDescriptor field = getField();
if (field != null) {
captionKey = field.getPath();
final String translation = getStringFromBundle(captionKey);
......@@ -179,14 +179,12 @@ public class FieldPluginHelper implements IDetachable {
caption = StringUtils.capitalize(captionKey);
}
} else {
final String key = getPluginConfig().getString("captionKey");
if ((key != null) && !key.isEmpty()) {
captionKey = key;
captionKey = config.getString("captionKey");
if (StringUtils.isNotEmpty(captionKey)) {
final String translation = getStringFromBundle(captionKey);
if (translation != null) {
return Model.of(translation);
}
if (caption == null) {
caption = StringUtils.capitalize(captionKey);
}
......@@ -208,7 +206,6 @@ public class FieldPluginHelper implements IDetachable {
}
public IModel<String> getHintModel(final Component component) {
final IFieldDescriptor field = getField();
if (field == null) {
return null;
}
......@@ -245,5 +242,4 @@ public class FieldPluginHelper implements IDetachable {
private String getBundleName() {
return (documentType == null) ? null : HIPPO_TYPES + "." + documentType.getName();
}
}
......@@ -22,14 +22,21 @@ import org.hippoecm.frontend.editor.ITemplateEngine;
import org.hippoecm.frontend.plugin.IPluginContext;
import org.hippoecm.frontend.plugin.config.IPluginConfig;
import org.hippoecm.frontend.plugin.config.impl.JavaPluginConfig;
import org.hippoecm.frontend.types.IFieldDescriptor;
import org.hippoecm.frontend.types.JavaFieldDescriptor;
import org.hippoecm.frontend.types.JavaTypeDescriptor;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import static org.junit.Assert.assertEquals;
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.management.*"})
@PrepareForTest({FieldPluginHelper.class})
public class FieldPluginHelperTest {
private static final String BUNDLE_FIELD_CAPTION_KEY_VALUE = "Bundle Field Caption Key Value";
......@@ -82,15 +89,12 @@ public class FieldPluginHelperTest {
@Test
public void testGetCaptionModelWithFieldFromBundle() {
final IPluginConfig pluginConfig = new JavaPluginConfig();
pluginConfig.put(AbstractFieldPlugin.FIELD, "testfield");
final FieldPluginHelper helper = new NoServiceFieldPluginHelper(null, pluginConfig) {
public IFieldDescriptor getField() {
return new JavaFieldDescriptor("testns", new JavaTypeDescriptor("testfield","String", null));
}
};
final FieldPluginHelper helper = new NoServiceFieldPluginHelper(null, pluginConfig);
final JavaFieldDescriptor field = new JavaFieldDescriptor("testns", new JavaTypeDescriptor("testfield", "String", null));
Whitebox.setInternalState(helper, field);
// caption taken from bundle base on field path
final IModel<String> caption = helper.getCaptionModel(page.getComponent());
......@@ -99,17 +103,14 @@ public class FieldPluginHelperTest {
@Test
public void testGetCaptionModelWithFieldNoBundle() {
final IPluginConfig pluginConfig = new JavaPluginConfig();
pluginConfig.put(AbstractFieldPlugin.FIELD, "testFieldNoBundle");
final FieldPluginHelper helper = new NoServiceFieldPluginHelper(null, pluginConfig) {
public IFieldDescriptor getField() {
return new JavaFieldDescriptor("testns", new JavaTypeDescriptor("testFieldNoBundle","String", null));
}
};
final FieldPluginHelper helper = new NoServiceFieldPluginHelper(null, pluginConfig);
final JavaFieldDescriptor field = new JavaFieldDescriptor("testns", new JavaTypeDescriptor("testFieldNoBundle","String", null));
Whitebox.setInternalState(helper, field);
// no config: capitalize lowercased field name
// no config: capitalize lowercase field name
IModel<String> caption = helper.getCaptionModel(page.getComponent());
assertEquals("Testfieldnobundle", caption.getObject());
......@@ -133,8 +134,8 @@ public class FieldPluginHelperTest {
return null;
}
@Override
String getStringFromBundle(final String key) {
if ("configuredCaptionKey".equals(key)) {
return BUNDLE_CAPTION_KEY_VALUE;
} else if ("testns:testfield".equals(key)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment