File: ObjectIdentityTest.java

package info (click to toggle)
libjdo-api-java 3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,540 kB
  • ctags: 33,888
  • sloc: java: 120,520; sql: 20,353; xml: 3,969; makefile: 4
file content (441 lines) | stat: -rw-r--r-- 19,923 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

/*
 * ObjectIdentityTest.java
 *
 */

package javax.jdo.identity;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import java.io.Serializable;

import java.math.BigDecimal;

import java.security.AccessController;
import java.security.PrivilegedAction;

import java.text.SimpleDateFormat;
import java.text.DateFormat;

import java.util.Currency;
import java.util.Date;
import java.util.Locale;

import javax.jdo.JDOUserException;
import javax.jdo.JDONullIdentityException;

import javax.jdo.spi.JDOImplHelper;

import javax.jdo.util.BatchTestRunner;

/**
 *
 */
public class ObjectIdentityTest extends SingleFieldIdentityTest {
    
    /** The JDOImplHelper instance used for Date formatting.
     */
    private static JDOImplHelper helper = (JDOImplHelper)
        AccessController.doPrivileged(
            new PrivilegedAction<JDOImplHelper> () {
                public JDOImplHelper run () {
                    return JDOImplHelper.getInstance();
                }
            }
        );
    
    /** Creates a new instance of ObjectIdentityTest */
    public ObjectIdentityTest() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        BatchTestRunner.run(ObjectIdentityTest.class);
    }
    
    public void testConstructor() {
        ObjectIdentity c1 = new ObjectIdentity(Object.class, new IdClass(1));
        ObjectIdentity c2 = new ObjectIdentity(Object.class, new IdClass(1));
        ObjectIdentity c3 = new ObjectIdentity(Object.class, new IdClass(2));
        assertEquals("Equal ObjectIdentity instances compare not equal.", c1, c2);
        assertFalse ("Not equal ObjectIdentity instances compare equal", c1.equals(c3));
    }
    
    public void testIntegerConstructor() {
        ObjectIdentity c1 = new ObjectIdentity(Object.class, new Integer(1));
        ObjectIdentity c2 = new ObjectIdentity(Object.class, new Integer(1));
        ObjectIdentity c3 = new ObjectIdentity(Object.class, new Integer(2));
        assertEquals("Equal ObjectIdentity instances compare not equal.", c1, c2);
        assertFalse ("Not equal ObjectIdentity instances compare equal", c1.equals(c3));
    }
    
    public void testLongConstructor() {
        ObjectIdentity c1 = new ObjectIdentity(Object.class, new Long(1));
        ObjectIdentity c2 = new ObjectIdentity(Object.class, new Long(1));
        ObjectIdentity c3 = new ObjectIdentity(Object.class, new Long(2));
        assertEquals("Equal ObjectIdentity instances compare not equal.", c1, c2);
        assertFalse ("Not equal ObjectIdentity instances compare equal", c1.equals(c3));
    }
    
    public void testDateConstructor() {
        ObjectIdentity c1 = new ObjectIdentity(Object.class, new Date(1));
        ObjectIdentity c2 = new ObjectIdentity(Object.class, new Date(1));
        ObjectIdentity c3 = new ObjectIdentity(Object.class, new Date(2));
        assertEquals("Equal ObjectIdentity instances compare not equal.", c1, c2);
        assertFalse ("Not equal ObjectIdentity instances compare equal", c1.equals(c3));
    }
    
    public void testLocaleConstructor() {
        ObjectIdentity c1 = new ObjectIdentity(Object.class, Locale.US);
        ObjectIdentity c2 = new ObjectIdentity(Object.class, Locale.US);
        ObjectIdentity c3 = new ObjectIdentity(Object.class, Locale.GERMANY);
        assertEquals("Equal ObjectIdentity instances compare not equal.", c1, c2);
        assertFalse ("Not equal ObjectIdentity instances compare equal", c1.equals(c3));
    }
    
    public void testCurrencyConstructor() {
        if (!isClassLoadable("java.util.Currency")) return;
        ObjectIdentity c1 = new ObjectIdentity(Object.class, 
                Currency.getInstance(Locale.US));
        ObjectIdentity c2 = new ObjectIdentity(Object.class, 
                Currency.getInstance(Locale.US));
        ObjectIdentity c3 = new ObjectIdentity(Object.class, 
                Currency.getInstance(Locale.GERMANY));
        assertEquals("Equal ObjectIdentity instances compare not equal.", c1, c2);
        assertFalse ("Not equal ObjectIdentity instances compare equal", c1.equals(c3));
    }
    
    public void testStringConstructor() {
        ObjectIdentity c1 = new ObjectIdentity(Object.class, 
                "javax.jdo.identity.ObjectIdentityTest$IdClass:1");        
        ObjectIdentity c2 = new ObjectIdentity(Object.class, 
                "javax.jdo.identity.ObjectIdentityTest$IdClass:1");        
        ObjectIdentity c3 = new ObjectIdentity(Object.class, 
                "javax.jdo.identity.ObjectIdentityTest$IdClass:2");        
        assertEquals("Equal ObjectIdentity instances compare not equal.", c1, c2);
        assertFalse ("Not equal ObjectIdentity instances compare equal", c1.equals(c3));
    }
    
    public void testToStringConstructor() {
        ObjectIdentity c1 = new ObjectIdentity(Object.class, new IdClass(1));
        ObjectIdentity c2 = new ObjectIdentity(Object.class, c1.toString());
        assertEquals ("Equal ObjectIdentity instances compare not equal.", c1, c2);
    }

    public void testDateCompareTo() {
    	ObjectIdentity c1 = new ObjectIdentity(Object.class, new Date(1));
    	ObjectIdentity c2 = new ObjectIdentity(Object.class, new Date(1));
    	ObjectIdentity c3 = new ObjectIdentity(Object.class, new Date(2));
        ObjectIdentity c4 = new ObjectIdentity(Class.class, new Date(1));
        assertEquals("Equal ObjectIdentity instances compare not equal.", 0, c1.compareTo(c2));
        assertTrue("Not equal ObjectIdentity instances have wrong compareTo result", c1.compareTo(c3) < 0);
        assertTrue("Not equal ObjectIdentity instances have wrong compareTo result", c3.compareTo(c1) > 0); 
        assertTrue("Not equal ObjectIdentity instances have wrong compareTo result", c1.compareTo(c4) > 0);
    }

    public void testBadStringConstructorNullClass() {
        try {
            ObjectIdentity c1 = new ObjectIdentity(null, "1");
        } catch (NullPointerException ex) {
            return;
        }
        fail ("Failed to catch expected exception.");
    }
    
    public void testBadStringConstructorNullParam() {
        try {
            ObjectIdentity c1 = new ObjectIdentity(Object.class, null);
        } catch (JDONullIdentityException ex) {
            return;
        }
        fail ("Failed to catch expected exception.");
    }
    
    public void testBadStringConstructorTooShort() {
        try {
            ObjectIdentity c1 = new ObjectIdentity(Object.class, "xx");
        } catch (JDOUserException ex) {
            return;
        }
        fail ("Failed to catch expected exception.");
    }
    
    public void testBadStringConstructorNoDelimiter() {
        try {
            ObjectIdentity c1 = new ObjectIdentity(Object.class, "xxxxxxxxx");
        } catch (JDOUserException ex) {
            return;
        }
        fail ("Failed to catch expected exception.");
    }
    
    public void testBadStringConstructorBadClassName() {
        try {
            ObjectIdentity c1 = new ObjectIdentity(Object.class, "xx:yy");
        } catch (JDOUserException ex) {
            validateNestedException(ex, ClassNotFoundException.class);
            return;
        }
        fail ("Failed to catch expected ClassNotFoundException.");
    }
    
    public void testBadStringConstructorNoStringConstructor() {
        try {
            ObjectIdentity c1 = new ObjectIdentity(Object.class, 
                    "javax.jdo.identity.ObjectIdentityTest$BadIdClassNoStringConstructor:yy");
        } catch (JDOUserException ex) {
            validateNestedException(ex, NoSuchMethodException.class);
            return;
        }
        fail ("Failed to catch expected NoSuchMethodException.");
    }
    
    public void testBadStringConstructorNoPublicStringConstructor() {
        try {
            ObjectIdentity c1 = new ObjectIdentity(Object.class, 
                    "javax.jdo.identity.ObjectIdentityTest$BadIdClassNoPublicStringConstructor:yy");
        } catch (JDOUserException ex) {
            validateNestedException(ex, NoSuchMethodException.class);
            return;
        }
        fail ("Failed to catch expected NoSuchMethodException.");
    }
    
    public void testBadStringConstructorIllegalArgument() {
        try {
            ObjectIdentity c1 = new ObjectIdentity(Object.class, 
                    "javax.jdo.identity.ObjectIdentityTest$IdClass:yy");
        } catch (JDOUserException ex) {
            validateNestedException(ex, InvocationTargetException.class);
            return;
        }
        fail ("Failed to catch expected InvocationTargetException.");
    }

    public void testStringDateConstructor() {
        SimpleDateFormat usDateFormat = new SimpleDateFormat
                ("MMM dd, yyyy hh:mm:ss a", Locale.US);
        helper.registerDateFormat(usDateFormat);
        Object c1 = new ObjectIdentity(Object.class, 
            "java.util.Date:Jan 01, 1970 00:00:00 AM");
        helper.registerDateFormat(DateFormat.getDateTimeInstance());
    }

    public void testStringDefaultDateConstructor() {
        DateFormat dateFormat = DateFormat.getDateTimeInstance();
        String rightNow = dateFormat.format(new Date());
        Object c1 = new ObjectIdentity(Object.class, 
            "java.util.Date:" + rightNow);
    }

    public void testBadStringDateConstructor() {
        try {
            ObjectIdentity c1 = new ObjectIdentity(Object.class, 
                "java.util.Date:Jop 1, 1970 00:00:00");
        } catch (JDOUserException ex) {
            return;
        }
        fail ("Failed to catch expected Exception.");
    }

    public void testStringLocaleConstructorLanguage() {
        if (!isClassLoadable("java.util.Currency")) return;
        SingleFieldIdentity c1 = new ObjectIdentity(Object.class, 
                    "java.util.Locale:en");
        assertEquals(new Locale("en"), c1.getKeyAsObject());
    }

    public void testStringLocaleConstructorCountry() {
        SingleFieldIdentity c1 = new ObjectIdentity(Object.class, 
                    "java.util.Locale:_US");
        assertEquals(new Locale("","US"), c1.getKeyAsObject());
    }

    public void testStringLocaleConstructorLanguageCountry() {
        SingleFieldIdentity c1 = new ObjectIdentity(Object.class, 
                    "java.util.Locale:en_US");
        assertEquals(new Locale("en","US"), c1.getKeyAsObject());
    }

    public void testStringLocaleConstructorLanguageCountryVariant() {
        SingleFieldIdentity c1 = new ObjectIdentity(Object.class, 
                    "java.util.Locale:en_US_MAC");
        assertEquals(new Locale("en","US","MAC"), c1.getKeyAsObject());
    }

    public void testStringCurrencyConstructor() {
        if (!isClassLoadable("java.util.Currency")) return;
        SingleFieldIdentity c1 = new ObjectIdentity(Object.class, 
                    "java.util.Currency:USD");
    }

    public void testBadStringCurrencyConstructor() {
        if (!isClassLoadable("java.util.Currency")) return;
        try {
            ObjectIdentity c1 = new ObjectIdentity(Object.class, 
                    "java.util.Currency:NowhereInTheWorld");
        } catch (JDOUserException ex) {
            validateNestedException(ex, IllegalArgumentException.class);
            return;
        }
        fail ("Failed to catch expected IllegalArgumentException.");
    }

    public void testSerializedIdClass() {
        ObjectIdentity c1 = new ObjectIdentity(Object.class, new IdClass(1));
        ObjectIdentity c2 = new ObjectIdentity(Object.class, new IdClass(1));
        ObjectIdentity c3 = new ObjectIdentity(Object.class, new IdClass(2));
        Object[] scis = writeReadSerialized(new Object[] {c1, c2, c3});
        Object sc1 = scis[0];
        Object sc2 = scis[1];
        Object sc3 = scis[2];
        assertEquals ("Equal ObjectIdentity instances compare not equal.", c1, sc1);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", c2, sc2);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", sc1, c2);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", sc2, c1);
        assertFalse ("Not equal ObjectIdentity instances compare equal.", c1.equals(sc3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc1.equals(c3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc1.equals(sc3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc3.equals(sc1));
    }
    
    public void testSerializedBigDecimal() {
        ObjectIdentity c1 = new ObjectIdentity(Object.class, new BigDecimal("123456789.012"));
        ObjectIdentity c2 = new ObjectIdentity(Object.class, new BigDecimal("123456789.012"));
        ObjectIdentity c3 = new ObjectIdentity(Object.class, new BigDecimal("123456789.01"));
        Object[] scis = writeReadSerialized(new Object[] {c1, c2, c3});
        Object sc1 = scis[0];
        Object sc2 = scis[1];
        Object sc3 = scis[2];
        assertEquals ("Equal ObjectIdentity instances compare not equal.", c1, sc1);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", c2, sc2);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", sc1, c2);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", sc2, c1);
        assertFalse ("Not equal ObjectIdentity instances compare equal.", c1.equals(sc3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc1.equals(c3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc1.equals(sc3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc3.equals(sc1));
    }
    
    public void testSerializedCurrency() {
        if (!isClassLoadable("java.util.Currency")) return;
        ObjectIdentity c1 = new ObjectIdentity(Object.class, Currency.getInstance(Locale.US));
        ObjectIdentity c2 = new ObjectIdentity(Object.class, Currency.getInstance(Locale.US));
        ObjectIdentity c3 = new ObjectIdentity(Object.class, Currency.getInstance(Locale.GERMANY));
        Object[] scis = writeReadSerialized(new Object[] {c1, c2, c3});
        Object sc1 = scis[0];
        Object sc2 = scis[1];
        Object sc3 = scis[2];
        assertEquals ("Equal ObjectIdentity instances compare not equal.", c1, sc1);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", c2, sc2);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", sc1, c2);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", sc2, c1);
        assertFalse ("Not equal ObjectIdentity instances compare equal.", c1.equals(sc3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc1.equals(c3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc1.equals(sc3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc3.equals(sc1));
    }
    
    public void testSerializedDate() {
        ObjectIdentity c1 = new ObjectIdentity(Object.class, new Date(1));
        ObjectIdentity c2 = new ObjectIdentity(Object.class, "java.util.Date:1");
        ObjectIdentity c3 = new ObjectIdentity(Object.class, new Date(2));
        Object[] scis = writeReadSerialized(new Object[] {c1, c2, c3});
        Object sc1 = scis[0];
        Object sc2 = scis[1];
        Object sc3 = scis[2];
        assertEquals ("Equal ObjectIdentity instances compare not equal.", c1, sc1);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", c2, sc2);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", sc1, c2);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", sc2, c1);
        assertFalse ("Not equal ObjectIdentity instances compare equal.", c1.equals(sc3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc1.equals(c3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc1.equals(sc3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc3.equals(sc1));
    }
    
    public void testSerializedLocale() {
        ObjectIdentity c1 = new ObjectIdentity(Object.class, Locale.US);
        ObjectIdentity c2 = new ObjectIdentity(Object.class, Locale.US);
        ObjectIdentity c3 = new ObjectIdentity(Object.class, Locale.GERMANY);
        Object[] scis = writeReadSerialized(new Object[] {c1, c2, c3});
        Object sc1 = scis[0];
        Object sc2 = scis[1];
        Object sc3 = scis[2];
        assertEquals ("Equal ObjectIdentity instances compare not equal.", c1, sc1);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", c2, sc2);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", sc1, c2);
        assertEquals ("Equal ObjectIdentity instances compare not equal.", sc2, c1);
        assertFalse ("Not equal ObjectIdentity instances compare equal.", c1.equals(sc3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc1.equals(c3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc1.equals(sc3));
        assertFalse ("Not equal ObjectIdentity instances compare equal.", sc3.equals(sc1));
    }
    
    public void testGetKeyAsObject() {
        ObjectIdentity c1 = new ObjectIdentity(Object.class, new IdClass(1));
        assertEquals("keyAsObject doesn't match.", c1.getKeyAsObject(), new IdClass(1));
    }

    private <T> void validateNestedException(JDOUserException ex, Class<T> expected) {
        Throwable[] nesteds = ex.getNestedExceptions();
        if (nesteds == null || nesteds.length != 1) {
            fail ("Nested exception is null or length 0");
        }
        Throwable nested = nesteds[0];
        if (nested != ex.getCause()) {
            fail ("Nested exception is not == getCause()");
        }
        if (!(expected.isAssignableFrom(nested.getClass()))) {
            fail ("Wrong nested exception. Expected "
                    + expected.getName() + ", got "
                    + nested.toString());
        }
        return;
    }
    public static class IdClass implements Serializable {
        public int value;
        public IdClass() {value = 0;}
        public IdClass(int value) {this.value = value;}
        public IdClass(String str) {this.value = Integer.parseInt(str);}
        public String toString() {return Integer.toString(value);}
        public int hashCode() {
            return value;
        }
        public boolean equals (Object obj) {
            if (this == obj) {
                return true;
            } else {
                IdClass other = (IdClass) obj;
                return value == other.value;
            }
        }
    }
    
    public static class BadIdClassNoStringConstructor {
    }
    
    public static class BadIdClassNoPublicStringConstructor {
        private BadIdClassNoPublicStringConstructor(String str) {}
    }
}