1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.rewriter;
18  
19  import java.io.File;
20  import java.io.FileReader;
21  import java.io.FileWriter;
22  import java.io.IOException;
23  import java.util.Arrays;
24  
25  import org.apache.jetspeed.rewriter.html.neko.NekoParserAdaptor;
26  import org.apache.jetspeed.rewriter.rules.Ruleset;
27  import org.apache.jetspeed.rewriter.xml.SaxParserAdaptor;
28  
29  import junit.framework.Test;
30  import junit.framework.TestCase;
31  import junit.framework.TestSuite;
32  
33  
34  /***
35   * TestNekoRewriter
36   * 
37   * @author <a href="mailto:dyoung@phase2systems.com">David L Young</a>
38   * @version $Id:$
39   */
40  public class TestNekoRewriter extends TestCase
41  {
42  
43      /***
44       * Defines the testcase name for JUnit.
45       * 
46       * @param name
47       *            the testcase's name.
48       */
49      public TestNekoRewriter(String name)
50      {
51          super(name);
52      }
53  
54      public String getBaseProject()
55      {
56          return "components/jetspeed";
57      }
58  
59      /***
60       * Start the tests.
61       * 
62       * @param args
63       *            the arguments. Not used
64       */
65      public static void main(String args[])
66      {
67          junit.awtui.TestRunner.main(new String[]
68          { TestNekoRewriter.class.getName()});
69      }
70  
71      public static Test suite()
72      {
73          // All methods starting with "test" will be executed in the test suite.
74          return new TestSuite(TestNekoRewriter.class);
75      }
76      
77      
78      // DOMParser example
79      
80      /* BOZO
81  
82      public void testDOM() throws Exception
83      {
84          System.out.println( "testing...DOM" ) ;
85  
86          // parse something and echo the DOM tree
87          String target = "http://www.google.com" ;
88          System.out.println( "Parsing: " + target ) ;
89          
90          DOMParser parser = new DOMParser() ;
91          parser.parse( target ) ;
92          System.out.println( "parse() result..." ) ;
93          print( parser.getDocument(), "" ) ;
94      }
95  
96      void print( Node node, String indent )
97      {
98          System.out.println(indent+node.getClass().getName());
99          Node child = node.getFirstChild();
100         while (child != null) {
101             print(child, indent+" ");
102             child = child.getNextSibling();
103         }
104     }
105     */
106     
107     
108     // SAXParser example
109     
110     /* BOZO
111 
112     public void testSAX() throws Exception
113     {
114         System.out.println( "testing...SAX" ) ;
115 
116         // parse something to stdout
117         String target = "http://www.google.com" ;
118         System.out.println( "Parsing: " + target ) ;
119         
120         SAXParser parser = new SAXParser() ;
121 
122         // create pipeline filters
123         org.cyberneko.html.filters.Writer writer = new org.cyberneko.html.filters.Writer();
124         Purifier purifier = new Purifier() ;
125 
126         // setup filter chain
127         XMLDocumentFilter[] filters = {
128             purifier,
129             writer,
130         };
131 
132         parser.setProperty("http://cyberneko.org/html/properties/filters", filters);
133 
134         // parse documents
135         XMLInputSource source = new XMLInputSource(null, target, null);
136         parser.parse(source);
137     }
138     */
139     
140     
141     
142     // NekoParserAdapter test
143     
144     public void testNekoParserAdaptor() throws Exception
145     {
146         RewriterController controller = getController();
147         FileReader rulesReader = getTestReader("test-remove-rules.xml");
148         Ruleset ruleset = controller.loadRuleset(rulesReader);
149         rulesReader.close();
150         assertNotNull("ruleset is null", ruleset);
151         RulesetRewriter rewriter = controller.createRewriter(ruleset);
152         assertNotNull("ruleset rewriter is null", rewriter);
153         
154         FileReader htmlReader = getTestReader("test-001.html");
155         FileWriter htmlWriter = getTestWriter("test-002-output.html");
156 
157         ParserAdaptor adaptor = controller.createParserAdaptor("text/html");
158         rewriter.setBaseUrl("http://www.rewriter.com");
159         rewriter.rewrite(adaptor, htmlReader, htmlWriter);
160         htmlReader.close();
161     }
162     
163     private RewriterController getController() throws Exception
164     {
165         Class[] rewriterClasses = new Class[]{ RulesetRewriterImpl.class, RulesetRewriterImpl.class};
166         
167         Class[] adaptorClasses = new Class[]{ NekoParserAdaptor.class, SaxParserAdaptor.class};
168         return new JetspeedRewriterController("test/WEB-INF/conf/rewriter-rules-mapping.xml", Arrays.asList(rewriterClasses), Arrays.asList(adaptorClasses));
169     }
170 
171     /*
172     private Reader getRemoteReader(String uri) throws IOException
173     {
174         HttpClient client = new HttpClient();
175         GetMethod get = new GetMethod(uri);
176         client.executeMethod(get);
177         BufferedInputStream bis = new BufferedInputStream(get.getResponseBodyAsStream());
178         String encoding = get.getResponseCharSet();
179         return new InputStreamReader(bis, encoding);
180     }
181     */
182     
183     /***
184      * Gets a reader for a given filename in the test directory.
185      * 
186      * @return A file reader to the test rules file
187      * @throws IOException
188      */
189     private FileReader getTestReader(String filename) throws IOException
190     {
191         return new FileReader("test/rewriter/" + filename);
192     }
193 
194     /***
195      * Gets a writer for a given filename in the test directory.
196      * 
197      * @return A file reader to the test rules file
198      * @throws IOException
199      */
200     private FileWriter getTestWriter(String filename) throws IOException
201     {
202         new File("target/test/rewriter").mkdirs();
203         return new FileWriter("target/test/rewriter/" + filename);
204     }
205     
206     
207     /* BOZO
208     public void testNekoWebTarget() throws Exception
209     {        
210         // parse something with the NekoParserAdaptor
211         String target = "http://www.google.com";
212                 
213         RewriterController controller = getController();
214         FileReader rulesReader = getTestReader("test-remove-rules.xml");
215         Ruleset ruleset = controller.loadRuleset(rulesReader);
216         rulesReader.close();
217         assertNotNull("ruleset is null", ruleset);
218         RulesetRewriter rewriter = controller.createRewriter(ruleset);
219         assertNotNull("ruleset rewriter is null", rewriter);
220 
221         java.io.Reader htmlReader = getRemoteReader(target);
222         java.io.Writer htmlWriter = new OutputStreamWriter(System.out);
223 
224         ParserAdaptor adaptor = controller.createParserAdaptor("text/html");
225         rewriter.setBaseUrl("http://www.rewriter.com");
226         rewriter.rewrite(adaptor, htmlReader, htmlWriter);
227         htmlReader.close();
228     }
229     */
230 }