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.content;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  import junit.framework.TestCase;
23  import nl.hippo.client.api.ClientException;
24  import nl.hippo.client.webdav.binding.content.DocumentCollectionImpl;
25  
26  import org.jibx.runtime.BindingDirectory;
27  import org.jibx.runtime.IBindingFactory;
28  import org.jibx.runtime.IMarshallingContext;
29  import org.jibx.runtime.IUnmarshallingContext;
30  
31  //TODO: Use org.jibx.extras.TestRoundtrip for this.
32  
33  public class SearchResultBindingTest extends TestCase {
34  
35  	private InputStream in;
36  
37  
38  	public void setUp() {
39  		in = SearchResultBindingTest.class.getResourceAsStream("resources/searchresult.xml");
40  	}
41  
42  
43  	/**
44  	 * Unmarshal the sample document from a file, then marshal it back out to
45  	 * a Stream
46  	 * @throws ClientException 
47  	 * @throws IOException 
48  	 */
49  	public void test() throws Exception {
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(DocumentCollectionImpl.class);
55  
56  		// unmarshal customer information from file
57  		IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
58  		DocumentCollectionImpl searchResult = (DocumentCollectionImpl) uctx.unmarshalDocument(in, null);
59  
60  		// you can add code here to alter the unmarshalled document
61  		//System.out.println(searchResult.toString());
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(searchResult, "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  }