View Javadoc

1   /*
2    * $Id: SiteGraphTest.java 454251 2006-10-09 02:10:57Z husted $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.sitegraph;
19  
20  import java.io.File;
21  import java.io.InputStream;
22  import java.io.StringWriter;
23  import java.net.URL;
24  
25  import org.apache.struts2.StrutsTestCase;
26  import org.apache.struts2.dispatcher.Dispatcher;
27  
28  import com.opensymphony.xwork2.util.ClassLoaderUtil;
29  
30  /***
31   */
32  public class SiteGraphTest extends StrutsTestCase {
33      public void testWebFlow() throws Exception {
34          Dispatcher.getInstance().getConfigurationManager().clearConfigurationProviders();
35          // use the classloader rather than relying on the
36          // working directory being an assumed value when
37          // running the test:  so let's get this class's parent dir 
38          URL url = ClassLoaderUtil.getResource("org/apache/struts2/sitegraph/struts.xml", SiteGraphTest.class);
39          File file = new File(url.toString().substring(5));
40          String dir = file.getParent();
41  
42          SiteGraph siteGraph = new SiteGraph(dir, dir, dir, "");
43          StringWriter writer = new StringWriter();
44          siteGraph.setWriter(writer);
45          siteGraph.prepare();
46  
47          URL compare = SiteGraphTest.class.getResource("out.txt");
48          StringBuffer buffer = new StringBuffer(128);
49          InputStream in = compare.openStream();
50          byte[] buf = new byte[4096];
51          int nbytes;
52  
53          while ((nbytes = in.read(buf)) > 0) {
54              buffer.append(new String(buf, 0, nbytes));
55          }
56  
57          in.close();
58          assertEquals(buffer.toString().replaceAll("\r\n", "\n"), writer.toString().replaceAll("\r\n", "\n"));
59      }
60  }