1   /*
2    * Copyright 2008 Hippo
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" 
12   * BASIS, 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 nl.hippo.client.webdav.binding.facets;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.InputStream;
20  
21  import nl.hippo.client.api.ClientException;
22  import nl.hippo.client.api.content.FacetCollection;
23  import nl.hippo.client.webdav.binding.facets.FacetCollectionImpl;
24  
25  import org.jibx.runtime.BindingDirectory;
26  import org.jibx.runtime.IBindingFactory;
27  import org.jibx.runtime.IMarshallingContext;
28  import org.jibx.runtime.IUnmarshallingContext;
29  import org.jibx.runtime.JiBXException;
30  
31  import junit.framework.TestCase;
32  
33  //TODO: Use org.jibx.extras.TestRoundtrip for this.
34  
35  public class FacetsResultBindingTest extends TestCase {
36  	
37  	 private InputStream in;
38  
39  
40  	  public void setUp() {
41  	    in = FacetsResultBindingTest.class.getResourceAsStream("resources/facets.xml");
42  	  }
43  	  
44  	  /**
45  	   * Unmarshal the sample document from a file, then marshal it back out to
46  	   * a Stream
47  	   * @throws ClientException 
48  	   */
49  	  public void test() throws JiBXException, ClientException {
50  
51  	    // note that you can use multiple bindings with the same class, in
52  	    //  which case you need to use the getFactory() call that takes the
53  	    //  binding name as the first parameter
54  	    IBindingFactory bfact = BindingDirectory.getFactory(FacetCollectionImpl.class);
55  
56  	    // unmarshal customer information from file
57  	    IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
58  	    FacetCollection facetCollection = (FacetCollection) uctx.unmarshalDocument(in, null);
59  	    //System.out.println(facetCollection);
60  	    
61  	    // you can add code here to alter the unmarshalled document
62  
63  	    // marshal object back out to file (with nice indentation, as UTF-8)
64  	    IMarshallingContext mctx = bfact.createMarshallingContext();
65  	    mctx.setIndent(2);
66  
67  	    ByteArrayOutputStream out = new ByteArrayOutputStream();
68  	    mctx.marshalDocument(facetCollection, "UTF-8", null, out);
69  
70  	    //TODO: Use org.jibx.extras.DocumentComparator from the jixb "extras" jar.
71  	    String result = new String(out.toByteArray());
72  	    System.out.println(result);
73  	  }
74  }