View Javadoc
1   package org.codehaus.plexus.util.xml;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.IOException;
21  
22  import junit.framework.TestCase;
23  
24  public class XmlStreamWriterTest
25      extends TestCase
26  {
27      /** french */
28      private static final String TEXT_LATIN1 = "eacute: \u00E9";
29  
30      /** greek */
31      private static final String TEXT_LATIN7 = "alpha: \u03B1";
32  
33      /** euro support */
34      private static final String TEXT_LATIN15 = "euro: \u20AC";
35  
36      /** japanese */
37      private static final String TEXT_EUC_JP = "hiragana A: \u3042";
38  
39      /** Unicode: support everything */
40      private static final String TEXT_UNICODE =
41          TEXT_LATIN1 + ", " + TEXT_LATIN7 + ", " + TEXT_LATIN15 + ", " + TEXT_EUC_JP;
42  
43      private static String createXmlContent( String text, String encoding )
44      {
45          String xmlDecl = "<?xml version=\"1.0\"?>";
46          if ( encoding != null )
47          {
48              xmlDecl = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>";
49          }
50          String xml = xmlDecl + "\n<text>" + text + "</text>";
51          return xml;
52      }
53  
54      private static void checkXmlContent( String xml, String encoding )
55          throws IOException
56      {
57          ByteArrayOutputStream out = new ByteArrayOutputStream();
58          XmlStreamWriter writer = new XmlStreamWriter( out );
59          writer.write( xml );
60          writer.close();
61          byte[] xmlContent = out.toByteArray();
62          String result = new String( xmlContent, encoding );
63          assertEquals( xml, result );
64      }
65  
66      private static void checkXmlWriter( String text, String encoding )
67          throws IOException
68      {
69          String xml = createXmlContent( text, encoding );
70          String effectiveEncoding = ( encoding == null ) ? "UTF-8" : encoding;
71          checkXmlContent( xml, effectiveEncoding );
72      }
73  
74      public void testNoXmlHeader()
75          throws IOException
76      {
77          String xml = "<text>text with no XML header</text>";
78          checkXmlContent( xml, "UTF-8" );
79      }
80  
81      public void testEmpty()
82          throws IOException
83      {
84          ByteArrayOutputStream out = new ByteArrayOutputStream();
85          XmlStreamWriter writer = new XmlStreamWriter( out );
86          writer.flush();
87          writer.write( "" );
88          writer.flush();
89          writer.write( "." );
90          writer.flush();
91          writer.close();
92      }
93  
94      public void testDefaultEncoding()
95          throws IOException
96      {
97          checkXmlWriter( TEXT_UNICODE, null );
98      }
99  
100     public void testUTF8Encoding()
101         throws IOException
102     {
103         checkXmlWriter( TEXT_UNICODE, "UTF-8" );
104     }
105 
106     public void testUTF16Encoding()
107         throws IOException
108     {
109         checkXmlWriter( TEXT_UNICODE, "UTF-16" );
110     }
111 
112     public void testUTF16BEEncoding()
113         throws IOException
114     {
115         checkXmlWriter( TEXT_UNICODE, "UTF-16BE" );
116     }
117 
118     public void testUTF16LEEncoding()
119         throws IOException
120     {
121         checkXmlWriter( TEXT_UNICODE, "UTF-16LE" );
122     }
123 
124     public void testLatin1Encoding()
125         throws IOException
126     {
127         checkXmlWriter( TEXT_LATIN1, "ISO-8859-1" );
128     }
129 
130     public void testLatin7Encoding()
131         throws IOException
132     {
133         checkXmlWriter( TEXT_LATIN7, "ISO-8859-7" );
134     }
135 
136     public void testLatin15Encoding()
137         throws IOException
138     {
139         checkXmlWriter( TEXT_LATIN15, "ISO-8859-15" );
140     }
141 
142     public void testEUC_JPEncoding()
143         throws IOException
144     {
145         checkXmlWriter( TEXT_EUC_JP, "EUC-JP" );
146     }
147 
148     public void testEBCDICEncoding()
149         throws IOException
150     {
151         checkXmlWriter( "simple text in EBCDIC", "CP1047" );
152     }
153 }