/** * This is used to assemble the model by perform registrations * based on the <code>Order</code> annotation. The initial * registrations performed by this establish the element and * attribute order for serialization of the schema class. * * @param model the model to perform registrations on * @param order this is the order specified by the class */ private void assembleAttributes(Model model, Order order) throws Exception { for(String value : order.attributes()) { Expression path = builder.build(value); if(!path.isAttribute() && path.isPath()) { throw new PathException("Ordered attribute '%s' references an element in %s", path, detail); } if(!path.isPath()) { Style style = format.getStyle(); String name = style.getAttribute(value); model.registerAttribute(name); } else { registerAttributes(model, path); } } }
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 testCombinationStrategyWithStyle() throws Exception { Registry registry = new Registry(); AnnotationStrategy annotationStrategy = new AnnotationStrategy(); RegistryStrategy registryStrategy = new RegistryStrategy(registry, annotationStrategy); Style style = new HyphenStyle(); Format format = new Format(style); Persister persister = new Persister(registryStrategy, format); CombinationExample example = new CombinationExample(1, 2, 3); StringWriter writer = new StringWriter(); registry.bind(Item.class, RegistryItemConverter.class); persister.write(example, writer); String text = writer.toString(); System.out.println(text); assertElementExists(text, "/combination-example/item/value"); assertElementHasValue(text, "/combination-example/item/value", "1"); assertElementHasValue(text, "/combination-example/item/type", RegistryItemConverter.class.getName()); assertElementExists(text, "/combination-example/overridden-item"); assertElementHasAttribute(text, "/combination-example/overridden-item", "value", "2"); assertElementHasAttribute(text, "/combination-example/overridden-item", "type", AnnotationItemConverter.class.getName()); assertElementExists(text, "/combination-example/extended-item"); assertElementHasAttribute(text, "/combination-example/extended-item", "value", "3"); assertElementHasAttribute(text, "/combination-example/extended-item", "type", ExtendedItemConverter.class.getName()); }
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 testConverterWithPathInHyphenStyle() throws Exception { Style style = new HyphenStyle(); 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 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 testHyphenStyle() throws Exception { Style style = new HyphenStyle(); Format format = new Format(style); Persister persister = new Persister(format); UnionListExample example = persister.read(UnionListExample.class, HYPHEN_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); }
/** * This is used to either provide the entry value provided within * the annotation or compute a entry value. If the entry string * is not provided the the entry value is calculated as the type * of primitive the object is as a simplified class name. * * @return this returns the name of the XML entry element used */ public String getEntry() throws Exception { Style style = format.getStyle(); if(detail.isEmpty(entry)) { entry = detail.getEntry(); } return style.getElement(entry); }
/** * This is used to acquire the name of the element or attribute * that is used by the class schema. The name is determined by * checking for an override within the annotation. If it contains * a name then that is used, if however the annotation does not * specify a name the the field or method name is used instead. * * @return returns the name that is used for the XML property */ public String getName() throws Exception{ if(name == null) { Style style = format.getStyle(); String value = detail.getName(); name = style.getElement(value); } return name; }
/** * This is used to either provide the entry value provided within * the annotation or compute a entry value. If the entry string * is not provided the the entry value is calculated as the type * of primitive the object is as a simplified class name. * * @return this returns the name of the XML entry element used */ public String getEntry() throws Exception { Style style = format.getStyle(); if(detail.isEmpty(parent)) { parent = detail.getEntry(); } return style.getElement(parent); }
/** * This is used to acquire the name of the element or attribute * that is used by the class schema. The name is determined by * checking for an override within the annotation. If it contains * a name then that is used, if however the annotation does not * specify a name the the field or method name is used instead. * * @return returns the name that is used for the XML property */ public String getName() throws Exception{ if(name == null) { Style style = format.getStyle(); String value = entry.getEntry(); if(!label.inline()) { value = detail.getName(); } name = style.getElement(value); } return name; }
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 testCase() throws Exception { Style style = new HyphenStyle(); Format format = new Format(style); Persister writer = new Persister(format); Persister reader = new Persister(); CaseExample example = reader.read(CaseExample.class, SOURCE); assertEquals(example.version, 1.0f); assertEquals(example.name, "example"); assertEquals(example.URL, "http://domain.com/"); writer.write(example, System.err); validate(example, reader); validate(example, writer); }
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"); }