View Javadoc

1   package org.apache.torque.generator.source.stream;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertEquals;
23  
24  import java.io.File;
25  
26  import org.apache.commons.io.FileUtils;
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.torque.generator.configuration.UnitConfiguration;
29  import org.apache.torque.generator.configuration.source.EntityReferences;
30  import org.apache.torque.generator.control.ControllerState;
31  import org.apache.torque.generator.source.SourceElement;
32  import org.apache.torque.generator.source.stream.FileSource;
33  import org.apache.torque.generator.source.stream.SourceToXml;
34  import org.apache.torque.generator.source.stream.XmlSourceFormat;
35  import org.junit.Test;
36  
37  /**
38   * Tests the SourceToXml functionality.
39   *
40   * $Id: SourceToXmlTest.java 1331190 2012-04-27 02:41:35Z tfischer $
41   */
42  public class SourceToXmlTest
43  {
44      private ControllerState controllerState = new ControllerState();
45  
46      public SourceToXmlTest()
47      {
48          UnitConfiguration unitConfiguration = new UnitConfiguration();
49          unitConfiguration.setEntityReferences(new EntityReferences());
50          controllerState.setUnitConfiguration(unitConfiguration);
51      }
52  
53      @Test
54      public void testSourceToXml() throws Exception
55      {
56          File xmlFile
57              = new File("src/test/resources/org/apache/torque/generator/source/xml/source.xml");
58          FileSource fileSource = new FileSource(
59                  new XmlSourceFormat(),
60                  xmlFile,
61                  controllerState);
62          SourceElement rootElement = fileSource.getRootElement();
63  
64          String result = new SourceToXml().toXml(rootElement, true);
65          String expected = FileUtils.readFileToString(new File(
66                  "src/test/resources/org/apache/torque/generator/source/xml/sourceToXmlResult.xml"));
67          // remove license from expected file
68          expected = StringUtils.substringAfterLast(expected, "-->\n\n");
69          assertEquals(expected, result);
70      }
71  
72      @Test
73      public void testSourceToXmlWithReferences() throws Exception
74      {
75          File xmlFile = new File(
76                  "src/test/resources/org/apache/torque/generator/source/xml/source.xml");
77          FileSource fileSource = new FileSource(
78                  new XmlSourceFormat(),
79                  xmlFile,
80                  controllerState);
81          SourceElement rootElement = fileSource.getRootElement();
82          rootElement.getChildren().get(2).getChildren().add(
83                  rootElement.getChildren().get(0).getChildren().get(0));
84  
85          String result = new SourceToXml().toXml(rootElement, true);
86          String expected = FileUtils.readFileToString(new File(
87                  "src/test/resources/org/apache/torque/generator/source/xml/sourceToXmlWithReferenceResult.xml"));
88          // remove license from expected file
89          expected = StringUtils.substringAfterLast(expected, "-->\n\n");
90          assertEquals(expected, result);
91      }
92  
93      @Test
94      public void testSourceToXmlTextEscaping() throws Exception
95      {
96          SourceElement rootElement = new SourceElement("root");
97          rootElement.setAttribute((String) null, "X&<>Y'\"Z");
98          String result = new SourceToXml().toXml(rootElement, true);
99          assertEquals(
100                 "<root id=\"1\">X&amp;&lt;&gt;Y&apo;&quot;Z</root>\n",
101                 result);
102     }
103 
104     @Test
105     public void testSourceToXmlAttributeEscaping() throws Exception
106     {
107         SourceElement rootElement = new SourceElement("root");
108         rootElement.setAttribute("attribute", "&<>'\"");
109         String result = new SourceToXml().toXml(rootElement, true);
110         assertEquals(
111                 "<root attribute=\"&amp;&lt;&gt;&apo;&quot;\" id=\"1\"/>\n",
112                 result);
113     }
114 
115     @Test
116     public void testSourceToXmlNoAutomaticIds() throws Exception
117     {
118         SourceElement rootElement = new SourceElement("root");
119         rootElement.getChildren().add(new SourceElement("child"));
120         String result = new SourceToXml().toXml(rootElement, false);
121         assertEquals(
122                 "<root>\n  <child/>\n</root>\n",
123                 result);
124     }
125 }