summaryrefslogtreecommitdiff
path: root/test/java/compat/src/com/sleepycat/persist/test/NegativeTest.java
blob: aba7e7c988d5c08c1774d2ac60d5470fbe50a1d0 (plain)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 */

package com.sleepycat.persist.test;

import static com.sleepycat.persist.model.DeleteAction.NULLIFY;
import static com.sleepycat.persist.model.Relationship.ONE_TO_MANY;
import static com.sleepycat.persist.model.Relationship.ONE_TO_ONE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import com.sleepycat.db.DatabaseConfig;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.SecondaryConfig;
import com.sleepycat.db.SequenceConfig;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;
import com.sleepycat.persist.StoreConfig;
import com.sleepycat.persist.model.AnnotationModel;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.KeyField;
import com.sleepycat.persist.model.Persistent;
import com.sleepycat.persist.model.PersistentProxy;
import com.sleepycat.persist.model.PrimaryKey;
import com.sleepycat.persist.model.SecondaryKey;
import com.sleepycat.util.test.TxnTestCase;

/**
 * Negative tests.
 *
 * @author Mark Hayes
 */
@RunWith(Parameterized.class)
public class NegativeTest extends TxnTestCase {

    @Parameters
    public static List<Object[]> genParams() {
        return getTxnParams(null, false);
    }
    
    public NegativeTest(String type){
        initEnvConfig();
        txnType = type;
        isTransactional = (txnType != TXN_NULL);
        customName = txnType;
    }

    private EntityStore store;

    private void open()
        throws DatabaseException {

        open(null);
    }

    private void open(Class<ProxyExtendsEntity> clsToRegister)
        throws DatabaseException {

        StoreConfig config = new StoreConfig();
        config.setAllowCreate(envConfig.getAllowCreate());
        config.setTransactional(envConfig.getTransactional());

        if (clsToRegister != null) {
            AnnotationModel model = new AnnotationModel();
            model.registerClass(clsToRegister);
            config.setModel(model);
        }

        store = new EntityStore(env, "test", config);
    }

    private void close()
        throws DatabaseException {

        store.close();
        store = null;
    }

    @After
    public void tearDown()
        throws Exception {

        if (store != null) {
            try {
                store.close();
            } catch (Throwable e) {
                System.out.println("tearDown: " + e);
            }
            store = null;
        }
        super.tearDown();
    }

    @Test
    public void testBadKeyClass1()
        throws DatabaseException {

        open();
        try {
            store.getPrimaryIndex(BadKeyClass1.class, UseBadKeyClass1.class);
            fail();
        } catch (IllegalArgumentException expected) {
            assertTrue(expected.getMessage().indexOf("@KeyField") >= 0);
        }
        close();
    }

    /** Missing @KeyField in composite key class. */
    @Persistent
    static class BadKeyClass1 {

        private int f1;
    }

    @Entity
    static class UseBadKeyClass1 {

        @PrimaryKey
        private final BadKeyClass1 f1 = new BadKeyClass1();

        @SecondaryKey(relate=ONE_TO_ONE)
        private final BadKeyClass1 f2 = new BadKeyClass1();
    }

    @Test
    public void testBadSequenceKeys()
        throws DatabaseException {

        open();
        try {
            store.getPrimaryIndex(Boolean.class, BadSequenceKeyEntity1.class);
            fail();
        } catch (IllegalArgumentException expected) {
            assertTrue(expected.getMessage().indexOf
                ("Type not allowed for sequence") >= 0);
        }
        try {
            store.getPrimaryIndex(BadSequenceKeyEntity2.Key.class,
                     BadSequenceKeyEntity2.class);
            fail();
        } catch (IllegalArgumentException expected) {
            assertTrue(expected.getMessage().indexOf
                ("Type not allowed for sequence") >= 0);
        }
        try {
            store.getPrimaryIndex(BadSequenceKeyEntity3.Key.class,
                     BadSequenceKeyEntity3.class);
            fail();
        } catch (IllegalArgumentException expected) {
            assertTrue(expected.getMessage().indexOf
                ("A composite key class used with a sequence may contain " +
                 "only a single key field")>= 0);
        }
        close();
    }

    /** Boolean not allowed for sequence key. */
    @Entity
    static class BadSequenceKeyEntity1 {

        @PrimaryKey(sequence="X")
        private boolean key;
    }

    /** Composite key with non-integer field not allowed for sequence key. */
    @Entity
    static class BadSequenceKeyEntity2 {

        @PrimaryKey(sequence="X")
        private Key key;

        @Persistent
        static class Key {
            @KeyField(1)
            boolean key;
        }
    }

    /** Composite key with multiple key fields not allowed for sequence key. */
    @Entity
    static class BadSequenceKeyEntity3 {

        @PrimaryKey(sequence="X")
        private Key key;

        @Persistent
        static class Key {
            @KeyField(1)
            int key;
            @KeyField(2)
            int key2;
        }
    }

    /**
     * A proxied object may not current contain a field that references the
     * parent proxy.  [#15815]
     */
    @Test
    public void testProxyNestedRef()
        throws DatabaseException {

        open();
        PrimaryIndex<Integer, ProxyNestedRef> index = store.getPrimaryIndex
            (Integer.class, ProxyNestedRef.class);
        ProxyNestedRef entity = new ProxyNestedRef();
        entity.list.add(entity.list);
        try {
            index.put(entity);
            fail();
        } catch (IllegalArgumentException expected) {
            assertTrue(expected.getMessage().indexOf
                ("Cannot embed a reference to a proxied object") >= 0);
        }
        close();
    }

    @Entity
    static class ProxyNestedRef {

        @PrimaryKey
        private int key;

        ArrayList<Object> list = new ArrayList<Object>();
    }

    /**
     * Disallow primary keys on entity subclasses.  [#15757]
     */
    @Test
    public void testEntitySubclassWithPrimaryKey()
        throws DatabaseException {

        open();
        PrimaryIndex<Integer, EntitySuperClass> index = store.getPrimaryIndex
            (Integer.class, EntitySuperClass.class);
        EntitySuperClass e1 = new EntitySuperClass(1, "one");
        index.put(e1);
        assertEquals(e1, index.get(1));
        EntitySubClass e2 = new EntitySubClass(2, "two", "foo", 9);
        try {
            index.put(e2);
            fail();
        } catch (IllegalArgumentException e) {
            assertTrue(e.getMessage().contains
                ("PrimaryKey may not appear on an Entity subclass"));
        }
        assertEquals(e1, index.get(1));
        close();
    }

    @Entity
    static class EntitySuperClass {

        @PrimaryKey
        private int x;

        private String y;

        EntitySuperClass(int x, String y) {
            assert y != null;
            this.x = x;
            this.y = y;
        }

        private EntitySuperClass() {}

        @Override
        public String toString() {
            return "x=" + x + " y=" + y;
        }

        @Override
        public boolean equals(Object other) {
            if (other instanceof EntitySuperClass) {
                EntitySuperClass o = (EntitySuperClass) other;
                return x == o.x && y.equals(o.y);
            } else {
                return false;
            }
        }
    }

    @Persistent
    static class EntitySubClass extends EntitySuperClass {

        @PrimaryKey
        private String foo;

        private int z;

        EntitySubClass(int x, String y, String foo, int z) {
            super(x, y);
            assert foo != null;
            this.foo = foo;
            this.z = z;
        }

        private EntitySubClass() {}

        @Override
        public String toString() {
            return super.toString() + " z=" + z;
        }

        @Override
        public boolean equals(Object other) {
            if (other instanceof EntitySubClass) {
                EntitySubClass o = (EntitySubClass) other;
                return super.equals(o) && z == o.z;
            } else {
                return false;
            }
        }
    }

    /**
     * Disallow storing null entities. [#19085]
     */
    @Test
    public void testNullEntity()
        throws DatabaseException {

        open();
        PrimaryIndex<Integer, EntitySuperClass> index = store.getPrimaryIndex
            (Integer.class, EntitySuperClass.class);
        try {
            index.put(null);
            fail();
        } catch (IllegalArgumentException expected) {
        }
        try {
            index.sortedMap().put(1, null);
            fail();
        } catch (IllegalArgumentException expected) {
        }
        close();
    }

    /**
     * Disallow embedded entity classes and subclasses.  [#16077]
     */
    @Test
    public void testEmbeddedEntity()
        throws DatabaseException {

        open();
        PrimaryIndex<Integer, EmbeddingEntity> index = store.getPrimaryIndex
            (Integer.class, EmbeddingEntity.class);
        EmbeddingEntity e1 = new EmbeddingEntity(1, null);
        index.put(e1);
        assertEquals(e1, index.get(1));

        EmbeddingEntity e2 =
            new EmbeddingEntity(2, new EntitySuperClass(2, "two"));
        try {
            index.put(e2);
            fail();
        } catch (IllegalArgumentException e) {
            assertTrue(e.getMessage().contains
                ("References to entities are not allowed"));
        }

        EmbeddingEntity e3 = new EmbeddingEntity
            (3, new EmbeddedEntitySubClass(3, "three", "foo", 9));
        try {
            index.put(e3);
            fail();
        } catch (IllegalArgumentException e) {
            assertTrue(e.toString(), e.getMessage().contains
                ("References to entities are not allowed"));
        }

        assertEquals(e1, index.get(1));
        close();
    }

    @Entity
    static class EmbeddingEntity {

        @PrimaryKey
        private int x;

        private EntitySuperClass y;

        /* References to self are allowed. [#17525] */
        private EmbeddingEntity self;

        EmbeddingEntity(int x, EntitySuperClass y) {
            this.x = x;
            this.y = y;
            this.self = this;
        }

        private EmbeddingEntity() {}

        @Override
        public String toString() {
            return "x=" + x + " y=" + y;
        }

        @Override
        public boolean equals(Object other) {
            if (other instanceof EmbeddingEntity) {
                EmbeddingEntity o = (EmbeddingEntity) other;
                return x == o.x &&
                       ((y == null) ? (o.y == null) : y.equals(o.y));
            } else {
                return false;
            }
        }
    }

    @Persistent
    static class EmbeddedEntitySubClass extends EntitySuperClass {

        private String foo;

        private int z;

        EmbeddedEntitySubClass(int x, String y, String foo, int z) {
            super(x, y);
            assert foo != null;
            this.foo = foo;
            this.z = z;
        }

        private EmbeddedEntitySubClass() {}

        @Override
        public String toString() {
            return super.toString() + " z=" + z;
        }

        @Override
        public boolean equals(Object other) {
            if (other instanceof EmbeddedEntitySubClass) {
                EmbeddedEntitySubClass o = (EmbeddedEntitySubClass) other;
                return super.equals(o) && z == o.z;
            } else {
                return false;
            }
        }
    }

    /**
     * Disallow SecondaryKey collection with no type parameter. [#15950]
     */
    @Test
    public void testTypelessKeyCollection()
        throws DatabaseException {

        open();
        try {
            store.getPrimaryIndex
                (Integer.class, TypelessKeyCollectionEntity.class);
            fail();
        } catch (IllegalArgumentException e) {
            assertTrue(e.toString(), e.getMessage().contains
                ("Collection typed secondary key field must have a " +
                 "single generic type argument and a wildcard or type " +
                 "bound is not allowed"));
        }
        close();
    }

    @Entity
    static class TypelessKeyCollectionEntity {

        @PrimaryKey
        private int x;

        @SecondaryKey(relate=ONE_TO_MANY)
        private final Collection keys = new ArrayList();

        TypelessKeyCollectionEntity(int x) {
            this.x = x;
        }

        private TypelessKeyCollectionEntity() {}
    }

    /**
     * Disallow a persistent proxy that extends an entity.  [#15950]
     */
    @Test
    public void testProxyEntity()
        throws DatabaseException {

        try {
            open(ProxyExtendsEntity.class);
            fail();
        } catch (IllegalArgumentException e) {
            assertTrue(e.toString(), e.getMessage().contains
                ("A proxy may not be an entity"));
        }
    }

    @Persistent(proxyFor=Locale.class)
    static class ProxyExtendsEntity
        extends EntitySuperClass
        implements PersistentProxy<Locale> {

        String language;
        String country;
        String variant;

        public void initializeProxy(Locale object) {
            language = object.getLanguage();
            country = object.getCountry();
            variant = object.getVariant();
        }

        public Locale convertProxy() {
            return new Locale(language, country, variant);
        }
    }

    /**
     * Wrapper type not allowed for nullified foreign key.
     */
    @Test
    public void testBadNullifyKey()
        throws DatabaseException {

        open();
        try {
            store.getPrimaryIndex(Integer.class, BadNullifyKeyEntity1.class);
            fail();
        } catch (IllegalArgumentException expected) {
            assertTrue(expected.getMessage().indexOf
                ("NULLIFY may not be used with primitive fields") >= 0);
        }
        close();
    }

    @Entity
    static class BadNullifyKeyEntity1 {

        @PrimaryKey
        private int key;

        @SecondaryKey(relate=ONE_TO_ONE,
                      relatedEntity=BadNullifyKeyEntity2.class,
                      onRelatedEntityDelete=NULLIFY)
        private int secKey; // Should be Integer, not int.
    }

    @Entity
    static class BadNullifyKeyEntity2 {

        @PrimaryKey
        private int key;
    }

    /**
     * @Persistent not allowed on an enum.
     */
    @Test
    public void testPersistentEnum()
        throws DatabaseException {

        open();
        try {
            store.getPrimaryIndex(Integer.class, PersistentEnumEntity.class);
            fail();
        } catch (IllegalArgumentException expected) {
            assertTrue(expected.getMessage().indexOf
                ("not allowed for enum, interface, or primitive") >= 0);
        }
        close();
    }

    @Entity
    static class PersistentEnumEntity {

        @PrimaryKey
        private int key;

        @Persistent
        enum MyEnum {X, Y, Z};

        MyEnum f1;
    }

    /**
     * Disallow a reference to an interface marked @Persistent.
     */
    @Test
    public void testPersistentInterface()
        throws DatabaseException {

        open();
        try {
            store.getPrimaryIndex(Integer.class,
                                  PersistentInterfaceEntity1.class);
            fail();
        } catch (IllegalArgumentException expected) {
            assertTrue(expected.getMessage().indexOf
                ("not allowed for enum, interface, or primitive") >= 0);
        }
        close();
    }

    @Entity
    static class PersistentInterfaceEntity1 {

        @PrimaryKey
        private int key;

        @SecondaryKey(relate=ONE_TO_ONE,
                      relatedEntity=PersistentInterfaceEntity2.class)
        private int secKey; // Should be Integer, not int.
    }

    @Persistent
    interface PersistentInterfaceEntity2 {
    }

    /**
     * Disallow reference to @Persistent inner class.
     */
    @Test
    public void testPersistentInnerClass()
        throws DatabaseException {

        open();
        try {
            store.getPrimaryIndex(Integer.class,
                                  PersistentInnerClassEntity1.class);
            fail();
        } catch (IllegalArgumentException expected) {
            assertTrue(expected.getMessage().indexOf
                ("Inner classes not allowed") >= 0);
        }
        close();
    }

    @Entity
    static class PersistentInnerClassEntity1 {

        @PrimaryKey
        private int key;

        private PersistentInnerClass f;
    }

    /* An inner (non-static) class is illegal. */
    @Persistent
    class PersistentInnerClass {

        private int x;
    }

    /**
     * Disallow @Entity inner class.
     */
    @Test
    public void testSetConfigAfterOpen()
        throws DatabaseException {

        open();
        PrimaryIndex<Integer, SetConfigAfterOpenEntity> priIndex =
            store.getPrimaryIndex(Integer.class,
                                  SetConfigAfterOpenEntity.class);
        SecondaryIndex<Integer, Integer, SetConfigAfterOpenEntity> secIndex =
            store.getSecondaryIndex(priIndex, Integer.class, "skey");

        DatabaseConfig priConfig =
            store.getPrimaryConfig(SetConfigAfterOpenEntity.class);
        assertNotNull(priConfig);
        try {
            store.setPrimaryConfig(SetConfigAfterOpenEntity.class, priConfig);
            fail();
        } catch (IllegalStateException expected) {
            assertTrue(expected.getMessage().indexOf
                ("Cannot set config after DB is open") >= 0);
        }

        SecondaryConfig secConfig =
            store.getSecondaryConfig(SetConfigAfterOpenEntity.class, "skey");
        assertNotNull(secConfig);
        try {
            store.setSecondaryConfig(SetConfigAfterOpenEntity.class, "skey",
                                     secConfig);
            fail();
        } catch (IllegalStateException expected) {
            assertTrue(expected.getMessage().indexOf
                ("Cannot set config after DB is open") >= 0);
        }

        SequenceConfig seqConfig = store.getSequenceConfig("foo");
        assertNotNull(seqConfig);
        try {
            store.setSequenceConfig("foo", seqConfig);
            fail();
        } catch (IllegalStateException expected) {
            assertTrue(expected.getMessage().indexOf
                ("Cannot set config after Sequence is open") >= 0);
        }

        close();
    }

    @Entity
    static class SetConfigAfterOpenEntity {

        @PrimaryKey(sequence="foo")
        private int key;

        @SecondaryKey(relate=ONE_TO_ONE)
        int skey;
    }
}