KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > io > EncodingTests


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: EncodingTests.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.io;
25
26 import java.io.File JavaDoc;
27 import java.io.FileWriter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.io.UnsupportedEncodingException JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32
33 import junit.framework.Test;
34
35 import org.enhydra.xml.driver.TestException;
36
37 /**
38  * Test of encodings.
39  */

40 public class EncodingTests extends IOTestCaseBase {
41     /** Extension for result file with missing encodings being listed */
42     private static final String JavaDoc MISSING_ENCODINGS_EXT
43         = "missing.encodings";
44
45     /**
46      * Table of java support encodings from JDK documentation.
47      * XMLC doesn't yet support all of these.
48      */

49     private static final String JavaDoc[] JAVA_ENCODINGS = {
50         "ASCII", "Cp1252", "ISO8859_1", "UnicodeBig", "UnicodeBigUnmarked",
51         "UnicodeLittle", "UnicodeLittleUnmarked", "UTF-8", "UTF-16",
52         "Big5", "Cp037", "Cp273", "Cp277", "Cp278",
53         "Cp280", "Cp284", "Cp285", "Cp297", "Cp420", "Cp424", "Cp437", "Cp500",
54         "Cp737", "Cp775", "Cp838", "Cp850", "Cp852", "Cp855", "Cp856", "Cp857",
55         "Cp858", "Cp860", "Cp861", "Cp862", "Cp863", "Cp864", "Cp865", "Cp866",
56         "Cp868", "Cp869", "Cp870", "Cp871", "Cp874", "Cp875", "Cp918", "Cp921",
57         "Cp922", "Cp930", "Cp933", "Cp935", "Cp937", "Cp939", "Cp942", "Cp942C",
58         "Cp943", "Cp943C", "Cp948", "Cp949", "Cp949C", "Cp950", "Cp964", "Cp970",
59         "Cp1006", "Cp1025", "Cp1026", "Cp1046", "Cp1097", "Cp1098", "Cp1112",
60         "Cp1122", "Cp1123", "Cp1124", "Cp1140", "Cp1141", "Cp1142", "Cp1143",
61         "Cp1144", "Cp1145", "Cp1146", "Cp1147", "Cp1148", "Cp1149", "Cp1250",
62         "Cp1251", "Cp1253", "Cp1254", "Cp1255", "Cp1256", "Cp1257", "Cp1258",
63         "Cp1381", "Cp1383", "Cp33722", "EUC_CN", "EUC_JP", "EUC_KR", "EUC_TW",
64         "GBK", "ISO2022CN", "ISO2022CN_CNS", "ISO2022CN_GB", "ISO2022JP",
65         "ISO2022KR", "ISO8859_2", "ISO8859_3", "ISO8859_4", "ISO8859_5",
66         "ISO8859_6", "ISO8859_7", "ISO8859_8", "ISO8859_9", "ISO8859_13",
67         "ISO8859_15_FDIS", "JIS0201", "JIS0208", "JIS0212", "JISAutoDetect",
68         "Johab", "KOI8_R", "MS874", "MS932", "MS936", "MS949", "MS950",
69         "MacArabic", "MacCentralEurope", "MacCroatian", "MacCyrillic",
70         "MacDingbat", "MacGreek", "MacHebrew", "MacIceland", "MacRoman",
71         "MacRomania", "MacSymbol", "MacThai", "MacTurkish", "MacUkraine", "SJIS",
72         "TIS620"
73     };
74
75     /** Count of encodings that were not found in XMLC tables */
76     private int fUnknownEncodingCnt = 0;
77
78     /** Factory method to create suite of these tests */
79     public static Test suite() {
80         return createSuite(EncodingTests.class, null);
81     }
82     
83     /** Required constructor */
84     public EncodingTests(Method JavaDoc method) {
85         super(method);
86     }
87
88     /**
89      * Check converting to encoding
90      */

91     private void checkEncodingConvert(String JavaDoc encoding) {
92         try {
93             String JavaDoc data = "This is a test";
94             byte[] b = data.getBytes(encoding);
95         } catch (UnsupportedEncodingException JavaDoc uee) {
96             msgPrintln("Note: encoding not supported by this JVM: " + encoding);
97         }
98     }
99         
100     /**
101      * Test an encoding.
102      */

103     private void testEncoding(String JavaDoc encoding,
104                               PrintWriter JavaDoc out) {
105         Encodings encodings = Encodings.getEncodings();
106         if (!encodings.isValid(encoding)) {
107             out.println("No entry for \"" + encoding + "\"");
108             fUnknownEncodingCnt++;
109         }
110         checkEncodingConvert(encoding);
111
112         // At least try to get it.
113
String JavaDoc mimePrefered = encodings.getMIMEPreferred(encoding);
114         if (mimePrefered != null) {
115             checkEncodingConvert(mimePrefered);
116         }
117     }
118
119     /**
120      * Test 1: Try converting a bunch of encodings. This really isn't
121      * much of a test
122      */

123     public void test1() {
124         File JavaDoc resultFile = getResultFile(MISSING_ENCODINGS_EXT);
125         try {
126             PrintWriter JavaDoc out = new PrintWriter JavaDoc(new FileWriter JavaDoc(resultFile));
127             try {
128                 for (int i = 0; i < JAVA_ENCODINGS.length; i++) {
129                     testEncoding(JAVA_ENCODINGS[i], out);
130                 }
131                 out.println("" + fUnknownEncodingCnt
132                             + " encodings not known to XMLC tables");
133             } finally {
134                 out.close();
135             }
136         } catch (IOException JavaDoc except) {
137             throw new TestException(except);
138         }
139
140         getDiffer().diff(getExpectedFile(MISSING_ENCODINGS_EXT),
141                          resultFile);
142     }
143 }
144
145
Popular Tags