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