1   
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one or more
4    * contributor license agreements.  See the NOTICE file distributed with
5    * this work for additional information regarding copyright ownership.
6    * The ASF licenses this file to You under the Apache License, Version 2.0
7    * (the "License"); you may not use this file except in compliance with
8    * the License.  You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */ 
18  package org.apache.commons.betwixt.digester;
19  
20  import java.io.FileInputStream;
21  import java.io.InputStream;
22  import java.io.StringWriter;
23  
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  import junit.textui.TestRunner;
27  
28  import org.apache.commons.betwixt.AbstractTestCase;
29  import org.apache.commons.betwixt.io.BeanReader;
30  import org.apache.commons.betwixt.io.BeanWriter;
31  
32  //import org.apache.commons.logging.Log;
33  //import org.apache.commons.logging.impl.SimpleLog;
34  //import org.apache.commons.betwixt.expression.MethodUpdater;
35  //import org.apache.commons.betwixt.io.BeanCreateRule;
36  //import org.apache.commons.betwixt.io.BeanRuleSet;
37  
38  
39  /** Test harness for ID-IDRef reading.
40    *
41    * @author Robert Burrell Donkin
42    * @version $Revision: 438373 $
43    */
44  public class TestIDRead extends AbstractTestCase {
45  
46      public static void main( String[] args ) {
47          TestRunner.run( suite() );
48      }
49  
50      public static Test suite() {
51          return new TestSuite(TestIDRead.class);
52      }
53  
54      public TestIDRead(String testName) {
55          super(testName);
56      }
57  
58      public void testSimpleRead() throws Exception {
59          StringWriter out = new StringWriter();
60          out.write("<?xml version='1.0'?>");
61          BeanWriter writer = new BeanWriter(out);
62          writer.getBindingConfiguration().setMapIDs(false);
63          IDBean bean = new IDBean("alpha","one");
64          bean.addChild(new IDBean("beta","two"));
65          bean.addChild(new IDBean("gamma","three"));
66          writer.write(bean);
67          
68          String xml = "<IDBean><name>one</name><children><child><name>two</name><children/>"
69                  + "<id>beta</id></child><child><name>three</name><children/>"
70                  + "<id>gamma</id></child></children><id>alpha</id></IDBean>";
71                  
72          xmlAssertIsomorphicContent(
73                      parseString(xml),
74                      parseString(out.getBuffer().toString()),
75                      true);
76          
77          BeanReader reader = new BeanReader();
78          
79  //         logging just for this method
80  //        SimpleLog log = new SimpleLog("[testSimpleRead:XMLIntrospectorHelper]");
81  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
82  //        XMLIntrospectorHelper.setLog(log);
83          
84  //        log = new SimpleLog("[testSimpleRead:MethodUpdater]");
85  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
86  //        MethodUpdater.setLog(log);
87          
88  //        log = new SimpleLog("[testSimpleRead:BeanCreateRule]");
89  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
90  //        BeanCreateRule.setLog(log);
91          
92  //        log = new SimpleLog("[testSimpleRead:BeanRuleSet]");
93  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
94  //        BeanRuleSet.setLog(log);        
95  
96  //        log = new SimpleLog("[testSimpleRead:IDBean]");
97  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
98  //        IDBean.log = log;
99  
100 //        log = new SimpleLog("[testSimpleRead:BeanReader]");
101 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);        
102 //        reader.setLog(log);
103 
104 //        log = new SimpleLog("[testSimpleRead:XMLIntrospector]");
105 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
106 //        reader.getXMLIntrospector().setLog(log);
107         
108         reader.registerBeanClass( IDBean.class );
109 
110         InputStream in = new FileInputStream( 
111             getTestFile("src/test/org/apache/commons/betwixt/digester/SimpleReadTest.xml") );
112             
113         try {        
114 //            log = new SimpleLog("[testSimpleRead]");
115 //            log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
116             Object obj = reader.parse( in );
117 //            log.debug(obj);
118             
119             assertEquals("Read bean type is incorrect", true, (obj instanceof IDBean) );
120             IDBean alpha = (IDBean) obj;
121             
122             assertEquals("Wrong list size", 2 ,  alpha.getChildren().size());
123             
124             IDBean beta = (IDBean) alpha.getChildren().get(0);
125             assertEquals("Wrong name (A)", "beta" ,  beta.getName());
126             
127             IDBean gamma = (IDBean) alpha.getChildren().get(1);
128             assertEquals("Wrong name (B)", "gamma" ,  gamma.getName());
129         }
130         finally {
131             in.close();
132         }
133     }
134     
135     public void testIDRead() throws Exception {
136         
137         BeanReader reader = new BeanReader();
138         
139 //         logging just for this method
140 //        SimpleLog log = new SimpleLog("[testIDRead:XMLIntrospectorHelper]");
141 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
142 //        XMLIntrospectorHelper.setLog(log);
143 //        
144 //        log = new SimpleLog("[testIDRead:BeanCreateRule]");
145 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
146 //        BeanCreateRule.setLog(log);
147 //
148 //        log = new SimpleLog("[testIDRead:BeanReader]");
149 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);        
150 //        reader.setLog(log);
151 //
152 //        log = new SimpleLog("[testIDRead:XMLIntrospector]");
153 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
154 //        reader.getXMLIntrospector().setLog(log);
155         
156         reader.registerBeanClass( IDBean.class );
157 
158         InputStream in = new FileInputStream( 
159             getTestFile("src/test/org/apache/commons/betwixt/digester/IDTest1.xml") );
160             
161         try {
162             Object obj = reader.parse( in );
163             
164             assertEquals("Read bean type is incorrect", true, (obj instanceof IDBean) );
165             IDBean alpha = (IDBean) obj;
166             
167             assertEquals("Wrong list size (A)", 2 ,  alpha.getChildren().size());
168             
169             IDBean beta = (IDBean) alpha.getChildren().get(0);
170             assertEquals("Wrong name (A)", "beta" ,  beta.getName());
171             
172             IDBean gamma = (IDBean) alpha.getChildren().get(1);
173             assertEquals("Wrong name (B)", "gamma" ,  gamma.getName());
174             assertEquals("Wrong list size (B)", 2 ,  gamma.getChildren().size());
175             
176             IDBean sonOfGamma = (IDBean) gamma.getChildren().get(1);
177             
178             assertEquals("Wrong id (A)", "two" ,  sonOfGamma.getId());
179             assertEquals("Wrong name (C)", "beta" ,  sonOfGamma.getName());
180             
181             assertEquals("IDREF bean not equal to ID bean", beta,  sonOfGamma);
182         }
183         finally {
184             in.close();
185         }
186     }
187 }