001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: SequenceTest.java,v 1.1.2.3 2008/01/07 15:14:35 cwl Exp $
007: */
008:
009: package com.sleepycat.persist.test;
010:
011: import java.io.File;
012: import java.io.IOException;
013:
014: import junit.framework.TestCase;
015:
016: import com.sleepycat.je.DatabaseException;
017: import com.sleepycat.je.Environment;
018: import com.sleepycat.je.EnvironmentConfig;
019: import com.sleepycat.je.util.TestUtils;
020: import com.sleepycat.persist.EntityStore;
021: import com.sleepycat.persist.PrimaryIndex;
022: import com.sleepycat.persist.StoreConfig;
023: import com.sleepycat.persist.model.Entity;
024: import com.sleepycat.persist.model.KeyField;
025: import com.sleepycat.persist.model.Persistent;
026: import com.sleepycat.persist.model.PrimaryKey;
027:
028: /**
029: * @author Mark Hayes
030: */
031: public class SequenceTest extends TestCase {
032:
033: private File envHome;
034: private Environment env;
035:
036: public void setUp() throws IOException {
037:
038: envHome = new File(System.getProperty(TestUtils.DEST_DIR));
039: TestUtils.removeLogFiles("Setup", envHome, false);
040: }
041:
042: public void tearDown() throws IOException {
043:
044: if (env != null) {
045: try {
046: env.close();
047: } catch (DatabaseException e) {
048: System.out.println("During tearDown: " + e);
049: }
050: }
051: try {
052: TestUtils.removeLogFiles("TearDown", envHome, false);
053: } catch (Error e) {
054: System.out.println("During tearDown: " + e);
055: }
056: envHome = null;
057: env = null;
058: }
059:
060: public void testSequenceKeys() throws Exception {
061:
062: Class[] classes = { SequenceEntity_Long.class,
063: SequenceEntity_Integer.class,
064: SequenceEntity_Short.class, SequenceEntity_Byte.class,
065: SequenceEntity_tlong.class, SequenceEntity_tint.class,
066: SequenceEntity_tshort.class,
067: SequenceEntity_tbyte.class,
068: SequenceEntity_Long_composite.class,
069: SequenceEntity_Integer_composite.class,
070: SequenceEntity_Short_composite.class,
071: SequenceEntity_Byte_composite.class,
072: SequenceEntity_tlong_composite.class,
073: SequenceEntity_tint_composite.class,
074: SequenceEntity_tshort_composite.class,
075: SequenceEntity_tbyte_composite.class, };
076:
077: EnvironmentConfig envConfig = new EnvironmentConfig();
078: envConfig.setAllowCreate(true);
079: env = new Environment(envHome, envConfig);
080:
081: StoreConfig storeConfig = new StoreConfig();
082: storeConfig.setAllowCreate(true);
083: EntityStore store = new EntityStore(env, "foo", storeConfig);
084:
085: long seq = 0;
086:
087: for (int i = 0; i < classes.length; i += 1) {
088: Class entityCls = classes[i];
089: SequenceEntity entity = (SequenceEntity) entityCls
090: .newInstance();
091: Class keyCls = entity.getKeyClass();
092:
093: PrimaryIndex<Object, SequenceEntity> index = store
094: .getPrimaryIndex(keyCls, entityCls);
095: index.putNoReturn(entity);
096: seq += 1;
097: assertEquals(seq, entity.getKey());
098:
099: index.putNoReturn(entity);
100: assertEquals(seq, entity.getKey());
101:
102: entity.nullifyKey();
103: index.putNoReturn(entity);
104: seq += 1;
105: assertEquals(seq, entity.getKey());
106: }
107:
108: store.close();
109: env.close();
110: env = null;
111: }
112:
113: interface SequenceEntity {
114: Class getKeyClass();
115:
116: long getKey();
117:
118: void nullifyKey();
119: }
120:
121: @Entity
122: static class SequenceEntity_Long implements SequenceEntity {
123:
124: @PrimaryKey(sequence="X")
125: Long priKey;
126:
127: public Class getKeyClass() {
128: return Long.class;
129: }
130:
131: public long getKey() {
132: return priKey;
133: }
134:
135: public void nullifyKey() {
136: priKey = null;
137: }
138: }
139:
140: @Entity
141: static class SequenceEntity_Integer implements SequenceEntity {
142:
143: @PrimaryKey(sequence="X")
144: Integer priKey;
145:
146: public Class getKeyClass() {
147: return Integer.class;
148: }
149:
150: public long getKey() {
151: return priKey;
152: }
153:
154: public void nullifyKey() {
155: priKey = null;
156: }
157: }
158:
159: @Entity
160: static class SequenceEntity_Short implements SequenceEntity {
161:
162: @PrimaryKey(sequence="X")
163: Short priKey;
164:
165: public Class getKeyClass() {
166: return Short.class;
167: }
168:
169: public long getKey() {
170: return priKey;
171: }
172:
173: public void nullifyKey() {
174: priKey = null;
175: }
176: }
177:
178: @Entity
179: static class SequenceEntity_Byte implements SequenceEntity {
180:
181: @PrimaryKey(sequence="X")
182: Byte priKey;
183:
184: public Class getKeyClass() {
185: return Byte.class;
186: }
187:
188: public long getKey() {
189: return priKey;
190: }
191:
192: public void nullifyKey() {
193: priKey = null;
194: }
195: }
196:
197: @Entity
198: static class SequenceEntity_tlong implements SequenceEntity {
199:
200: @PrimaryKey(sequence="X")
201: long priKey;
202:
203: public Class getKeyClass() {
204: return Long.class;
205: }
206:
207: public long getKey() {
208: return priKey;
209: }
210:
211: public void nullifyKey() {
212: priKey = 0;
213: }
214: }
215:
216: @Entity
217: static class SequenceEntity_tint implements SequenceEntity {
218:
219: @PrimaryKey(sequence="X")
220: int priKey;
221:
222: public Class getKeyClass() {
223: return Integer.class;
224: }
225:
226: public long getKey() {
227: return priKey;
228: }
229:
230: public void nullifyKey() {
231: priKey = 0;
232: }
233: }
234:
235: @Entity
236: static class SequenceEntity_tshort implements SequenceEntity {
237:
238: @PrimaryKey(sequence="X")
239: short priKey;
240:
241: public Class getKeyClass() {
242: return Short.class;
243: }
244:
245: public long getKey() {
246: return priKey;
247: }
248:
249: public void nullifyKey() {
250: priKey = 0;
251: }
252: }
253:
254: @Entity
255: static class SequenceEntity_tbyte implements SequenceEntity {
256:
257: @PrimaryKey(sequence="X")
258: byte priKey;
259:
260: public Class getKeyClass() {
261: return Byte.class;
262: }
263:
264: public long getKey() {
265: return priKey;
266: }
267:
268: public void nullifyKey() {
269: priKey = 0;
270: }
271: }
272:
273: @Entity
274: static class SequenceEntity_Long_composite implements
275: SequenceEntity {
276:
277: @PrimaryKey(sequence="X")
278: Key priKey;
279:
280: @Persistent
281: static class Key {
282: @KeyField(1)
283: Long priKey;
284: }
285:
286: public Class getKeyClass() {
287: return Key.class;
288: }
289:
290: public long getKey() {
291: return priKey.priKey;
292: }
293:
294: public void nullifyKey() {
295: priKey = null;
296: }
297: }
298:
299: @Entity
300: static class SequenceEntity_Integer_composite implements
301: SequenceEntity {
302:
303: @PrimaryKey(sequence="X")
304: Key priKey;
305:
306: @Persistent
307: static class Key {
308: @KeyField(1)
309: Integer priKey;
310: }
311:
312: public Class getKeyClass() {
313: return Key.class;
314: }
315:
316: public long getKey() {
317: return priKey.priKey;
318: }
319:
320: public void nullifyKey() {
321: priKey = null;
322: }
323: }
324:
325: @Entity
326: static class SequenceEntity_Short_composite implements
327: SequenceEntity {
328:
329: @PrimaryKey(sequence="X")
330: Key priKey;
331:
332: @Persistent
333: static class Key {
334: @KeyField(1)
335: Short priKey;
336: }
337:
338: public Class getKeyClass() {
339: return Key.class;
340: }
341:
342: public long getKey() {
343: return priKey.priKey;
344: }
345:
346: public void nullifyKey() {
347: priKey = null;
348: }
349: }
350:
351: @Entity
352: static class SequenceEntity_Byte_composite implements
353: SequenceEntity {
354:
355: @PrimaryKey(sequence="X")
356: Key priKey;
357:
358: @Persistent
359: static class Key {
360: @KeyField(1)
361: Byte priKey;
362: }
363:
364: public Class getKeyClass() {
365: return Key.class;
366: }
367:
368: public long getKey() {
369: return priKey.priKey;
370: }
371:
372: public void nullifyKey() {
373: priKey = null;
374: }
375: }
376:
377: @Entity
378: static class SequenceEntity_tlong_composite implements
379: SequenceEntity {
380:
381: @PrimaryKey(sequence="X")
382: Key priKey;
383:
384: @Persistent
385: static class Key {
386: @KeyField(1)
387: long priKey;
388: }
389:
390: public Class getKeyClass() {
391: return Key.class;
392: }
393:
394: public long getKey() {
395: return priKey.priKey;
396: }
397:
398: public void nullifyKey() {
399: priKey = null;
400: }
401: }
402:
403: @Entity
404: static class SequenceEntity_tint_composite implements
405: SequenceEntity {
406:
407: @PrimaryKey(sequence="X")
408: Key priKey;
409:
410: @Persistent
411: static class Key {
412: @KeyField(1)
413: int priKey;
414: }
415:
416: public Class getKeyClass() {
417: return Key.class;
418: }
419:
420: public long getKey() {
421: return priKey.priKey;
422: }
423:
424: public void nullifyKey() {
425: priKey = null;
426: }
427: }
428:
429: @Entity
430: static class SequenceEntity_tshort_composite implements
431: SequenceEntity {
432:
433: @PrimaryKey(sequence="X")
434: Key priKey;
435:
436: @Persistent
437: static class Key {
438: @KeyField(1)
439: short priKey;
440: }
441:
442: public Class getKeyClass() {
443: return Key.class;
444: }
445:
446: public long getKey() {
447: return priKey.priKey;
448: }
449:
450: public void nullifyKey() {
451: priKey = null;
452: }
453: }
454:
455: @Entity
456: static class SequenceEntity_tbyte_composite implements
457: SequenceEntity {
458:
459: @PrimaryKey(sequence="X")
460: Key priKey;
461:
462: @Persistent
463: static class Key {
464: @KeyField(1)
465: byte priKey;
466: }
467:
468: public Class getKeyClass() {
469: return Key.class;
470: }
471:
472: public long getKey() {
473: return priKey.priKey;
474: }
475:
476: public void nullifyKey() {
477: priKey = null;
478: }
479: }
480: }
|