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.commons.betwixt.strategy;
17  
18  import java.io.StringReader;
19  import java.io.StringWriter;
20  import java.util.Iterator;
21  
22  import org.apache.commons.betwixt.AbstractTestCase;
23  import org.apache.commons.betwixt.expression.Context;
24  import org.apache.commons.betwixt.io.BeanReader;
25  import org.apache.commons.betwixt.io.BeanWriter;
26  
27  /***
28   */
29  public class TestIdStorageStrategy extends AbstractTestCase {
30  
31      public TestIdStorageStrategy(String testName) {
32          super(testName);   
33      }
34  
35      public void testWrite() throws Exception {
36          
37          final Element alpha = new Element("ONE");
38          Element beta = new Element("TWO");
39          ElementsList elements = new ElementsList();
40          elements.addElement(alpha);
41          elements.addElement(beta);
42          
43          IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() {
44  
45              public String getReferenceFor(Context context, Object bean) {
46                  String result = null;
47                  if (bean == alpha) {
48                      result = "ALPHA";
49                  }
50                  else
51                  {
52                      result = super.getReferenceFor(context, bean);
53                  }
54                  return result;
55              }
56  
57              public void setReference(Context context, Object bean, String id) {
58                  if (bean != alpha) {
59                       super.setReference(context, bean, id);
60                  }
61              }            
62          };
63          
64          StringWriter out = new StringWriter();
65          out.write("<?xml version='1.0'?>");
66          BeanWriter writer = new BeanWriter(out);
67          writer.getBindingConfiguration().setIdMappingStrategy(storingStrategy);
68          writer.write(elements);
69          
70          String expected = "<?xml version='1.0'?>" +
71                  "<ElementsList id='1'>" +
72                  "   <elements>" +
73                  "       <element idref='ALPHA'/>" +
74                  "       <element id='2'>" +
75                  "           <value>TWO</value>" +
76                  "       </element>" +
77                  "   </elements>" +
78                  "</ElementsList>";
79  
80          xmlAssertIsomorphicContent(parseString(expected), parseString(out));
81      }
82      
83      public void testRead() throws Exception {
84          
85          String xml = "<?xml version='1.0'?>" +
86          "<ElementsList id='1'>" +
87          "   <elements>" +
88          "       <element idref='ALPHA'/>" +
89          "       <element id='2'>" +
90          "           <value>TWO</value>" +
91          "       </element>" +
92          "   </elements>" +
93          "</ElementsList>";
94          
95          final Element alpha = new Element("ONE");
96          
97          IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() {
98  
99              public void setReference(Context context, Object bean, String id) {
100                 if (bean != alpha) {
101                      super.setReference(context, bean, id);
102                 }
103             }     
104             
105             public Object getReferenced(Context context, String id) {
106                 if ("ALPHA".equals(id)) {
107                     return alpha;
108                 }
109                 return getReferenced(context, id);
110             }
111             
112         };
113         
114         BeanReader reader = new BeanReader();
115         reader.getBindingConfiguration().setIdMappingStrategy(storingStrategy);
116         reader.registerBeanClass(ElementsList.class);
117         ElementsList elements = (ElementsList) reader.parse(new StringReader(xml));
118         assertNotNull(elements);
119         Element one = elements.get(0);
120         assertTrue(one == alpha);
121         Element two = elements.get(1);
122         assertNotNull(two);
123     }
124     
125     
126 }