1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * 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   **/
18  package org.codehaus.xfire.xmlbeans;
19  
20  import java.io.ByteArrayOutputStream;
21  import java.io.IOException;
22  import java.net.URL;
23  import javax.xml.stream.XMLInputFactory;
24  import javax.xml.stream.XMLStreamException;
25  import javax.xml.stream.XMLStreamReader;
26  import org.apache.xmlbeans.XmlObject;
27  import org.codehaus.xfire.AbstractXFireTest;
28  import org.codehaus.xfire.MessageContext;
29  import org.codehaus.xfire.service.SimpleService;
30  
31  /***
32   * @version $Revision: 1.2 $
33   */
34  public class XMLBeansTest 
35      extends AbstractXFireTest
36  {
37      public void testRequestResponse() throws Exception
38      {
39          ByteArrayOutputStream out = new ByteArrayOutputStream();
40          
41          MessageContext context = 
42              new MessageContext( "Test",
43                                  null,
44                                  out,
45                                  null,
46                                  null );
47          
48          XMLBeansHandler handler = getXMLBeansHandler();
49          
50          handler.invoke(context, 
51              createXMLStreamReader(getClass().getResource("sampleRequest.xml" )));
52          
53          XmlObject xmlObject = handler.getObject();
54  
55          assertTrue( "should have received an object: " + xmlObject,
56                      xmlObject != null );
57          
58          System.out.println( "Parsed: " + xmlObject );
59          System.out.println( "Response: " + out.toString() );
60          
61          // is there an easy way to test if the 
62          // response is equal to the request, ignoring whitespace et al?
63          assertTrue( out.toString().length() > 0 );
64      }
65  
66      /***
67       * Test the handler using the XFire service mechanism.
68       */
69      public void testService() throws Exception
70      {
71          SimpleService service = new SimpleService();
72          service.setName("Test");
73          
74          getServiceRegistry().register( service );
75  
76          XMLBeansHandler handler = getXMLBeansHandler();
77          service.setServiceHandler( handler );
78          
79          ByteArrayOutputStream out = new ByteArrayOutputStream();
80          MessageContext context = 
81              new MessageContext( "Test",
82                                  null,
83                                  out,
84                                  null,
85                                  null );
86          getXFire().invoke( 
87              getClass().getResourceAsStream("sampleRequest.xml" ),
88              context );
89          
90          XmlObject xmlObject = handler.getObject();
91          
92          assertTrue( "should have received an object: " + xmlObject,
93                      xmlObject != null );
94  
95          // is there an easy way to test if the 
96          // response is equal to the request, ignoring whitespace et al?
97          assertTrue( out.toString().length() > 0 );
98      }
99      
100     public XMLBeansHandler getXMLBeansHandler()
101     {
102         XMLBeansHandler handler = new XMLBeansHandler()
103         {
104             protected void handleBody( MessageContext context, XmlObject body ) 
105                 throws Exception
106             {
107                 reply( context, body );
108             }
109         };
110         
111         return handler;
112     }
113     protected XMLStreamReader createXMLStreamReader( URL resource )
114             throws XMLStreamException, IOException
115     {
116         assertTrue( "Found resource", resource != null );
117         XMLInputFactory inputFactory = XMLInputFactory.newInstance();
118         XMLStreamReader in = inputFactory.createXMLStreamReader( resource
119                 .openStream() );
120         
121         return in;
122     }
123 }