KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > xml > cookies > TransformableSupportTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.spi.xml.cookies;
20
21 import java.io.*;
22 import java.net.*;
23
24 import junit.framework.*;
25 import org.netbeans.junit.*;
26
27 import org.xml.sax.*;
28 import javax.xml.parsers.*;
29 import javax.xml.transform.*;
30 import javax.xml.transform.sax.*;
31 import javax.xml.transform.stream.*;
32
33 import org.netbeans.api.xml.cookies.*;
34 import org.netbeans.spi.xml.cookies.*;
35
36 /**
37  *
38  * @author Libor Kramolis
39  */

40 public class TransformableSupportTest extends NbTestCase {
41
42     public TransformableSupportTest(java.lang.String JavaDoc testName) {
43         super(testName);
44     }
45     
46     public static void main(java.lang.String JavaDoc[] args) {
47         junit.textui.TestRunner.run(suite());
48     }
49     
50     public static Test suite() {
51         TestSuite suite = new NbTestSuite(TransformableSupportTest.class);
52         return suite;
53     }
54     
55     
56     public void testTransform () {
57         assertTrue ("Correct XML and correct XSLT must pass!", transform ("data/doc.xml", "data/doc2xhtml.xsl"));
58         assertTrue ("Incorrect XML and correct XSLT must not pass!", false==transform ("data/InvalidDocument.xml", "data/doc2xhtml.xsl"));
59         assertTrue ("Correct XML and incorrect XSLT must not pass!", false==transform ("data/doc.xml", "data/InvalidDocument.xml"));
60         assertTrue ("Incrrect XML and incorrect XSLT must not pass!", false==transform ("data/InvalidDocument.xml", "data/InvalidDocument.xml"));
61     }
62     
63     private boolean transform (String JavaDoc xml, String JavaDoc xslt) {
64         URL xmlURL = getClass().getResource(xml);
65         URL xsltURL = getClass().getResource(xslt);
66         Source xmlSource = new SAXSource (new InputSource (xmlURL.toExternalForm()));
67         Source xsltSource = new SAXSource (new InputSource (xsltURL.toExternalForm()));
68         Result outputResult = new StreamResult (new StringWriter());
69         
70         TransformableSupport support = new TransformableSupport (xmlSource);
71         Observer observer = new Observer(); // not yet used
72
boolean exceptionThrown = false;
73         try {
74             support.transform (xsltSource, outputResult, null);
75         } catch (TransformerException exc) {
76             System.err.println("!!! " + exc);
77             exceptionThrown = true;
78         }
79         
80         System.out.println(xml + " & " + xslt + " => " + ( exceptionThrown ? "WRONG" : "OK" ));
81         return exceptionThrown==false;
82     }
83     
84
85     //
86
// class Observer
87
//
88

89     private static class Observer implements CookieObserver {
90         private int receives;
91         private int warnings;
92         
93         public void receive(CookieMessage msg) {
94             receives++;
95             if (msg.getLevel() >= msg.WARNING_LEVEL) {
96                 warnings++;
97             }
98         }
99         public int getWarnings() {
100             return warnings;
101         }
102         
103     }
104         
105 }
106
Popular Tags