WDSObjectField implements IField { @Override public <T> T in(final IObject env) throws ReadValueException, InvalidArgumentException { if (null == env) { throw new InvalidArgumentException("Environment should not be null."); } Object value = null; try { for (IObject rule : this.rules) { List<String> args = (ArrayList<String>) rule.getValue(this.args); String name = (String) rule.getValue(this.strategyName); IFunction<StrategyAndArgs, Object> func = this.strategyExecutors.get(name); if (null == func) { func = this.strategyExecutors.get(OUTER_STRATEGY_NAME); } value = func.execute( new StrategyAndArgs(this.strategies.get(name), env, args, value) ); } } catch (Exception e) { throw new ReadValueException("Could not apply rules.", e); } if (null == value) { return null; } return (T) value; } WDSObjectField(final List<IObject> methodRules); @Override T in(final IObject env); @Override T in(final IObject env, final Class type); @Override void out(final IObject env, final T in);  }
@Test public void checkFieldCreationAndSimpleGetter() throws Exception { IObject rule = mock(IObject.class); IObject env = mock(IObject.class); IObject message = mock(IObject.class); IObject submessage = mock(IObject.class); when(env.getValue(new FieldName("message"))).thenReturn(message); when(message.getValue(new FieldName("submessage"))).thenReturn(submessage); when(submessage.getValue(new FieldName("intValue"))).thenReturn("1"); when(rule.getValue(new FieldName("name"))).thenReturn("wds_getter_strategy"); when(rule.getValue(new FieldName("args"))).thenReturn(new ArrayList<String>(){{add("message/submessage/intValue");}}); IField field = new WDSObjectField(new ArrayList<IObject>(){{add(rule);}}); assertNotNull(field); assertEquals(field.in(env), "1"); }
@Test (expected = ReadValueException.class) public void checkSimpleGetterWithException() throws Exception { IObject rule = mock(IObject.class); IObject env = mock(IObject.class); IObject message = mock(IObject.class); IObject submessage = mock(IObject.class); when(env.getValue(new FieldName("message"))).thenReturn(message); when(message.getValue(new FieldName("submessage"))).thenReturn(submessage); when(submessage.getValue(new FieldName("intValue"))).thenReturn(1); when(rule.getValue(new FieldName("name"))).thenReturn("wds_getter_strategy"); when(rule.getValue(new FieldName("args"))).thenReturn(new ArrayList<String>(){{add("message/undefined/submessage/intValue");}}); IField field = new WDSObjectField(new ArrayList<IObject>(){{add(rule);}}); field.in(env); fail(); }
@Test public void checkInMethod() throws Exception { IResolveDependencyStrategy strategy1 = mock(IResolveDependencyStrategy.class); IOC.resolve( Keys.getOrAdd(IResolveDependencyStrategy.class.getCanonicalName()), "GetValue", strategy1 ); IResolveDependencyStrategy strategy2 = mock(IResolveDependencyStrategy.class); IOC.resolve( Keys.getOrAdd(IResolveDependencyStrategy.class.getCanonicalName()), "Transform", strategy2 ); when(strategy1.resolve("CONST/a", 1)).thenReturn("CONST/a1"); when(strategy2.resolve("CONST/a1", "abc")).thenReturn("CONST/a1abc"); IObject rule1 = mock(IObject.class); IObject rule2 = mock(IObject.class); IObject env = mock(IObject.class); IObject message = mock(IObject.class); when(rule1.getValue(new FieldName("name"))).thenReturn("GetValue"); when(rule1.getValue(new FieldName("args"))).thenReturn(new ArrayList<String>(){{add("const/CONST/a"); add("message/intValue");}}); when(rule2.getValue(new FieldName("name"))).thenReturn("Transform"); when(rule2.getValue(new FieldName("args"))).thenReturn(new ArrayList<String>(){{add("local/value"); add("const/abc");}}); when(env.getValue(new FieldName("message"))).thenReturn(message); when(message.getValue(new FieldName("intValue"))).thenReturn(1); IField field = new WDSObjectField(new ArrayList<IObject>(){{add(rule1);add(rule2);}}); assertEquals(field.in(env), "CONST/a1abc"); when(strategy2.resolve("CONST/a1", "abc")).thenReturn(null); assertNull(field.in(env)); }
@Test (expected = InvalidArgumentException.class) public void checkInMethodExceptionOnNullArgs() throws Exception { IObject rule = mock(IObject.class); when(rule.getValue(new FieldName("name"))).thenReturn("wds_getter_strategy"); IField field = new WDSObjectField(new ArrayList<IObject>(){{add(rule);}}); field.in(null); fail(); }
@Test (expected = ReadValueException.class) public void checkInMethodExceptionOnWrongArgs() throws Exception { IResolveDependencyStrategy strategy1 = mock(IResolveDependencyStrategy.class); IOC.resolve( Keys.getOrAdd(IResolveDependencyStrategy.class.getCanonicalName()), "GetValue", strategy1 ); IObject rule = mock(IObject.class); IObject env = mock(IObject.class); when(rule.getValue(new FieldName("name"))).thenReturn("GetValue"); when(rule.getValue(new FieldName("args"))).thenReturn(new ArrayList<String>(){{add("message/getInt");}}); IField field = new WDSObjectField(new ArrayList<IObject>(){{add(rule);}}); field.in(env); fail(); }
@Test (expected = ReadValueException.class) public void checkNotImplementedInMethodException() throws Exception { IObject rule = mock(IObject.class); when(rule.getValue(new FieldName("name"))).thenReturn("wds_getter_strategy"); IField field = new WDSObjectField(new ArrayList<IObject>(){{add(rule);}}); field.in(null, Object.class); fail(); }