View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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  }