MetaDataLocatorImpl implements MetaDataLocator { public <T> T findMeta(String key, final ComponentResources resources, Class<T> expectedType) { String value = getSymbolExpandedValueFromCache(key, resources.getCompleteId() + "/" + key, new ValueLocator() { public String valueForKey(String key) { return locate(key, resources); } }); return typeCoercer.coerce(value, expectedType); } MetaDataLocatorImpl(SymbolSource symbolSource, TypeCoercer typeCoercer,
                               ComponentModelSource modelSource, Map<String, String> configuration); @PostInjection void setupInvalidation(@ComponentClasses InvalidationEventHub invalidationEventHub); T findMeta(String key, final ComponentResources resources, Class<T> expectedType); T findMeta(String key, final String pageName, Class<T> expectedType);  }
@Test public void not_found_by_page_name_but_found_in_configuration() { ComponentModel model = mockComponentModel(); SymbolSource symbolSource = mockSymbolSource(); ComponentModelSource modelSource = mockComponentModelSource(); String key = "foo.bar"; String value = "zaphod"; String pageName = "gnip/Gnop"; expect(modelSource.getPageModel(pageName)).andReturn(model); train_getMeta(model, key, null); train_expandSymbols(symbolSource, value, "*expanded*"); replay(); Map<String, String> configuration = CollectionFactory.newMap(); configuration.put("gnip:foo.bar", value); MetaDataLocator locator = new MetaDataLocatorImpl(symbolSource, typeCoercer, modelSource, configuration); assertSame(locator.findMeta(key, pageName, String.class), "*expanded*"); verify(); }
@Test public void found_in_container() { ComponentResources resources = mockComponentResources(); ComponentResources containerResources = mockComponentResources(); ComponentModel model = mockComponentModel(); ComponentModel containerModel = mockComponentModel(); SymbolSource symbolSource = mockSymbolSource(); ComponentModelSource modelSource = mockComponentModelSource(); String key = "foo.bar"; String value = "zaphod"; String completeId = "foo.Bar:baz"; train_getCompleteId(resources, completeId); train_getComponentModel(resources, model); train_getMeta(model, key, null); train_getContainerResources(resources, containerResources); train_getComponentModel(containerResources, containerModel); train_getMeta(containerModel, key, value); train_expandSymbols(symbolSource, value, value); replay(); Map<String, String> configuration = Collections.emptyMap(); MetaDataLocator locator = new MetaDataLocatorImpl(symbolSource, typeCoercer, modelSource, configuration); assertSame(locator.findMeta(key, resources, String.class), value); verify(); }
@Test public void found_via_high_level_default() { ComponentResources resources = mockComponentResources(); ComponentModel model = mockComponentModel(); SymbolSource symbolSource = mockSymbolSource(); ComponentModelSource modelSource = mockComponentModelSource(); String key = "foo.bar"; String value = "zaphod"; String completeId = "Bar"; String logicalPageName = completeId; train_getCompleteId(resources, completeId); train_getComponentModel(resources, model); train_getMeta(model, key, null); train_getContainerResources(resources, null); train_getPageName(resources, logicalPageName); train_expandSymbols(symbolSource, value, value); replay(); Map<String, String> configuration = newMap(); configuration.put(key, value); MetaDataLocator locator = new MetaDataLocatorImpl(symbolSource, typeCoercer, modelSource, configuration); assertSame(locator.findMeta(key, resources, String.class), value); verify(); train_getCompleteId(resources, completeId); replay(); assertSame(locator.findMeta(key, resources, String.class), value); verify(); }
@Test public void default_matching_is_case_insensitive() { ComponentResources resources = mockComponentResources(); ComponentModel model = mockComponentModel(); SymbolSource symbolSource = mockSymbolSource(); ComponentModelSource modelSource = mockComponentModelSource(); String key = "foo.bar"; String value = "zaphod"; String completeId = "foo.Bar"; train_getCompleteId(resources, completeId); train_getComponentModel(resources, model); train_getMeta(model, key, null); train_getContainerResources(resources, null); train_getPageName(resources, "foo/Bar"); train_expandSymbols(symbolSource, value, value); replay(); Map<String, String> configuration = newMap(); configuration.put(key.toUpperCase(), value); MetaDataLocator locator = new MetaDataLocatorImpl(symbolSource, typeCoercer, modelSource, configuration); assertSame(locator.findMeta(key, resources, String.class), value); verify(); train_getCompleteId(resources, completeId); replay(); assertSame(locator.findMeta(key, resources, String.class), value); verify(); }
@Test public void subfolder_default_overrides_high_level_default() { ComponentResources resources = mockComponentResources(); ComponentModel model = mockComponentModel(); SymbolSource symbolSource = mockSymbolSource(); ComponentModelSource modelSource = mockComponentModelSource(); String key = "foo.bar"; String value = "zaphod"; String completeId = "foo.Bar"; train_getCompleteId(resources, completeId); train_getComponentModel(resources, model); train_getMeta(model, key, null); train_getContainerResources(resources, null); train_getPageName(resources, "foo/Bar"); train_expandSymbols(symbolSource, value, value); replay(); Map<String, String> configuration = newMap(); configuration.put(key, "xxx"); configuration.put("foo:" + key, value); MetaDataLocator locator = new MetaDataLocatorImpl(symbolSource, typeCoercer, modelSource, configuration); assertSame(locator.findMeta(key, resources, String.class), value); verify(); train_getCompleteId(resources, completeId); replay(); assertSame(locator.findMeta(key, resources, String.class), value); verify(); }
@Test public void train_symbols_expanded_and_types_coerced() { ComponentResources resources = mockComponentResources(); ComponentModel model = mockComponentModel(); SymbolSource symbolSource = mockSymbolSource(); ComponentModelSource modelSource = mockComponentModelSource(); String key = "foo.bar"; String value = "${zaphod}"; String expandedValue = "99"; String completeId = "foo.Bar:baz"; train_getCompleteId(resources, completeId); train_getComponentModel(resources, model); train_getMeta(model, key, value); train_expandSymbols(symbolSource, value, expandedValue); replay(); Map<String, String> configuration = Collections.emptyMap(); MetaDataLocator locator = new MetaDataLocatorImpl(symbolSource, typeCoercer, modelSource, configuration); assertEquals(locator.findMeta(key, resources, Integer.class), new Integer(99)); verify(); }
@Test public void found_in_component() { ComponentResources resources = mockComponentResources(); ComponentModel model = mockComponentModel(); SymbolSource symbolSource = mockSymbolSource(); ComponentModelSource modelSource = mockComponentModelSource(); String key = "foo.bar"; String value = "zaphod"; String completeId = "foo.Bar:baz"; train_getCompleteId(resources, completeId); train_getComponentModel(resources, model); train_getMeta(model, key, value); train_expandSymbols(symbolSource, value, value); replay(); Map<String, String> configuration = Collections.emptyMap(); MetaDataLocator locator = new MetaDataLocatorImpl(symbolSource, typeCoercer, modelSource, configuration); assertSame(locator.findMeta(key, resources, String.class), value); verify(); train_getCompleteId(resources, completeId); replay(); assertSame(locator.findMeta(key, resources, String.class), value); verify(); }
@Test public void default_to_symbol_source() { ComponentResources resources = mockComponentResources(); ComponentModel model = mockComponentModel(); SymbolSource symbolSource = mockSymbolSource(); ComponentModelSource modelSource = mockComponentModelSource(); String key = "foo.bar"; String value = "zaphod"; String completeId = "foo/Bar:baz"; train_getCompleteId(resources, completeId); train_getComponentModel(resources, model); train_getMeta(model, key, null); train_getContainerResources(resources, null); train_getPageName(resources, "foo/Bar"); train_valueForSymbol(symbolSource, key, value); replay(); Map<String, String> configuration = Collections.emptyMap(); MetaDataLocator locator = new MetaDataLocatorImpl(symbolSource, typeCoercer, modelSource, configuration); assertSame(locator.findMeta(key, resources, String.class), value); verify(); train_getCompleteId(resources, completeId); replay(); assertSame(locator.findMeta(key, resources, String.class), value); verify(); }
@Test public void find_by_page_name() { ComponentModel model = mockComponentModel(); SymbolSource symbolSource = mockSymbolSource(); ComponentModelSource modelSource = mockComponentModelSource(); String key = "foo.bar"; String value = "zaphod"; String pageName = "foo/Bar"; expect(modelSource.getPageModel(pageName)).andReturn(model); train_getMeta(model, key, value); train_expandSymbols(symbolSource, value, "*expanded*"); replay(); Map<String, String> configuration = Collections.emptyMap(); MetaDataLocator locator = new MetaDataLocatorImpl(symbolSource, typeCoercer, modelSource, configuration); assertSame(locator.findMeta(key, pageName, String.class), "*expanded*"); verify(); }