TemplateManager build() throws TemplateManagementServiceConfigurationException { try { MutableTemplateLibrary newLibrary=new MutableTemplateLibrary(); for(Class<?> handlerClass:handlerClasses) { if(!newLibrary.isHandlerRegistered(handlerClass)) { newLibrary.registerHandler(handlerClass); } } Builder<HandlerId, ResourceHandler> builder = ImmutableMap.<HandlerId, ResourceHandler>builder(); newLibrary.accept(new HandlerMapBuilder(builder,this.handlers)); return new TemplateManager(new ImmutableTemplateLibrary(newLibrary), builder.build()); } catch (TemplateCreationException e) { throw new TemplateManagementServiceConfigurationException(e); } catch (ResourceHandlerInstantiationException e) { throw new TemplateManagementServiceConfigurationException(e); } }
private ResourceTemplate loadTemplates(Class<? extends ResourceHandler> targetClass, TemplateLoaderContext resolver) { ResourceTemplate template=createTemplate(targetClass,resolver); resolver.register(targetClass,template); return template; }
boolean isHandlerRegistered(Class<?> handlerClass) { return findByHandler(toResourceHandlerClass(handlerClass))!=null; }
ResourceTemplate registerHandler(Class<?> targetClass) { Class<? extends ResourceHandler> handlerClass=toResourceHandlerClass(targetClass); checkArgument(!this.context.isRegistered(handlerClass),"Handler '%s' is already registered",handlerClass.getCanonicalName()); return loadTemplates(handlerClass,this.context); }
@Override protected TemplateLibrary getLibrary() { MutableTemplateLibrary library=new MutableTemplateLibrary(); library.registerHandler(PersonContainerHandler.class); return library; }
@Test public void testRegistareHandlersWithClashingIds() throws Exception { this.sut.registerHandler(Fixture.Valid.ResourceHC.Handler.class); try { this.sut.registerHandler(Fixture.Valid.ContainerHC.Basic.Handler.class); fail("Should not allow registering handlers with clashing ids"); } catch (Exception e) { logFailure(e); } try { this.sut.registerHandler(Fixture.Valid.ContainerHC.Direct.Handler.class); fail("Should not allow registering handlers with clashing ids"); } catch (Exception e) { logFailure(e); } try { this.sut.registerHandler(Fixture.Valid.ContainerHC.Indirect.Handler.class); fail("Should not allow registering handlers with clashing ids"); } catch (Exception e) { logFailure(e); } }
@Test public void testInstrospection() { MutableTemplateLibrary library=new MutableTemplateLibrary(); library.registerHandler(PersonContainerHandler.class); library.accept( new TemplateVisitor() {
@Before public void setUp() { this.sut=new MutableTemplateLibrary(); }
@Override public boolean contains(ResourceTemplate template) { checkNotNull(template,"Template cannot be null"); return findByHandler(template.handlerClass())!=null; }
@Test public void testRegisterContainerHandlerWithMultipleContainerAnnotations() throws Exception { try { this.sut.registerHandler(Fixture.Invalid.MultipleContainerAnnotations.class); fail("Should not allow registering container handlers with multiple container annotations"); } catch (Exception e) { logFailure(e); } }
@Override protected TemplateLibrary getLibrary() { MutableTemplateLibrary library= new MutableTemplateLibrary(); library.registerHandler(PersonContainerHandler.class); return new ImmutableTemplateLibrary(library); }
@Test public void testBadMembershipPredicate$directContainer() throws Exception { try { this.sut.registerHandler(Fixture.Invalid.ContainerHC.Direct.BadMembershipPredicate.class); fail("Should not allow registering container handlers annotated as direct containers with bad membership predicate"); } catch (Exception e) { logFailure(e); } }
@Test public void testNullInsertedContentRelation() throws Exception { try { this.sut.registerHandler(Fixture.Invalid.ContainerHC.Indirect.NullInsertedContentRelation.class); fail("Should not allow registering container handlers annotated as indirect containers with null inserted content relation"); } catch (Exception e) { logFailure(e); } }
@Test public void testBadInsertedContentRelation() throws Exception { try { this.sut.registerHandler(Fixture.Invalid.ContainerHC.Indirect.BadInsertedContentRelation.class); fail("Should not allow registering container handlers annotated as indirect containers with bad inserted content relation"); } catch (Exception e) { logFailure(e); } }
@Test public void testRegisterUnannotatedResourceHandler$unknownAnnotations() throws Exception { try { this.sut.registerHandler(Fixture.Invalid.ResourceHC.AnnotatedWithUnsupportedAnnotation.class); fail("Should not allow registering resource handlers without a know annotation"); } catch (Exception e) { logFailure(e); } }
@Test public void testRegisterContainerWithUnannotatedMemberHandler$indirectContainer() throws Exception { try { this.sut.registerHandler(Fixture.Invalid.ContainerHC.Indirect.UnnanotatedMemberResourceHandler.class); fail("Should not allow registering container handlers annotated as indirect containers with unnanotated member handlers"); } catch (Exception e) { logFailure(e); } }
@Test public void testRegisterContainerHandlerWithMixedTypeAnnotations() throws Exception { try { this.sut.registerHandler(Fixture.Invalid.MixedTypeAnnotations.class); fail("Should not allow registering container handlers with multiple mixed annotations"); } catch (Exception e) { logFailure(e); } }
@Test public void testEmptyId$basicContainer() throws Exception { try { this.sut.registerHandler(Fixture.Invalid.ContainerHC.Basic.EmptyID.class); fail("Should not allow registering container handlers annotated as basic containers with no id"); } catch (Exception e) { logFailure(e); } }
@Test public void testRelativeMembershipPredicate$directContainer() throws Exception { try { this.sut.registerHandler(Fixture.Invalid.ContainerHC.Direct.RelativeMembershipPredicate.class); fail("Should not allow registering container handlers annotated as direct containers with relative membership predicate"); } catch (Exception e) { logFailure(e); } }
@Test public void testEmptyInsertedContentRelation() throws Exception { try { this.sut.registerHandler(Fixture.Invalid.ContainerHC.Indirect.EmptyInsertedContentRelation.class); fail("Should not allow registering container handlers annotated as indirect containers with empty inserted content relation"); } catch (Exception e) { logFailure(e); } }