public void testCycle() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Registry registry = new Registry(); Address address = new Address("An Address"); Person person = new Person(address, "Niall", 30); CycleStrategy referencer = new CycleStrategy(); RegistryStrategy strategy = new RegistryStrategy(registry, referencer); Serializer serializer = new Persister(strategy, format); Converter converter = new PersonConverter(serializer); Club club = new Club(address); club.addMember(person); registry.bind(Person.class, converter); serializer.write(club, System.out); }
public void testPersonConverter() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Registry registry = new Registry(); Person person = new Person("Niall", 30); RegistryStrategy strategy = new RegistryStrategy(registry); Serializer serializer = new Persister(strategy, format); Converter converter = new PersonConverter(serializer); StringWriter writer = new StringWriter(); registry.bind(Person.class, converter); PersonProfile profile = new PersonProfile(person); serializer.write(profile, writer); System.out.println(writer.toString()); PersonProfile read = serializer.read(PersonProfile.class, writer.toString()); assertEquals(read.person.name, "Niall"); assertEquals(read.person.age, 30); }
public void testOtherStyle() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); OtherCaseExample example = new OtherCaseExample("a", "b"); Persister persister = new Persister(format); StringWriter writer = new StringWriter(); boolean exception = false; try { persister.write(example, writer); }catch(Exception e) { e.printStackTrace(); exception = true; } System.out.println(writer.toString()); assertFalse("No exception should be thrown with the elements are not the same name", exception); }
public void testConverter() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Strategy cycle = new CycleStrategy(); Strategy strategy = new AnnotationStrategy(cycle); Persister persister = new Persister(strategy, format); List<ConverterDecorationExample> list = new ArrayList<ConverterDecorationExample>(); List<NormalExample> normal = new ArrayList<NormalExample>(); ConverterDecoration example = new ConverterDecoration(list, normal); ConverterDecorationExample duplicate = new ConverterDecorationExample("duplicate"); NormalExample normalDuplicate = new NormalExample("duplicate"); list.add(duplicate); list.add(new ConverterDecorationExample("a")); list.add(new ConverterDecorationExample("b")); list.add(new ConverterDecorationExample("c")); list.add(duplicate); list.add(new ConverterDecorationExample("d")); list.add(duplicate); normal.add(normalDuplicate); normal.add(new NormalExample("1")); normal.add(new NormalExample("2")); normal.add(normalDuplicate); persister.write(example, System.err); }
public void testConverterWithPathInCamelStyle() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Strategy strategy = new AnnotationStrategy(); Persister persister = new Persister(strategy, format); ServerDetails primary = new ServerDetails("host1.blah.com", 4567, "PRIMARY"); ServerDetails secondary = new ServerDetails("host2.foo.com", 4567, "SECONDARY"); ServerDetailsReference reference = new ServerDetailsReference(primary, secondary); StringWriter writer = new StringWriter(); persister.write(reference, writer); System.out.println(writer); ServerDetailsReference recovered = persister.read(ServerDetailsReference.class, writer.toString()); assertEquals(recovered.getPrimary().getHost(), reference.getPrimary().getHost()); assertEquals(recovered.getPrimary().getPort(), reference.getPrimary().getPort()); assertEquals(recovered.getPrimary().getName(), reference.getPrimary().getName()); assertEquals(recovered.getSecondary().getHost(), reference.getSecondary().getHost()); assertEquals(recovered.getSecondary().getPort(), reference.getSecondary().getPort()); assertEquals(recovered.getSecondary().getName(), reference.getSecondary().getName()); }
public void testCamelCaseStyle() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Persister persister = new Persister(format); UnionListExample example = persister.read(UnionListExample.class, CAMEL_CASE_SOURCE); List<Entry> entry = example.list; assertEquals(entry.size(), 4); assertEquals(entry.get(0).getClass(), IntegerEntry.class); assertEquals(entry.get(0).foo(), 111); assertEquals(entry.get(1).getClass(), DoubleEntry.class); assertEquals(entry.get(1).foo(), 222.0); assertEquals(entry.get(2).getClass(), StringEntry.class); assertEquals(entry.get(2).foo(), "A"); assertEquals(entry.get(3).getClass(), StringEntry.class); assertEquals(entry.get(3).foo(), "B"); assertEquals(example.entry.getClass(), IntegerEntry.class); assertEquals(example.entry.foo(), 777); persister.write(example, System.out); validate(persister, example); }
public void testConverter() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Registry registry = new Registry(); Customer customer = new Customer("Niall", "Some Place"); Envelope envelope = new Envelope(customer); RegistryStrategy strategy = new RegistryStrategy(registry); Serializer serializer = new Persister(strategy, format); Converter converter = new EnvelopeConverter(serializer); registry.bind(Envelope.class, converter); OrderItem order = new OrderItem(envelope); serializer.write(order, System.out); }
public void testArrayExample() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Persister persister = new Persister(format); ArrayExample example = persister.read(ArrayExample.class, ARRAY); assertEquals(example.getArray().length, 5); assertEquals(example.getArray()[0], "entry one"); assertEquals(example.getArray()[1], "entry two"); assertEquals(example.getArray()[2], "entry three"); assertEquals(example.getArray()[3], "entry four"); assertEquals(example.getArray()[4], "entry five"); validate(persister, example); }
public void testStyledPath() throws Exception { Type type = new ClassType(PathParserTest.class); Expression expression = new PathParser( "this/is/a/path", type, new Format(new CamelCaseStyle())); String attributePath = expression.getAttribute("final"); assertEquals(attributePath, "This[1]/Is[1]/A[1]/Path[1]/@final"); String elementPath = expression.getElement("final"); assertEquals(elementPath, "This[1]/Is[1]/A[1]/Path[1]/Final[1]"); }
public void testStyledPathWithPrefixes() throws Exception { Type type = new ClassType(PathParserTest.class); Expression expression = new PathParser("pre:this/is/a/pre:path", type, new Format(new CamelCaseStyle())); String attributePath = expression.getAttribute("final"); assertEquals(attributePath, "pre:This[1]/Is[1]/A[1]/pre:Path[1]/@final"); String elementPath = expression.getElement("final"); assertEquals(elementPath, "pre:This[1]/Is[1]/A[1]/pre:Path[1]/Final[1]"); }
public void testStyleDup() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); StyleExample example = new StyleExample("a", "b"); Persister persister = new Persister(format); StringWriter writer = new StringWriter(); persister.write(example, writer); System.out.println(writer); StyleExample restored = persister.read(StyleExample.class, writer.toString()); assertEquals(example.a, restored.a); assertEquals(example.b, restored.b); }
public void testStyle() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); CollisionExample example = new CollisionExample("a", "b"); Persister persister = new Persister(format); StringWriter writer = new StringWriter(); boolean exception = false; try { persister.write(example, writer); }catch(Exception e) { e.printStackTrace(); exception = true; } assertTrue("Exception must be thrown when paths collide on case", exception); }
public void testStyleDup() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); TextExample example = new TextExample("a", "b"); Persister persister = new Persister(format); StringWriter writer = new StringWriter(); persister.write(example, writer); System.out.println(writer); TextExample restored = persister.read(TextExample.class, writer.toString()); assertEquals(example.a, restored.a); assertEquals(example.b, restored.b); }
public void testVisitor() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Visitor visitor = new RenameVisitor(); VisitorStrategy strategy = new VisitorStrategy(visitor); Persister renamePersister = new Persister(strategy); Persister normalPersister = new Persister(format); RenameExample example = new RenameExample("example name", "example value"); StringWriter renameWriter = new StringWriter(); StringWriter normalWriter = new StringWriter(); renamePersister.write(example, renameWriter); normalPersister.write(example, normalWriter); String renameResult = renameWriter.toString(); String normalResult = normalWriter.toString(); System.out.println(renameResult); System.out.println(normalResult); assertElementExists(renameResult, "/RENAMEEXAMPLE"); assertElementExists(renameResult, "/RENAMEEXAMPLE/NAME"); assertElementExists(renameResult, "/RENAMEEXAMPLE/VALUE"); assertElementHasValue(renameResult, "/RENAMEEXAMPLE/NAME", "example name"); assertElementHasValue(renameResult, "/RENAMEEXAMPLE/VALUE", "example value"); assertElementExists(normalResult, "/RenameExample"); assertElementExists(normalResult, "/RenameExample/Name"); assertElementExists(normalResult, "/RenameExample/Value"); assertElementHasValue(normalResult, "/RenameExample/Name", "example name"); assertElementHasValue(normalResult, "/RenameExample/Value", "example value"); }
public void testSerialization() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Persister camelPersister = new Persister(format); Persister persister = new Persister(); ExampleAssembledType example = new ExampleAssembledType(); StringWriter writer = new StringWriter(); StringWriter camelWriter = new StringWriter(); example.setA("This is a"); example.setB("This is b"); example.setC("This is c"); example.setD("This is d"); example.setE("This is e"); example.setF("This is f"); example.setG("This is g"); example.setH("This is h"); example.setFirst("The first element"); example.setSecond("The second element"); example.setThird("The third element"); example.setFourth("The fourth element"); example.setFifth("The fifth element"); example.setSixth("The sixth element"); example.setSeventh("The seventh element"); example.setEight("The eight element"); example.setNinth("The ninth element"); example.setTenth("The tenth element"); camelPersister.write(example, System.err); persister.write(example, System.out); persister.write(example, writer); camelPersister.write(example, camelWriter); ExampleAssembledType recovered = persister.read(ExampleAssembledType.class, writer.toString()); ExampleAssembledType camelRecovered = camelPersister.read(ExampleAssembledType.class, camelWriter.toString()); assertEquals(recovered.a, example.a); assertEquals(recovered.b, example.b); assertEquals(recovered.c, example.c); assertEquals(recovered.d, example.d); assertEquals(recovered.e, example.e); assertEquals(recovered.f, example.f); assertEquals(recovered.g, example.g); assertEquals(recovered.h, example.h); assertEquals(recovered.first, example.first); assertEquals(recovered.second, example.second); assertEquals(recovered.third, example.third); assertEquals(recovered.fourth, example.fourth); assertEquals(recovered.fifth, example.fifth); assertEquals(recovered.sixth, example.sixth); assertEquals(recovered.seventh, example.seventh); assertEquals(recovered.eight, example.eight); assertEquals(recovered.ninth, example.ninth); assertEquals(recovered.tenth, example.tenth); assertEquals(camelRecovered.a, example.a); assertEquals(camelRecovered.b, example.b); assertEquals(camelRecovered.c, example.c); assertEquals(camelRecovered.d, example.d); assertEquals(camelRecovered.e, example.e); assertEquals(camelRecovered.f, example.f); assertEquals(camelRecovered.g, example.g); assertEquals(camelRecovered.h, example.h); assertEquals(camelRecovered.first, example.first); assertEquals(camelRecovered.second, example.second); assertEquals(camelRecovered.third, example.third); assertEquals(camelRecovered.fourth, example.fourth); assertEquals(camelRecovered.fifth, example.fifth); assertEquals(camelRecovered.sixth, example.sixth); assertEquals(camelRecovered.seventh, example.seventh); assertEquals(camelRecovered.eight, example.eight); assertEquals(camelRecovered.ninth, example.ninth); assertEquals(camelRecovered.tenth, example.tenth); validate(example, persister); }
public void testStyledComplicatedMap() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Map<Key, Entry> map = new LinkedHashMap<Key, Entry>(); List<Entry> list = new ArrayList<Entry>(); ComplicatedExample example = new ComplicatedExample( map, list, "element 1", "element 2", "element 3", "text", "attribute 1", "attribute 2"); map.put(new X(), new A("1")); map.put(new Z(), new C("2")); map.put(new Z(), new C("3")); map.put(new Y(), new B("4")); list.add(new C("a")); list.add(new A("b")); list.add(new B("c")); list.add(new B("d")); Persister persister = new Persister(format); StringWriter writer = new StringWriter(); persister.write(example, writer); String resultingXml = writer.toString(); System.out.println(resultingXml); assertElementExists(resultingXml, "ComplicatedExample/Path"); assertElementHasNamespace(resultingXml, "ComplicatedExample/Path", "http://www.x.com/x"); assertElementExists(resultingXml, "ComplicatedExample/Path[1]/A"); assertElementExists(resultingXml, "ComplicatedExample/Path[1]/A[1]/X"); assertElementExists(resultingXml, "ComplicatedExample/Path[1]/A[1]/A"); assertElementHasAttribute(resultingXml, "ComplicatedExample/Path[1]/A[1]/A", "id", "1"); assertElementExists(resultingXml, "ComplicatedExample/Path[1]/C"); assertElementExists(resultingXml, "ComplicatedExample/Path[1]/C[1]/Z"); assertElementExists(resultingXml, "ComplicatedExample/Path[1]/C[1]/C"); assertElementHasAttribute(resultingXml, "ComplicatedExample/Path[1]/C[1]/C", "id", "2"); assertElementExists(resultingXml, "ComplicatedExample/Path[1]/C"); assertElementExists(resultingXml, "ComplicatedExample/Path[1]/C[2]/Z"); assertElementExists(resultingXml, "ComplicatedExample/Path[1]/C[2]/Z"); assertElementHasAttribute(resultingXml, "ComplicatedExample/Path[1]/C[2]/C", "id", "3"); assertElementExists(resultingXml, "ComplicatedExample/Path[1]/B"); assertElementExists(resultingXml, "ComplicatedExample/Path[1]/B[1]/Y"); assertElementExists(resultingXml, "ComplicatedExample/Path[1]/B[1]/B"); assertElementHasAttribute(resultingXml, "ComplicatedExample/Path[1]/B[1]/B", "id", "4"); assertElementExists(resultingXml, "ComplicatedExample/Path[2]/C"); assertElementHasAttribute(resultingXml, "ComplicatedExample/Path[2]/C", "id", "a"); assertElementExists(resultingXml, "ComplicatedExample/Path[2]/A"); assertElementHasAttribute(resultingXml, "ComplicatedExample/Path[2]/A", "id", "b"); assertElementExists(resultingXml, "ComplicatedExample/Path[2]/A"); assertElementHasAttribute(resultingXml, "ComplicatedExample/Path[2]/B", "id", "c"); assertElementExists(resultingXml, "ComplicatedExample/Path[2]/B"); assertElementHasValue(resultingXml, "ComplicatedExample/Path[2]/ElementOne", "element 1"); assertElementHasValue(resultingXml, "ComplicatedExample/Path[2]/ElementTwo", "element 2"); assertElementHasValue(resultingXml, "ComplicatedExample/Path[2]/ElementThree", "element 3"); assertElementHasValue(resultingXml, "ComplicatedExample/Path[2]/SomeOtherPath", "text"); assertElementHasNamespace(resultingXml, "ComplicatedExample/Path[2]/SomeOtherPath", "http://www.x.com/x"); assertElementHasAttribute(resultingXml, "ComplicatedExample/Path[2]/SomeOtherPath", "attributeOne", "attribute 1"); assertElementHasAttribute(resultingXml, "ComplicatedExample/Path[2]/SomeOtherPath", "attributeTwo", "attribute 2"); validate(persister, example); }
protected Serializer createSerializer() { Style style = new CamelCaseStyle(true); Format format = new Format(style); return new Persister(format); }