1 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 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 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 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 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 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 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 int first = s.indexOf("#id0"); 125 int last = s.lastIndexOf("#id1"); 126 assertTrue(s,first >= 0); 127 assertTrue(s,last >= 0); 128 } 129 130 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 int first = s.indexOf("#id0"); 149 int last = s.lastIndexOf("#id1"); 150 assertTrue(s,first >= 0); 151 assertTrue(s,last >= 0); 152 } 153 154 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 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 } 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 if (this.s1 == null) return super.hashCode(); 203 else return this.s1.hashCode(); 204 } 205 } 206 } 207 | Popular Tags |