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  package javax.jdo.spi;
19  
20  import java.util.Collection;
21  
22  import javax.jdo.pc.PCPoint;
23  import javax.jdo.util.AbstractTest;
24  import javax.jdo.util.BatchTestRunner;
25  
26  /** 
27   * Tests class javax.jdo.spi.JDOImplHelper.
28   * <p>
29   * Missing: testNewInstance + testNewObjectIdInstance
30   * Missing: tests for JDOImplHelper methods: copyKeyFieldsToObjectId and 
31   * copyKeyFieldsFromObjectId.
32   */
33  public class JDOImplHelperTest extends AbstractTest {
34      
35      /** */
36      private RegisterClassEvent event;
37  
38      /** */
39      public static void main(String args[]) {
40          BatchTestRunner.run(JDOImplHelperTest.class);
41      }
42  
43      /** */
44      public void setUp() {
45          // make sure PCClass is loaded before any tests are run
46          PCPoint p = new PCPoint(1, new Integer(1));        
47      }
48  
49      /** */
50      public void testGetFieldNames() {
51          JDOImplHelper implHelper = JDOImplHelper.getInstance();
52          String[] fieldNames = implHelper.getFieldNames(PCPoint.class);
53          if (fieldNames == null) {
54              fail("array of field names is null");
55          }
56          if (fieldNames.length != 2) {
57              fail("Unexpected length of fieldNames; expected 2, got " + 
58                   fieldNames.length);
59          }
60          if (!fieldNames[0].equals("x")) {
61              fail("Unexpected field; expected x, got " + fieldNames[0]);
62          }
63          if (!fieldNames[1].equals("y")) {
64              fail("Unexpected field; expected y, got " + fieldNames[1]);
65          }
66      }
67  
68      /** */
69      public void testGetFieldTypes() {
70          JDOImplHelper implHelper = JDOImplHelper.getInstance();
71          Class[] fieldTypes = implHelper.getFieldTypes(PCPoint.class);
72          if (fieldTypes == null) {
73              fail("array of field types is null");
74          }
75          if (fieldTypes.length != 2) {
76              fail("Unexpected length of fieldTypes; expected 2, got " + 
77                   fieldTypes.length);
78          }
79          if (fieldTypes[0] != int.class) {
80              fail("Unexpected field type; expected int, got " + 
81                   fieldTypes[0]);
82          }
83          if (fieldTypes[1] != Integer.class) {
84              fail("Unexpected field type; expected Integer, got " + 
85                   fieldTypes[1]);
86          }
87      }
88      
89      /** */
90      public void testGetFieldFlags() {
91          byte expected = (byte) (PersistenceCapable.CHECK_READ +
92              PersistenceCapable.CHECK_WRITE + PersistenceCapable.SERIALIZABLE);
93              
94          JDOImplHelper implHelper = JDOImplHelper.getInstance();
95          byte[] fieldFlags = implHelper.getFieldFlags(PCPoint.class);
96          if (fieldFlags == null) {
97              fail("array of field flags is null");
98          }
99          if (fieldFlags.length != 2) {
100             fail("Unexpected length of fieldFlags; expected 2, got " + 
101                  fieldFlags.length);
102         }
103         if (fieldFlags[0] != expected) {
104             fail("Unexpected field flag; expected " + expected + 
105                  ", got " + fieldFlags[0]);
106         }
107         if (fieldFlags[1] != expected) {
108             fail("Unexpected field flag; expected " + expected + 
109                  ", got " + fieldFlags[1]);
110         }
111     }
112 
113     /** */
114     public void testGetPCSuperclass() {
115         JDOImplHelper implHelper = JDOImplHelper.getInstance();
116         Class pcSuper = 
117             implHelper.getPersistenceCapableSuperclass(PCPoint.class);
118         if (pcSuper != null) {
119             fail("Wrong pc superclass of PCPoint; expected null, got " + 
120                  pcSuper);
121         }
122     }
123 
124     /** */
125     public void testNewInstance() {
126         // TBD: test JDOImplHelper.newInstance(pcClass, sm) and
127         // JDOImplHelper.newInstance(pcClass, sm, oid)  
128     }
129 
130     /** */
131     public void testNewObjectIdInstance() {
132         // TBD: test JDOImplHelper.newObjectIdInstance(pcClass)
133     }
134     
135     /** */
136     public void testClassRegistration() {
137         JDOImplHelper implHelper = JDOImplHelper.getInstance();
138 
139         Collection registeredClasses = implHelper.getRegisteredClasses();
140         // test whether PCPoint is registered
141         if (!registeredClasses.contains(PCPoint.class)) {
142             fail("Missing registration of pc class PCPoint");
143         }
144 
145         // Save registered meta data for restoring
146         String[] fieldNames = implHelper.getFieldNames(PCPoint.class);
147         Class[] fieldTypes = implHelper.getFieldTypes(PCPoint.class);
148         byte[] fieldFlags = implHelper.getFieldFlags(PCPoint.class);
149         Class pcSuperclass = implHelper.getPersistenceCapableSuperclass(PCPoint.class);
150         
151         // test unregisterClass with null parameter
152         try {
153             implHelper.unregisterClass(null);
154             fail("Missing exception when calling unregisterClass(null)");
155         }
156         catch (NullPointerException ex) {
157             // expected exception => OK
158         }
159 
160         // test unregister PCPoint class
161         implHelper.unregisterClass(PCPoint.class);
162         registeredClasses = implHelper.getRegisteredClasses();
163         if (registeredClasses.contains(PCPoint.class)) {
164             fail("PCPoint still registered");
165         }
166 
167         // register PCPoint again
168         JDOImplHelper.registerClass(PCPoint.class, fieldNames, fieldTypes, 
169                                     fieldFlags, pcSuperclass, new PCPoint());
170     }
171 
172     /** */
173     public void testClassListenerRegistration() {
174         JDOImplHelper implHelper = JDOImplHelper.getInstance();
175 
176         // add listener and check event
177         event = null;
178         RegisterClassListener listener = new SimpleListener();
179         implHelper.addRegisterClassListener(listener);
180         JDOImplHelper.registerClass(JDOImplHelperTest.class, new String[0], 
181                                     new Class[0], new byte[0], null, null);
182         if (event == null) {
183             fail("Missing event "); 
184         }
185 
186         // remove listener and check event
187         event = null;
188         implHelper.removeRegisterClassListener(listener);
189         JDOImplHelper.registerClass(JDOImplHelperTest.class, new String[0], 
190                                     new Class[0], new byte[0], null, null);
191         if (event != null) {
192             fail("Unexpected event " + event);
193         }
194     }
195 
196     /** */
197     class SimpleListener implements RegisterClassListener {
198 
199         /** */
200         public void registerClass(RegisterClassEvent event) {
201             JDOImplHelperTest.this.event = event;
202         }
203         
204     }
205     
206 }
207