KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tests > EBCDICTest


1 /* Copyright 2002-2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.tests;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.UnsupportedEncodingException JavaDoc;
29
30 import nu.xom.Builder;
31 import nu.xom.Document;
32 import nu.xom.Element;
33 import nu.xom.ParsingException;
34 import nu.xom.Serializer;
35
36 /**
37  * <p>
38  * Tests support for the typical U.S. EBCDIC encoding.
39  * Unfortunately this test exposes a <a target="_top" HREF=
40  * "http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4867251">bug<a/>
41  * in the handling of NEL, character 0x85, in Sun's JDK. Specifically
42  * InputStreamReader maps 0x85 to a line feed rather than
43  * NEL. I've reported the bug to the Java Developer Connection,
44  * but until it's fixed this test fails. I don't have an easy
45  * workaround.
46  * </p>
47  *
48  * @author Elliotte Rusty Harold
49  * @version 1.0
50  *
51  */

52 public class EBCDICTest extends XOMTestCase {
53     
54     public final static char NEL = 0x85;
55
56     public EBCDICTest(String JavaDoc name) {
57         super(name);
58     }
59
60     private Document doc;
61     private String JavaDoc data;
62
63     protected void setUp() {
64         Element root = new Element("r");
65         doc = new Document(root);
66         data = "\u0085";
67         root.appendChild(data);
68     }
69   
70     
71     // This test will only pass if Java's NEL handling is fixed
72
public void testEBCDIC037()
73       throws ParsingException, UnsupportedEncodingException JavaDoc {
74         
75         Builder builder = new Builder();
76         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
77         try {
78             // Write data into a byte array using encoding
79
Serializer serializer = new Serializer(out, "Cp037");
80             serializer.write(doc);
81             serializer.flush();
82             out.flush();
83             out.close();
84             byte[] result = out.toByteArray();
85
86             // We have to look directly rather than converting to
87
// a String because Java gets the conversion of NEL to
88
// Unicode wrong
89
for (int i = 0; i < result.length; i++) {
90                 if (result[i] == 0x15) fail("Bad NEL output");
91             }
92
93             InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(result);
94             Document reparsed = builder.build(in);
95             assertEquals(doc, reparsed);
96         }
97         catch (UnsupportedEncodingException JavaDoc ex) {
98             throw ex;
99         }
100         catch (IOException JavaDoc ex) {
101             ex.printStackTrace();
102         }
103             
104     }
105     
106
107 }
108
Popular Tags