PersistentFieldManagerImpl implements PersistentFieldManager { public void postChange(String pageName, ComponentResources resources, String fieldName, Object newValue) { String strategyName = findStrategy(resources, fieldName); PersistentFieldStrategy strategy = getStrategy(strategyName); strategy.postChange(pageName, resources.getNestedId(), fieldName, newValue); } PersistentFieldManagerImpl(MetaDataLocator locator, Map<String, PersistentFieldStrategy> strategies); PersistentFieldBundle gatherChanges(String pageName); void discardChanges(String pageName); void discardChanges(String pageName, String strategyName); void postChange(String pageName, ComponentResources resources, String fieldName, Object newValue);  }
@Test public void post_change_with_unknown_strategy() { String fieldName = "field"; PersistentFieldStrategy strat1 = newPersistentFieldStrategy(); PersistentFieldStrategy strat2 = newPersistentFieldStrategy(); ComponentResources resources = mockComponentResources(); ComponentModel model = mockComponentModel(); Map<String, PersistentFieldStrategy> strategies = newMap(); strategies.put("foo", strat1); strategies.put("bar", strat2); train_getComponentModel(resources, model); train_getFieldPersistenceStrategy(model, fieldName, "braveheart"); replay(); PersistentFieldManager manager = new PersistentFieldManagerImpl(null, strategies); try { manager.postChange("foo.Bar", resources, fieldName, null); unreachable(); } catch (UnknownValueException ex) { assertEquals(ex.getMessage(), "'braveheart' is not a defined persistent strategy."); assertListsEquals(ex.getAvailableValues().getValues(), "bar", "foo"); } verify(); }
@Test public void post_change() { String pageName = "foo.Bar"; String nestedId = "nested"; String fieldName = "field"; String strategyName = "foo"; ComponentResources resources = mockComponentResources(); ComponentModel model = mockComponentModel(); PersistentFieldStrategy strat = newPersistentFieldStrategy(); Object value = new Object(); Map<String, PersistentFieldStrategy> strategies = newMap(); strategies.put(strategyName, strat); train_getComponentModel(resources, model); train_getFieldPersistenceStrategy(model, fieldName, strategyName); train_getNestedId(resources, nestedId); strat.postChange(pageName, nestedId, fieldName, value); replay(); PersistentFieldManager manager = new PersistentFieldManagerImpl(null, strategies); manager.postChange(pageName, resources, fieldName, value); verify(); }
@Test public void post_change_strategy_by_meta_data() { String pageName = "foo.Bar"; String nestedId = "nested"; String fieldName = "field"; String strategyName = "foo"; ComponentResources resources = mockComponentResources(); ComponentModel model = mockComponentModel(); PersistentFieldStrategy strat = newPersistentFieldStrategy(); MetaDataLocator locator = mockMetaDataLocator(); Object value = new Object(); Map<String, PersistentFieldStrategy> strategies = newMap(); strategies.put(strategyName, strat); train_getComponentModel(resources, model); train_getFieldPersistenceStrategy(model, fieldName, ""); train_findMeta(locator, SymbolConstants.PERSISTENCE_STRATEGY, resources, String.class, strategyName); train_getNestedId(resources, nestedId); strat.postChange(pageName, nestedId, fieldName, value); replay(); PersistentFieldManager manager = new PersistentFieldManagerImpl(locator, strategies); manager.postChange(pageName, resources, fieldName, value); verify(); }
@Test public void post_change_with_ultimate_default_strategy() { String pageName = "foo.Bar"; String nestedId = "nested"; String fieldName = "field"; ComponentResources resources = mockComponentResources(); ComponentModel model = mockComponentModel(); MetaDataLocator locator = mockMetaDataLocator(); PersistentFieldStrategy strat = newPersistentFieldStrategy(); Object value = new Object(); Map<String, PersistentFieldStrategy> strategies = newMap(); strategies.put(PersistenceConstants.SESSION, strat); train_getComponentModel(resources, model); train_getFieldPersistenceStrategy(model, fieldName, ""); train_findMeta(locator, SymbolConstants.PERSISTENCE_STRATEGY, resources, String.class, PersistenceConstants.SESSION); train_getNestedId(resources, nestedId); strat.postChange(pageName, nestedId, fieldName, value); replay(); PersistentFieldManager manager = new PersistentFieldManagerImpl(locator, strategies); manager.postChange(pageName, resources, fieldName, value); verify(); }