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