1   package de.fu_berlin.ties.xml;
2   
3   import junit.framework.TestCase;
4   // JUnitDoclet begin import
5   import java.io.StringWriter;
6   
7   import de.fu_berlin.ties.xml.XMLAdjuster;
8   // JUnitDoclet end import
9   
10  /***
11  * Generated by JUnitDoclet, a tool provided by
12  * ObjectFab GmbH under LGPL.
13  * Please see www.junitdoclet.org, www.gnu.org
14  * and www.objectfab.de for informations about
15  * the tool, the licence and the authors.
16  */
17  
18  
19  public class XMLAdjusterTest
20  // JUnitDoclet begin extends_implements
21  extends TestCase
22  // JUnitDoclet end extends_implements
23  {
24    // JUnitDoclet begin class
25    de.fu_berlin.ties.xml.XMLAdjuster xmladjuster = null;
26    private final static String INPUT_TEXT =
27      "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
28      + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
29      + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
30      + "<?xml-stylesheet type=\"text/css\" href=\"augmented.css\"?>\n"
31      + "<!-- http://money.cnn.com/2003/09/09/technology/disney_dvd.reut/ -->\n"
32      + "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
33      + "<head>"
34      + "<title>  Disney unveils DVDs that self-destruct in 48 hours - Sep. 9,\n"
35      + "2003</title>\n"
36      + "<link href=\"http://money.cnn.com/styles/cnnmoney.css\"\n" 
37      + "rel=\"stylesheet\" type=\"text/css\" />"
38      + "</head>Raw characters after the head of the document. \n "
39      + "<![CDATA[<greeting>Followed by a CDATA section.</greeting>]]> </html>\n";
40  
41      private final static XMLConstituent[] EXPECTED_OUTPUT = {
42          new OtherConstituent(OtherConstituent.XML_PROLOG, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"),
43          new OtherConstituent(OtherConstituent.OUTER_WHITESPACE, "\n"),
44          new OtherConstituent(OtherConstituent.DOCTYPE,
45              "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
46              + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"),
47          new OtherConstituent(OtherConstituent.OUTER_WHITESPACE, "\n"),
48          new OtherConstituent(OtherConstituent.PI,
49              "<?xml-stylesheet type=\"text/css\" href=\"augmented.css\"?>"),
50          new OtherConstituent(OtherConstituent.OUTER_WHITESPACE, "\n"),
51          new OtherConstituent(OtherConstituent.COMMENT,
52           "<!-- http://money.cnn.com/2003/09/09/technology/disney_dvd.reut/ -->"),
53          new OtherConstituent(OtherConstituent.OUTER_WHITESPACE, "\n"),
54          new TagConstituent(TagConstituent.START_TAG, "html",
55              "<html xmlns=\"http://www.w3.org/1999/xhtml\">", 0),
56          new OtherConstituent(OtherConstituent.OUTER_WHITESPACE, "\n"),
57          new TagConstituent(TagConstituent.START_TAG, "head", "<head>", 0),
58          new TagConstituent(TagConstituent.START_TAG, "title", "<title>", 0),
59          new OtherConstituent(OtherConstituent.OUTER_WHITESPACE, "  "),
60          new OtherConstituent(OtherConstituent.TEXT, 
61              "Disney unveils DVDs that self-destruct in 48 hours - Sep. 9,\n"
62                  + "2003"),
63          new TagConstituent(TagConstituent.END_TAG, "title", "</title>", 1),
64          new OtherConstituent(OtherConstituent.OUTER_WHITESPACE, "\n"),
65          new TagConstituent(TagConstituent.EMPTY_TAG, "link",
66              "<link href=\"http://money.cnn.com/styles/cnnmoney.css\"\n" 
67              + "rel=\"stylesheet\" type=\"text/css\" />", 1),
68          new TagConstituent(TagConstituent.END_TAG, "head", "</head>", 1),
69          new OtherConstituent(OtherConstituent.TEXT,
70              "Raw characters after the head of the document."),
71          new OtherConstituent(OtherConstituent.OUTER_WHITESPACE, " \n "),
72          new OtherConstituent(OtherConstituent.CDATA_SECTION,
73              "<![CDATA[<greeting>Followed by a CDATA section.</greeting>]]>"),
74          new OtherConstituent(OtherConstituent.OUTER_WHITESPACE, " "),
75          new TagConstituent(TagConstituent.END_TAG, "html", "</html>", 2),
76          new OtherConstituent(OtherConstituent.OUTER_WHITESPACE, "\n"),
77      };
78  
79      private final static String[] IN_FRAGMENTS = {
80        "<paragraph>"
81      + "    A"
82      + "  </sentence>"
83      + "  <list>"
84      + "    B"
85      + "  </sentence>"
86      + "    C"
87      + "  </list>"
88      + "</paragraph>",
89  
90        "<root xml:ns=\"Nonsense>Text"
91        + "  <tag count=3.4 a1=unquoted value quote-type='strange\" attrib=value/>"
92        + "  <quote in=attri\"bute another=on'e\">Iiih!!</quote>"
93        + "  <dir base=/usr path=/local/bin/>"
94        + "  <!-- same, but not quite as confusing: -->"
95        + "  <dir base=/usr path=/local/bin />"
96        + "  <file basepath=local/sub/directory/ name=dir/file.txt/>"
97        + "</root>",
98  
99        "<root xml:ns=\"Nonsense>"
100       + "  <tag count=\"3.4\" a1='quoted value' quote-type=\"normal\" attrib=\"value\"/>"
101       + "</root>",
102     };
103 
104     private final static String[] OUT_FRAGMENTS = {
105       "<paragraph><sentence>"
106       + "    A"
107       + "  </sentence>"
108       + "  <list><sentence>"
109       + "    B"
110       + "  </sentence>"
111       + "    C"
112       + "  </list>"
113       + "</paragraph>",
114 
115       "<root xml:ns=\"Nonsense\">Text"
116       + "  <tag count=\"3.4\" a1=\"unquoted value\" quote-type=\"strange\" attrib=\"value\"/>"
117       + "  <quote in=\"attri&quot;bute\" another=\"on'e\">Iiih!!</quote>"
118       + "  <dir base=\"/usr\" path=\"/local/bin\"/>"
119       + "  <!-- same, but not quite as confusing: -->"
120       + "  <dir base=\"/usr\" path=\"/local/bin\" />"
121       + "  <file basepath=\"local/sub/directory/\" name=\"dir/file.txt\"/>"
122       + "</root>",
123 
124       "<root xml:ns=\"Nonsense\">"
125       + "  <tag count=\"3.4\" a1='quoted value' quote-type=\"normal\" attrib=\"value\"/>"
126       + "</root>",
127     };
128 
129   // JUnitDoclet end class
130   
131   public XMLAdjusterTest(String name) {
132     // JUnitDoclet begin method XMLAdjusterTest
133     super(name);
134     // JUnitDoclet end method XMLAdjusterTest
135   }
136   
137   public de.fu_berlin.ties.xml.XMLAdjuster createInstance() throws Exception {
138     // JUnitDoclet begin method testcase.createInstance
139     return new XMLAdjuster("xml");
140     // JUnitDoclet end method testcase.createInstance
141   }
142   
143   protected void setUp() throws Exception {
144     // JUnitDoclet begin method testcase.setUp
145     super.setUp();
146     xmladjuster = createInstance();
147     // JUnitDoclet end method testcase.setUp
148   }
149   
150   protected void tearDown() throws Exception {
151     // JUnitDoclet begin method testcase.tearDown
152     xmladjuster = null;
153     super.tearDown();
154     // JUnitDoclet end method testcase.tearDown
155   }
156   
157   public void testAdjust() throws Exception {
158     // JUnitDoclet begin method adjust
159     // JUnitDoclet end method adjust
160   }
161   
162   public void testFixedConstituents() throws Exception {
163     // JUnitDoclet begin method fixedConstituents
164     // JUnitDoclet end method fixedConstituents
165   }
166   
167   public void testIsDeletingControlChars() throws Exception {
168     // JUnitDoclet begin method isDeletingControlChars
169     // JUnitDoclet end method isDeletingControlChars
170   }
171   
172   public void testIsDeletingPseudoTags() throws Exception {
173     // JUnitDoclet begin method isDeletingPseudoTags
174     // JUnitDoclet end method isDeletingPseudoTags
175   }
176   
177   public void testIsEscapingPseudoEntities() throws Exception {
178     // JUnitDoclet begin method isEscapingPseudoEntities
179     // JUnitDoclet end method isEscapingPseudoEntities
180   }
181   
182   public void testRawConstituents() throws Exception {
183     // JUnitDoclet begin method rawConstituents
184     XMLConstituent firstConst, currentConst;
185     TagConstituent currentTag, expectedTag;
186     firstConst = xmladjuster.rawConstituents(INPUT_TEXT, true);
187     assertFalse(firstConst.hasPrevious());
188     currentConst = firstConst;
189     int i = 0;
190     while (currentConst != null) {
191 /*        if (currentConst.getType() != EXPECTED_OUTPUT[i].getType()) {
192             System.out.println("Got type " + currentConst.getType()
193                 + " instead of " + EXPECTED_OUTPUT[i].getType() + " for "
194                 + currentConst.getRepresentantion());
195         }*/
196         assertEquals(currentConst.getType(), EXPECTED_OUTPUT[i].getType());
197 /*        if (!currentConst.getRepresentantion().equals(
198                 EXPECTED_OUTPUT[i].getRepresentantion())) {
199             System.out.println("Got '" + currentConst.getRepresentantion()
200                 + "' instead of: " + EXPECTED_OUTPUT[i].getRepresentantion());
201         }*/
202         assertEquals(currentConst.getRepresentantion(),
203             EXPECTED_OUTPUT[i].getRepresentantion());
204         if (currentConst instanceof TagConstituent) {
205             currentTag = (TagConstituent) currentConst;
206             expectedTag = (TagConstituent) EXPECTED_OUTPUT[i];
207             assertEquals(currentTag.getName(), expectedTag.getName());
208             assertEquals(currentTag.getMarkupSeriesNo(),
209                 expectedTag.getMarkupSeriesNo());
210         }
211         currentConst = currentConst.nextConstituent();
212         i++;
213     }
214     assertEquals(i, EXPECTED_OUTPUT.length);
215 
216     // adjust + compare broken XML fragments
217     StringWriter out;
218 
219     for (int j = 0; j < IN_FRAGMENTS.length; j++) {
220         out = new StringWriter();
221         xmladjuster.adjust(IN_FRAGMENTS[j], out);
222         assertEquals(out.toString(), OUT_FRAGMENTS[j]);
223     }
224     // JUnitDoclet end method rawConstituents
225   }
226   
227   public void testToString() throws Exception {
228     // JUnitDoclet begin method toString
229     // no need to test toString()
230     // JUnitDoclet end method toString
231   }
232   
233   
234   
235   /***
236   * JUnitDoclet moves marker to this method, if there is not match
237   * for them in the regenerated code and if the marker is not empty.
238   * This way, no test gets lost when regenerating after renaming.
239   * Method testVault is supposed to be empty.
240   */
241   public void testVault() throws Exception {
242     // JUnitDoclet begin method testcase.testVault
243     // JUnitDoclet end method testcase.testVault
244   }
245   
246   public static void main(String[] args) {
247     // JUnitDoclet begin method testcase.main
248     junit.textui.TestRunner.run(XMLAdjusterTest.class);
249     // JUnitDoclet end method testcase.main
250   }
251 }