Debian Codegraph @

test/encoding/TestMultiRefIdentity.java

raw
1 /*
2 * Copyright 2002-2004 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
17 package test.encoding;
18
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22 import org.apache.axis.encoding.SerializationContext;
23
24 import javax.xml.namespace.QName;
25 import java.io.CharArrayWriter;
26
27 /**
28 * @author John Gregg ([email protected])
29 * @author $Author: gdaniels $
30 * @version $Revision: 258817 $
31 */
32 public class TestMultiRefIdentity extends TestCase {
33
34 public static Test suite() {
35 return new TestSuite(test.encoding.TestMultiRefIdentity.class);
36 }
37
38 public static void main(String[] argv) {
39
40 boolean swing = false;
41 if (argv.length > 0) {
42 if ("-swing".equals(argv[0])) {
43 swing = true;
44 }
45 }
46
47 if (swing) {
48 junit.swingui.TestRunner.main(new String[] {"test.encoding.TestMultiRefIdentity"});
49 } else {
50 System.out.println("use '-swing' for the Swing version.");
51 junit.textui.TestRunner.main(new String[] {"test.encoding.TestMultiRefIdentity"});
52 }
53 }
54
55
56 public TestMultiRefIdentity(String name) {
57 super(name);
58 }
59
60 /**
61 Tests when beans are identical and use default hashCode().
62 */
63 public void testIdentity1() throws Exception {
64 TestBeanA tb1 = new TestBeanA();
65 tb1.s1 = "john";
66 TestBeanA tb2 = tb1;
67
68 CharArrayWriter caw = new CharArrayWriter();
69 SerializationContext sci = new SerializationContext(caw);
70 sci.setDoMultiRefs(true);
71 sci.serialize(new QName("someLocalPart"), null, tb1);
72 sci.serialize(new QName("someOtherLocalPart"), null, tb2);
73
74 String s = caw.toString();
75
76 // Cheap but fragile.
77 int first = s.indexOf("#id0");
78 int last = s.lastIndexOf("#id0");
79 assertTrue(s, first >= 0);
80 assertTrue(s, last >= 0 && last != first);
81 }
82
83 /**
84 Tests when beans are identical and use their own hashCode().
85 */
86 public void testIdentity2() throws Exception {
87 TestBeanB tb1 = new TestBeanB();
88 tb1.s1 = "john";
89 TestBeanB tb2 = tb1;
90
91 CharArrayWriter caw = new CharArrayWriter();
92 SerializationContext sci = new SerializationContext(caw);
93 sci.setDoMultiRefs(true);
94 sci.serialize(new QName("someLocalPart"), null, tb1);
95 sci.serialize(new QName("someOtherLocalPart"), null, tb2);
96
97 String s = caw.toString();
98
99 // Cheap but fragile.
100 int first = s.indexOf("#id0");
101 int last = s.lastIndexOf("#id0");
102 assertTrue(s,first >= 0);
103 assertTrue(s,last >= 0 && last != first);
104 }
105
106 /**
107 Tests when beans have different contents and rely on default hashCode().
108 */
109 public void testEquality1() throws Exception {
110 TestBeanA tb1 = new TestBeanA();
111 tb1.s1 = "john";
112 TestBeanA tb2 = new TestBeanA();
113 tb2.s1 = "gregg";
114
115 CharArrayWriter caw = new CharArrayWriter();
116 SerializationContext sci = new SerializationContext(caw);
117 sci.setDoMultiRefs(true);
118 sci.serialize(new QName("someLocalPart"), null, tb1);
119 sci.serialize(new QName("someOtherLocalPart"), null, tb2);
120
121 String s = caw.toString();
122
123 // Cheap but fragile.
124 int first = s.indexOf("#id0");
125 int last = s.lastIndexOf("#id1");
126 assertTrue(s,first >= 0);
127 assertTrue(s,last >= 0);
128 }
129
130 /**
131 Tests when beans have same contents but rely on default hashCode().
132 */
133 public void testEquality2() throws Exception {
134 TestBeanA tb1 = new TestBeanA();
135 tb1.s1 = "john";
136 TestBeanA tb2 = new TestBeanA();
137 tb2.s1 = "john";
138
139 CharArrayWriter caw = new CharArrayWriter();
140 SerializationContext sci = new SerializationContext(caw);
141 sci.setDoMultiRefs(true);
142 sci.serialize(new QName("someLocalPart"), null, tb1);
143 sci.serialize(new QName("someOtherLocalPart"), null, tb2);
144
145 String s = caw.toString();
146
147 // Cheap but fragile.
148 int first = s.indexOf("#id0");
149 int last = s.lastIndexOf("#id1");
150 assertTrue(s,first >= 0);
151 assertTrue(s,last >= 0);
152 }
153
154 /**
155 Tests when beans have same contents and use their own hashCode().
156 */
157 public void testEquality3() throws Exception {
158 TestBeanB tb1 = new TestBeanB();
159 tb1.s1 = "john";
160 TestBeanB tb2 = new TestBeanB();
161 tb2.s1 = "john";
162
163 CharArrayWriter caw = new CharArrayWriter();
164 SerializationContext sci = new SerializationContext(caw);
165 sci.setDoMultiRefs(true);
166 sci.serialize(new QName("someLocalPart"), null, tb1);
167 sci.serialize(new QName("someOtherLocalPart"), null, tb2);
168
169 String s = caw.toString();
170
171 // Cheap but fragile.
172 int first = s.indexOf("#id0");
173 int last = s.lastIndexOf("#id1");
174 assertTrue(s,first >= 0);
175 assertTrue(s,last >= 0 && last != first);
176 }
177
178 class TestBeanA {
179 String s1 = null;
180
181 // uses default equals() and hashCode().
182 }
183
184 class TestBeanB {
185 String s1 = null;
186
187 public boolean equals(Object o) {
188 if (o == null) return false;
189 if (this == o) return true;
190 if (!o.getClass().equals(this.getClass())) return false;
191
192 TestBeanB tbb = (TestBeanB)o;
193 if (this.s1 != null) {
194 return this.s1.equals(tbb.s1);
195 } else {
196 return this.s1 == tbb.s1;
197 }
198 }
199
200 public int hashCode() {
201 // XXX???
202 if (this.s1 == null) return super.hashCode();
203 else return this.s1.hashCode();
204 }
205 }
206 }