SimpleXmlFactory { static Serializer createSerializer(CRestConfig crestConfig, Class<?> source){ String prefix = source.getName(); Serializer serializer = crestConfig.get(prefix + SERIALIZER); if(serializer != null) { return serializer; } Map<Class, Transform> registry = new HashMap<Class, Transform>(); registry.put(Date.class, new DateMatcher(crestConfig.getDateFormat())); registry.put(Boolean.class, new BooleanMatcher(crestConfig.getBooleanTrue(), crestConfig.getBooleanFalse())); registry.put(boolean.class, registry.get(Boolean.class)); return new Persister(new MatcherRegistry(registry)); } private  SimpleXmlFactory();   }
@Test public void shouldCreateSerializerWithGivenConfig() throws Exception { Serializer actual = SimpleXmlFactory.createSerializer(config, getClass()); verify(config).getBooleanFalse(); verify(config).getBooleanTrue(); verify(config).getDateFormat(); verify(config).get(getClass().getName() + SimpleXmlFactory.SERIALIZER); StringWriter sw = new StringWriter(); V v1 = new V(true, false, Values.DATE); actual.write(v1, sw); V read = actual.read(V.class, new StringReader(sw.toString())); assertEquals("<root bool=\"myTrue\" bool2=\"myFalse\" date=\"13/03/1983 00:35:10\"/>", sw.toString()); assertEquals(v1, read); }
@Test public void shouldUseConfigSerializer() throws Exception { Serializer mock = mock(Serializer.class); when(config.get(getClass().getName() + SimpleXmlFactory.SERIALIZER)).thenReturn(mock); assertSame(mock, SimpleXmlFactory.createSerializer(config, getClass())); verify(config, never()).getBooleanFalse(); verify(config, never()).getBooleanTrue(); verify(config, never()).getDateFormat(); verify(config).get(getClass().getName() + SimpleXmlFactory.SERIALIZER); }