001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.rewriter;
018:
019: import java.io.File;
020: import java.io.FileReader;
021: import java.io.FileWriter;
022: import java.io.IOException;
023: import java.util.Arrays;
024:
025: import org.apache.jetspeed.rewriter.html.neko.NekoParserAdaptor;
026: import org.apache.jetspeed.rewriter.rules.Ruleset;
027: import org.apache.jetspeed.rewriter.xml.SaxParserAdaptor;
028:
029: import junit.framework.Test;
030: import junit.framework.TestCase;
031: import junit.framework.TestSuite;
032:
033: /**
034: * TestNekoRewriter
035: *
036: * @author <a href="mailto:dyoung@phase2systems.com">David L Young</a>
037: * @version $Id:$
038: */
039: public class TestNekoRewriter extends TestCase {
040:
041: /**
042: * Defines the testcase name for JUnit.
043: *
044: * @param name
045: * the testcase's name.
046: */
047: public TestNekoRewriter(String name) {
048: super (name);
049: }
050:
051: public String getBaseProject() {
052: return "components/jetspeed";
053: }
054:
055: /**
056: * Start the tests.
057: *
058: * @param args
059: * the arguments. Not used
060: */
061: public static void main(String args[]) {
062: junit.awtui.TestRunner
063: .main(new String[] { TestNekoRewriter.class.getName() });
064: }
065:
066: public static Test suite() {
067: // All methods starting with "test" will be executed in the test suite.
068: return new TestSuite(TestNekoRewriter.class);
069: }
070:
071: // DOMParser example
072:
073: /* BOZO
074:
075: public void testDOM() throws Exception
076: {
077: System.out.println( "testing...DOM" ) ;
078:
079: // parse something and echo the DOM tree
080: String target = "http://www.google.com" ;
081: System.out.println( "Parsing: " + target ) ;
082:
083: DOMParser parser = new DOMParser() ;
084: parser.parse( target ) ;
085: System.out.println( "parse() result..." ) ;
086: print( parser.getDocument(), "" ) ;
087: }
088:
089: void print( Node node, String indent )
090: {
091: System.out.println(indent+node.getClass().getName());
092: Node child = node.getFirstChild();
093: while (child != null) {
094: print(child, indent+" ");
095: child = child.getNextSibling();
096: }
097: }
098: */
099:
100: // SAXParser example
101: /* BOZO
102:
103: public void testSAX() throws Exception
104: {
105: System.out.println( "testing...SAX" ) ;
106:
107: // parse something to stdout
108: String target = "http://www.google.com" ;
109: System.out.println( "Parsing: " + target ) ;
110:
111: SAXParser parser = new SAXParser() ;
112:
113: // create pipeline filters
114: org.cyberneko.html.filters.Writer writer = new org.cyberneko.html.filters.Writer();
115: Purifier purifier = new Purifier() ;
116:
117: // setup filter chain
118: XMLDocumentFilter[] filters = {
119: purifier,
120: writer,
121: };
122:
123: parser.setProperty("http://cyberneko.org/html/properties/filters", filters);
124:
125: // parse documents
126: XMLInputSource source = new XMLInputSource(null, target, null);
127: parser.parse(source);
128: }
129: */
130:
131: // NekoParserAdapter test
132: public void testNekoParserAdaptor() throws Exception {
133: RewriterController controller = getController();
134: FileReader rulesReader = getTestReader("test-remove-rules.xml");
135: Ruleset ruleset = controller.loadRuleset(rulesReader);
136: rulesReader.close();
137: assertNotNull("ruleset is null", ruleset);
138: RulesetRewriter rewriter = controller.createRewriter(ruleset);
139: assertNotNull("ruleset rewriter is null", rewriter);
140:
141: FileReader htmlReader = getTestReader("test-001.html");
142: FileWriter htmlWriter = getTestWriter("test-002-output.html");
143:
144: ParserAdaptor adaptor = controller
145: .createParserAdaptor("text/html");
146: rewriter.setBaseUrl("http://www.rewriter.com");
147: rewriter.rewrite(adaptor, htmlReader, htmlWriter);
148: htmlReader.close();
149: }
150:
151: private RewriterController getController() throws Exception {
152: Class[] rewriterClasses = new Class[] {
153: RulesetRewriterImpl.class, RulesetRewriterImpl.class };
154:
155: Class[] adaptorClasses = new Class[] { NekoParserAdaptor.class,
156: SaxParserAdaptor.class };
157: return new JetspeedRewriterController(
158: "test/WEB-INF/conf/rewriter-rules-mapping.xml", Arrays
159: .asList(rewriterClasses), Arrays
160: .asList(adaptorClasses));
161: }
162:
163: /*
164: private Reader getRemoteReader(String uri) throws IOException
165: {
166: HttpClient client = new HttpClient();
167: GetMethod get = new GetMethod(uri);
168: client.executeMethod(get);
169: BufferedInputStream bis = new BufferedInputStream(get.getResponseBodyAsStream());
170: String encoding = get.getResponseCharSet();
171: return new InputStreamReader(bis, encoding);
172: }
173: */
174:
175: /**
176: * Gets a reader for a given filename in the test directory.
177: *
178: * @return A file reader to the test rules file
179: * @throws IOException
180: */
181: private FileReader getTestReader(String filename)
182: throws IOException {
183: return new FileReader("test/rewriter/" + filename);
184: }
185:
186: /**
187: * Gets a writer for a given filename in the test directory.
188: *
189: * @return A file reader to the test rules file
190: * @throws IOException
191: */
192: private FileWriter getTestWriter(String filename)
193: throws IOException {
194: new File("target/test/rewriter").mkdirs();
195: return new FileWriter("target/test/rewriter/" + filename);
196: }
197:
198: /* BOZO
199: public void testNekoWebTarget() throws Exception
200: {
201: // parse something with the NekoParserAdaptor
202: String target = "http://www.google.com";
203:
204: RewriterController controller = getController();
205: FileReader rulesReader = getTestReader("test-remove-rules.xml");
206: Ruleset ruleset = controller.loadRuleset(rulesReader);
207: rulesReader.close();
208: assertNotNull("ruleset is null", ruleset);
209: RulesetRewriter rewriter = controller.createRewriter(ruleset);
210: assertNotNull("ruleset rewriter is null", rewriter);
211:
212: java.io.Reader htmlReader = getRemoteReader(target);
213: java.io.Writer htmlWriter = new OutputStreamWriter(System.out);
214:
215: ParserAdaptor adaptor = controller.createParserAdaptor("text/html");
216: rewriter.setBaseUrl("http://www.rewriter.com");
217: rewriter.rewrite(adaptor, htmlReader, htmlWriter);
218: htmlReader.close();
219: }
220: */
221: }
|