001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.util.xml.test;
006:
007: import com.sun.portal.rewriter.RewriterModule;
008: import com.sun.portal.rewriter.test.util.BasicTestCase;
009: import com.sun.portal.rewriter.util.Resource;
010: import com.sun.portal.rewriter.util.test.TestResource;
011: import com.sun.portal.rewriter.util.xml.Document;
012: import junit.framework.TestSuite;
013:
014: import java.io.File;
015: import java.util.ArrayList;
016: import java.util.Arrays;
017: import java.util.Locale;
018:
019: public class TestDocument extends BasicTestCase {
020: private static final String sysEncoding = System
021: .getProperty("file.encoding");
022: private static final String i18nString = Locale.JAPAN
023: .getDisplayLanguage(Locale.JAPAN);
024: private static final String[][] data = {
025: { "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "UTF-8", }, //1
026:
027: { "\n \t <?xml version=\"1.0\" encoding=\"UTF-8\" \n \t?>",
028: "UTF-8", }, //0
029:
030: { "<?xml version=\"1.0\" encoding=\"MS932\"?>", "MS932", }, //1
031:
032: { "<?xml version=\"1.0\"?>", sysEncoding, }, //2
033:
034: { "<?xml?>", sysEncoding, }, //3
035:
036: { "", sysEncoding, }, //3
037:
038: { null, sysEncoding, }, //4
039:
040: };
041:
042: private static final String[] encodingList = {
043: "Cp1252", //0
044: "ISO8859_1", //1
045: "UTF8", //2
046: "ASCII", //3
047: "Big5", //4
048: "Cp437", "Cp737", "Cp775", "Cp850", "Cp852", "Cp855",
049: "Cp856", "Cp857", "Cp858", "Cp860", "Cp861", "Cp862",
050: "Cp863", "Cp864", "Cp865", "Cp866", "Cp868", "Cp869",
051: "Cp874", "Cp921", "Cp922", "Cp923", "Cp942", "Cp942C",
052: "Cp943", "Cp943C", "Cp948", "Cp949", "Cp949C", "Cp950",
053: "Cp964", "Cp970", "Cp1006", "Cp1046", "Cp1098", "Cp1124",
054: "Cp1250", "Cp1251", "Cp1252", "Cp1253", "Cp1254", "Cp1255",
055: "Cp1256", "Cp1257", "Cp1258", "Cp1381", "Cp1383",
056: "Cp33722", "EUC_CN", "EUC_JP", "EUC_KR", "EUC_TW", "GBK",
057: "ISO2022JP", "ISO2022KR", "ISO8859_2", "ISO8859_3",
058: "ISO8859_4", "ISO8859_5", "ISO8859_6", "ISO8859_7",
059: "ISO8859_8", "ISO8859_9", "ISO8859_15_FDIS", "JIS0201",
060: "JIS0208", "JIS0212", "Johab", "KOI8_R", "MS874", "MS932",
061: "MS936", "MS949", "MS950", "MacArabic", "MacCentralEurope",
062: "MacCroatian", "MacCyrillic", "MacGreek", "MacHebrew",
063: "MacIceland", "MacRoman", "MacRomania", "MacThai",
064: "MacTurkish", "MacUkraine", "SJIS", "TIS620",
065: "ISO2022CN_CNS", "ISO2022CN_GB",
066: /*"JISAutoDetect",
067: "ISO2022CN",
068: "Cp273",
069: "Cp277",
070: "Cp278",
071: "Cp280",
072: "Cp284",
073: "Cp285",
074: "Cp297",
075: "Cp420",
076: "Cp424",
077: "Cp500",
078: "Cp838",
079: "Cp870",
080: "Cp871",
081: "Cp875",
082: "Cp918",
083: "Cp930",
084: "Cp933",
085: "Cp935",
086: "Cp937",
087: "Cp939",
088: "Cp1025",
089: "Cp1026",
090: "Cp1097",
091: "Cp1112",
092: "Cp1122",
093: "Cp1123",
094: "Cp1140",
095: "Cp1141",
096: "Cp1142",
097: "Cp1143",
098: "Cp1144",
099: "Cp1145",
100: "Cp1146",
101: "Cp1147",
102: "Cp1148",
103: "Cp1149",
104: "MacDingbat",
105: "MacSymbol",
106: "UnicodeBig",
107: "UnicodeLittle",*/
108: };
109:
110: private String fileName;
111:
112: public TestDocument(String aName) {
113: super (aName);
114: }//constuctor
115:
116: public void setUp() throws Exception {
117: File f = File.createTempFile("TempDocumentRuleSet", "xml");
118: f.deleteOnExit();
119: fileName = f.getAbsolutePath();
120: }//setUp()
121:
122: public void testParseEncodingString() throws Exception {
123: for (int i = 0; i < data.length; i++) {
124: String encoding = Document.parseEncoding(data[i][0]);
125: assertEquals("Failed at i= " + i, data[i][1], encoding);
126: }//for loop
127: }//testParseEncodingString()
128:
129: public void testParseEncodingByteArray() throws Exception {
130: for (int i = 0; i < encodingList.length; i++) {
131: for (int j = 0; j < data.length; j++) {
132: if (data[j][0] != null) {
133: String encoding = Document.parseEncoding(data[j][0]
134: .getBytes(encodingList[i]));
135: assertEquals("Failed at i= " + i + " and j= " + j,
136: data[j][1], encoding);
137: }
138: }//for loop
139: }
140: }//testParseEncodingByteArray()
141:
142: public void testParseEncodingByteArrayAddJAPAN() throws Exception {
143: for (int i = 0; i < encodingList.length; i++) {
144: for (int j = 0; j < data.length; j++) {
145: if (data[j][0] != null) {
146: String encoding = Document
147: .parseEncoding((data[j][0] + i18nString)
148: .getBytes(encodingList[i]));
149: assertEquals("Failed at i= " + i + " and j= " + j,
150: data[j][1], encoding);
151: }
152: }//for loop
153: }
154: }//testParseEncodingByteArrayAddJAPAN()
155:
156: public void testParseEncodingByteArrayAddJAPAN1() throws Exception {
157: for (int i = 0; i < encodingList.length; i++) {
158: for (int j = 0; j < data.length; j++) {
159: if (data[j][0] != null) {
160: String string2Parse = i18nString + data[j][0]
161: + i18nString;
162: String encoding = Document
163: .parseEncoding(string2Parse
164: .getBytes(encodingList[i]));
165: assertEquals("Failed at i= " + i + " and j= " + j,
166: data[j][1], encoding);
167: }
168: }//for loop
169: }
170: }//testParseEncodingByteArrayAddJAPAN1()
171:
172: public void testDOMWithI18NDataNegative1() {
173: try {
174: Resource.save(fileName, TestResource.I18N_RULE_SET);
175: Document.create(Resource.readXML(fileName));
176: fail();
177: } catch (Exception e) {
178: assertTrue(true);
179: }
180: }//testDOMWithI18NDataNegative1()
181:
182: public void testDOMWithI18NDataNegative2() {
183: Resource.saveXML(fileName, TestResource.I18N_RULE_SET);
184: try {
185: Document.create(Resource.read(fileName));
186: fail();
187: } catch (Exception e) {
188: assertTrue(true);
189: }
190: }//testDOMWithI18NDataNegative2()
191:
192: public void testDOMWithI18NDataNegative3() {
193: Resource.save(fileName, TestResource.I18N_RULE_SET);
194: try {
195: Document.create(Resource.read(fileName));
196: fail();
197: } catch (Exception e) {
198: assertTrue(true);
199: }
200: }//testDOMWithI18NDataNegative3()
201:
202: public void testDOMWithI18NData() {
203: Resource.saveXML(fileName, TestResource.I18N_RULE_SET);
204: try {
205: Document.create(Resource.readXML(fileName));
206: assertTrue(true);
207: } catch (Exception e) {
208: fail("Unable to parse valid XML Content:"
209: + Resource.readXML(fileName));
210: }
211: }//testDOMWithI18NData()
212:
213: final String[] fileNames = {
214: "/com/sun/portal/rewriter/test/samples/rulesets/ExampleRuleSet.xml", //ASCII Data - Stored in ASCII
215: "/com/sun/portal/rewriter/test/samples/rulesets/ExampleI18NRuleSet.xml", //ASCII Data - Stored in UTF-8
216: "/com/sun/portal/rewriter/test/samples/rulesets/xmlspyUTF8RuleSet.xml",
217: "/com/sun/portal/rewriter/test/samples/rulesets/xmlspyEUCJPRuleSet.xml",
218: "/com/sun/portal/rewriter/test/samples/rulesets/xmlspyIS02022JPRuleSet.xml",
219: "/com/sun/portal/rewriter/test/samples/rulesets/xmlspySHIFTJISRuleSet.xml",
220: //"com/sun/portal/rewriter/test/samples/rulesets/iconvUTF16LE2UTF8RuleSet.xml",
221: //"com/sun/portal/rewriter/test/samples/rulesets/iconvUTF16BE2UTF8RuleSet.xml",
222:
223: /*
224: "com/sun/portal/rewriter/test/samples/rulesets/xmlspyUTF7RuleSet.xml", //parser does not support this encoding
225: "com/sun/portal/rewriter/test/samples/rulesets/notepadUTF8Ruleset.xml", //UTF-8 data edit using Notepad on win 2000 - BOM
226: */
227: };
228:
229: public void testDOMWithI18NString() {
230: for (int i = 0; i < fileNames.length; i++) {
231: String xml = Resource.readXML(fileNames[i]);
232: try {
233: Document.create(xml);
234: assertTrue(true);
235: } catch (Exception e) {
236: e.printStackTrace();
237: fail("Failed for file Name : " + fileNames[i]);
238: }
239: }//for loop
240: }//testDOMWithI18NString()
241:
242: public void testDOMWithI18NBytes() {
243: //work as bytes and not as String
244: String[] list = {
245: "/com/sun/portal/rewriter/test/samples/rulesets/xmlspyUTF16BERuleSet.xml",
246: "/com/sun/portal/rewriter/test/samples/rulesets/xmlspyUTF16LERuleSet.xml",
247: "/com/sun/portal/rewriter/test/samples/rulesets/notepadUTF16BERuleSet.xml",
248: "/com/sun/portal/rewriter/test/samples/rulesets/notepadUTF16LERuleSet.xml", };
249:
250: ArrayList lList = new ArrayList();
251: lList.addAll(Arrays.asList(fileNames));
252: lList.addAll(Arrays.asList(list));
253:
254: for (int i = 0; i < lList.size(); i++) {
255: byte[] xml = Resource.readBytes(lList.get(i).toString());
256: try {
257: Document.create(xml);
258: assertTrue(true);
259: } catch (Exception e) {
260: e.printStackTrace();
261: fail("Failed for file Name : "
262: + lList.get(i).toString());
263: }
264: }//for loop
265: }//atestDOMWithI18NBytes()
266:
267: public void atestDOMWithI18NDataDebug() throws Exception {
268: String[] files = { "c:\\temp\\doc.xml",
269: "c:\\temp\\doc_ch_utf.xml",
270: //"c:\\temp\\doc_jp_utf.xml",
271: "c:\\temp\\doc_fr_utf.xml", };
272: for (int i = 0; i < files.length; i++) {
273: byte[] xml = Resource.readBytes(files[i]);
274: Document.create(xml, false, false);
275: }
276: }
277:
278: public void testCreateDocument() throws Exception {
279: String lInput = "<XML id=\"xmlDefaultView\">\n"
280: + "<view xmlns=\"http://schemas.microsoft.com/schemas/view\" xmlns:d=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882\">\n"
281: + "<baseroot>https://rajanagendra.India.Sun.COM/http://pserv9.iportal.com/exchweb/img/</baseroot>\n"
282: + "<viewstyle>table-layout:fixed;width:100%;font-size:x-small</viewstyle>\n"
283: + "<viewname>Messages</viewname>\n"
284: + "<column><heading>Importance</heading>\n"
285: + "<prop d:dt=\"i4\">http://schemas.microsoft.com/exchange/x-priority-long</prop>\n"
286: + "<width>1%</width>\n"
287: + "<sortable>1</sortable>\n"
288: + "<bitmap>1</bitmap>\n"
289: + "<multivalued>0</multivalued>\n"
290: + "<visible>1</visible>\n"
291: + "<style>padding-left:3px;text-align: center</style>\n"
292: + "</column>\n"
293: + "<column><heading>Item Type</heading>\n"
294: + "<prop d:dt=\"string\">http://schemas.microsoft.com/exchange/outlookmessageclass</prop>\n"
295: + "<width>3%</width>\n"
296: + "<sortable>1</sortable>\n"
297: + "<bitmap>1</bitmap>\n"
298: + "<multivalued>0</multivalued>\n"
299: + "<visible>1</visible>\n"
300: + "<style>padding-left:3px;text-align: center</style>\n"
301: + "</column>\n"
302: + "<column><heading>FlagStatus</heading>\n"
303: + "<prop d:dt=\"i4\">http://schemas.microsoft.com/mapi/proptag/x10900003</prop>\n"
304: + "<width>3%</width>\n"
305: + "<sortable>1</sortable>\n"
306: + "<bitmap>1</bitmap>\n"
307: + "<multivalued>0</multivalued>\n"
308: + "<visible>1</visible>\n"
309: + "<style>padding-left:3px;text-align: center</style>\n"
310: + "</column>\n"
311: + "<column><heading>Attachment</heading>\n"
312: + "<prop d:dt=\"boolean\">urn:schemas:httpmail:hasattachment</prop>\n"
313: + "<width>2%</width>\n"
314: + "<sortable>1</sortable>\n"
315: + "<bitmap>1</bitmap>\n"
316: + "<multivalued>0</multivalued>\n"
317: + "<visible>1</visible>\n"
318: + "<style>padding-left:3px;text-align: center</style>\n"
319: + "</column>\n"
320: + "<column><heading>From</heading>\n"
321: + "<prop d:dt=\"string\">http://schemas.microsoft.com/mapi/sent_representing_name</prop>\n"
322: + "<width>21%</width>\n"
323: + "<sortable>1</sortable>\n"
324: + "<bitmap>0</bitmap>\n"
325: + "<multivalued>0</multivalued>\n"
326: + "<visible>1</visible>\n"
327: + "<style>padding-left:3px;text-align: left</style>\n"
328: + "</column>\n"
329: + "<column><heading>Subject</heading>\n"
330: + "<prop d:dt=\"string\">http://schemas.microsoft.com/mapi/subject</prop>\n"
331: + "<width>36%</width>\n"
332: + "<sortable>1</sortable>\n"
333: + "<bitmap>0</bitmap>\n"
334: + "<multivalued>0</multivalued>\n"
335: + "<visible>1</visible>\n"
336: + "<style>padding-left:3px;text-align: left</style>\n"
337: + "</column>\n"
338: + "<column><heading>Received</heading>\n"
339: + "<prop d:dt=\"date\">urn:schemas:httpmail:datereceived</prop>\n"
340: + "<width>24%</width>\n"
341: + "<sortable>1</sortable>\n"
342: + "<bitmap>0</bitmap>\n"
343: + "<multivalued>0</multivalued>\n"
344: + "<visible>1</visible>\n"
345: + "<style>padding-left:3px;text-align: left</style>\n"
346: + "<dateformat>ddd dd-MM-yyyy</dateformat>\n"
347: + "<timeformat>HH:mm</timeformat>\n"
348: + "</column>\n"
349: + "<column><heading>Size</heading>\n"
350: + "<prop d:dt=\"i4\">http://schemas.microsoft.com/mapi/proptag/x0e080003</prop>\n"
351: + "<width>8%</width>\n"
352: + "<sortable>1</sortable>\n"
353: + "<bitmap>0</bitmap>\n"
354: + "<multivalued>0</multivalued>\n"
355: + "<visible>1</visible>\n"
356: + "<style>padding-left:3px;text-align: right</style>\n"
357: + "</column>\n"
358: + "<filter>\"http://schemas.microsoft.com/mapi/proptag/0x67aa000b\" = false AND \"DAV:isfolder\" = false</filter>\n"
359: + "<orderby><order><prop>http://schemas.microsoft.com/mapi/sent_representing_name</prop>\n"
360: + "<sort>ASC</sort>\n"
361: + "</order>\n"
362: + "</orderby>\n"
363: + "<headerstyle>background-color:buttonface</headerstyle>\n"
364: + "<rowstyle>background-color:window</rowstyle>\n"
365: + "</view>\n" + "</XML>\n";
366:
367: try {
368: Document.create(lInput, false, true, true);
369: Document.create(lInput, false, false, true);
370: assertTrue(true);
371: } catch (Exception e) {
372: fail();
373: }
374:
375: try {
376: Document.create(lInput);
377: fail();
378: } catch (Exception e) {
379: assertTrue(true);
380: }
381:
382: try {
383: Document.create(lInput, true, false, true);
384: fail();
385: } catch (Exception e) {
386: assertTrue(true);
387: }
388:
389: try {
390: Document.create(lInput, true, true, true);
391: fail();
392: } catch (Exception e) {
393: assertTrue(true);
394: }
395: }//testCreateDocument()
396:
397: public static void main(String[] args) {
398: RewriterModule.initFile();
399: //BasicTestCase.run( TestDocument.class );
400: TestSuite testSuite = new TestSuite();
401: testSuite.addTest(new TestDocument("testDOMWithI18NString"));
402: BasicTestCase.run(testSuite);
403: }//main()
404: }//class TestDocument
|