1 package org.apache.torque.generator.source.stream;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
39
40
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
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
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&<>Y&apo;"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=\"&<>&apo;"\" 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 }