LocaleDefinitionsFactory extends
        UnresolvingLocaleDefinitionsFactory { @Override public Definition getDefinition(String name, Request tilesContext) { Definition retValue; Locale locale = null; if (tilesContext != null) { locale = localeResolver.resolveLocale(tilesContext); } retValue = definitionDao.getDefinition(name, locale); if (retValue != null) { retValue = new Definition(retValue); String parentDefinitionName = retValue.getExtends(); while (parentDefinitionName != null) { Definition parent = definitionDao.getDefinition( parentDefinitionName, locale); if (parent == null) { throw new NoSuchDefinitionException("Cannot find definition '" + parentDefinitionName + "' ancestor of '" + retValue.getName() + "'"); } retValue.inherit(parent); parentDefinitionName = parent.getExtends(); } } return retValue; }  @Override Definition getDefinition(String name,
            Request tilesContext);  }
@SuppressWarnings("unchecked") @Test public void testGetDefinition() { DefinitionDAO<Locale> dao = createMock(DefinitionDAO.class); LocaleResolver localeResolver = createMock(LocaleResolver.class); Request request = createMock(Request.class); Attribute templateAttribute = Attribute.createTemplateAttribute("/mytemplate.jsp"); Definition definition = new Definition("myDefinition", null, null); definition.setExtends("anotherDefinition"); Map<String, Attribute> attributes = new HashMap<String, Attribute>(); attributes.put("first", new Attribute("myValue")); Definition anotherDefinition = new Definition("anotherDefinition", templateAttribute, attributes); Locale locale = Locale.ITALY; expect(localeResolver.resolveLocale(request)).andReturn(locale); expect(dao.getDefinition("myDefinition", locale)).andReturn(definition); expect(dao.getDefinition("anotherDefinition", locale)).andReturn(anotherDefinition); LocaleDefinitionsFactory factory = new LocaleDefinitionsFactory(); replay(dao, localeResolver, request); factory.setDefinitionDAO(dao); factory.setLocaleResolver(localeResolver); Definition realDefinition = new Definition(definition); realDefinition.inherit(anotherDefinition); assertEquals(realDefinition, factory.getDefinition("myDefinition", request)); verify(dao, localeResolver, request); }
@SuppressWarnings("unchecked") @Test(expected = NoSuchDefinitionException.class) public void testGetDefinitionNoParent() { DefinitionDAO<Locale> dao = createMock(DefinitionDAO.class); LocaleResolver localeResolver = createMock(LocaleResolver.class); Request request = createMock(Request.class); Definition definition = new Definition("myDefinition", null, null); definition.setExtends("anotherDefinition"); Map<String, Attribute> attributes = new HashMap<String, Attribute>(); attributes.put("first", new Attribute("myValue")); Locale locale = Locale.ITALY; expect(localeResolver.resolveLocale(request)).andReturn(locale); expect(dao.getDefinition("myDefinition", locale)).andReturn(definition); expect(dao.getDefinition("anotherDefinition", locale)).andReturn(null); LocaleDefinitionsFactory factory = new LocaleDefinitionsFactory(); replay(dao, localeResolver, request); try { factory.setDefinitionDAO(dao); factory.setLocaleResolver(localeResolver); factory.getDefinition("myDefinition", request); } finally { verify(dao, localeResolver, request); } }