LayerBuilder { protected String notifyLayerListeners(ILayerListener.EventType type, HttpServletRequest request, IModule module) throws IOException { StringBuffer sb = new StringBuffer(); List<IServiceReference> refs = null; if(aggr.getPlatformServices() != null){ try { IServiceReference[] ary = aggr.getPlatformServices().getServiceReferences(ILayerListener.class.getName(), "(name="+aggr.getName()+")"); if (ary != null) refs = Arrays.asList(ary); } catch (PlatformServicesException e) { if (log.isLoggable(Level.SEVERE)) { log.log(Level.SEVERE, e.getMessage(), e); } } if (refs != null) { if (type == EventType.END_LAYER) { refs = new ArrayList<IServiceReference>(refs); Collections.reverse(refs); } for (IServiceReference ref : refs) { ILayerListener listener = (ILayerListener)aggr.getPlatformServices().getService(ref); try { Set<String> depFeatures = new HashSet<String>(); String str = listener.layerBeginEndNotifier(type, request, type == ILayerListener.EventType.BEGIN_MODULE ? Arrays.asList(new IModule[]{module}) : umLayerListenerModuleList, depFeatures); dependentFeatures.addAll(depFeatures); if (str != null) { sb.append(str); } } finally { aggr.getPlatformServices().ungetService(ref); } } } } return sb.toString(); } LayerBuilder(HttpServletRequest request, List<ICacheKeyGenerator> keyGens,
			ModuleList moduleList);   }
@Test public void testNotifyLayerListeners() throws Exception { IAggregator mockAggregator = TestUtils.createMockAggregator(); final IPlatformServices mockPlatformServices = createMock(IPlatformServices.class); HttpServletRequest mockRequest = TestUtils.createMockRequest(mockAggregator); IServiceReference mockServiceRef1 = createMock(IServiceReference.class), mockServiceRef2 = createMock(IServiceReference.class); IServiceReference[] serviceReferences = new IServiceReference[]{mockServiceRef1, mockServiceRef2}; final String[] listener1Result = new String[1], listener2Result = new String[1]; final List<IModule> expectedModuleList = new ArrayList<IModule>(); final Set<String> dependentFeatures1 = new HashSet<String>(), dependentFeatures2 = new HashSet<String>(); final List<String> checkpoints = new ArrayList<String>(); ModuleList moduleList = new ModuleList(); IModule module1 = new ModuleImpl("module1", new URI("file: module2 = new ModuleImpl("module2", new URI("file: moduleList.add(new ModuleListEntry(module1, ModuleSpecifier.MODULES)); moduleList.add(new ModuleListEntry(module2, ModuleSpecifier.LAYER)); ILayerListener testListener1 = new ILayerListener() { @Override public String layerBeginEndNotifier(EventType type, HttpServletRequest request, List<IModule> modules, Set<String> dependentFeatures) { Assert.assertEquals(expectedModuleList, modules); dependentFeatures.addAll(dependentFeatures1); checkpoints.add("Listener1_" + type.toString()); return listener1Result[0]; } }, testListener2 = new ILayerListener() { @Override public String layerBeginEndNotifier(EventType type, HttpServletRequest request, List<IModule> modules, Set<String> dependentFeatures) { Assert.assertEquals(expectedModuleList, modules); dependentFeatures.addAll(dependentFeatures2); checkpoints.add("Listener2_" + type.toString()); return listener2Result[0]; } }; expect(mockPlatformServices.getServiceReferences(ILayerListener.class.getName(), "(name=test)")).andReturn(serviceReferences).anyTimes(); expect(mockPlatformServices.getService(mockServiceRef1)).andReturn(testListener1); expect(mockPlatformServices.getService(mockServiceRef2)).andReturn(testListener2); expect(mockPlatformServices.ungetService(mockServiceRef1)).andReturn(true); expect(mockPlatformServices.ungetService(mockServiceRef2)).andReturn(true); expect(mockAggregator.getPlatformServices()).andReturn(mockPlatformServices).anyTimes(); replay(mockAggregator, mockRequest, mockServiceRef1, mockServiceRef2, mockPlatformServices); LayerBuilder builder = new LayerBuilder(mockRequest, null, moduleList) { @Override public String notifyLayerListeners(ILayerListener.EventType type, HttpServletRequest request, IModule module) throws IOException { return super.notifyLayerListeners(type, request, module); } }; listener1Result[0] = "foo"; listener2Result[0] = "bar"; expectedModuleList.addAll(moduleList.getModules()); String result = builder.notifyLayerListeners(EventType.BEGIN_LAYER, mockRequest, module1); Assert.assertEquals("foobar", result); Assert.assertEquals(0, moduleList.getDependentFeatures().size()); Assert.assertEquals(Arrays.asList(new String[]{"Listener1_BEGIN_LAYER", "Listener2_BEGIN_LAYER"}), checkpoints); reset(mockPlatformServices); expect(mockPlatformServices.getServiceReferences(ILayerListener.class.getName(), "(name=test)")).andReturn(serviceReferences).anyTimes(); expect(mockPlatformServices.getService(mockServiceRef1)).andReturn(testListener1); expect(mockPlatformServices.getService(mockServiceRef2)).andReturn(testListener2); expect(mockPlatformServices.ungetService(mockServiceRef1)).andReturn(true); expect(mockPlatformServices.ungetService(mockServiceRef2)).andReturn(true); replay(mockPlatformServices); listener2Result[0] = null; checkpoints.clear(); result = builder.notifyLayerListeners(EventType.END_LAYER, mockRequest, module1); Assert.assertEquals("foo", result); Assert.assertEquals(0, moduleList.getDependentFeatures().size()); Assert.assertEquals(Arrays.asList(new String[]{"Listener2_END_LAYER", "Listener1_END_LAYER"}), checkpoints); reset(mockPlatformServices); expect(mockPlatformServices.getServiceReferences(ILayerListener.class.getName(), "(name=test)")).andReturn(serviceReferences).anyTimes(); expect(mockPlatformServices.getService(mockServiceRef1)).andReturn(testListener1); expect(mockPlatformServices.getService(mockServiceRef2)).andReturn(testListener2); expect(mockPlatformServices.ungetService(mockServiceRef1)).andReturn(true); expect(mockPlatformServices.ungetService(mockServiceRef2)).andReturn(true); replay(mockPlatformServices); listener1Result[0] = null; checkpoints.clear(); result = builder.notifyLayerListeners(EventType.END_LAYER, mockRequest, module1); Assert.assertEquals("", result); Assert.assertEquals(0, moduleList.getDependentFeatures().size()); Assert.assertEquals(Arrays.asList(new String[]{"Listener2_END_LAYER", "Listener1_END_LAYER"}), checkpoints); reset(mockPlatformServices); expect(mockPlatformServices.getServiceReferences(ILayerListener.class.getName(), "(name=test)")).andReturn(serviceReferences).anyTimes(); expect(mockPlatformServices.getService(mockServiceRef1)).andReturn(testListener1); expect(mockPlatformServices.getService(mockServiceRef2)).andReturn(testListener2); expect(mockPlatformServices.ungetService(mockServiceRef1)).andReturn(true); expect(mockPlatformServices.ungetService(mockServiceRef2)).andReturn(true); replay(mockPlatformServices); listener1Result[0] = "foo"; listener2Result[0] = "bar"; expectedModuleList.clear(); expectedModuleList.add(module2); checkpoints.clear(); result = builder.notifyLayerListeners(EventType.BEGIN_MODULE, mockRequest, module2); Assert.assertEquals("foobar", result); Assert.assertEquals(0, moduleList.getDependentFeatures().size()); Assert.assertEquals(Arrays.asList(new String[]{"Listener1_BEGIN_MODULE", "Listener2_BEGIN_MODULE"}), checkpoints); reset(mockPlatformServices); expect(mockPlatformServices.getServiceReferences(ILayerListener.class.getName(), "(name=test)")).andReturn(serviceReferences).anyTimes(); expect(mockPlatformServices.getService(mockServiceRef1)).andReturn(testListener1); expect(mockPlatformServices.ungetService(mockServiceRef1)).andReturn(true); replay(mockPlatformServices); try { result = builder.notifyLayerListeners(EventType.BEGIN_MODULE, mockRequest, module1); Assert.fail(); } catch (AssertionError e) { } reset(mockPlatformServices); expect(mockPlatformServices.getServiceReferences(ILayerListener.class.getName(), "(name=test)")).andReturn(serviceReferences).anyTimes(); expect(mockPlatformServices.getService(mockServiceRef1)).andReturn(testListener1); expect(mockPlatformServices.getService(mockServiceRef2)).andReturn(testListener2); expect(mockPlatformServices.ungetService(mockServiceRef1)).andReturn(true); expect(mockPlatformServices.ungetService(mockServiceRef2)).andReturn(true); replay(mockPlatformServices); dependentFeatures1.add("feature1"); dependentFeatures2.add("feature2"); checkpoints.clear(); result = builder.notifyLayerListeners(EventType.BEGIN_MODULE, mockRequest, module2); Assert.assertEquals("foobar", result); Assert.assertEquals(new HashSet<String>(Arrays.asList(new String[]{"feature1", "feature2"})), builder.getDepenedentFeatures()); Assert.assertEquals(Arrays.asList(new String[]{"Listener1_BEGIN_MODULE", "Listener2_BEGIN_MODULE"}), checkpoints); }