1   
2   /*
3    * Copyright 2001-2004 The Apache Software Foundation.
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  package org.apache.commons.betwixt.digester;
18  
19  import java.io.FileInputStream;
20  import java.io.InputStream;
21  import java.io.StringWriter;
22  
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  import junit.textui.TestRunner;
26  
27  import org.apache.commons.betwixt.AbstractTestCase;
28  import org.apache.commons.betwixt.io.BeanReader;
29  import org.apache.commons.betwixt.io.BeanWriter;
30  
31  //import org.apache.commons.logging.Log;
32  //import org.apache.commons.logging.impl.SimpleLog;
33  //import org.apache.commons.betwixt.expression.MethodUpdater;
34  //import org.apache.commons.betwixt.io.BeanCreateRule;
35  //import org.apache.commons.betwixt.io.BeanRuleSet;
36  
37  
38  /*** Test harness for ID-IDRef reading.
39    *
40    * @author Robert Burrell Donkin
41    * @version $Revision: 155402 $
42    */
43  public class TestIDRead extends AbstractTestCase {
44  
45      public static void main( String[] args ) {
46          TestRunner.run( suite() );
47      }
48  
49      public static Test suite() {
50          return new TestSuite(TestIDRead.class);
51      }
52  
53      public TestIDRead(String testName) {
54          super(testName);
55      }
56  
57      public void testSimpleRead() throws Exception {
58          StringWriter out = new StringWriter();
59          out.write("<?xml version='1.0'?>");
60          BeanWriter writer = new BeanWriter(out);
61          writer.getBindingConfiguration().setMapIDs(false);
62          IDBean bean = new IDBean("alpha","one");
63          bean.addChild(new IDBean("beta","two"));
64          bean.addChild(new IDBean("gamma","three"));
65          writer.write(bean);
66          
67          String xml = "<IDBean><name>one</name><children><child><name>two</name><children/>"
68                  + "<id>beta</id></child><child><name>three</name><children/>"
69                  + "<id>gamma</id></child></children><id>alpha</id></IDBean>";
70                  
71          xmlAssertIsomorphicContent(
72                      parseString(xml),
73                      parseString(out.getBuffer().toString()),
74                      true);
75          
76          BeanReader reader = new BeanReader();
77          
78  //         logging just for this method
79  //        SimpleLog log = new SimpleLog("[testSimpleRead:XMLIntrospectorHelper]");
80  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
81  //        XMLIntrospectorHelper.setLog(log);
82          
83  //        log = new SimpleLog("[testSimpleRead:MethodUpdater]");
84  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
85  //        MethodUpdater.setLog(log);
86          
87  //        log = new SimpleLog("[testSimpleRead:BeanCreateRule]");
88  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
89  //        BeanCreateRule.setLog(log);
90          
91  //        log = new SimpleLog("[testSimpleRead:BeanRuleSet]");
92  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
93  //        BeanRuleSet.setLog(log);        
94  
95  //        log = new SimpleLog("[testSimpleRead:IDBean]");
96  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
97  //        IDBean.log = log;
98  
99  //        log = new SimpleLog("[testSimpleRead:BeanReader]");
100 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);        
101 //        reader.setLog(log);
102 
103 //        log = new SimpleLog("[testSimpleRead:XMLIntrospector]");
104 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
105 //        reader.getXMLIntrospector().setLog(log);
106         
107         reader.registerBeanClass( IDBean.class );
108 
109         InputStream in = new FileInputStream( 
110             getTestFile("src/test/org/apache/commons/betwixt/digester/SimpleReadTest.xml") );
111             
112         try {        
113 //            log = new SimpleLog("[testSimpleRead]");
114 //            log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
115             Object obj = reader.parse( in );
116 //            log.debug(obj);
117             
118             assertEquals("Read bean type is incorrect", true, (obj instanceof IDBean) );
119             IDBean alpha = (IDBean) obj;
120             
121             assertEquals("Wrong list size", 2 ,  alpha.getChildren().size());
122             
123             IDBean beta = (IDBean) alpha.getChildren().get(0);
124             assertEquals("Wrong name (A)", "beta" ,  beta.getName());
125             
126             IDBean gamma = (IDBean) alpha.getChildren().get(1);
127             assertEquals("Wrong name (B)", "gamma" ,  gamma.getName());
128         }
129         finally {
130             in.close();
131         }
132     }
133     
134     public void testIDRead() throws Exception {
135         
136         BeanReader reader = new BeanReader();
137         
138 //         logging just for this method
139 //        SimpleLog log = new SimpleLog("[testIDRead:XMLIntrospectorHelper]");
140 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
141 //        XMLIntrospectorHelper.setLog(log);
142 //        
143 //        log = new SimpleLog("[testIDRead:BeanCreateRule]");
144 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
145 //        BeanCreateRule.setLog(log);
146 //
147 //        log = new SimpleLog("[testIDRead:BeanReader]");
148 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);        
149 //        reader.setLog(log);
150 //
151 //        log = new SimpleLog("[testIDRead:XMLIntrospector]");
152 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
153 //        reader.getXMLIntrospector().setLog(log);
154         
155         reader.registerBeanClass( IDBean.class );
156 
157         InputStream in = new FileInputStream( 
158             getTestFile("src/test/org/apache/commons/betwixt/digester/IDTest1.xml") );
159             
160         try {
161             Object obj = reader.parse( in );
162             
163             assertEquals("Read bean type is incorrect", true, (obj instanceof IDBean) );
164             IDBean alpha = (IDBean) obj;
165             
166             assertEquals("Wrong list size (A)", 2 ,  alpha.getChildren().size());
167             
168             IDBean beta = (IDBean) alpha.getChildren().get(0);
169             assertEquals("Wrong name (A)", "beta" ,  beta.getName());
170             
171             IDBean gamma = (IDBean) alpha.getChildren().get(1);
172             assertEquals("Wrong name (B)", "gamma" ,  gamma.getName());
173             assertEquals("Wrong list size (B)", 2 ,  gamma.getChildren().size());
174             
175             IDBean sonOfGamma = (IDBean) gamma.getChildren().get(1);
176             
177             assertEquals("Wrong id (A)", "two" ,  sonOfGamma.getId());
178             assertEquals("Wrong name (C)", "beta" ,  sonOfGamma.getName());
179             
180             assertEquals("IDREF bean not equal to ID bean", beta,  sonOfGamma);
181         }
182         finally {
183             in.close();
184         }
185     }
186 }