0001: /*
0002: Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
0003:
0004: This file is part of the JODB (Java Objects Database) open source project.
0005:
0006: JODB is free software; you can redistribute it and/or modify it under
0007: the terms of version 2 of the GNU General Public License as published
0008: by the Free Software Foundation.
0009:
0010: JODB is distributed in the hope that it will be useful, but WITHOUT ANY
0011: WARRANTY; without even the implied warranty of MERCHANTABILITY or
0012: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0013: for more details.
0014:
0015: You should have received a copy of the GNU General Public License along
0016: with this program; if not, write to the Free Software Foundation, Inc.,
0017: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
0018: */
0019: package com.mobixess.jodb.tests;
0020:
0021: import java.io.File;
0022: import java.io.FileOutputStream;
0023: import java.io.IOException;
0024: import java.io.InputStream;
0025: import java.io.OutputStreamWriter;
0026: import java.io.RandomAccessFile;
0027: import java.lang.reflect.Field;
0028: import java.net.URL;
0029: import java.net.URLClassLoader;
0030: import java.nio.ByteBuffer;
0031: import java.util.Arrays;
0032: import java.util.Iterator;
0033: import java.util.List;
0034: import java.util.Map;
0035: import java.util.Vector;
0036: import java.util.WeakHashMap;
0037:
0038: import com.mobixess.jodb.core.IPersistentObjectStatistics;
0039: import com.mobixess.jodb.core.IllegalClassTypeException;
0040: import com.mobixess.jodb.core.JODB;
0041: import com.mobixess.jodb.core.JODBConfig;
0042: import com.mobixess.jodb.core.JODBSessionContainer;
0043: import com.mobixess.jodb.core.io.IRandomAccessDataBuffer;
0044: import com.mobixess.jodb.core.io.IRandomAccessBufferFactory.BUFFER_TYPE;
0045: import com.mobixess.jodb.soda.api.Query;
0046: import com.mobixess.jodb.tests.io.buffers.ITransactionBufferListener;
0047: import com.mobixess.jodb.tests.io.buffers.RandomAccessTestFileBufferFactory;
0048: import com.mobixess.jodb.tests.io.buffers.RandomAccessTestTransactionBuffer;
0049: import com.mobixess.jodb.tests.testobjects.InnerClassObject;
0050: import com.mobixess.jodb.tests.testobjects.ObjectA;
0051: import com.mobixess.jodb.tests.testobjects.ObjectB;
0052: import com.mobixess.jodb.tests.testobjects.ObjectC;
0053: import com.mobixess.jodb.tests.testobjects.ObjectD;
0054: import com.mobixess.jodb.tests.testobjects.ObjectWithTransientID;
0055:
0056: public class SimpleAddTest {
0057:
0058: public static int _testCounter;
0059:
0060: public static byte _modificationCounter;
0061:
0062: /**
0063: * @param args
0064: * @throws Exception
0065: */
0066: public static void main(String[] args) throws Exception {
0067: SimpleAddTest simpleAddTest = new SimpleAddTest();
0068: simpleAddTest.ensureTestFolderExists();
0069: //innerClassTest();
0070: simpleAddTest.objectWithChildFieldsTest();
0071: simpleAddTest.checkInputBuffersCount();
0072: simpleAddTest.arraysTest();
0073: simpleAddTest.checkInputBuffersCount();
0074: simpleAddTest.sizeAndPositioningTest();
0075: simpleAddTest.checkInputBuffersCount();
0076: simpleAddTest.noFieldsObjectTest();
0077: simpleAddTest.checkInputBuffersCount();
0078: simpleAddTest.multipleSubsTablesTest();
0079: simpleAddTest.fileLockCheck();
0080: simpleAddTest.checkInputBuffersCount();
0081: simpleAddTest.transactionTest();
0082: simpleAddTest.checkInputBuffersCount();
0083: simpleAddTest.stringTest();
0084: simpleAddTest.checkInputBuffersCount();
0085: simpleAddTest.transientTest();
0086: simpleAddTest.checkInputBuffersCount();
0087: simpleAddTest.sizeAndPositioningTest1();
0088: simpleAddTest.checkInputBuffersCount();
0089: simpleAddTest.largeSubstTablesTest();
0090: simpleAddTest.checkInputBuffersCount();
0091: simpleAddTest.checkInputBuffersCount();
0092: simpleAddTest.sizeIncreaseTest();
0093: simpleAddTest.checkInputBuffersCount();
0094: simpleAddTest.arraysTest1();
0095: simpleAddTest.checkInputBuffersCount();
0096: simpleAddTest.setTestBufferFactory();
0097: simpleAddTest.checkInputBuffersCount();
0098: simpleAddTest.objectWithFieldsTest();
0099: simpleAddTest.checkInputBuffersCount();
0100: simpleAddTest.multipleObjectWithFieldsTest();
0101: simpleAddTest.checkInputBuffersCount();
0102: simpleAddTest.objectWithChildFieldsModificationTest1();
0103: simpleAddTest.checkInputBuffersCount();
0104: simpleAddTest.objectWithChildFieldsModificationTest0();
0105: simpleAddTest.checkInputBuffersCount();
0106: simpleAddTest.deleteTest();
0107: simpleAddTest.checkInputBuffersCount();
0108: simpleAddTest.gcTest();
0109: simpleAddTest.checkInputBuffersCount();
0110: System.out.println("Test complete");
0111: }
0112:
0113: public void fileLockCheck() throws Exception {
0114: File testFileDir = new File("./testData/");
0115: testFileDir.mkdirs();
0116: File testFile = new File(testFileDir, SimpleAddTest.class
0117: .getSimpleName()
0118: + (_testCounter++) + ".jdb");
0119: testFile.delete();
0120:
0121: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0122:
0123: RandomAccessFile randomAccessFile = new RandomAccessFile(
0124: testFile, "rw");
0125: byte[] result = new byte[(int) randomAccessFile.length()];
0126: ByteBuffer buffer = ByteBuffer.wrap(result);
0127: try {
0128: randomAccessFile.getChannel().read(buffer, 0);
0129: } catch (Exception e) {
0130: sessionContainer.close();
0131: return;
0132: }
0133: sessionContainer.close();
0134: throw new IOException();
0135: }
0136:
0137: /**
0138: * @param args
0139: * @throws IOException
0140: * @throws IllegalClassTypeException
0141: */
0142: public void noFieldsObjectTest() throws IOException,
0143: IllegalClassTypeException {
0144: File testFileDir = new File("./testData/");
0145: testFileDir.mkdirs();
0146: File testFile = new File(testFileDir, SimpleAddTest.class
0147: .getSimpleName()
0148: + (_testCounter++) + ".jdb");
0149: testFile.delete();
0150:
0151: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0152: ObjectC objectC = new ObjectC();
0153: sessionContainer.set(objectC);
0154: sessionContainer.commit();
0155: sessionContainer.close();
0156:
0157: sessionContainer = getContainerForFile(testFile);
0158: List classes = sessionContainer.getAllObjects();
0159: if (classes.size() != 1) {
0160: throw new RuntimeException("" + classes.size());
0161: }
0162: Object obj = classes.get(0);
0163: if (obj.getClass() != objectC.getClass()) {
0164: throw new RuntimeException();
0165: }
0166: sessionContainer.close();
0167: }
0168:
0169: /**
0170: * @param args
0171: * @throws IOException
0172: * @throws IllegalClassTypeException
0173: */
0174: public void objectWithFieldsTest() throws IOException,
0175: IllegalClassTypeException {
0176: File testFileDir = new File("./testData/");
0177: testFileDir.mkdirs();
0178: File testFile = new File(testFileDir, SimpleAddTest.class
0179: .getSimpleName()
0180: + (_testCounter++) + ".jdb");
0181: testFile.delete();
0182:
0183: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0184: ObjectA objectA = new ObjectA((byte) 1, (short) 2, null);
0185: sessionContainer.set(objectA);
0186: sessionContainer.commit();
0187: sessionContainer.close();
0188:
0189: sessionContainer = getContainerForFile(testFile);
0190: List classes = sessionContainer.getAllObjects();
0191: if (classes.size() != 1) {
0192: throw new RuntimeException();
0193: }
0194: Object obj = classes.get(0);
0195: if (obj.getClass() != objectA.getClass()) {
0196: throw new RuntimeException();
0197: }
0198: ObjectA objA = (ObjectA) obj;
0199: if (objA.getVal1() != objectA.getVal1()
0200: || objA.getVal2() != objectA.getVal2()
0201: || objA.getVal3() != objectA.getVal3()) {
0202: throw new RuntimeException("objA=" + objA + " objectA="
0203: + objectA);
0204: }
0205: sessionContainer.close();
0206: }
0207:
0208: /**
0209: * @param args
0210: * @throws IOException
0211: * @throws IllegalClassTypeException
0212: */
0213: public void multipleObjectWithFieldsTest() throws IOException,
0214: IllegalClassTypeException {
0215: File testFileDir = new File("./testData/");
0216: testFileDir.mkdirs();
0217: File testFile = new File(testFileDir, SimpleAddTest.class
0218: .getSimpleName()
0219: + (_testCounter++) + ".jdb");
0220: testFile.delete();
0221:
0222: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0223: ObjectA objectA = new ObjectA((byte) 1, (short) 2, null);
0224: ObjectA objectA1 = new ObjectA((byte) 3, (short) 4, null);
0225: sessionContainer.set(objectA);
0226: sessionContainer.set(objectA1);
0227: sessionContainer.close();
0228:
0229: sessionContainer = getContainerForFile(testFile);
0230: List classes = sessionContainer.getAllObjects();
0231: if (classes.size() != 2) {
0232: throw new RuntimeException();
0233: }
0234: int index = classes.indexOf(objectA);
0235: if (index == -1) {
0236: throw new RuntimeException();
0237: }
0238: Object obj = classes.get(index);
0239: if (obj.getClass() != objectA.getClass()) {
0240: throw new RuntimeException();
0241: }
0242: ObjectA objA = (ObjectA) obj;
0243: if (objA.getVal1() != objectA.getVal1()
0244: || objA.getVal2() != objectA.getVal2()
0245: || objA.getVal3() != objectA.getVal3()) {
0246: throw new RuntimeException();
0247: }
0248: sessionContainer.close();
0249: }
0250:
0251: /**
0252: * @param args
0253: * @throws IOException
0254: * @throws IllegalClassTypeException
0255: */
0256: public void objectWithChildFieldsTest() throws IOException,
0257: IllegalClassTypeException {
0258: File testFileDir = new File("./testData/");
0259: testFileDir.mkdirs();
0260: File testFile = new File(testFileDir, SimpleAddTest.class
0261: .getSimpleName()
0262: + (_testCounter++) + ".jdb");
0263: testFile.delete();
0264:
0265: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0266: ObjectB objectB = new ObjectB();
0267: ObjectA objectA = new ObjectA((byte) 1, (short) 2, objectB);
0268: objectB._val3 = objectA;
0269: sessionContainer.set(objectA);
0270: sessionContainer.commit();
0271: sessionContainer.close();
0272:
0273: sessionContainer = getContainerForFile(testFile);
0274: List classes = sessionContainer.getAllObjects();
0275:
0276: if (classes.size() != 2) {
0277: throw new RuntimeException("" + classes.size());
0278: }
0279:
0280: int index = classes.indexOf(objectA);
0281:
0282: if (index == -1) {
0283: throw new RuntimeException();
0284: }
0285: Object obj = classes.get(index);
0286: if (obj.getClass() != objectA.getClass()) {
0287: throw new RuntimeException();
0288: }
0289: ObjectA objA = (ObjectA) obj;
0290: if (objA.getVal1() != objectA.getVal1()
0291: || objA.getVal2() != objectA.getVal2()) {
0292: throw new RuntimeException();
0293: }
0294:
0295: if (objA.getVal3().getClass() != objectB.getClass()) {
0296: throw new RuntimeException();
0297: }
0298:
0299: int bIndex = classes.indexOf(objectB);
0300:
0301: if (bIndex == -1) {
0302: throw new RuntimeException();
0303: }
0304:
0305: ObjectB objectB2 = (ObjectB) classes.get(bIndex);
0306:
0307: if (objectB2._val3 != objA) {
0308: throw new RuntimeException();
0309: }
0310:
0311: objectB2 = new ObjectB();
0312: objA.setVal3(objectB2);
0313: sessionContainer.set(objA);
0314:
0315: sessionContainer.commit();
0316:
0317: sessionContainer.close();
0318: sessionContainer = getContainerForFile(testFile);
0319: classes = sessionContainer.getAllObjects();
0320:
0321: if (classes.size() != 3) {
0322: throw new RuntimeException();
0323: }
0324:
0325: for (int i = 0; i < classes.size(); i++) {
0326: classes.get(i);
0327: }
0328:
0329: sessionContainer.close();
0330: }
0331:
0332: /**
0333: * @param args
0334: * @throws IOException
0335: * @throws IllegalClassTypeException
0336: */
0337: public void objectWithChildFieldsModificationTest0()
0338: throws IOException, IllegalClassTypeException {
0339: File testFileDir = new File("./testData/");
0340: testFileDir.mkdirs();
0341: File testFile = new File(testFileDir, SimpleAddTest.class
0342: .getSimpleName()
0343: + (_testCounter++) + ".jdb");
0344: testFile.delete();
0345:
0346: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0347: ObjectB objectB = new ObjectB();
0348: ObjectA objectA = new ObjectA((byte) 1, (short) 2, objectB);
0349: ObjectA objectA2 = new ObjectA((byte) 3, (byte) 3, objectB);
0350: objectB._val3 = objectA;
0351: sessionContainer.set(objectA);
0352: sessionContainer.set(objectA2);
0353: sessionContainer.commit();
0354: sessionContainer.close();
0355:
0356: sessionContainer = getContainerForFile(testFile);
0357: List classes = sessionContainer.getAllObjects();
0358:
0359: if (classes.size() != 3) {
0360: throw new RuntimeException();
0361: }
0362:
0363: int index = classes.indexOf(objectA);
0364:
0365: if (index == -1) {
0366: throw new RuntimeException();
0367: }
0368: Object obj = classes.get(index);
0369: if (obj.getClass() != objectA.getClass()) {
0370: throw new RuntimeException();
0371: }
0372: ObjectA objA = (ObjectA) obj;
0373: if (objA.getVal1() != objectA.getVal1()
0374: || objA.getVal2() != objectA.getVal2()) {
0375: throw new RuntimeException();
0376: }
0377:
0378: if (objA.getVal3().getClass() != objectB.getClass()) {
0379: throw new RuntimeException();
0380: }
0381:
0382: int bIndex = classes.indexOf(objectB);
0383:
0384: if (bIndex == -1) {
0385: throw new RuntimeException();
0386: }
0387:
0388: ObjectB objectB2 = (ObjectB) classes.get(bIndex);
0389:
0390: if (objectB2._val3 != objA) {
0391: throw new RuntimeException();
0392: }
0393:
0394: objA.setVal2((short) 33);
0395:
0396: sessionContainer.set(objA);
0397:
0398: sessionContainer.close();
0399:
0400: sessionContainer = getContainerForFile(testFile);
0401: classes = sessionContainer.getAllObjects();
0402:
0403: if (classes.size() != 3) {
0404: throw new RuntimeException();
0405: }
0406:
0407: index = classes.indexOf(objA);
0408:
0409: if (index == -1) {
0410: throw new RuntimeException();
0411: }
0412:
0413: objA = (ObjectA) classes.get(index);
0414: if (objA.getVal2() != 33) {
0415: throw new RuntimeException();
0416: }
0417:
0418: objA.setVal1((byte) 100);
0419: objA.setVal2((short) 300);
0420: sessionContainer.set(objA);
0421:
0422: sessionContainer.close();
0423:
0424: sessionContainer = getContainerForFile(testFile);
0425: //sessionContainer.printFileMap();
0426: classes = sessionContainer.getAllObjects();
0427:
0428: if (classes.size() != 3) {
0429: throw new RuntimeException();
0430: }
0431:
0432: index = classes.indexOf(objA);
0433:
0434: if (index == -1) {
0435: throw new RuntimeException();
0436: }
0437:
0438: objA = (ObjectA) classes.get(index);
0439: if (objA.getVal1() != 100 || objA.getVal2() != 300
0440: || objA.getVal3() == null) {
0441: throw new RuntimeException();
0442: }
0443:
0444: objA.setVal1((byte) 111);
0445: objA.setVal2((short) 333);
0446: objA.setVal3(null);
0447: sessionContainer.set(objA);
0448:
0449: sessionContainer.close();
0450:
0451: sessionContainer = getContainerForFile(testFile);
0452: sessionContainer.printFileMap();
0453: classes = sessionContainer.getAllObjects();
0454:
0455: if (classes.size() != 3) {
0456: throw new RuntimeException();
0457: }
0458:
0459: index = classes.indexOf(objA);
0460:
0461: if (index == -1) {
0462: throw new RuntimeException();
0463: }
0464:
0465: objA = (ObjectA) classes.get(index);
0466: if (objA.getVal1() != 111 || objA.getVal2() != 333
0467: || objA.getVal3() != null) {
0468: throw new RuntimeException();
0469: }
0470:
0471: sessionContainer.printFileMap();
0472: sessionContainer.close();
0473: }
0474:
0475: /**
0476: * @param args
0477: * @throws IOException
0478: * @throws IllegalClassTypeException
0479: */
0480: public void objectWithChildFieldsModificationTest1()
0481: throws IOException, IllegalClassTypeException {
0482: File testFileDir = new File("./testData/");
0483: testFileDir.mkdirs();
0484: File testFile = new File(testFileDir, SimpleAddTest.class
0485: .getSimpleName()
0486: + (_testCounter++) + ".jdb");
0487: testFile.delete();
0488:
0489: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0490: ObjectB objectB = new ObjectB();
0491: ObjectA objectA = new ObjectA((byte) 1, (short) 2, null);
0492: ObjectA objectA2 = new ObjectA((byte) 3, (byte) 3, objectB);
0493: objectB._val3 = objectA;
0494: sessionContainer.set(objectA);
0495: sessionContainer.set(objectA2);
0496: sessionContainer.commit();
0497: sessionContainer.close();
0498:
0499: sessionContainer = getContainerForFile(testFile);
0500: List classes = sessionContainer.getAllObjects();
0501:
0502: if (classes.size() != 3) {
0503: throw new RuntimeException();
0504: }
0505:
0506: int index = classes.indexOf(objectA);
0507:
0508: if (index == -1) {
0509: throw new RuntimeException();
0510: }
0511: Object obj = classes.get(index);
0512: if (obj.getClass() != objectA.getClass()) {
0513: throw new RuntimeException();
0514: }
0515: ObjectA objA = (ObjectA) obj;
0516: if (objA.getVal1() != objectA.getVal1()
0517: || objA.getVal2() != objectA.getVal2()) {
0518: throw new RuntimeException();
0519: }
0520:
0521: int bIndex = classes.indexOf(objectB);
0522:
0523: if (bIndex == -1) {
0524: throw new RuntimeException();
0525: }
0526:
0527: ObjectB objectB2 = (ObjectB) classes.get(bIndex);
0528:
0529: if (objectB2._val3 != objA) {
0530: throw new RuntimeException();
0531: }
0532:
0533: objA.setVal2((short) 33);
0534:
0535: int indexA2 = classes.indexOf(objectA2);
0536:
0537: ObjectA objA2 = (ObjectA) classes.get(indexA2);
0538:
0539: objA2.setVal3(null);
0540:
0541: sessionContainer.set(objA);
0542: sessionContainer.set(objA2);
0543:
0544: sessionContainer.printFileMap();
0545:
0546: sessionContainer.close();
0547:
0548: sessionContainer = getContainerForFile(testFile);
0549: classes = sessionContainer.getAllObjects();
0550:
0551: if (classes.size() != 3) {
0552: throw new RuntimeException();
0553: }
0554:
0555: index = classes.indexOf(objA);
0556:
0557: if (index == -1) {
0558: throw new RuntimeException();
0559: }
0560:
0561: objA = (ObjectA) classes.get(index);
0562: if (objA.getVal2() != 33) {
0563: throw new RuntimeException();
0564: }
0565:
0566: bIndex = classes.indexOf(objectB);
0567:
0568: if (bIndex == -1) {
0569: throw new RuntimeException();
0570: }
0571:
0572: objectB2 = (ObjectB) classes.get(bIndex);
0573:
0574: objA.setVal1((byte) 100);
0575: objA.setVal2((short) 300);
0576: objA.setVal3(objectB2);
0577: sessionContainer.set(objA);
0578:
0579: sessionContainer.printFileMap();
0580: sessionContainer.close();
0581:
0582: sessionContainer = getContainerForFile(testFile);
0583: //sessionContainer.printFileMap();
0584: classes = sessionContainer.getAllObjects();
0585:
0586: if (classes.size() != 3) {
0587: throw new RuntimeException();
0588: }
0589:
0590: index = classes.indexOf(objA);
0591:
0592: if (index == -1) {
0593: throw new RuntimeException();
0594: }
0595:
0596: objA = (ObjectA) classes.get(index);
0597: if (objA.getVal1() != 100 || objA.getVal2() != 300
0598: || objA.getVal3() == null) {
0599: throw new RuntimeException();
0600: }
0601:
0602: objA.setVal1((byte) 111);
0603: objA.setVal2((short) 333);
0604: objA.setVal3(null);
0605: sessionContainer.set(objA);
0606:
0607: sessionContainer.close();
0608:
0609: sessionContainer = getContainerForFile(testFile);
0610: sessionContainer.printFileMap();
0611: classes = sessionContainer.getAllObjects();
0612:
0613: if (classes.size() != 3) {
0614: throw new RuntimeException();
0615: }
0616:
0617: index = classes.indexOf(objA);
0618:
0619: if (index == -1) {
0620: throw new RuntimeException();
0621: }
0622:
0623: objA = (ObjectA) classes.get(index);
0624: if (objA.getVal1() != 111 || objA.getVal2() != 333
0625: || objA.getVal3() != null) {
0626: throw new RuntimeException();
0627: }
0628: sessionContainer.close();
0629: }
0630:
0631: /**
0632: * @param args
0633: * @throws IOException
0634: * @throws IllegalClassTypeException
0635: */
0636: public void deleteTest() throws IOException,
0637: IllegalClassTypeException {
0638: File testFileDir = new File("./testData/");
0639: testFileDir.mkdirs();
0640: File testFile = new File(testFileDir, SimpleAddTest.class
0641: .getSimpleName()
0642: + (_testCounter++) + ".jdb");
0643: testFile.delete();
0644:
0645: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0646: ObjectB objectB = new ObjectB();
0647: ObjectA objectA = new ObjectA((byte) 1, (short) 2, objectB);
0648: ObjectA objectA2 = new ObjectA((byte) 3, (byte) 3, null);
0649: objectB._val3 = objectA2;
0650: sessionContainer.set(objectA);
0651: sessionContainer.commit();
0652: sessionContainer.close();
0653:
0654: sessionContainer = getContainerForFile(testFile);
0655: List classes = sessionContainer.getAllObjects();
0656:
0657: if (classes.size() != 3) {
0658: throw new RuntimeException();
0659: }
0660:
0661: int index = classes.indexOf(objectA);
0662:
0663: if (index == -1) {
0664: throw new RuntimeException();
0665: }
0666: Object obj = classes.get(index);
0667:
0668: sessionContainer.delete(obj);
0669:
0670: sessionContainer.close();
0671: /////
0672: sessionContainer = getContainerForFile(testFile);
0673: classes = sessionContainer.getAllObjects();
0674:
0675: if (classes.size() != 2) {
0676: throw new RuntimeException();
0677: }
0678:
0679: index = classes.indexOf(objectB);
0680:
0681: if (index == -1) {
0682: throw new RuntimeException();
0683: }
0684: obj = classes.get(index);
0685:
0686: sessionContainer.delete(obj);
0687:
0688: sessionContainer.close();
0689: ////
0690: sessionContainer = getContainerForFile(testFile);
0691: classes = sessionContainer.getAllObjects();
0692:
0693: if (classes.size() != 1) {
0694: throw new RuntimeException();
0695: }
0696:
0697: index = classes.indexOf(objectA2);
0698:
0699: if (index == -1) {
0700: throw new RuntimeException();
0701: }
0702:
0703: obj = classes.get(index);
0704:
0705: sessionContainer.delete(obj);
0706:
0707: sessionContainer.close();
0708:
0709: ////
0710: sessionContainer = getContainerForFile(testFile);
0711: classes = sessionContainer.getAllObjects();
0712:
0713: if (classes.size() != 0) {
0714: throw new RuntimeException();
0715: }
0716:
0717: //sessionContainer.printFileMap();
0718:
0719: sessionContainer.set(objectA);
0720: sessionContainer.close();
0721:
0722: sessionContainer = getContainerForFile(testFile);
0723:
0724: classes = sessionContainer.getAllObjects();
0725:
0726: if (classes.size() != 3) {
0727: throw new RuntimeException();
0728: }
0729:
0730: index = classes.indexOf(objectA);
0731:
0732: if (index == -1) {
0733: throw new RuntimeException();
0734: }
0735:
0736: obj = classes.get(index);
0737:
0738: sessionContainer.delete(obj, Integer.MAX_VALUE);//delete all
0739:
0740: sessionContainer.close();
0741:
0742: ////
0743: sessionContainer = getContainerForFile(testFile);
0744: classes = sessionContainer.getAllObjects();
0745:
0746: if (classes.size() != 0) {
0747: throw new RuntimeException();
0748: }
0749:
0750: //sessionContainer.printFileMap();
0751:
0752: sessionContainer.set(objectA);
0753: sessionContainer.close();
0754: ////
0755: sessionContainer = getContainerForFile(testFile);
0756: classes = sessionContainer.getAllObjects();
0757:
0758: if (classes.size() != 3) {
0759: throw new RuntimeException();
0760: }
0761:
0762: index = classes.indexOf(objectB);
0763:
0764: if (index == -1) {
0765: throw new RuntimeException();
0766: }
0767:
0768: obj = classes.get(index);
0769:
0770: sessionContainer.delete(obj);
0771: //sessionContainer.printFileMap();
0772: sessionContainer.close();
0773: ////
0774: sessionContainer = getContainerForFile(testFile);
0775: sessionContainer.printFileMap();
0776: classes = sessionContainer.getAllObjects();
0777:
0778: if (classes.size() != 2) {
0779: throw new RuntimeException();
0780: }
0781:
0782: index = classes.indexOf(objectA);
0783:
0784: if (index == -1) {
0785: throw new RuntimeException();
0786: }
0787: obj = classes.get(index);
0788:
0789: if (((ObjectA) obj).getVal3() != null) {
0790: throw new RuntimeException();
0791: }
0792:
0793: sessionContainer.printFileMap();
0794: sessionContainer.close();
0795: }
0796:
0797: public void stringTest() throws IOException,
0798: IllegalClassTypeException {
0799: File testFileDir = new File("./testData/");
0800: testFileDir.mkdirs();
0801: File testFile = new File(testFileDir, SimpleAddTest.class
0802: .getSimpleName()
0803: + (_testCounter++) + ".jdb");
0804: testFile.delete();
0805:
0806: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0807: String testString = "testString";
0808: sessionContainer.set(testString);
0809: sessionContainer.commit();
0810: sessionContainer.close();
0811:
0812: sessionContainer = getContainerForFile(testFile);
0813: List classes = sessionContainer.getAllObjects();
0814: if (classes.size() != 1) {
0815: throw new RuntimeException();
0816: }
0817: String stringFromDB = (String) classes.get(0);
0818: if (!stringFromDB.equals(testString)) {
0819: throw new RuntimeException();
0820: }
0821: IPersistentObjectStatistics statistics = sessionContainer
0822: .getPersistenceStatistics(stringFromDB);
0823: if (statistics.getTotalRecords() != 1) {
0824: throw new RuntimeException();
0825: }
0826:
0827: sessionContainer.set(stringFromDB);
0828:
0829: sessionContainer.commit();
0830:
0831: classes = sessionContainer.getAllObjects();
0832: if (classes.size() != 1) {
0833: throw new RuntimeException();
0834: }
0835: stringFromDB = (String) classes.get(0);
0836: if (!stringFromDB.equals(testString)) {
0837: throw new RuntimeException();
0838: }
0839: statistics = sessionContainer
0840: .getPersistenceStatistics(stringFromDB);
0841: if (statistics.getTotalRecords() != 1) {
0842: throw new RuntimeException();
0843: }
0844: sessionContainer.close();
0845: }
0846:
0847: /**
0848: * @param args
0849: * @throws IOException
0850: * @throws IllegalClassTypeException
0851: */
0852: public void gcTest() throws IOException, IllegalClassTypeException {
0853: File testFileDir = new File("./testData/");
0854: testFileDir.mkdirs();
0855: File testFile = new File(testFileDir, SimpleAddTest.class
0856: .getSimpleName()
0857: + (_testCounter++) + ".jdb");
0858: testFile.delete();
0859:
0860: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0861: ObjectB objectB = new ObjectB();
0862: ObjectA objectA = new ObjectA((byte) 1, (short) 2, objectB);
0863: ObjectA objectA2 = new ObjectA((byte) 3, (byte) 3, objectB);
0864: objectB._val3 = objectA;
0865: sessionContainer.set(objectA);
0866: sessionContainer.set(objectA2);
0867: sessionContainer.commit();
0868: sessionContainer.close();
0869:
0870: sessionContainer = getContainerForFile(testFile);
0871: List classes = sessionContainer.getAllObjects();
0872:
0873: if (classes.size() != 3) {
0874: throw new RuntimeException();
0875: }
0876:
0877: int index = classes.indexOf(objectA);
0878:
0879: if (index == -1) {
0880: throw new RuntimeException();
0881: }
0882: Object obj = classes.get(index);
0883: if (obj.getClass() != objectA.getClass()) {
0884: throw new RuntimeException();
0885: }
0886: ObjectA objA = (ObjectA) obj;
0887: if (objA.getVal1() != objectA.getVal1()
0888: || objA.getVal2() != objectA.getVal2()) {
0889: throw new RuntimeException();
0890: }
0891:
0892: if (objA.getVal3().getClass() != objectB.getClass()) {
0893: throw new RuntimeException();
0894: }
0895:
0896: int bIndex = classes.indexOf(objectB);
0897:
0898: if (bIndex == -1) {
0899: throw new RuntimeException();
0900: }
0901:
0902: ObjectB objectB2 = (ObjectB) classes.get(bIndex);
0903:
0904: if (objectB2._val3 != objA) {
0905: throw new RuntimeException();
0906: }
0907:
0908: objA.setVal2((short) 33);
0909:
0910: sessionContainer.set(objA);
0911:
0912: sessionContainer.close();
0913:
0914: sessionContainer = getContainerForFile(testFile);
0915: classes = sessionContainer.getAllObjects();
0916:
0917: if (classes.size() != 3) {
0918: throw new RuntimeException();
0919: }
0920:
0921: index = classes.indexOf(objA);
0922:
0923: if (index == -1) {
0924: throw new RuntimeException();
0925: }
0926:
0927: ObjectA objectA3 = (ObjectA) classes.get(index);
0928: if (objectA3.getVal2() != 33) {
0929: throw new RuntimeException();
0930: }
0931:
0932: if (sessionContainer.getCachedObjectsCount() <= 0) {
0933: throw new RuntimeException();
0934: }
0935:
0936: System.out.println("DELAY START --------------->"
0937: + sessionContainer.getCachedObjectsCount());
0938: objectA3 = null;
0939:
0940: objectB = null;
0941: objectA = null;
0942: objectA2 = null;
0943: objectB2 = null;
0944: objectA3 = null;
0945: obj = null;
0946: objA = null;
0947: classes = null;
0948:
0949: System.gc();
0950: try {
0951: Thread.sleep(2000);
0952: } catch (InterruptedException e) {
0953: e.printStackTrace();
0954: }
0955: System.out.println("DELAY END ---------------"
0956: + sessionContainer.getCachedObjectsCount());
0957: if (sessionContainer.getCachedObjectsCount() > 0) {
0958: throw new RuntimeException();
0959: }
0960: classes = sessionContainer.getAllObjects();
0961:
0962: if (classes.size() != 3) {
0963: throw new RuntimeException();
0964: }
0965: objA = new ObjectA((byte) 1, (short) 33, null);
0966: index = classes.indexOf(objA);
0967:
0968: if (index == -1) {
0969: throw new RuntimeException();
0970: }
0971:
0972: objectA3 = (ObjectA) classes.get(index);
0973: if (objectA3.getVal2() != 33) {
0974: throw new RuntimeException();
0975: }
0976:
0977: // try {
0978: // Thread.sleep(2000000);
0979: // } catch (InterruptedException e) {
0980: // e.printStackTrace();
0981: // }
0982: sessionContainer.close();
0983: }
0984:
0985: /**
0986: * @param args
0987: * @throws IOException
0988: * @throws IllegalClassTypeException
0989: */
0990: public void arraysTest() throws IOException,
0991: IllegalClassTypeException {
0992: File testFileDir = new File("./testData/");
0993: testFileDir.mkdirs();
0994: File testFile = new File(testFileDir, SimpleAddTest.class
0995: .getSimpleName()
0996: + (_testCounter++) + ".jdb");
0997: testFile.delete();
0998:
0999: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
1000: ObjectA objectA = new ObjectA((byte) 1, (short) 2, null);
1001: ObjectB objectB = new ObjectB();
1002: Object[] array = new Object[] { objectA, objectB };
1003:
1004: sessionContainer.set(objectB);
1005: sessionContainer.set(array);
1006: sessionContainer.set(objectA);
1007:
1008: sessionContainer.commit();
1009: sessionContainer.close();
1010:
1011: sessionContainer = getContainerForFile(testFile);
1012: //sessionContainer.printFileMap();
1013: List classes = sessionContainer.getAllObjects();
1014: if (classes.size() != 3) {
1015: throw new RuntimeException();
1016: }
1017: Object arrayObj = null;
1018: for (int i = 0; i < classes.size(); i++) {
1019: if (classes.get(i).getClass().isArray()) {
1020: arrayObj = classes.get(i);
1021: }
1022: }
1023: Object[] array1 = (Object[]) arrayObj;
1024: List<Object> list = Arrays.asList(array1);
1025: int index = list.indexOf(objectA);
1026: if (index != 0) {
1027: throw new RuntimeException();
1028: }
1029:
1030: ObjectA objectA2 = (ObjectA) array1[index];
1031:
1032: index = list.indexOf(objectB);
1033: if (index != 1) {
1034: throw new RuntimeException();
1035: }
1036:
1037: ObjectB objectB2 = (ObjectB) array1[index];
1038:
1039: objectA2.setVal3(objectB2);
1040: objectB2._val3 = objectA2;
1041:
1042: sessionContainer.set(objectB2);
1043: sessionContainer.set(objectA2);
1044: sessionContainer.set(arrayObj);
1045:
1046: sessionContainer.close();
1047:
1048: sessionContainer = getContainerForFile(testFile);
1049: //sessionContainer.printFileMap();
1050: //sessionContainer.close();
1051:
1052: objectA = new ObjectA((byte) 2, (short) 3, null);
1053: objectB = new ObjectB();
1054:
1055: classes = sessionContainer.getAllObjects();
1056: if (classes.size() != 3) {
1057: throw new RuntimeException();
1058: }
1059: arrayObj = null;
1060: for (int i = 0; i < classes.size(); i++) {
1061: if (classes.get(i).getClass().isArray()) {
1062: arrayObj = classes.get(i);
1063: }
1064: }
1065: array1 = (Object[]) arrayObj;
1066: array1[0] = objectB;
1067: array1[1] = objectA;
1068: sessionContainer.set(array1);
1069: sessionContainer.commit();
1070: sessionContainer.printFileMap();
1071:
1072: sessionContainer.close();
1073:
1074: sessionContainer = getContainerForFile(testFile);
1075:
1076: classes = sessionContainer.getAllObjects();
1077: if (classes.size() != 5) {
1078: throw new RuntimeException();
1079: }
1080: arrayObj = null;
1081: for (int i = 0; i < classes.size(); i++) {
1082: if (classes.get(i).getClass().isArray()) {
1083: arrayObj = classes.get(i);
1084: }
1085: }
1086: array1 = (Object[]) arrayObj;
1087: list = Arrays.asList(array1);
1088:
1089: index = list.indexOf(objectA);
1090: if (index != 1) {
1091: throw new RuntimeException();
1092: }
1093:
1094: index = list.indexOf(objectB);
1095: if (index != 0) {
1096: throw new RuntimeException();
1097: }
1098:
1099: sessionContainer.close();
1100: }
1101:
1102: /**
1103: * @param args
1104: * @throws IOException
1105: * @throws IllegalClassTypeException
1106: */
1107: public void sizeIncreaseTest() throws IOException,
1108: IllegalClassTypeException {
1109: File testFileDir = new File("./testData/");
1110: testFileDir.mkdirs();
1111: File testFile = new File(testFileDir, SimpleAddTest.class
1112: .getSimpleName()
1113: + (_testCounter++) + ".jdb");
1114: testFile.delete();
1115:
1116: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
1117: for (int i = 0; i < 0x300; i++) {
1118: System.err.println("sizeIncreaseTest " + i);
1119: byte[] array = new byte[i];
1120: sessionContainer.set(array);
1121: sessionContainer.commit();
1122: List classes = sessionContainer.getAllObjects();
1123: if (classes.size() != i + 1) {
1124: throw new RuntimeException();
1125: }
1126: boolean found = false;
1127: for (int j = classes.size() - 1; j >= 0; --j) {
1128: byte[] arr = (byte[]) classes.get(j);
1129: if (arr.length == array.length) {
1130: found = true;
1131: break;
1132: }
1133: }
1134: if (!found) {
1135: throw new RuntimeException();
1136: }
1137: }
1138: sessionContainer.close();
1139: }
1140:
1141: public void transientTest() throws Exception {
1142: File testFileDir = new File("./testData/");
1143: testFileDir.mkdirs();
1144: File testFile = new File(testFileDir, SimpleAddTest.class
1145: .getSimpleName()
1146: + (_testCounter++) + ".jdb");
1147: testFile.delete();
1148: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
1149: ObjectWithTransientID objectWithTransientID = new ObjectWithTransientID();
1150: objectWithTransientID._memb1 = 1;
1151: objectWithTransientID._memb2 = 2;
1152: objectWithTransientID._memb3 = objectWithTransientID;
1153: sessionContainer.set(objectWithTransientID);
1154: sessionContainer.close();
1155:
1156: sessionContainer = getContainerForFile(testFile);
1157: List list = sessionContainer.getAllObjects();
1158: objectWithTransientID = (ObjectWithTransientID) list.get(0);
1159: if (objectWithTransientID._memb1 != 1
1160: || objectWithTransientID._memb2 != 0
1161: || objectWithTransientID._memb3 != null) {
1162: throw new RuntimeException();
1163: }
1164: sessionContainer.close();
1165: }
1166:
1167: public void sizeAndPositioningTest1() throws Exception {
1168: File testFileDir = new File("./testData/");
1169: testFileDir.mkdirs();
1170: File testFile = new File(testFileDir, SimpleAddTest.class
1171: .getSimpleName()
1172: + (_testCounter++) + ".jdb");
1173: testFile.delete();
1174: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
1175: Object firstObject = new Object();
1176: sessionContainer.set(firstObject);
1177: sessionContainer.commit();
1178:
1179: ObjectD testObjectD = new ObjectD();
1180: testObjectD._obj0 = firstObject;
1181: sessionContainer.set(testObjectD);
1182: sessionContainer.commit();
1183: //sessionContainer.printFileMap();
1184:
1185: testObjectD._obj0 = null;
1186: sessionContainer.set(testObjectD);
1187: sessionContainer.commit();
1188: //sessionContainer.printFileMap();
1189:
1190: if (sessionContainer.getPersistenceStatistics(testObjectD)
1191: .getTotalRecords() != 1) {
1192: throw new RuntimeException();
1193: }
1194:
1195: testObjectD._obj0 = firstObject;
1196: sessionContainer.set(testObjectD);
1197: sessionContainer.commit();
1198: //sessionContainer.printFileMap();
1199: if (sessionContainer.getPersistenceStatistics(testObjectD)
1200: .getTotalRecords() != 1) {
1201: throw new RuntimeException();
1202: }
1203:
1204: testObjectD._obj1 = testObjectD;
1205: sessionContainer.set(testObjectD);
1206: sessionContainer.commit();
1207: //sessionContainer.printFileMap();
1208: if (sessionContainer.getPersistenceStatistics(testObjectD)
1209: .getTotalRecords() != 2) {
1210: throw new RuntimeException();
1211: }
1212:
1213: testObjectD._member1 = 10;
1214: sessionContainer.set(testObjectD);
1215: sessionContainer.commit();
1216: //sessionContainer.printFileMap();
1217: if (sessionContainer.getPersistenceStatistics(testObjectD)
1218: .getTotalRecords() != 2) {
1219: throw new RuntimeException();
1220: }
1221:
1222: testObjectD._obj0 = null;
1223: sessionContainer.set(testObjectD);
1224: sessionContainer.commit();
1225: //sessionContainer.printFileMap();
1226: if (sessionContainer.getPersistenceStatistics(testObjectD)
1227: .getTotalRecords() != 1) {
1228: throw new RuntimeException();
1229: }
1230:
1231: testObjectD._obj1 = null;
1232: sessionContainer.set(testObjectD);
1233: sessionContainer.commit();
1234: //sessionContainer.printFileMap();
1235: if (sessionContainer.getPersistenceStatistics(testObjectD)
1236: .getTotalRecords() != 1) {
1237: throw new RuntimeException();
1238: }
1239:
1240: testObjectD._obj0 = testObjectD._obj1 = testObjectD;
1241: sessionContainer.set(testObjectD);
1242: sessionContainer.commit();
1243: //sessionContainer.printFileMap();
1244: if (sessionContainer.getPersistenceStatistics(testObjectD)
1245: .getTotalRecords() != 2) {
1246: throw new RuntimeException();
1247: }
1248:
1249: testObjectD._obj0 = testObjectD._obj1 = null;
1250: sessionContainer.set(testObjectD);
1251: sessionContainer.commit();
1252: //sessionContainer.printFileMap();
1253: if (sessionContainer.getPersistenceStatistics(testObjectD)
1254: .getTotalRecords() != 1) {
1255: throw new RuntimeException();
1256: }
1257:
1258: List list = sessionContainer.getAllObjects();
1259: if (list.size() != 2) {
1260: throw new IOException();
1261: }
1262:
1263: Object[] array = new Object[3];
1264: array[0] = firstObject;
1265: array[1] = firstObject;
1266: sessionContainer.set(testObjectD);
1267: sessionContainer.commit();
1268:
1269: array[2] = array[2];
1270: sessionContainer.set(array);
1271: sessionContainer.commit();
1272:
1273: //sessionContainer.printFileMap();
1274: if (sessionContainer.getPersistenceStatistics(testObjectD)
1275: .getTotalRecords() != 1) {
1276: throw new RuntimeException();
1277: }
1278:
1279: int inCache = sessionContainer.getCachedObjectsCount();
1280: System.gc();
1281: Thread.sleep(500);
1282: array = null;
1283: System.gc();
1284: Thread.sleep(500);
1285: if (inCache == sessionContainer.getCachedObjectsCount()) {
1286: throw new RuntimeException("test flow error");
1287: }
1288: //sessionContainer.printFileMap();
1289: list = sessionContainer.getAllObjects();
1290: for (int i = 0; i < list.size(); i++) {
1291: Object obj = list.get(i);
1292: if (obj.getClass().isArray()) {
1293: array = (Object[]) obj;
1294: }
1295: }
1296:
1297: if (array[0] != firstObject || array[1] != firstObject
1298: || array[2] != null) {
1299: throw new RuntimeException();
1300: }
1301:
1302: array[2] = array;
1303: sessionContainer.set(array);
1304: sessionContainer.commit();
1305:
1306: //sessionContainer.printFileMap();
1307: if (sessionContainer.getPersistenceStatistics(testObjectD)
1308: .getTotalRecords() != 1) {
1309: throw new RuntimeException();
1310: }
1311:
1312: inCache = sessionContainer.getCachedObjectsCount();
1313: System.gc();
1314: Thread.sleep(500);
1315: array = null;
1316: System.gc();
1317: Thread.sleep(500);
1318: if (inCache == sessionContainer.getCachedObjectsCount()) {
1319: throw new RuntimeException("test flow error");
1320: }
1321:
1322: list = sessionContainer.getAllObjects();
1323: for (int i = 0; i < list.size(); i++) {
1324: Object obj = list.get(i);
1325: if (obj.getClass().isArray()) {
1326: array = (Object[]) obj;
1327: }
1328: }
1329:
1330: if (array[0] != firstObject || array[1] != firstObject
1331: || array[2] != array) {
1332: throw new RuntimeException();
1333: }
1334:
1335: sessionContainer.close();
1336: }
1337:
1338: public void sizeAndPositioningTest() throws Exception {
1339: File testFileDir = new File("./testData/");
1340: testFileDir.mkdirs();
1341: File testFile = new File(testFileDir, SimpleAddTest.class
1342: .getSimpleName()
1343: + (_testCounter++) + ".jdb");
1344: testFile.delete();
1345:
1346: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
1347: ClassLoader classLoaderForShortLengthObject = null;
1348: String className = "Test" + (_testCounter - 1) + "Class";
1349: int i = 1;
1350: for (; i < Integer.MAX_VALUE; i++) {
1351: DynamicTestClass dynamicTestClass = new DynamicTestClass(
1352: className, i);
1353: Object obj = dynamicTestClass.load(sessionContainer);
1354: classLoaderForShortLengthObject = dynamicTestClass._classLoader;
1355: sessionContainer.set(obj);
1356: sessionContainer.commit();
1357: IPersistentObjectStatistics objectStatistics = sessionContainer
1358: .getPersistenceStatistics(obj);
1359: dynamicTestClass.delete();
1360: long size = objectStatistics.getBodySize();
1361: if (size >= 0xFF) {
1362: break;
1363: }
1364: }
1365: sessionContainer.close();
1366:
1367: testFile = new File(testFileDir, SimpleAddTest.class
1368: .getSimpleName()
1369: + (_testCounter++) + ".jdb");
1370:
1371: testFile.delete();
1372:
1373: sessionContainer = getContainerForFile(testFile);
1374:
1375: DynamicTestClass byteLengthDynamicTestClass = new DynamicTestClass(
1376: className, i - 1);
1377: Object obj = byteLengthDynamicTestClass.load(sessionContainer);
1378: sessionContainer.set(obj);
1379: sessionContainer.close();
1380:
1381: sessionContainer = getContainerForFile(testFile);
1382: sessionContainer
1383: .setClassLoader(classLoaderForShortLengthObject);
1384:
1385: List list = sessionContainer.getAllObjects();
1386: obj = list.get(0);
1387: sessionContainer.set(obj);
1388: sessionContainer.close();
1389:
1390: sessionContainer = getContainerForFile(testFile);
1391: sessionContainer
1392: .setClassLoader(classLoaderForShortLengthObject);
1393: list = sessionContainer.getAllObjects();
1394: obj = list.get(0);
1395: IPersistentObjectStatistics objectStatistics = sessionContainer
1396: .getPersistenceStatistics(obj);
1397: if (objectStatistics.getTotalRecords() != 1) {
1398: throw new RuntimeException();
1399: }
1400: modify(obj);
1401: sessionContainer.set(obj);
1402: sessionContainer.close();
1403:
1404: sessionContainer = getContainerForFile(testFile);
1405: sessionContainer
1406: .setClassLoader(classLoaderForShortLengthObject);
1407: list = sessionContainer.getAllObjects();
1408: obj = list.get(0);
1409: objectStatistics = sessionContainer
1410: .getPersistenceStatistics(obj);
1411: if (objectStatistics.getTotalRecords() != 2) {
1412: throw new RuntimeException();
1413: }
1414: sessionContainer.close();
1415:
1416: sessionContainer = getContainerForFile(testFile);
1417: sessionContainer
1418: .setClassLoader(byteLengthDynamicTestClass._classLoader);
1419: list = sessionContainer.getAllObjects();
1420: obj = list.get(0);
1421: modify(obj);
1422: sessionContainer.set(obj);
1423: sessionContainer.close();
1424:
1425: sessionContainer = getContainerForFile(testFile);
1426: sessionContainer
1427: .setClassLoader(classLoaderForShortLengthObject);
1428: list = sessionContainer.getAllObjects();
1429: obj = list.get(0);
1430: objectStatistics = sessionContainer
1431: .getPersistenceStatistics(obj);
1432: if (objectStatistics.getTotalRecords() != 1) {
1433: throw new RuntimeException(""
1434: + objectStatistics.getTotalRecords());
1435: }
1436: sessionContainer.close();
1437: byteLengthDynamicTestClass.delete();
1438: sessionContainer.close();
1439: }
1440:
1441: public void modify(Object object) throws Exception {
1442: Field[] fields = object.getClass().getDeclaredFields();
1443: for (int i = 0; i < fields.length; i++) {
1444: if (fields[i].getType() == byte.class) {
1445: _modificationCounter++;
1446: Field field = fields[i];
1447: field.setAccessible(true);
1448: field.set(object, _modificationCounter);
1449: }
1450: }
1451: }
1452:
1453: public void largeSubstTablesTest() throws Exception {
1454: File testFileDir = new File("./testData/");
1455: testFileDir.mkdirs();
1456: File testFile = new File(testFileDir, SimpleAddTest.class
1457: .getSimpleName()
1458: + (_testCounter++) + ".jdb");
1459: testFile.delete();
1460:
1461: WriteIntersectingFactory factory = new WriteIntersectingFactory();
1462: JODBConfig.setRandomAccessBufferFactory(factory);
1463:
1464: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
1465:
1466: long substTableFreeSize = sessionContainer
1467: .getDatabaseStatistics().literalSubstFreeSize(0);
1468: long expectedSize = substTableFreeSize;
1469: StringBuffer className = new StringBuffer("TestClass");
1470: int i = 0;
1471: for (; className.length() < expectedSize
1472: && className.length() < 200; i++) {
1473: className.append("Test" + i);
1474: }
1475:
1476: DynamicTestClass dynamicTestClass = new DynamicTestClass(
1477: className.toString(), i * 10);
1478: Object object = dynamicTestClass.load(sessionContainer,
1479: long.class, false);
1480: sessionContainer.set(object);
1481: sessionContainer.commit();
1482: sessionContainer.close();
1483: sessionContainer = getContainerForFile(testFile);
1484: sessionContainer.setClassLoader(dynamicTestClass._classLoader);
1485:
1486: if (sessionContainer.getDatabaseStatistics().totalDataEntries() != 2) {
1487: throw new RuntimeException();
1488: }
1489:
1490: long firstPageSize = sessionContainer.getDatabaseStatistics()
1491: .literalSubstMaxSize(0);
1492: long secondPageSize = sessionContainer.getDatabaseStatistics()
1493: .literalSubstMaxSize(1);
1494: if (secondPageSize <= firstPageSize) {
1495: throw new RuntimeException();
1496: }
1497: List list = sessionContainer.getAllObjects();
1498: if (list.size() != 1) {
1499: throw new RuntimeException();
1500: }
1501: dynamicTestClass.delete();
1502: i++;
1503: className.append("Test" + i);
1504: dynamicTestClass = new DynamicTestClass(className.toString(),
1505: i * 10);
1506: object = dynamicTestClass.load(sessionContainer, long.class,
1507: false);
1508:
1509: sessionContainer.set(object);
1510: sessionContainer.close();
1511:
1512: sessionContainer = getContainerForFile(testFile);
1513: sessionContainer.setClassLoader(dynamicTestClass._classLoader);
1514:
1515: if (sessionContainer.getDatabaseStatistics().totalDataEntries() != 3) {
1516: throw new RuntimeException();
1517: }
1518:
1519: sessionContainer.close();
1520: dynamicTestClass.delete();
1521: }
1522:
1523: public void innerClassTest() throws Exception {
1524: innerClassTest(true);
1525: innerClassTest(false);
1526: }
1527:
1528: public void innerClassTest(boolean closeAfterCommit)
1529: throws Exception {
1530: File testFileDir = new File("./testData/");
1531: testFileDir.mkdirs();
1532: File testFile = new File(testFileDir, SimpleAddTest.class
1533: .getSimpleName()
1534: + (_testCounter++) + ".jdb");
1535: testFile.delete();
1536:
1537: WriteIntersectingFactory factory = new WriteIntersectingFactory();
1538: JODBConfig.setRandomAccessBufferFactory(factory);
1539:
1540: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
1541: InnerClassObject innerClassObject = new InnerClassObject();
1542: sessionContainer.set(innerClassObject);
1543: sessionContainer.commit();
1544:
1545: if (closeAfterCommit) {
1546: sessionContainer.close();
1547: sessionContainer = getContainerForFile(testFile);
1548: }
1549:
1550: Query query = sessionContainer.query();
1551:
1552: query.constrain(innerClassObject.getInnerClass().getClass());
1553:
1554: List list = query.execute();
1555:
1556: if (list.size() != 1
1557: || list.get(0).getClass() != innerClassObject
1558: .getInnerClass().getClass()) {
1559: throw new RuntimeException(" +++" + list.size());
1560: }
1561:
1562: }
1563:
1564: public void multipleSubsTablesTest() throws Exception {
1565: multipleSubsTablesTest0(true);
1566: multipleSubsTablesTest0(false);
1567: }
1568:
1569: public void multipleSubsTablesTest0(boolean closeAfterCommit)
1570: throws Exception {
1571: File testFileDir = new File("./testData/");
1572: testFileDir.mkdirs();
1573: File testFile = new File(testFileDir, SimpleAddTest.class
1574: .getSimpleName()
1575: + (_testCounter++) + ".jdb");
1576: testFile.delete();
1577:
1578: WriteIntersectingFactory factory = new WriteIntersectingFactory();
1579: JODBConfig.setRandomAccessBufferFactory(factory);
1580:
1581: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
1582:
1583: int dataEntries = sessionContainer.getDatabaseStatistics()
1584: .totalDataEntries();
1585: StringBuffer className = new StringBuffer("TestClass");
1586: final int testIndexStart = 100;
1587: int testIndex = testIndexStart;
1588: int totalWritten = 0;
1589: Object object = null;
1590: int newTableCreationIOCalls = 0;
1591: while (dataEntries < 2 && className.length() < 0xFFFF) {
1592: factory.intersectionInit(null, Integer.MAX_VALUE, false);
1593: factory.setIntersectEnabled(true);
1594: className.append("Test" + testIndex);
1595: DynamicTestClass dynamicTestClass = new DynamicTestClass(
1596: className.toString(), testIndex);
1597: object = dynamicTestClass.load(sessionContainer);
1598: totalWritten++;
1599: testIndex++;
1600: sessionContainer.set(object);
1601: sessionContainer.commit();
1602: newTableCreationIOCalls = factory._counter;
1603: if (closeAfterCommit) {
1604: sessionContainer.close();
1605: sessionContainer = getContainerForFile(testFile);
1606: sessionContainer
1607: .setClassLoader(dynamicTestClass._classLoader);
1608: }
1609: dataEntries = sessionContainer.getDatabaseStatistics()
1610: .totalDataEntries();
1611: dynamicTestClass.delete();
1612: }
1613: if (dataEntries != 2) {
1614: throw new RuntimeException();
1615: }
1616: List list = sessionContainer.getAllObjects();
1617: if (list.size() != totalWritten) {
1618: throw new RuntimeException();
1619: }
1620:
1621: int index = list.indexOf(object);
1622:
1623: if (index == -1) {
1624: throw new RuntimeException();
1625: }
1626:
1627: DynamicTestClass dynamicTestClass = new DynamicTestClass(
1628: className.toString(), testIndex);
1629: object = dynamicTestClass.load(sessionContainer);
1630: totalWritten++;
1631: testIndex++;
1632: sessionContainer.set(object);
1633: sessionContainer.commit();
1634: if (closeAfterCommit) {
1635: sessionContainer.close();
1636: sessionContainer = getContainerForFile(testFile);
1637: sessionContainer
1638: .setClassLoader(dynamicTestClass._classLoader);
1639: }
1640: dataEntries = sessionContainer.getDatabaseStatistics()
1641: .totalDataEntries();
1642: dynamicTestClass.delete();
1643:
1644: if (dataEntries != 2) {
1645: throw new RuntimeException();
1646: }
1647: list = sessionContainer.getAllObjects();
1648: if (list.size() != totalWritten) {
1649: throw new RuntimeException();
1650: }
1651:
1652: index = list.indexOf(object);
1653:
1654: if (index == -1) {
1655: throw new RuntimeException();
1656: }
1657:
1658: sessionContainer.close();
1659:
1660: className = new StringBuffer("TestClass");
1661: testIndex = testIndexStart;
1662: testFile = new File(testFileDir, SimpleAddTest.class
1663: .getSimpleName()
1664: + (_testCounter++) + ".jdb");
1665: testFile.delete();
1666: sessionContainer = getContainerForFile(testFile);
1667: sessionContainer.setClassLoader(dynamicTestClass._classLoader);
1668:
1669: for (int i = 0; i < totalWritten - 2; i++) {
1670: className.append("Test" + testIndex);
1671: dynamicTestClass = new DynamicTestClass(className
1672: .toString(), testIndex);
1673: object = dynamicTestClass.load(sessionContainer);
1674: testIndex++;
1675: sessionContainer.set(object);
1676: sessionContainer.commit();
1677: if (closeAfterCommit) {
1678: sessionContainer.close();
1679: sessionContainer = getContainerForFile(testFile);
1680: sessionContainer
1681: .setClassLoader(dynamicTestClass._classLoader);
1682: }
1683: dataEntries = sessionContainer.getDatabaseStatistics()
1684: .totalDataEntries();
1685: dynamicTestClass.delete();
1686: }
1687:
1688: if (dataEntries != 1) {
1689: throw new RuntimeException();
1690: }
1691:
1692: className.append("Test" + testIndex);
1693: dynamicTestClass = new DynamicTestClass(className.toString(),
1694: testIndex);
1695: object = dynamicTestClass.load(sessionContainer);
1696:
1697: for (int i = 0; i < newTableCreationIOCalls; i++) {
1698: factory.intersectionInit(null, i, true);
1699:
1700: factory.setIntersectEnabled(true);
1701:
1702: sessionContainer.set(object);
1703:
1704: try {
1705: sessionContainer.commit();
1706: break;
1707: } catch (IOException e) {
1708: e.printStackTrace();
1709: }
1710:
1711: factory.setIntersectEnabled(false);
1712: if (factory._bufferedType == BUFFER_TYPE.MAIN) {
1713: byte[] incompleteTransaction = factory.getBuffer();
1714: verifyDataReadable(incompleteTransaction, null,
1715: totalWritten - 2, totalWritten - 1);
1716: }
1717:
1718: List classes = sessionContainer.getAllObjects();
1719: if (classes.size() != totalWritten - 2) {
1720: throw new RuntimeException();
1721: }
1722:
1723: if (sessionContainer.getDatabaseStatistics()
1724: .totalDataEntries() != 1) {
1725: throw new RuntimeException();
1726: }
1727: }
1728: factory.setIntersectEnabled(false);
1729: sessionContainer.close();
1730: sessionContainer = getContainerForFile(testFile);
1731: sessionContainer.setClassLoader(dynamicTestClass._classLoader);
1732: sessionContainer.set(object);
1733: sessionContainer.commit();
1734:
1735: testIndex++;
1736: dynamicTestClass.delete();
1737:
1738: className.append("Test" + testIndex);
1739: dynamicTestClass = new DynamicTestClass(className.toString(),
1740: testIndex);
1741: object = dynamicTestClass.load(sessionContainer);
1742:
1743: for (int i = 0; i < Integer.MAX_VALUE; i++) {
1744: factory.intersectionInit(null, i, true);
1745:
1746: factory.setIntersectEnabled(true);
1747:
1748: sessionContainer.set(object);
1749:
1750: try {
1751: sessionContainer.commit();
1752: break;
1753: } catch (Exception e) {
1754: e.printStackTrace();
1755: }
1756:
1757: factory.setIntersectEnabled(false);
1758: if (factory._bufferedType == BUFFER_TYPE.MAIN) {
1759: byte[] incompleteTransaction = factory.getBuffer();
1760: verifyDataReadable(incompleteTransaction, null,
1761: totalWritten, totalWritten + 1);
1762: }
1763:
1764: List classes = sessionContainer.getAllObjects();
1765: if (classes.size() != totalWritten) {
1766: throw new RuntimeException();
1767: }
1768:
1769: if (sessionContainer.getDatabaseStatistics()
1770: .totalDataEntries() != 2) {
1771: throw new RuntimeException();
1772: }
1773: }
1774: factory.setIntersectEnabled(false);
1775: dynamicTestClass.delete();
1776: sessionContainer.close();
1777: }
1778:
1779: /**
1780: * @param args
1781: * @throws IOException
1782: * @throws IllegalClassTypeException
1783: */
1784: public void arraysTest1() throws IOException,
1785: IllegalClassTypeException {
1786: File testFileDir = new File("./testData/");
1787: testFileDir.mkdirs();
1788: File testFile = new File(testFileDir, SimpleAddTest.class
1789: .getSimpleName()
1790: + (_testCounter++) + ".jdb");
1791: testFile.delete();
1792:
1793: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
1794: ObjectA objectA = new ObjectA((byte) 1, (short) 2, null);
1795: ObjectB objectB = new ObjectB();
1796: int[] array = new int[] { 10, 20, 30 };
1797:
1798: sessionContainer.set(objectB);
1799: sessionContainer.set(array);
1800: sessionContainer.set(objectA);
1801:
1802: sessionContainer.commit();
1803: sessionContainer.close();
1804:
1805: sessionContainer = getContainerForFile(testFile);
1806: sessionContainer.printFileMap();
1807: List classes = sessionContainer.getAllObjects();
1808: if (classes.size() != 3) {
1809: throw new RuntimeException();
1810: }
1811: Object arrayObj = null;
1812: for (int i = 0; i < classes.size(); i++) {
1813: if (classes.get(i).getClass().isArray()) {
1814: arrayObj = classes.get(i);
1815: }
1816: }
1817: int[] array1 = (int[]) arrayObj;
1818:
1819: if (!Arrays.equals(array, array1)) {
1820: throw new RuntimeException();
1821: }
1822: int index = classes.indexOf(objectA);
1823: if (index == -1) {
1824: throw new RuntimeException();
1825: }
1826: ObjectA objectA2 = (ObjectA) classes.get(index);
1827:
1828: index = classes.indexOf(objectB);
1829: if (index == -1) {
1830: throw new RuntimeException();
1831: }
1832:
1833: ObjectB objectB2 = (ObjectB) classes.get(index);
1834:
1835: array1[0] = 100;
1836:
1837: objectA2.setVal3(objectB2);
1838: objectB2._val3 = objectA2;
1839:
1840: sessionContainer.set(objectB2);
1841: sessionContainer.set(objectA2);
1842: sessionContainer.set(arrayObj);
1843:
1844: sessionContainer.close();
1845:
1846: sessionContainer = getContainerForFile(testFile);
1847: sessionContainer.printFileMap();
1848:
1849: classes = sessionContainer.getAllObjects();
1850: if (classes.size() != 3) {
1851: throw new RuntimeException();
1852: }
1853: arrayObj = null;
1854: for (int i = 0; i < classes.size(); i++) {
1855: if (classes.get(i).getClass().isArray()) {
1856: arrayObj = classes.get(i);
1857: }
1858: }
1859: int[] array2 = (int[]) arrayObj;
1860:
1861: if (!Arrays.equals(array2, array1)) {
1862: throw new RuntimeException();
1863: }
1864: index = classes.indexOf(objectA);
1865: if (index == -1) {
1866: throw new RuntimeException();
1867: }
1868: objectA2 = (ObjectA) classes.get(index);
1869:
1870: index = classes.indexOf(objectB);
1871: if (index == -1) {
1872: throw new RuntimeException();
1873: }
1874:
1875: objectB2 = (ObjectB) classes.get(index);
1876:
1877: sessionContainer.close();
1878:
1879: }
1880:
1881: public void transactionTest() throws IllegalClassTypeException,
1882: IOException {
1883: File testFileDir = new File("./testData/");
1884: testFileDir.mkdirs();
1885: File testFile = new File(testFileDir, SimpleAddTest.class
1886: .getSimpleName()
1887: + (_testCounter++) + ".jdb");
1888: testFile.delete();
1889:
1890: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
1891: sessionContainer.close();
1892:
1893: WriteIntersectingFactory factory = new WriteIntersectingFactory();
1894: JODBConfig.setRandomAccessBufferFactory(factory);
1895: factory.intersectionInit(null, Integer.MAX_VALUE, false);
1896:
1897: sessionContainer = getContainerForFile(testFile);
1898: //factory.setIntersectEnabled(true);
1899: ObjectA objectA = new ObjectA((byte) 1, (short) 2, null);
1900: ObjectB objectB = new ObjectB();
1901: int[] array = new int[] { 10, 20, 30 };
1902:
1903: sessionContainer.set(objectB);
1904: //sessionContainer.set(array);
1905: sessionContainer.set(objectA);
1906: sessionContainer.close();
1907:
1908: sessionContainer = getContainerForFile(testFile);
1909: factory.setIntersectEnabled(true);
1910: sessionContainer.set(array);
1911: sessionContainer.close();
1912:
1913: Vector verificationObjects = new Vector();
1914: verificationObjects.add(objectA);
1915: verificationObjects.add(objectB);
1916:
1917: int maxOperationsCount = factory.getCounter();
1918:
1919: testFile = new File(testFileDir, SimpleAddTest.class
1920: .getSimpleName()
1921: + (_testCounter++) + ".jdb");
1922:
1923: for (int i = 0; i < maxOperationsCount; i++) {
1924: System.gc();
1925: // try {
1926: // Thread.sleep(200);
1927: // } catch (InterruptedException e1) {
1928: // }
1929: factory.intersectionInit(null, i, true);
1930: testFile.delete();
1931: if (testFile.exists()) {
1932: throw new RuntimeException();
1933: }
1934:
1935: sessionContainer = getContainerForFile(testFile);
1936: sessionContainer.set(objectB);
1937: sessionContainer.set(objectA);
1938:
1939: sessionContainer.close();
1940:
1941: // testFile.delete();
1942: // if(testFile.exists()){
1943: // System.err.println();
1944: // }
1945:
1946: factory.setIntersectEnabled(true);
1947: sessionContainer = getContainerForFile(testFile);
1948:
1949: sessionContainer.set(array);
1950:
1951: try {
1952: sessionContainer.close();
1953: throw new RuntimeException("Test Framework Error");
1954: } catch (Exception e) {
1955: // if(i==25){
1956: // factory.printUnclosedBuffers();
1957: // }
1958: e.printStackTrace();
1959: }
1960:
1961: // testFile.delete();
1962: // if(testFile.exists()){
1963: // System.err.println();
1964: // }
1965:
1966: factory.setIntersectEnabled(false);
1967: if (factory._bufferedType == BUFFER_TYPE.MAIN) {
1968: byte[] incompleteTransaction = factory.getBuffer();
1969: verifyDataReadable(incompleteTransaction,
1970: verificationObjects, 2, 3);
1971: }
1972: sessionContainer = getContainerForFile(testFile);
1973:
1974: List classes = sessionContainer.getAllObjects();
1975: if (classes.size() != 2) {
1976: throw new RuntimeException("" + classes.size() + " "
1977: + i);
1978: }
1979: // Object arrayObj = null;
1980: // for (int j = 0; j < classes.size(); j++) {
1981: // if(classes.get(j).getClass().isArray()){
1982: // arrayObj = classes.get(j);
1983: // }
1984: // }
1985: // int[] array1 = (int[]) arrayObj;
1986: //
1987: // if(!Arrays.equals(array, array1)){
1988: // throw new RuntimeException();
1989: // }
1990: int index = classes.indexOf(objectA);
1991: if (index == -1) {
1992: throw new RuntimeException();
1993: }
1994: ObjectA objectA2 = (ObjectA) classes.get(index);
1995:
1996: index = classes.indexOf(objectB);
1997: if (index == -1) {
1998: throw new RuntimeException();
1999: }
2000: ObjectB objectB2 = (ObjectB) classes.get(index);
2001: sessionContainer.close();
2002: }
2003:
2004: }
2005:
2006: public void checkInputBuffersCount() {
2007: if (TestBufferFactory._executionError != null) {
2008: throw new RuntimeException(
2009: TestBufferFactory._executionError);
2010: }
2011: if (RandomAccessTestTransactionBuffer.getOpenBuffersCount() != 0) {
2012: throw new RuntimeException();
2013: }
2014: }
2015:
2016: public void verifyDataReadable(byte[] data,
2017: Vector verificationObjects, int minObjects, int maxObjects)
2018: throws IOException {
2019: try {
2020: Thread.sleep(200);
2021: } catch (InterruptedException e1) {
2022: }
2023: File file = File.createTempFile("incompleteTransaction", "");
2024: FileOutputStream fos = new FileOutputStream(file);
2025: fos.write(data);
2026: fos.close();
2027: JODBSessionContainer sessionContainer = (JODBSessionContainer) JODB
2028: .open(file);
2029: List classes = sessionContainer.getAllObjects();
2030: //sessionContainer.printFileMap();
2031: if (classes.size() < minObjects || classes.size() > maxObjects) {
2032: throw new RuntimeException();
2033: }
2034: for (int i = 0; verificationObjects != null
2035: && i < verificationObjects.size(); i++) {
2036: if (classes.indexOf(verificationObjects.elementAt(i)) == -1) {
2037: throw new RuntimeException();
2038: }
2039: }
2040: sessionContainer.close();
2041: file.deleteOnExit();
2042: file.delete();
2043: }
2044:
2045: public void setTestBufferFactory() {
2046: JODBConfig
2047: .setRandomAccessBufferFactory(new TestBufferFactory());
2048: }
2049:
2050: public void ensureTestFolderExists() {
2051: File testFileDir = new File("./testData/");
2052: boolean result = testFileDir.mkdirs();
2053: System.err.println("" + testFileDir.getAbsolutePath()
2054: + " --------->" + result + " "
2055: + testFileDir.isDirectory() + " "
2056: + testFileDir.exists());
2057:
2058: }
2059:
2060: public JODBSessionContainer getContainerForFile(File file)
2061: throws IOException {
2062: return (JODBSessionContainer) JODB.open(file);
2063: }
2064:
2065: public static class WriteIntersectingFactory extends
2066: TestBufferFactory {
2067:
2068: public BUFFER_TYPE _typeToIntersect;
2069: public int _operationsBeforeIntersect;
2070: public boolean _throwException;
2071: public byte[] _buffer;
2072: public BUFFER_TYPE _bufferedType;
2073: public int _counter;
2074: public boolean _intersectEnabled;
2075:
2076: //public boolean _excludeClientIntersect;
2077: /**
2078: * @param typeToIntersect
2079: * @param length
2080: */
2081: public WriteIntersectingFactory() {
2082: }
2083:
2084: public void intersectionInit(BUFFER_TYPE typeToIntersect,
2085: int operationsBeforeIntersect, boolean throwException) {
2086: _typeToIntersect = typeToIntersect;
2087: _operationsBeforeIntersect = operationsBeforeIntersect;
2088: _throwException = throwException;
2089: _buffer = null;
2090: _counter = 0;
2091: _intersectEnabled = false;
2092: _bufferedType = null;
2093: }
2094:
2095: public int getCounter() {
2096: return _counter;
2097: }
2098:
2099: public byte[] getBuffer() {
2100: return _buffer;
2101: }
2102:
2103: public void printUnclosedBuffers() {
2104: Map<RandomAccessTestTransactionBuffer, Throwable> hashMap = RandomAccessTestTransactionBuffer._constructionKeepers;
2105: Iterator<Throwable> iterator = hashMap.values().iterator();
2106: while (iterator.hasNext()) {
2107: Throwable element = (Throwable) iterator.next();
2108: element.printStackTrace();
2109: }
2110: }
2111:
2112: @Override
2113: public void writeComplete0(IRandomAccessDataBuffer src,
2114: BUFFER_TYPE type, long start, long end)
2115: throws IOException {
2116: if (_intersectEnabled) {
2117: if (_typeToIntersect == null
2118: || _typeToIntersect == type) {
2119: _counter++;
2120: if (_counter > _operationsBeforeIntersect) {
2121: _bufferedType = type;
2122: _buffer = ((RandomAccessTestTransactionBuffer) src)
2123: .getAsByteArray();
2124: if (_throwException) {
2125: _intersectEnabled = false;
2126: throw new IOException();
2127: }
2128: }
2129: }
2130: }
2131: super .writeComplete0(src, type, start, end);
2132: }
2133:
2134: public void setIntersectEnabled(boolean intersectEnabled) {
2135: _intersectEnabled = intersectEnabled;
2136: }
2137:
2138: }
2139:
2140: public static class DynamicTestClass {
2141: final String _stubName = "ClassStub";
2142: final String _membersString = "/*members*/";
2143: final String _equalsEntry = "/*equals entry*/";
2144: public String _stubText;
2145: public String _name;
2146: public int _entries;
2147: public ClassLoader _classLoader;
2148: File _outputClassSrc;
2149: File _outputDir;
2150:
2151: public DynamicTestClass(String name, int totalMembers)
2152: throws IOException {
2153: _name = name;
2154: _entries = totalMembers;
2155: InputStream is = getClass().getResourceAsStream(
2156: "dynamicClasses/ClassStub.txt");
2157: int size = is.available();
2158: byte[] data = new byte[size];
2159: is.read(data);
2160: _stubText = new String(data);
2161: }
2162:
2163: public Object load(JODBSessionContainer sessionContainer)
2164: throws Exception {
2165: return load(sessionContainer, byte.class, true);
2166: }
2167:
2168: public Object load(JODBSessionContainer sessionContainer,
2169: Class fieldType, boolean generateEquals)
2170: throws Exception {
2171: int nameIndex = _stubText.indexOf(_stubName);
2172: _outputDir = new File("./testData/");
2173: _outputDir.mkdirs();
2174: _outputClassSrc = new File(_outputDir, _name + ".java");
2175: FileOutputStream fos = new FileOutputStream(_outputClassSrc);
2176: OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
2177: fos);
2178: outputStreamWriter.write(_stubText.substring(0, nameIndex));
2179: outputStreamWriter.write(_name);
2180: int membIndex = _stubText.indexOf(_membersString);
2181: outputStreamWriter.write(_stubText.substring(nameIndex
2182: + _stubName.length(), membIndex));
2183: StringBuffer equalText = new StringBuffer();
2184: String memberNamePrefix = "_memberMemberMemberMemberMemberMember";
2185: for (int i = 0; i < _entries; i++) {
2186: outputStreamWriter.write(fieldType.getName() + " "
2187: + memberNamePrefix + i + "=" + (byte) i
2188: + ";\r\n");
2189: if (i != 0) {
2190: equalText.append("&&");
2191: }
2192: equalText.append(memberNamePrefix + i + "==o1."
2193: + memberNamePrefix + i);
2194: }
2195: int equalEntryIndex = _stubText.indexOf(_equalsEntry);
2196: outputStreamWriter.write(_stubText.substring(membIndex
2197: + _membersString.length(), equalEntryIndex));
2198: outputStreamWriter.write("if(!(obj instanceof " + _name
2199: + ")) return false;\r\n");
2200: outputStreamWriter.write(_name + " o1 =(" + _name
2201: + ")obj;\r\nreturn ");
2202: if (generateEquals) {
2203: outputStreamWriter
2204: .write(equalText.toString() + ";\r\n");
2205: } else {
2206: outputStreamWriter.write("false;\r\n");
2207: }
2208: outputStreamWriter
2209: .write(_stubText.substring(equalEntryIndex
2210: + _equalsEntry.length()));
2211: outputStreamWriter.close();
2212:
2213: int errorCode = com.sun.tools.javac.Main
2214: .compile(new String[] {
2215: "-classpath",
2216: _outputDir.toString() + '\\',
2217: "-d",
2218: _outputDir.toString() + '\\',
2219: _outputDir.toString() + '\\' + _name
2220: + ".java" });
2221: if (errorCode != 0) {
2222: throw new RuntimeException();
2223: }
2224:
2225: // The dir contains the compiled classes.
2226: File classesDir = _outputDir;
2227:
2228: // The parent classloader
2229: ClassLoader parentLoader = SimpleAddTest.class
2230: .getClassLoader();
2231:
2232: // Load class "sample.PostmanImpl" with our own classloader.
2233: _classLoader = new URLClassLoader(new URL[] { classesDir
2234: .toURL() }, parentLoader);
2235: Class cls1 = _classLoader.loadClass(_name);
2236: Object result = cls1.newInstance();
2237: sessionContainer.setClassLoader(_classLoader);
2238: return result;
2239: }
2240:
2241: public ClassLoader getClassLoader() {
2242: return _classLoader;
2243: }
2244:
2245: public void delete() {
2246: _outputClassSrc.delete();
2247: File compiledClass = new File(_outputDir, _name + ".class");
2248: compiledClass.delete();
2249: }
2250:
2251: }
2252:
2253: public static class TestBufferFactory extends
2254: RandomAccessTestFileBufferFactory {
2255:
2256: public static Throwable _executionError;
2257:
2258: public boolean PRINT = false;
2259:
2260: public WeakHashMap<IRandomAccessDataBuffer, IRandomAccessDataBuffer> _cache = new WeakHashMap<IRandomAccessDataBuffer, IRandomAccessDataBuffer>();
2261:
2262: ITransactionBufferListener _transactionBufferListenerMAIN = new TransactionBufferListener(
2263: BUFFER_TYPE.MAIN);
2264: ITransactionBufferListener _transactionBufferListenerNEW_DATA = new TransactionBufferListener(
2265: BUFFER_TYPE.NEW_DATA);
2266: ITransactionBufferListener _transactionBufferListenerREPLACEMENTS = new TransactionBufferListener(
2267: BUFFER_TYPE.REPLACEMENTS);
2268: ITransactionBufferListener _transactionBufferListenerROLLBACK = new TransactionBufferListener(
2269: BUFFER_TYPE.ROLLBACK);
2270: ITransactionBufferListener _transactionBufferListenerCLIENT_TEMP = new TransactionBufferListener(
2271: BUFFER_TYPE.CLIENT_TEMP_FILE);
2272: ITransactionBufferListener _transactionBufferListenerSERVER_TEMP = new TransactionBufferListener(
2273: BUFFER_TYPE.SERVER_TEMP_BUFFER);
2274:
2275: @Override
2276: protected IRandomAccessDataBuffer createBuffer(File name,
2277: BUFFER_TYPE type, boolean write) throws IOException {
2278: ITransactionBufferListener listener;
2279: switch (type) {
2280: case MAIN:
2281: listener = _transactionBufferListenerMAIN;
2282: break;
2283: case NEW_DATA:
2284: listener = _transactionBufferListenerNEW_DATA;
2285: break;
2286: case REPLACEMENTS:
2287: listener = _transactionBufferListenerREPLACEMENTS;
2288: break;
2289: case ROLLBACK:
2290: listener = _transactionBufferListenerREPLACEMENTS;
2291: break;
2292: case CLIENT_TEMP_FILE:
2293: listener = _transactionBufferListenerCLIENT_TEMP;
2294: break;
2295: case SERVER_TEMP_BUFFER:
2296: listener = _transactionBufferListenerSERVER_TEMP;
2297: break;
2298: default:
2299: throw new RuntimeException();
2300: }
2301: IRandomAccessDataBuffer result = new RandomAccessTestTransactionBuffer(
2302: name, listener, write);
2303: _cache.put(result, null);
2304: return result;
2305: }
2306:
2307: public void readComplete0(IRandomAccessDataBuffer src,
2308: BUFFER_TYPE type, long start, long end)
2309: throws IOException {
2310: if (PRINT)
2311: System.err.println("readComplete " + type + " "
2312: + start + " " + end);
2313: }
2314:
2315: public void readStart0(IRandomAccessDataBuffer src,
2316: BUFFER_TYPE type, long position) throws IOException {
2317: if (PRINT)
2318: System.err.println("readStart " + type + " "
2319: + position);
2320: }
2321:
2322: public void writeComplete0(IRandomAccessDataBuffer src,
2323: BUFFER_TYPE type, long start, long end)
2324: throws IOException {
2325: if (PRINT)
2326: System.err.println("writeComplete " + type + " "
2327: + start + " " + end);
2328: }
2329:
2330: public void writeStart0(IRandomAccessDataBuffer src,
2331: BUFFER_TYPE type, long position) throws IOException {
2332: if (PRINT)
2333: System.err.println("readComplete " + type + " "
2334: + position);
2335: }
2336:
2337: public void printCache() {
2338: System.err
2339: .println("TestBufferFactory **********START************");
2340: Iterator<IRandomAccessDataBuffer> iterator = _cache
2341: .keySet().iterator();
2342: while (iterator.hasNext()) {
2343: IRandomAccessDataBuffer element = (IRandomAccessDataBuffer) iterator
2344: .next();
2345: System.err.println("TestBufferFactory "
2346: + element.toString());
2347: }
2348: System.err
2349: .println("TestBufferFactory **********END************");
2350: }
2351:
2352: public class TransactionBufferListener implements
2353: ITransactionBufferListener {
2354: BUFFER_TYPE type;
2355:
2356: /**
2357: * @param type
2358: */
2359: public TransactionBufferListener(BUFFER_TYPE type) {
2360: super ();
2361: this .type = type;
2362: }
2363:
2364: public void readComplete(IRandomAccessDataBuffer src,
2365: long start, long end) throws IOException {
2366: readComplete0(src, type, start, end);
2367: }
2368:
2369: public void readStart(IRandomAccessDataBuffer src,
2370: long position) throws IOException {
2371: readStart0(src, type, position);
2372: }
2373:
2374: public void writeComplete(IRandomAccessDataBuffer src,
2375: long start, long end) throws IOException {
2376: writeComplete0(src, type, start, end);
2377: }
2378:
2379: public void writeStart(IRandomAccessDataBuffer src,
2380: long position) throws IOException {
2381: writeStart0(src, type, position);
2382: }
2383:
2384: public void reportError(Throwable th) {
2385: _executionError = th;
2386: }
2387:
2388: }
2389: }
2390:
2391: }
|