Skip to content

Commit

Permalink
Merge f603c8e into 1de850a
Browse files Browse the repository at this point in the history
  • Loading branch information
badvision committed May 16, 2019
2 parents 1de850a + f603c8e commit a31202e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ private static Class getInstantiatableListType(Class<?> type) {
public static Map<String, FieldComponent> getFormFields(Class source, SlingScriptHelper sling) {
return FieldUtils.getFieldsListWithAnnotation(source, FormField.class)
.stream()
.sorted(AnnotatedFieldDeserializer::superclassFieldsFirst)
.collect(Collectors.toMap(Field::getName, f -> {
FormField fieldDefinition = f.getAnnotation(FormField.class);
FieldComponent component;
Expand All @@ -197,6 +198,16 @@ public static Map<String, FieldComponent> getFormFields(Class source, SlingScrip
}, (a, b) -> a, LinkedHashMap::new));
}

private static int superclassFieldsFirst(Field a, Field b) {
if (a.getDeclaringClass() == b.getDeclaringClass()) {
return 0;
} else if (a.getDeclaringClass().isAssignableFrom(b.getDeclaringClass())) {
return -1;
} else {
return 1;
}
}

private AnnotatedFieldDeserializer() {
// Utility class has no constructor
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package com.adobe.acs.commons.mcp.form;

import java.util.Iterator;
import java.util.List;
import org.apache.sling.api.resource.Resource;
import org.junit.Before;
Expand Down Expand Up @@ -116,6 +117,31 @@ public void testClientLibraryHandling() {
assert(testPojo.getCssClientLibraries().contains("component-css"));
}

@Test
public void testSubclassBehaviors() {
TestInherited subclass = new TestInherited();
subclass.init();
subclass.getFieldComponents();
Resource form = subclass.getFormResource();
// Check for correct number of categories
AbstractResourceImpl tabs = (AbstractResourceImpl) form.getChild("items/tabs/items");
assertEquals("Should have 4 tabs", 4, tabs.children.size());
// Assert correct number and order of tabs
Iterator<Resource> children = tabs.listChildren();
assertEquals("Tab 1 should be first", "1", children.next().getValueMap().get("jcr:title"));
assertEquals("Tab 2 should be second", "2", children.next().getValueMap().get("jcr:title"));
assertEquals("Tab 3 should be third", "3", children.next().getValueMap().get("jcr:title"));
assertEquals("Tab 4 should be fourth", "4", children.next().getValueMap().get("jcr:title"));
// Check if somethingElse is last
AbstractResourceImpl tab3 = (AbstractResourceImpl) tabs.children.get(2).getChild("items");
assertEquals("readOnly should be first", "readOnly", tab3.children.get(0).getName());
assertEquals("tags should be second", "tags", tab3.children.get(1).getName());
assertEquals("somethingElse should be last", "somethingElse", tab3.children.get(2).getName());
// Check if additionalTestArea is last
AbstractResourceImpl tab4 = (AbstractResourceImpl) tabs.children.get(3).getChild("items");
assertEquals("additionalTextArea should be last", "additionalTextArea", tab4.children.get(1).getName());
}

public static class ComponentWithClientLibraries extends FieldComponent {
@Override
public void init() {
Expand Down Expand Up @@ -168,4 +194,12 @@ public static class TestSubtype {
@FormField(component = ComponentWithClientLibraries.class, name = "Component with client libs")
String subField6;
}

public static class TestInherited extends TestPojo {
@FormField(component = TextareaComponent.class, name = "Something Else", category="3")
String somethingElse;

@FormField(component = TextareaComponent.class, name = "Text Area", category="4")
String additionalTextArea;
}
}

0 comments on commit a31202e

Please sign in to comment.