001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: NegativeTest.java,v 1.4.2.6 2008/01/07 15:14:35 cwl Exp $
007: */
008:
009: package com.sleepycat.persist.test;
010:
011: import java.util.ArrayList;
012:
013: import static com.sleepycat.persist.model.Relationship.ONE_TO_ONE;
014: import junit.framework.Test;
015:
016: import com.sleepycat.je.DatabaseException;
017: import com.sleepycat.je.test.TxnTestCase;
018: import com.sleepycat.persist.EntityStore;
019: import com.sleepycat.persist.PrimaryIndex;
020: import com.sleepycat.persist.StoreConfig;
021: import com.sleepycat.persist.model.Entity;
022: import com.sleepycat.persist.model.KeyField;
023: import com.sleepycat.persist.model.Persistent;
024: import com.sleepycat.persist.model.PrimaryKey;
025: import com.sleepycat.persist.model.SecondaryKey;
026:
027: /**
028: * Negative tests.
029: *
030: * @author Mark Hayes
031: */
032: public class NegativeTest extends TxnTestCase {
033:
034: public static Test suite() {
035: return txnTestSuite(NegativeTest.class, null, null);
036: }
037:
038: private EntityStore store;
039:
040: private void open() throws DatabaseException {
041:
042: StoreConfig config = new StoreConfig();
043: config.setAllowCreate(envConfig.getAllowCreate());
044: config.setTransactional(envConfig.getTransactional());
045:
046: store = new EntityStore(env, "test", config);
047: }
048:
049: private void close() throws DatabaseException {
050:
051: store.close();
052: }
053:
054: public void testBadKeyClass1() throws DatabaseException {
055:
056: open();
057: try {
058: PrimaryIndex<BadKeyClass1, UseBadKeyClass1> index = store
059: .getPrimaryIndex(BadKeyClass1.class,
060: UseBadKeyClass1.class);
061: fail();
062: } catch (IllegalArgumentException expected) {
063: assertTrue(expected.getMessage().indexOf("@KeyField") >= 0);
064: }
065: close();
066: }
067:
068: /** Missing @KeyField in composite key class. */
069: @Persistent
070: static class BadKeyClass1 {
071:
072: private int f1;
073: }
074:
075: @Entity
076: static class UseBadKeyClass1 {
077:
078: @PrimaryKey
079: private BadKeyClass1 f1 = new BadKeyClass1();
080:
081: @SecondaryKey(relate=ONE_TO_ONE)
082: private BadKeyClass1 f2 = new BadKeyClass1();
083: }
084:
085: public void testBadSequenceKeys() throws DatabaseException {
086:
087: open();
088: try {
089: PrimaryIndex<Boolean, BadSequenceKeyEntity1> index = store
090: .getPrimaryIndex(Boolean.class,
091: BadSequenceKeyEntity1.class);
092: fail();
093: } catch (IllegalArgumentException expected) {
094: assertTrue(expected.getMessage().indexOf(
095: "Type not allowed for sequence") >= 0);
096: }
097: try {
098: PrimaryIndex<BadSequenceKeyEntity2.Key, BadSequenceKeyEntity2> index = store
099: .getPrimaryIndex(BadSequenceKeyEntity2.Key.class,
100: BadSequenceKeyEntity2.class);
101: fail();
102: } catch (IllegalArgumentException expected) {
103: assertTrue(expected.getMessage().indexOf(
104: "Type not allowed for sequence") >= 0);
105: }
106: try {
107: PrimaryIndex<BadSequenceKeyEntity3.Key, BadSequenceKeyEntity3> index = store
108: .getPrimaryIndex(BadSequenceKeyEntity3.Key.class,
109: BadSequenceKeyEntity3.class);
110: fail();
111: } catch (IllegalArgumentException expected) {
112: assertTrue(expected.getMessage().indexOf(
113: "A composite key class used with a sequence may contain "
114: + "only a single integer key field") >= 0);
115: }
116: close();
117: }
118:
119: /** Boolean not allowed for sequence key. */
120: @Entity
121: static class BadSequenceKeyEntity1 {
122:
123: @PrimaryKey(sequence="X")
124: private boolean key;
125: }
126:
127: /** Composite key with non-integer field not allowed for sequence key. */
128: @Entity
129: static class BadSequenceKeyEntity2 {
130:
131: @PrimaryKey(sequence="X")
132: private Key key;
133:
134: @Persistent
135: static class Key {
136: @KeyField(1)
137: boolean key;
138: }
139: }
140:
141: /** Composite key with multiple key fields not allowed for sequence key. */
142: @Entity
143: static class BadSequenceKeyEntity3 {
144:
145: @PrimaryKey(sequence="X")
146: private Key key;
147:
148: @Persistent
149: static class Key {
150: @KeyField(1)
151: int key;
152: @KeyField(2)
153: int key2;
154: }
155: }
156:
157: /**
158: * A proxied object may not current contain a field that references the
159: * parent proxy. [#15815]
160: */
161: public void testProxyNestedRef() throws DatabaseException {
162:
163: open();
164: PrimaryIndex<Integer, ProxyNestedRef> index = store
165: .getPrimaryIndex(Integer.class, ProxyNestedRef.class);
166: ProxyNestedRef entity = new ProxyNestedRef();
167: entity.list.add(entity.list);
168: try {
169: index.put(entity);
170: fail();
171: } catch (IllegalArgumentException expected) {
172: assertTrue(expected.getMessage().indexOf(
173: "Cannot embed a reference to a proxied object") >= 0);
174: }
175: close();
176: }
177:
178: @Entity
179: static class ProxyNestedRef {
180:
181: @PrimaryKey
182: private int key;
183:
184: ArrayList<Object> list = new ArrayList<Object>();
185: }
186: }
|