1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.impex.schema.impl;
17
18 import java.io.StringReader;
19 import java.io.StringWriter;
20 import java.util.Collections;
21 import javax.xml.bind.JAXBContext;
22 import javax.xml.bind.JAXBException;
23 import javax.xml.bind.Marshaller;
24 import javax.xml.bind.Unmarshaller;
25
26 import org.junit.Test;
27 import org.kuali.common.impex.model.Schema;
28 import org.kuali.common.impex.model.Sequence;
29 import org.kuali.common.impex.model.Table;
30 import org.kuali.common.impex.model.util.ModelUtils;
31 import org.kuali.common.util.CollectionUtils;
32
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertTrue;
35
36 public class TestXmlMarshalling {
37
38 @Test
39 public void testMarshal() throws JAXBException {
40 Schema schema = new Schema();
41 schema.setTables(Collections.singletonList(MockDataUtil.buildSimpleTable()));
42 schema.setForeignKeys(Collections.singletonList(MockDataUtil.buildSimpleForeignKey()));
43 schema.setViews(Collections.singletonList(MockDataUtil.buildSimpleView()));
44 schema.setSequences(Collections.singletonList(new Sequence("FOO_SEQ", "200")));
45
46 JAXBContext context = JAXBContext.newInstance(Schema.class);
47 Marshaller marshaller = context.createMarshaller();
48 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
49
50 StringWriter sw = new StringWriter();
51 marshaller.marshal(schema, sw);
52
53 System.out.println(sw.toString());
54 }
55
56 @Test
57 public void testUnmarshal() throws JAXBException {
58 Schema schema = new Schema();
59 schema.setTables(Collections.singletonList(MockDataUtil.buildSimpleTable()));
60 schema.setForeignKeys(Collections.singletonList(MockDataUtil.buildSimpleForeignKey()));
61
62 Table table = schema.getTables().iterator().next();
63
64 JAXBContext context = JAXBContext.newInstance(Schema.class);
65 Marshaller marshaller = context.createMarshaller();
66
67 StringWriter sw = new StringWriter();
68 marshaller.marshal(schema, sw);
69
70 Unmarshaller unmarshaller = context.createUnmarshaller();
71 Object o = unmarshaller.unmarshal(new StringReader(sw.toString()));
72
73 assertTrue(o instanceof Schema);
74
75 Schema s = (Schema) o;
76
77 assertEquals(1, s.getTables().size());
78 assertEquals(1, s.getForeignKeys().size());
79
80 Table loaded = s.getTables().iterator().next();
81
82 assertEquals(table.getName(), loaded.getName());
83 assertEquals(table.getDescription(), loaded.getDescription());
84 assertEquals(table.getColumns().size(), loaded.getColumns().size());
85 assertEquals(ModelUtils.getCsvPrimaryKeyColumnNames(table), ModelUtils.getCsvPrimaryKeyColumnNames(loaded));
86 assertEquals(ModelUtils.getCsvColumnNames(table.getColumns()), ModelUtils.getCsvColumnNames(loaded.getColumns()));
87 assertEquals("NAME", CollectionUtils.getCSV(loaded.getUniqueConstraints().get(0).getColumnNames()));
88 }
89
90 }