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  
17  /*
18   * SingleFieldIdentityTest.java
19   *
20   */
21  
22  package javax.jdo.identity;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.ByteArrayOutputStream;
26  import java.io.IOException;
27  import java.io.ObjectInputStream;
28  import java.io.ObjectOutputStream;
29  import java.io.ObjectInput;
30  import java.io.ObjectOutput;
31  
32  import javax.jdo.util.AbstractTest;
33  import javax.jdo.util.BatchTestRunner;
34  
35  /***
36   * 
37   */
38  public class SingleFieldIdentityTest extends AbstractTest {
39      
40      ConcreteTestIdentity cti1;
41      ConcreteTestIdentity cti2;
42      ConcreteTestIdentity cti3;
43      
44      Object scti1;
45      Object scti2;
46      Object scti3;
47  
48      /*** Creates a new instance of SingleFieldIdentityTest */
49      public SingleFieldIdentityTest() {
50      }
51      
52      /***
53       * @param args the command line arguments
54       */
55      public static void main(String[] args) {
56          BatchTestRunner.run(SingleFieldIdentityTest.class);
57      }
58      
59      public void testConstructor() {
60      cti1 = new ConcreteTestIdentity(Object.class);
61      cti2 = new ConcreteTestIdentity(Object.class);
62      cti3 = new ConcreteTestIdentity(Class.class);
63      
64          assertEquals ("Equal identity instances compare not equal.", cti1, cti2);
65          if (cti1.equals(cti3)) 
66              fail ("Not equal identity instances compare equal.");
67      }
68      
69      public void testSerialized() {
70          cti1 = new ConcreteTestIdentity(Object.class);
71          cti2 = new ConcreteTestIdentity(Object.class);
72          cti3 = new ConcreteTestIdentity(Class.class);
73          Object[] sctis = writeReadSerialized(new Object[]{cti1, cti2, cti3});
74          scti1 = sctis[0];
75          scti2 = sctis[1];
76          scti3 = sctis[2];
77          assertEquals ("Deserialized instance compare not equal.", cti1, scti1);
78          assertEquals ("Deserialized instance compare not equal.", cti2, scti2);
79          assertEquals ("Deserialized instance compare not equal.", cti3, scti3);
80          assertEquals ("Deserialized instance compare not equal.", scti1, cti1);
81          assertEquals ("Deserialized instance compare not equal.", scti2, cti2);
82          assertEquals ("Deserialized instance compare not equal.", scti3, cti3);
83          if (scti1.equals(scti3)) 
84              fail ("Not equal identity instances compare equal.");
85          
86      }
87      
88      protected Object[] writeReadSerialized(Object[] in) {
89          int length = in.length;
90          Object[] result = new Object[length];
91          try {
92              ByteArrayOutputStream baos = new ByteArrayOutputStream();
93              ObjectOutputStream oos = new ObjectOutputStream(baos);
94              for (int i = 0; i < length; ++i) {
95                  oos.writeObject(in[i]);
96              }
97              byte[] ba = baos.toByteArray();
98              ByteArrayInputStream bais = new ByteArrayInputStream(ba);
99              ObjectInputStream ois = new ObjectInputStream(bais);
100             for (int i = 0; i < length; ++i) {
101                 result[i] = ois.readObject();
102             }
103         } catch (Exception e) {
104             fail(e.toString());
105         }
106         return result;
107     }
108   
109 }