1   /*
2    * Copyright 2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.pluto.descriptors.servlet;
17  
18  import java.io.FileWriter;
19  import java.io.InputStream;
20  import java.io.InputStreamReader;
21  import java.net.URL;
22  
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  import org.apache.xml.serialize.OutputFormat;
27  import org.apache.xml.serialize.XMLSerializer;
28  import org.exolab.castor.mapping.Mapping;
29  import org.exolab.castor.xml.Marshaller;
30  import org.exolab.castor.xml.Unmarshaller;
31  
32  /***
33   *
34   * @author <a href="ddewolf@apache.org">David H. DeWolf</a>
35   * @version $Id: CastorMappingTest.java 156743 2005-03-10 05:50:30Z ddewolf $
36   * @since Mar 4, 2005
37   */
38  public class CastorMappingTest extends TestCase {
39  
40      private boolean debug = false;
41  
42  
43  //
44      public void setUp() throws Exception {
45          this.debug = false;
46      }
47  
48      public void testUnmarshalWebXml() throws Exception {
49          URL webXmlMapping = getClass().getResource("castor-web-xml-mapping.xml");
50          Mapping mapping = new Mapping();
51          mapping.loadMapping(webXmlMapping);
52          InputStream webXml = getClass().getResourceAsStream("web.xml");
53          Unmarshaller unmarshaller = new Unmarshaller(mapping);
54          unmarshaller.setIgnoreExtraElements(false);
55          unmarshaller.setDebug(debug);
56          WebAppDD config = (WebAppDD)unmarshaller.unmarshal(new InputStreamReader(webXml));
57          assertTrue(config!=null);
58      }
59  
60      public void testMarshalWebXml() throws Exception {
61          URL webXmlMapping = getClass().getResource("castor-web-xml-mapping.xml");
62          Mapping mapping = new Mapping();
63          mapping.loadMapping(webXmlMapping);
64          InputStream webXml = getClass().getResourceAsStream("web.xml");
65          Unmarshaller unmarshaller = new Unmarshaller(mapping);
66          WebAppDD config = (WebAppDD)unmarshaller.unmarshal(new InputStreamReader(webXml));
67  
68             OutputFormat of = new OutputFormat();
69              of.setIndenting(true);
70              of.setIndent(4); // 2-space indention
71              of.setLineWidth(16384);
72              // As large as needed to prevent linebreaks in text nodes
73              of.setDoctype(
74                   "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN",
75                   "http://java.sun.com/dtd/web-app_2_3.dtd");
76  
77              FileWriter writer = new FileWriter("newWeb.xml");
78              XMLSerializer serializer = new XMLSerializer(writer, of);
79          Marshaller marsh = new Marshaller(serializer.asDocumentHandler());
80          marsh.setDebug(debug);
81          marsh.setMapping(mapping);
82          marsh.marshal(config);
83  
84      }
85  
86  
87      /***
88       * Start the tests.
89       *
90       * @param theArgs the arguments. Not used
91       */
92      public static void main(String[] theArgs) {
93          junit.awtui.TestRunner.main(
94              new String[] { CastorMappingTest.class.getName()});
95      }
96  
97      /***
98       * @return a test suite (<code>TestSuite</code>) that includes all methods
99       *         starting with "test"
100      */
101     public static Test suite() {
102         // All methods starting with "test" will be executed in the test suite.
103         return new TestSuite(CastorMappingTest.class);
104     }
105 
106 }
107