001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.spi.xml.cookies;
042:
043: import java.io.*;
044: import java.net.*;
045:
046: import junit.framework.*;
047: import org.netbeans.junit.*;
048:
049: import org.xml.sax.*;
050: import javax.xml.parsers.*;
051: import javax.xml.transform.*;
052: import javax.xml.transform.sax.*;
053: import javax.xml.transform.stream.*;
054:
055: import org.netbeans.api.xml.cookies.*;
056: import org.netbeans.spi.xml.cookies.*;
057:
058: /**
059: *
060: * @author Libor Kramolis
061: */
062: public class TransformableSupportTest extends NbTestCase {
063:
064: public TransformableSupportTest(java.lang.String testName) {
065: super (testName);
066: }
067:
068: public static void main(java.lang.String[] args) {
069: junit.textui.TestRunner.run(suite());
070: }
071:
072: public static Test suite() {
073: TestSuite suite = new NbTestSuite(
074: TransformableSupportTest.class);
075: return suite;
076: }
077:
078: public void testTransform() {
079: assertTrue("Correct XML and correct XSLT must pass!",
080: transform("data/doc.xml", "data/doc2xhtml.xsl"));
081: assertTrue("Incorrect XML and correct XSLT must not pass!",
082: false == transform("data/InvalidDocument.xml",
083: "data/doc2xhtml.xsl"));
084: assertTrue("Correct XML and incorrect XSLT must not pass!",
085: false == transform("data/doc.xml",
086: "data/InvalidDocument.xml"));
087: assertTrue("Incrrect XML and incorrect XSLT must not pass!",
088: false == transform("data/InvalidDocument.xml",
089: "data/InvalidDocument.xml"));
090: }
091:
092: private boolean transform(String xml, String xslt) {
093: URL xmlURL = getClass().getResource(xml);
094: URL xsltURL = getClass().getResource(xslt);
095: Source xmlSource = new SAXSource(new InputSource(xmlURL
096: .toExternalForm()));
097: Source xsltSource = new SAXSource(new InputSource(xsltURL
098: .toExternalForm()));
099: Result outputResult = new StreamResult(new StringWriter());
100:
101: TransformableSupport support = new TransformableSupport(
102: xmlSource);
103: Observer observer = new Observer(); // not yet used
104: boolean exceptionThrown = false;
105: try {
106: support.transform(xsltSource, outputResult, null);
107: } catch (TransformerException exc) {
108: System.err.println("!!! " + exc);
109: exceptionThrown = true;
110: }
111:
112: System.out.println(xml + " & " + xslt + " => "
113: + (exceptionThrown ? "WRONG" : "OK"));
114: return exceptionThrown == false;
115: }
116:
117: //
118: // class Observer
119: //
120:
121: private static class Observer implements CookieObserver {
122: private int receives;
123: private int warnings;
124:
125: public void receive(CookieMessage msg) {
126: receives++;
127: if (msg.getLevel() >= msg.WARNING_LEVEL) {
128: warnings++;
129: }
130: }
131:
132: public int getWarnings() {
133: return warnings;
134: }
135:
136: }
137:
138: }
|