Skip to content

Commit

Permalink
GH-156 fix NPE for corner case + docs
Browse files Browse the repository at this point in the history
  • Loading branch information
npeltier committed Sep 25, 2020
1 parent 839a9ba commit a4c9ef9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -58,9 +59,10 @@ public class AttributeServiceImpl extends AbstractWorkerManager<AttributeWorker>
if (worker != null) {
logger.debug("get attributes from worker {}", worker.getKey());
Map<String, String> workerMap = worker.getAttributes(request);
if (attributes == null) {
attributes = workerMap;
} else {
if (workerMap != null){
if (attributes == null) {
attributes = new HashMap<>();
}
attributes.putAll(workerMap);
}
}
Expand All @@ -76,10 +78,12 @@ public class AttributeServiceImpl extends AbstractWorkerManager<AttributeWorker>
if (worker != null) {
logger.debug("get classes from worker {}", worker.getKey());
Collection<String> workerClasses = worker.getClasses(request);
if (classes == null) {
classes = new ArrayList<>();
if (workerClasses != null) {
if (classes == null) {
classes = new ArrayList<>();
}
classes.addAll(workerClasses);
}
classes.addAll(workerClasses);
}
}
return classes != null ? String.join(SPACE, classes) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public String getKey() {
void setup() {
List<Breakpoint> breakpoints = ResponsivePropertiesImplTest.initResponsiveConfiguration(context);
String someComp = CONTENT_ROOT + "/comp";
final String[] array = new String[] {"worker1", "worker2"};
final String[] array = new String[] {"worker1", "worker2", "workerNull"};
context.build().resource(CONFIG_ROOTS + "/apps/foo/bar", "attributeWorkers", array);
context.build().resource(someComp, "sling:resourceType", "foo/bar");
context.currentResource(someComp);
Expand Down Expand Up @@ -114,4 +114,26 @@ public void testTwoWorker() {
assertTrue(CollectionUtils.isEqualCollection(Arrays.asList("11", "12", "21", "22"), service.getAttributes(context.request()).keySet()));
assertEquals("c1 c2", service.getClassesString(context.request()));
}

@Test
public void testNullWorker() {
service.bindWorker(new AttributeWorker() {
@Override
public Map<String, String> getAttributes(SlingHttpServletRequest request) {
return null;
}

@Override
public Collection<String> getClasses(SlingHttpServletRequest request) {
return null;
}

@Override
public String getKey() {
return "workerNull";
}
});
assertNull(service.getClassesString(context.request()));
assertNull(service.getAttributes(context.request()));
}
}

0 comments on commit a4c9ef9

Please sign in to comment.