OSDN Git Service

Use Windows line delimiters on Windows
[android-x86/sdk.git] / eclipse / plugins / com.android.ide.eclipse.tests / unittests / com / android / ide / eclipse / adt / internal / editors / formatting / XmlPrettyPrinterTest.java
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.ide.eclipse.adt.internal.editors.formatting;
17
18 import org.w3c.dom.Document;
19 import org.xml.sax.ErrorHandler;
20 import org.xml.sax.InputSource;
21 import org.xml.sax.SAXException;
22 import org.xml.sax.SAXParseException;
23
24 import java.io.StringReader;
25
26 import javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.DocumentBuilderFactory;
28
29 import junit.framework.TestCase;
30
31 public class XmlPrettyPrinterTest extends TestCase {
32     private void checkFormat(XmlFormatPreferences prefs, XmlFormatStyle style,
33             String xml, String expected, String delimiter) throws Exception {
34
35         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
36         InputSource is = new InputSource(new StringReader(xml));
37         factory.setIgnoringComments(false);
38         factory.setIgnoringElementContentWhitespace(false);
39         factory.setCoalescing(false);
40         factory.setNamespaceAware(true);
41         factory.setValidating(true);
42         DocumentBuilder builder = factory.newDocumentBuilder();
43         builder.setErrorHandler(new ErrorHandler() {
44             public void error(SAXParseException arg0) throws SAXException {
45             }
46             public void fatalError(SAXParseException arg0) throws SAXException {
47             }
48             public void warning(SAXParseException arg0) throws SAXException {
49             }
50         });
51         Document document = builder.parse(is);
52
53         XmlPrettyPrinter printer = new XmlPrettyPrinter(prefs, style, delimiter);
54
55         StringBuilder sb = new StringBuilder(1000);
56         printer.prettyPrint(-1, document, document, document, sb);
57         String formatted = sb.toString();
58         if (!expected.equals(formatted)) {
59             System.out.println(formatted);
60         }
61         assertEquals(expected, formatted);
62     }
63
64     private void checkFormat(XmlFormatPreferences prefs, XmlFormatStyle style,
65             String xml, String expected) throws Exception {
66         checkFormat(prefs, style, xml, expected, "\n"); //$NON-NLS-1$
67     }
68     private void checkFormat(XmlFormatStyle style, String xml, String expected)
69             throws Exception {
70         XmlFormatPreferences prefs = XmlFormatPreferences.create();
71         checkFormat(prefs, style, xml, expected);
72     }
73
74     private void checkFormat(String xml, String expected) throws Exception {
75         checkFormat(XmlFormatStyle.LAYOUT, xml, expected);
76     }
77
78     public void testLayout1() throws Exception {
79         checkFormat(
80                 "<LinearLayout><Button></Button></LinearLayout>",
81                 "<LinearLayout>\n" +
82                 "\n" +
83                 "    <Button>\n" +
84                 "    </Button>\n" +
85                 "\n" +
86                 "</LinearLayout>");
87     }
88
89     public void testLayout2() throws Exception {
90         checkFormat(
91                 "<LinearLayout><Button foo=\"bar\"></Button></LinearLayout>",
92                 "<LinearLayout>\n" +
93                 "\n" +
94                 "    <Button\n" +
95                 "        foo=\"bar\">\n" +
96                 "    </Button>\n" +
97                 "\n" +
98                 "</LinearLayout>");
99     }
100
101     public void testLayout3() throws Exception {
102         XmlFormatPreferences prefs = XmlFormatPreferences.create();
103         prefs.oneAttributeOnFirstLine = true;
104         checkFormat(
105                 prefs, XmlFormatStyle.LAYOUT,
106                 "<LinearLayout><Button foo=\"bar\"></Button></LinearLayout>",
107                 "<LinearLayout>\n" +
108                 "\n" +
109                 "    <Button foo=\"bar\">\n" +
110                 "    </Button>\n" +
111                 "\n" +
112                 "</LinearLayout>");
113     }
114
115     /*
116     // TODO: This test will only work with the Eclipse DOM because our parser doesn't
117     // handle the ElementImpl case which records empty elements
118     public void testClosedElements() throws Exception {
119         checkFormat(
120                 XmlPrettyPrinter.Style.RESOURCE,
121                 "<resources>\n" +
122                 "<item   name=\"title_container\"  type=\"id\"   />\n" +
123                 "<item name=\"title_logo\" type=\"id\"/>\n" +
124                 "</resources>\n",
125                 "<resources>\n" +
126                 "    <item name=\"title_container\" type=\"id\" />\n" +
127                 "    <item name=\"title_logo\" type=\"id\" />\n" +
128                 "</resources>");
129     }
130      */
131
132     public void testResources() throws Exception {
133         checkFormat(
134                 XmlFormatStyle.RESOURCE,
135                 "<resources><item name=\"foo\">Text value here </item></resources>",
136                 "<resources>\n\n" +
137                 "    <item name=\"foo\">Text value here </item>\n" +
138                 "\n</resources>");
139     }
140
141     public void testNodeTypes() throws Exception {
142         // Ensures that a document with all kinds of node types is serialized correctly
143         checkFormat(
144                 XmlFormatStyle.LAYOUT,
145                 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
146                 "<!--\n" +
147                 "/**\n" +
148                 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
149                 " */\n" +
150                 "-->\n" +
151                 "<!DOCTYPE metadata [\n" +
152                 "<!ELEMENT metadata (category)*>\n" +
153                 "<!ENTITY % ISOLat2\n" +
154                 "         SYSTEM \"http://www.xml.com/iso/isolat2-xml.entities\" >\n" +
155                 "]>\n" +
156                 "<LinearLayout\n" +
157                 "    xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
158                 "    android:orientation=\"vertical\">\n" +
159                 "<![CDATA[\n" +
160                 "This is character data!\n" +
161                 "<!-- This is not a comment! -->\n" +
162                 "and <this is not an element>\n" +
163                 "]]>         \n" +
164                 "This is text: &lt; and &amp;\n" +
165                 "<!-- comment 1 \"test\"... -->\n" +
166                 "<!-- ... comment2 -->\n" +
167                 "%ISOLat2;        \n" +
168                 "<!-- \n" +
169                 "Type <key>less-than</key> (&#x3C;)\n" +
170                 "-->        \n" +
171                 "</LinearLayout>",
172
173                 /* For some reason the SAX document builder does not include the processing
174                  * instruction node (the Eclipse XML parser luckily does; try to change
175                  * unit test to use it since we want to make sure the pretty printer
176                  * handles it properly)
177                 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
178                 */
179                 "<!--\n" +
180                 "/**\n" +
181                 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
182                 " */\n" +
183                 "-->\n" +
184                 /* For some reason the SAX document builder does not include the
185                  * doc type node (the Eclipse XML parser luckily does; try to change
186                  * unit test to use it since we want to make sure the pretty printer
187                  * handles it properly)
188                 "<!DOCTYPE metadata [\n" +
189                 "<!ELEMENT metadata (category)*>\n" +
190                 "<!ENTITY % ISOLat2\n" +
191                 "         SYSTEM \"http://www.xml.com/iso/isolat2-xml.entities\" >\n" +
192                 "]>\n" +
193                  */
194                 "<LinearLayout\n" +
195                 "    xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
196                 "    android:orientation=\"vertical\">\n" +
197                 "    <![CDATA[\n" +
198                 "This is character data!\n" +
199                 "<!-- This is not a comment! -->\n" +
200                 "and <this is not an element>\n" +
201                 "]]>\n" +
202                 "         \n" +
203                 "This is text: &lt; and &amp;\n" +
204                 "\n" +
205                 "    <!-- comment 1 \"test\"... -->\n" +
206                 "    <!-- ... comment2 -->\n" +
207                 "\n" +
208                 "%ISOLat2;        \n" +
209                 "\n" +
210                 "    <!--\n" +
211                 "Type <key>less-than</key> (&#x3C;)\n" +
212                 "    -->\n" +
213                 "\n" +
214                 "</LinearLayout>");
215     }
216
217     public void testWindowsDelimiters() throws Exception {
218         checkFormat(
219                 XmlFormatPreferences.create(), XmlFormatStyle.LAYOUT,
220                 "<LinearLayout><Button foo=\"bar\"></Button></LinearLayout>",
221                 "<LinearLayout>\r\n" +
222                 "\r\n" +
223                 "    <Button\r\n" +
224                 "        foo=\"bar\">\r\n" +
225                 "    </Button>\r\n" +
226                 "\r\n" +
227                 "</LinearLayout>",
228                 "\r\n");
229     }
230 }