/*
Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
This file is part of the JODB (Java Objects Database) open source project.
JODB is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.
JODB is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package com.mobixess.jodb.tests;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.channels.FileLock;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.WeakHashMap;
import com.mobixess.jodb.core.IPersistentObjectStatistics;
import com.mobixess.jodb.core.IllegalClassTypeException;
import com.mobixess.jodb.core.JODBConfig;
import com.mobixess.jodb.core.JODBSessionContainer;
import com.mobixess.jodb.core.JodbMini;
import com.mobixess.jodb.core.io.IRandomAccessDataBuffer;
import com.mobixess.jodb.core.io.IRandomAccessBufferFactory.BUFFER_TYPE;
import com.mobixess.jodb.soda.api.Query;
import com.mobixess.jodb.tests.io.buffers.ITransactionBufferListener;
import com.mobixess.jodb.tests.io.buffers.RandomAccessTestFileBufferFactory;
import com.mobixess.jodb.tests.io.buffers.RandomAccessTestTransactionBuffer;
import com.mobixess.jodb.tests.testobjects.InnerClassObject;
import com.mobixess.jodb.tests.testobjects.ObjectA;
import com.mobixess.jodb.tests.testobjects.ObjectB;
import com.mobixess.jodb.tests.testobjects.ObjectC;
import com.mobixess.jodb.tests.testobjects.ObjectD;
import com.mobixess.jodb.tests.testobjects.ObjectWithNativeTransientID;
import com.mobixess.jodb.tests.testobjects.ObjectWithPrimitiveWrappers;
import com.mobixess.jodb.tests.testobjects.ObjectWithTransientID;
import com.mobixess.jodb.tests.testobjects.Object_Enum;
import com.mobixess.jodb.tests.testobjects.Object_Enum.TEST_ENUM;
public class SimpleAddTest {
public static int _testCounter;
public static byte _modificationCounter;
String _testPath;
public SimpleAddTest() {
_testPath = ".";
}
/**
* @param testPath
*/
public SimpleAddTest(String testPath) {
super();
_testPath = testPath;
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
SimpleAddTest simpleAddTest = new SimpleAddTest();
simpleAddTest.run();
}
public void run() throws Exception{
ensureTestFolderExists();
//innerClassTest();
deleteTest();
checkInputBuffersCount();
objectWithPrimitiveWrappersTest();
checkInputBuffersCount();
objectWithChildFieldsTest();
checkInputBuffersCount();
arraysTest();
checkInputBuffersCount();
checkInputBuffersCount();
noFieldsObjectTest();
checkInputBuffersCount();
fileLockCheck();
checkInputBuffersCount();
transactionTest();
checkInputBuffersCount();
stringTest();
checkInputBuffersCount();
transientTest();
checkInputBuffersCount();
transientNativeTest();
checkInputBuffersCount();
sizeAndPositioningTest1();
checkInputBuffersCount();
checkInputBuffersCount();
checkInputBuffersCount();
sizeIncreaseTest();
checkInputBuffersCount();
arraysTest1();
checkInputBuffersCount();
setTestBufferFactory();
checkInputBuffersCount();
objectWithFieldsTest();
checkInputBuffersCount();
multipleObjectWithFieldsTest();
checkInputBuffersCount();
objectWithChildFieldsModificationTest1();
checkInputBuffersCount();
objectWithChildFieldsModificationTest0();
checkInputBuffersCount();
gcTest();
checkInputBuffersCount();
System.out.println("Test complete");
}
public void fileLockCheck() throws Exception{
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
RandomAccessFile randomAccessFile = new RandomAccessFile(testFile,"rw");
byte[] result = new byte[(int) randomAccessFile.length()];
ByteBuffer buffer = ByteBuffer.wrap(result);
try {
randomAccessFile.getChannel().read(buffer, 0);
FileLock fileLock = randomAccessFile.getChannel().tryLock(0,1,false);//read(buffer, 0);
if(fileLock!=null){
throw new IOException();
}
} catch (Exception e) {
sessionContainer.close();
return;
}finally{
sessionContainer.close();
}
throw new IOException();
}
/**
* @param args
* @throws IOException
* @throws IllegalClassTypeException
*/
public void noFieldsObjectTest() throws IOException, IllegalClassTypeException {
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
ObjectC objectC = new ObjectC();
sessionContainer.set(objectC);
sessionContainer.commit();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
List classes = sessionContainer.getAllObjects();
if(classes.size()!=1){
throw new RuntimeException(""+classes.size());
}
Object obj = classes.get(0);
if(obj.getClass() != objectC.getClass()){
throw new RuntimeException();
}
sessionContainer.close();
}
/**
* @param args
* @throws IOException
* @throws IllegalClassTypeException
*/
public void objectWithFieldsTest() throws IOException, IllegalClassTypeException {
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
ObjectA objectA = new ObjectA((byte)1,(short)2,null);
sessionContainer.set(objectA);
sessionContainer.commit();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
List classes = sessionContainer.getAllObjects();
if(classes.size()!=1){
throw new RuntimeException();
}
Object obj = classes.get(0);
if(obj.getClass() != objectA.getClass()){
throw new RuntimeException();
}
ObjectA objA = (ObjectA) obj;
if(objA.getVal1() != objectA.getVal1() || objA.getVal2() != objectA.getVal2() || objA.getVal3() != objectA.getVal3() ){
throw new RuntimeException("objA="+objA+" objectA="+objectA);
}
sessionContainer.close();
}
public void objectWithPrimitiveWrappersTest() throws IOException, IllegalClassTypeException {
objectWithPrimitiveWrappersTest(true);
objectWithPrimitiveWrappersTest(false);
}
/**
* @param args
* @throws IOException
* @throws IllegalClassTypeException
*/
public void objectWithPrimitiveWrappersTest(boolean reopen) throws IOException, IllegalClassTypeException {
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
ObjectWithPrimitiveWrappers initialObject = new ObjectWithPrimitiveWrappers();
initialObject._boolean = true;
initialObject._byte = Byte.MAX_VALUE;
initialObject._character = Character.MAX_VALUE;
initialObject._double = Double.MAX_VALUE;
initialObject._float = Float.MAX_VALUE;
initialObject._integer = Integer.MAX_VALUE;
initialObject._long = Long.MAX_VALUE;
initialObject._short = Short.MAX_VALUE;
sessionContainer.set(initialObject);
sessionContainer.commit();
if (reopen) {
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
}
// List classes = sessionContainer.getAllObjects();
Query query = sessionContainer.query();
query.constrain(initialObject.getClass());
List classes = query.execute();
if (classes.size() != 1) {
throw new RuntimeException();
}
Object obj = classes.get(0);
if(obj.getClass() != initialObject.getClass()){
throw new RuntimeException();
}
ObjectWithPrimitiveWrappers persistedObject = (ObjectWithPrimitiveWrappers) obj;
if(!initialObject._boolean.equals(persistedObject._boolean)){
throw new RuntimeException();
}
if(!initialObject._byte.equals(persistedObject._byte)){
throw new RuntimeException();
}
if(!initialObject._character.equals(persistedObject._character)){
throw new RuntimeException();
}
if(!initialObject._double.equals(persistedObject._double)){
throw new RuntimeException();
}
if(!initialObject._float.equals(persistedObject._float)){
throw new RuntimeException();
}
if(!initialObject._integer.equals(persistedObject._integer)){
throw new RuntimeException();
}
if(!initialObject._long.equals(persistedObject._long)){
throw new RuntimeException();
}
if(!initialObject._short.equals(persistedObject._short)){
throw new RuntimeException();
}
sessionContainer.close();
}
/**
* @param args
* @throws IOException
* @throws IllegalClassTypeException
*/
public void multipleObjectWithFieldsTest() throws IOException, IllegalClassTypeException {
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
ObjectA objectA = new ObjectA((byte)1,(short)2,null);
ObjectA objectA1 = new ObjectA((byte)3,(short)4,null);
sessionContainer.set(objectA);
sessionContainer.set(objectA1);
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
List classes = sessionContainer.getAllObjects();
if(classes.size()!=2){
throw new RuntimeException();
}
int index = classes.indexOf(objectA);
if(index == -1){
throw new RuntimeException();
}
Object obj = classes.get(index);
if(obj.getClass() != objectA.getClass()){
throw new RuntimeException();
}
ObjectA objA = (ObjectA) obj;
if(objA.getVal1() != objectA.getVal1() || objA.getVal2() != objectA.getVal2() || objA.getVal3() != objectA.getVal3() ){
throw new RuntimeException();
}
sessionContainer.close();
}
/**
* @param args
* @throws IOException
* @throws IllegalClassTypeException
*/
public void objectWithChildFieldsTest() throws IOException, IllegalClassTypeException {
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
ObjectB objectB = new ObjectB();
ObjectA objectA = new ObjectA((byte)1,(short)2,objectB);
objectB._val3 = objectA;
sessionContainer.set(objectA);
sessionContainer.commit();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
sessionContainer.printFileMap();
List classes = sessionContainer.getAllObjects();
if(classes.size()!=2){
throw new RuntimeException(""+classes.size());
}
int index = classes.indexOf(objectA);
if(index == -1){
throw new RuntimeException();
}
Object obj = classes.get(index);
if(obj.getClass() != objectA.getClass()){
throw new RuntimeException();
}
ObjectA objA = (ObjectA) obj;
if(objA.getVal1() != objectA.getVal1() || objA.getVal2() != objectA.getVal2()){
throw new RuntimeException();
}
if(objA.getVal3().getClass() != objectB.getClass()){
throw new RuntimeException();
}
int bIndex = classes.indexOf(objectB);
if(bIndex == -1){
throw new RuntimeException();
}
ObjectB objectB2 = (ObjectB) classes.get(bIndex);
if(objectB2._val3 != objA){
throw new RuntimeException();
}
objectB2 = new ObjectB();
objA.setVal3(objectB2);
sessionContainer.set(objA);
sessionContainer.commit();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
for (int i = 0; i < classes.size(); i++) {
classes.get(i);
}
sessionContainer.close();
}
/**
* @param args
* @throws IOException
* @throws IllegalClassTypeException
*/
public void objectWithChildFieldsModificationTest0() throws IOException, IllegalClassTypeException {
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
ObjectB objectB = new ObjectB();
ObjectA objectA = new ObjectA((byte)1,(short)2,objectB);
ObjectA objectA2 = new ObjectA((byte)3,(byte)3,objectB);
objectB._val3 = objectA;
sessionContainer.set(objectA);
sessionContainer.set(objectA2);
sessionContainer.commit();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
List classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
int index = classes.indexOf(objectA);
if(index == -1){
throw new RuntimeException();
}
Object obj = classes.get(index);
if(obj.getClass() != objectA.getClass()){
throw new RuntimeException();
}
ObjectA objA = (ObjectA) obj;
if(objA.getVal1() != objectA.getVal1() || objA.getVal2() != objectA.getVal2()){
throw new RuntimeException();
}
if(objA.getVal3().getClass() != objectB.getClass()){
throw new RuntimeException();
}
int bIndex = classes.indexOf(objectB);
if(bIndex == -1){
throw new RuntimeException();
}
ObjectB objectB2 = (ObjectB) classes.get(bIndex);
if(objectB2._val3 != objA){
throw new RuntimeException();
}
objA.setVal2((short) 33);
sessionContainer.set(objA);
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
index = classes.indexOf(objA);
if(index == -1){
throw new RuntimeException();
}
objA = (ObjectA) classes.get(index);
if(objA.getVal2() != 33){
throw new RuntimeException();
}
objA.setVal1((byte) 100);
objA.setVal2((short) 300);
sessionContainer.set(objA);
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
//sessionContainer.printFileMap();
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
index = classes.indexOf(objA);
if(index == -1){
throw new RuntimeException();
}
objA = (ObjectA) classes.get(index);
if(objA.getVal1() != 100 || objA.getVal2() != 300 || objA.getVal3()==null ){
throw new RuntimeException();
}
objA.setVal1((byte) 111);
objA.setVal2((short) 333);
objA.setVal3(null);
sessionContainer.set(objA);
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
sessionContainer.printFileMap();
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
index = classes.indexOf(objA);
if(index == -1){
throw new RuntimeException();
}
objA = (ObjectA) classes.get(index);
if(objA.getVal1() != 111 || objA.getVal2() != 333 || objA.getVal3()!=null ){
throw new RuntimeException();
}
sessionContainer.printFileMap();
sessionContainer.close();
}
/**
* @param args
* @throws IOException
* @throws IllegalClassTypeException
*/
public void objectWithChildFieldsModificationTest1() throws IOException, IllegalClassTypeException {
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
ObjectB objectB = new ObjectB();
ObjectA objectA = new ObjectA((byte)1,(short)2,null);
ObjectA objectA2 = new ObjectA((byte)3,(byte)3,objectB);
objectB._val3 = objectA;
sessionContainer.set(objectA);
sessionContainer.set(objectA2);
sessionContainer.commit();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
List classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
int index = classes.indexOf(objectA);
if(index == -1){
throw new RuntimeException();
}
Object obj = classes.get(index);
if(obj.getClass() != objectA.getClass()){
throw new RuntimeException();
}
ObjectA objA = (ObjectA) obj;
if(objA.getVal1() != objectA.getVal1() || objA.getVal2() != objectA.getVal2()){
throw new RuntimeException();
}
int bIndex = classes.indexOf(objectB);
if(bIndex == -1){
throw new RuntimeException();
}
ObjectB objectB2 = (ObjectB) classes.get(bIndex);
if(objectB2._val3 != objA){
throw new RuntimeException();
}
objA.setVal2((short) 33);
int indexA2 = classes.indexOf(objectA2);
ObjectA objA2 = (ObjectA) classes.get(indexA2);
objA2.setVal3(null);
sessionContainer.set(objA);
sessionContainer.set(objA2);
sessionContainer.printFileMap();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
index = classes.indexOf(objA);
if(index == -1){
throw new RuntimeException();
}
objA = (ObjectA) classes.get(index);
if(objA.getVal2() != 33){
throw new RuntimeException();
}
bIndex = classes.indexOf(objectB);
if(bIndex == -1){
throw new RuntimeException();
}
objectB2 = (ObjectB) classes.get(bIndex);
objA.setVal1((byte) 100);
objA.setVal2((short) 300);
objA.setVal3(objectB2);
sessionContainer.set(objA);
sessionContainer.printFileMap();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
//sessionContainer.printFileMap();
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
index = classes.indexOf(objA);
if(index == -1){
throw new RuntimeException();
}
objA = (ObjectA) classes.get(index);
if(objA.getVal1() != 100 || objA.getVal2() != 300 || objA.getVal3()==null ){
throw new RuntimeException();
}
objA.setVal1((byte) 111);
objA.setVal2((short) 333);
objA.setVal3(null);
sessionContainer.set(objA);
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
sessionContainer.printFileMap();
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
index = classes.indexOf(objA);
if(index == -1){
throw new RuntimeException();
}
objA = (ObjectA) classes.get(index);
if(objA.getVal1() != 111 || objA.getVal2() != 333 || objA.getVal3()!=null ){
throw new RuntimeException();
}
sessionContainer.close();
}
/**
* @param args
* @throws IOException
* @throws IllegalClassTypeException
*/
public void deleteTest() throws IOException, IllegalClassTypeException {
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
ObjectB objectB = new ObjectB();
ObjectA objectA = new ObjectA((byte)1,(short)2,objectB);
ObjectA objectA2 = new ObjectA((byte)3,(byte)3,null);
objectB._val3 = objectA2;
sessionContainer.set(objectA);
sessionContainer.commit();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
List classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
int index = classes.indexOf(objectA);
if(index == -1){
throw new RuntimeException();
}
Object obj = classes.get(index);
sessionContainer.delete(obj);
sessionContainer.close();
/////
sessionContainer = getContainerForFile(testFile);
classes = sessionContainer.getAllObjects();
if(classes.size()!=2){
throw new RuntimeException();
}
index = classes.indexOf(objectB);
if(index == -1){
throw new RuntimeException();
}
obj = classes.get(index);
sessionContainer.delete(obj);
sessionContainer.close();
////
sessionContainer = getContainerForFile(testFile);
classes = sessionContainer.getAllObjects();
if(classes.size()!=1){
throw new RuntimeException();
}
index = classes.indexOf(objectA2);
if(index == -1){
throw new RuntimeException();
}
obj = classes.get(index);
sessionContainer.delete(obj);
sessionContainer.close();
////
sessionContainer = getContainerForFile(testFile);
classes = sessionContainer.getAllObjects();
if(classes.size()!=0){
throw new RuntimeException();
}
//sessionContainer.printFileMap();
sessionContainer.set(objectA);
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
index = classes.indexOf(objectA);
if(index == -1){
throw new RuntimeException();
}
obj = classes.get(index);
sessionContainer.delete(obj,Integer.MAX_VALUE);//delete all
sessionContainer.close();
////
sessionContainer = getContainerForFile(testFile);
classes = sessionContainer.getAllObjects();
if(classes.size()!=0){
throw new RuntimeException();
}
//sessionContainer.printFileMap();
sessionContainer.set(objectA);
sessionContainer.close();
////
sessionContainer = getContainerForFile(testFile);
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
index = classes.indexOf(objectB);
if(index == -1){
throw new RuntimeException();
}
obj = classes.get(index);
sessionContainer.delete(obj);
//sessionContainer.printFileMap();
sessionContainer.close();
////
sessionContainer = getContainerForFile(testFile);
sessionContainer.printFileMap();
classes = sessionContainer.getAllObjects();
if(classes.size()!=2){
throw new RuntimeException();
}
index = classes.indexOf(objectA);
if(index == -1){
throw new RuntimeException();
}
obj = classes.get(index);
if( ((ObjectA)obj).getVal3()!=null){
throw new RuntimeException();
}
sessionContainer.printFileMap();
Object_Enum object_Enum = new Object_Enum();
object_Enum._val1 = TEST_ENUM.ENUM_VAL1;
sessionContainer.set(object_Enum);
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
classes = sessionContainer.getAllObjects();
if(classes.size()!=4){
throw new RuntimeException();
}
index = classes.indexOf(object_Enum);
if(index == -1){
throw new RuntimeException();
}
obj = classes.get(index);
sessionContainer.delete(obj);
sessionContainer.commit();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
index = classes.indexOf(object_Enum);
if(index != -1){
throw new RuntimeException();
}
sessionContainer.close();
}
public void stringTest() throws IOException, IllegalClassTypeException {
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
String testString = "testString";
sessionContainer.set(testString);
sessionContainer.commit();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
List classes = sessionContainer.getAllObjects();
if(classes.size()!=1){
throw new RuntimeException();
}
String stringFromDB = (String) classes.get(0);
if(!stringFromDB.equals(testString)){
throw new RuntimeException();
}
IPersistentObjectStatistics statistics = sessionContainer.getPersistenceStatistics(stringFromDB);
if(statistics.getTotalRecords()!=1){
throw new RuntimeException();
}
sessionContainer.set(stringFromDB);
sessionContainer.commit();
classes = sessionContainer.getAllObjects();
if(classes.size()!=1){
throw new RuntimeException();
}
stringFromDB = (String) classes.get(0);
if(!stringFromDB.equals(testString)){
throw new RuntimeException();
}
statistics = sessionContainer.getPersistenceStatistics(stringFromDB);
if(statistics.getTotalRecords()!=1){
throw new RuntimeException();
}
sessionContainer.close();
}
/**
* @param args
* @throws IOException
* @throws IllegalClassTypeException
*/
public void gcTest() throws IOException, IllegalClassTypeException {
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
ObjectB objectB = new ObjectB();
ObjectA objectA = new ObjectA((byte)1,(short)2,objectB);
ObjectA objectA2 = new ObjectA((byte)3,(byte)3,objectB);
objectB._val3 = objectA;
sessionContainer.set(objectA);
sessionContainer.set(objectA2);
sessionContainer.commit();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
List classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
int index = classes.indexOf(objectA);
if(index == -1){
throw new RuntimeException();
}
Object obj = classes.get(index);
if(obj.getClass() != objectA.getClass()){
throw new RuntimeException();
}
ObjectA objA = (ObjectA) obj;
if(objA.getVal1() != objectA.getVal1() || objA.getVal2() != objectA.getVal2()){
throw new RuntimeException();
}
if(objA.getVal3().getClass() != objectB.getClass()){
throw new RuntimeException();
}
int bIndex = classes.indexOf(objectB);
if(bIndex == -1){
throw new RuntimeException();
}
ObjectB objectB2 = (ObjectB) classes.get(bIndex);
if(objectB2._val3 != objA){
throw new RuntimeException();
}
objA.setVal2((short) 33);
sessionContainer.set(objA);
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
index = classes.indexOf(objA);
if(index == -1){
throw new RuntimeException();
}
ObjectA objectA3 = (ObjectA) classes.get(index);
if(objectA3.getVal2() != 33){
throw new RuntimeException();
}
if(sessionContainer.getCachedObjectsCount() <= 0){
throw new RuntimeException();
}
System.out.println("DELAY START --------------->"+sessionContainer.getCachedObjectsCount());
objectA3 = null;
objectB = null;
objectA = null;
objectA2 = null;
objectB2 = null;
objectA3 = null;
obj = null;
objA = null;
classes = null;
System.gc();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("DELAY END ---------------"+sessionContainer.getCachedObjectsCount());
if(sessionContainer.getCachedObjectsCount() > 0){
throw new RuntimeException();
}
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
objA = new ObjectA((byte)1,(short)33,null);
index = classes.indexOf(objA);
if(index == -1){
throw new RuntimeException();
}
objectA3 = (ObjectA) classes.get(index);
if(objectA3.getVal2() != 33){
throw new RuntimeException();
}
// try {
// Thread.sleep(2000000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
sessionContainer.close();
}
/**
* @param args
* @throws IOException
* @throws IllegalClassTypeException
*/
public void arraysTest() throws IOException, IllegalClassTypeException {
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
ObjectA objectA = new ObjectA((byte)1,(short)2,null);
ObjectB objectB = new ObjectB();
Object[] array = new Object[]{objectA,objectB};
sessionContainer.set(objectB);
sessionContainer.set(array);
sessionContainer.set(objectA);
sessionContainer.commit();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
//sessionContainer.printFileMap();
List classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
Object arrayObj = null;
for (int i = 0; i < classes.size(); i++) {
if(classes.get(i).getClass().isArray()){
arrayObj = classes.get(i);
}
}
Object[] array1 = (Object[]) arrayObj;
List<Object> list = Arrays.asList(array1);
int index = list.indexOf(objectA);
if(index !=0){
throw new RuntimeException();
}
ObjectA objectA2 = (ObjectA) array1[index];
index = list.indexOf(objectB);
if(index != 1){
throw new RuntimeException();
}
ObjectB objectB2 = (ObjectB) array1[index];
objectA2.setVal3(objectB2);
objectB2._val3 = objectA2;
sessionContainer.set(objectB2);
sessionContainer.set(objectA2);
sessionContainer.set(arrayObj);
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
//sessionContainer.printFileMap();
//sessionContainer.close();
objectA = new ObjectA((byte)2,(short)3,null);
objectB = new ObjectB();
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
arrayObj = null;
for (int i = 0; i < classes.size(); i++) {
if(classes.get(i).getClass().isArray()){
arrayObj = classes.get(i);
}
}
array1 = (Object[]) arrayObj;
array1[0] = objectB;
array1[1] = objectA;
sessionContainer.set(array1);
sessionContainer.commit();
sessionContainer.printFileMap();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
classes = sessionContainer.getAllObjects();
if(classes.size()!=5){
throw new RuntimeException();
}
arrayObj = null;
for (int i = 0; i < classes.size(); i++) {
if(classes.get(i).getClass().isArray()){
arrayObj = classes.get(i);
}
}
array1 = (Object[]) arrayObj;
list = Arrays.asList(array1);
index = list.indexOf(objectA);
if(index !=1){
throw new RuntimeException();
}
index = list.indexOf(objectB);
if(index != 0){
throw new RuntimeException();
}
sessionContainer.close();
}
/**
* @param args
* @throws IOException
* @throws IllegalClassTypeException
*/
public void sizeIncreaseTest() throws IOException, IllegalClassTypeException {
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
for (int i = 0; i < 0x300; i++) {
System.err.println("sizeIncreaseTest "+i);
byte[] array = new byte[i];
sessionContainer.set(array);
sessionContainer.commit();
List classes = sessionContainer.getAllObjects();
if(classes.size() != i+1){
throw new RuntimeException();
}
boolean found = false;
for (int j = classes.size()-1; j >=0; --j) {
byte[] arr = (byte[]) classes.get(j);
if(arr.length == array.length){
found = true;
break;
}
}
if(!found){
throw new RuntimeException();
}
}
sessionContainer.close();
}
public void transientTest() throws Exception{
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
ObjectWithTransientID objectWithTransientID = new ObjectWithTransientID();
objectWithTransientID._memb1 = 1;
objectWithTransientID._memb2 = 2;
objectWithTransientID._memb3 = objectWithTransientID;
sessionContainer.set(objectWithTransientID);
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
List list = sessionContainer.getAllObjects();
objectWithTransientID = (ObjectWithTransientID) list.get(0);
if(objectWithTransientID._memb1 != 1 || objectWithTransientID._memb2!=0 || objectWithTransientID._memb3!=null){
throw new RuntimeException();
}
sessionContainer.close();
}
public void transientNativeTest() throws Exception{
JODBConfig.setSkipNativeTransientFields(true);
try {
transientNativeTest0();
} finally {
JODBConfig.setSkipNativeTransientFields(false);
}
}
private void transientNativeTest0() throws Exception{
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
ObjectWithNativeTransientID objectWithTransientID = new ObjectWithNativeTransientID();
objectWithTransientID._memb1 = 1;
objectWithTransientID._memb2 = 2;
objectWithTransientID._memb3 = objectWithTransientID;
sessionContainer.set(objectWithTransientID);
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
List list = sessionContainer.getAllObjects();
objectWithTransientID = (ObjectWithNativeTransientID) list.get(0);
if(objectWithTransientID._memb1 != 1 || objectWithTransientID._memb2!=0 || objectWithTransientID._memb3!=null){
throw new RuntimeException();
}
sessionContainer.close();
}
public void sizeAndPositioningTest1() throws Exception{
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
Object firstObject = new Object();
sessionContainer.set(firstObject);
sessionContainer.commit();
ObjectD testObjectD = new ObjectD();
testObjectD._obj0 = firstObject;
sessionContainer.set(testObjectD);
sessionContainer.commit();
//sessionContainer.printFileMap();
testObjectD._obj0 = null;
sessionContainer.set(testObjectD);
sessionContainer.commit();
//sessionContainer.printFileMap();
if(sessionContainer.getPersistenceStatistics(testObjectD).getTotalRecords()!=1){
throw new RuntimeException();
}
testObjectD._obj0 = firstObject;
sessionContainer.set(testObjectD);
sessionContainer.commit();
//sessionContainer.printFileMap();
if(sessionContainer.getPersistenceStatistics(testObjectD).getTotalRecords()!=1){
throw new RuntimeException();
}
testObjectD._obj1 = testObjectD;
sessionContainer.set(testObjectD);
sessionContainer.commit();
//sessionContainer.printFileMap();
if(sessionContainer.getPersistenceStatistics(testObjectD).getTotalRecords()!=2){
throw new RuntimeException();
}
testObjectD._member1 = 10;
sessionContainer.set(testObjectD);
sessionContainer.commit();
//sessionContainer.printFileMap();
if(sessionContainer.getPersistenceStatistics(testObjectD).getTotalRecords()!=2){
throw new RuntimeException();
}
testObjectD._obj0 = null;
sessionContainer.set(testObjectD);
sessionContainer.commit();
//sessionContainer.printFileMap();
if(sessionContainer.getPersistenceStatistics(testObjectD).getTotalRecords()!=1){
throw new RuntimeException();
}
testObjectD._obj1 = null;
sessionContainer.set(testObjectD);
sessionContainer.commit();
//sessionContainer.printFileMap();
if(sessionContainer.getPersistenceStatistics(testObjectD).getTotalRecords()!=1){
throw new RuntimeException();
}
testObjectD._obj0 = testObjectD._obj1 = testObjectD;
sessionContainer.set(testObjectD);
sessionContainer.commit();
//sessionContainer.printFileMap();
if(sessionContainer.getPersistenceStatistics(testObjectD).getTotalRecords()!=2){
throw new RuntimeException();
}
testObjectD._obj0 = testObjectD._obj1 = null;
sessionContainer.set(testObjectD);
sessionContainer.commit();
//sessionContainer.printFileMap();
if(sessionContainer.getPersistenceStatistics(testObjectD).getTotalRecords()!=1){
throw new RuntimeException();
}
List list = sessionContainer.getAllObjects();
if(list.size()!=2){
throw new IOException();
}
Object[] array = new Object[3];
array[0] = firstObject;
array[1] = firstObject;
sessionContainer.set(testObjectD);
sessionContainer.commit();
array[2] = array[2];
sessionContainer.set(array);
sessionContainer.commit();
//sessionContainer.printFileMap();
if(sessionContainer.getPersistenceStatistics(testObjectD).getTotalRecords()!=1){
throw new RuntimeException();
}
int inCache = sessionContainer.getCachedObjectsCount();
System.gc();
Thread.sleep(1000);
array = null;
System.gc();
Thread.sleep(1000);
if(inCache == sessionContainer.getCachedObjectsCount()){
throw new RuntimeException("test flow error");
}
//sessionContainer.printFileMap();
list = sessionContainer.getAllObjects();
for (int i = 0; i < list.size(); i++) {
Object obj = list.get(i);
if(obj.getClass().isArray()){
array = (Object[]) obj;
}
}
if(array[0]!=firstObject || array[1]!=firstObject || array[2]!=null){
throw new RuntimeException();
}
array[2] = array;
sessionContainer.set(array);
sessionContainer.commit();
//sessionContainer.printFileMap();
if(sessionContainer.getPersistenceStatistics(testObjectD).getTotalRecords()!=1){
throw new RuntimeException();
}
inCache = sessionContainer.getCachedObjectsCount();
System.gc();
Thread.sleep(500);
array = null;
System.gc();
Thread.sleep(500);
if(inCache == sessionContainer.getCachedObjectsCount()){
throw new RuntimeException("test flow error");
}
list = sessionContainer.getAllObjects();
for (int i = 0; i < list.size(); i++) {
Object obj = list.get(i);
if(obj.getClass().isArray()){
array = (Object[]) obj;
}
}
if(array[0]!=firstObject || array[1]!=firstObject || array[2]!=array){
throw new RuntimeException();
}
sessionContainer.close();
}
public void modify(Object object) throws Exception{
Field[] fields = object.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
if(fields[i].getType() == byte.class){
_modificationCounter++;
Field field = fields[i];
field.setAccessible(true);
field.set(object, _modificationCounter);
}
}
}
public void innerClassTest() throws Exception{
innerClassTest(true);
innerClassTest(false);
}
public void innerClassTest(boolean closeAfterCommit) throws Exception{
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
WriteIntersectingFactory factory = new WriteIntersectingFactory();
JODBConfig.setRandomAccessBufferFactory(factory);
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
InnerClassObject innerClassObject = new InnerClassObject();
sessionContainer.set(innerClassObject);
sessionContainer.commit();
if(closeAfterCommit){
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
}
Query query = sessionContainer.query();
query.constrain(innerClassObject.getInnerClass().getClass());
List list = query.execute();
if(list.size()!=1 || list.get(0).getClass() != innerClassObject.getInnerClass().getClass()){
throw new RuntimeException(" +++"+list.size());
}
}
/**
* @param args
* @throws IOException
* @throws IllegalClassTypeException
*/
public void arraysTest1() throws IOException, IllegalClassTypeException {
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
ObjectA objectA = new ObjectA((byte)1,(short)2,null);
ObjectB objectB = new ObjectB();
int[] array = new int[]{10,20,30};
sessionContainer.set(objectB);
sessionContainer.set(array);
sessionContainer.set(objectA);
sessionContainer.commit();
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
sessionContainer.printFileMap();
List classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
Object arrayObj = null;
for (int i = 0; i < classes.size(); i++) {
if(classes.get(i).getClass().isArray()){
arrayObj = classes.get(i);
}
}
int[] array1 = (int[]) arrayObj;
if(!Arrays.equals(array, array1)){
throw new RuntimeException();
}
int index = classes.indexOf(objectA);
if(index == -1){
throw new RuntimeException();
}
ObjectA objectA2 = (ObjectA) classes.get(index);
index = classes.indexOf(objectB);
if(index == -1){
throw new RuntimeException();
}
ObjectB objectB2 = (ObjectB) classes.get(index);
array1[0] = 100;
objectA2.setVal3(objectB2);
objectB2._val3 = objectA2;
sessionContainer.set(objectB2);
sessionContainer.set(objectA2);
sessionContainer.set(arrayObj);
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
sessionContainer.printFileMap();
classes = sessionContainer.getAllObjects();
if(classes.size()!=3){
throw new RuntimeException();
}
arrayObj = null;
for (int i = 0; i < classes.size(); i++) {
if(classes.get(i).getClass().isArray()){
arrayObj = classes.get(i);
}
}
int[] array2 = (int[]) arrayObj;
if(!Arrays.equals(array2, array1)){
throw new RuntimeException();
}
index = classes.indexOf(objectA);
if(index == -1){
throw new RuntimeException();
}
objectA2 = (ObjectA) classes.get(index);
index = classes.indexOf(objectB);
if(index == -1){
throw new RuntimeException();
}
objectB2 = (ObjectB) classes.get(index);
sessionContainer.close();
}
public void transactionTest() throws IllegalClassTypeException, IOException{
File testFileDir = new File(_testPath+"/testData/");
testFileDir.mkdirs();
File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
testFile.delete();
JODBSessionContainer sessionContainer = getContainerForFile(testFile);
sessionContainer.close();
WriteIntersectingFactory factory = new WriteIntersectingFactory();
JODBConfig.setRandomAccessBufferFactory(factory);
factory.intersectionInit(null, Integer.MAX_VALUE, false);
sessionContainer = getContainerForFile(testFile);
//factory.setIntersectEnabled(true);
ObjectA objectA = new ObjectA((byte)1,(short)2,null);
ObjectB objectB = new ObjectB();
int[] array = new int[]{10,20,30};
sessionContainer.set(objectB);
//sessionContainer.set(array);
sessionContainer.set(objectA);
sessionContainer.close();
sessionContainer = getContainerForFile(testFile);
factory.setIntersectEnabled(true);
sessionContainer.set(array);
sessionContainer.close();
Vector verificationObjects = new Vector();
verificationObjects.add(objectA);
verificationObjects.add(objectB);
int maxOperationsCount = factory.getCounter();
testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
for (int i = 0; i < maxOperationsCount; i++) {
System.err.println("Iteration ="+i);
//System.gc();
// try {
// Thread.sleep(200);
// } catch (InterruptedException e1) {
// }
factory.intersectionInit(null, i, true);
testFile.delete();
if(testFile.exists()){
throw new RuntimeException();
}
sessionContainer = getContainerForFile(testFile);
sessionContainer.set(objectB);
sessionContainer.set(objectA);
sessionContainer.close();
// testFile.delete();
// if(testFile.exists()){
// System.err.println();
// }
factory.setIntersectEnabled(true);
sessionContainer = getContainerForFile(testFile);
sessionContainer.set(array);
try {
sessionContainer.close();
throw new RuntimeException("Test Framework Error");
} catch (Exception e) {
// if(i==25){
// factory.printUnclosedBuffers();
// }
e.printStackTrace();
}
// testFile.delete();
// if(testFile.exists()){
// System.err.println();
// }
factory.setIntersectEnabled(false);
if (factory._bufferedType == BUFFER_TYPE.MAIN) {
byte[] incompleteTransaction = factory.getBuffer();
verifyDataReadable(incompleteTransaction, verificationObjects,2,3);
}
sessionContainer = getContainerForFile(testFile);
List classes = sessionContainer.getAllObjects();
if(classes.size()!=2){
throw new RuntimeException(""+classes.size()+" "+i);
}
// Object arrayObj = null;
// for (int j = 0; j < classes.size(); j++) {
// if(classes.get(j).getClass().isArray()){
// arrayObj = classes.get(j);
// }
// }
// int[] array1 = (int[]) arrayObj;
//
// if(!Arrays.equals(array, array1)){
// throw new RuntimeException();
// }
int index = classes.indexOf(objectA);
if(index == -1){
throw new RuntimeException();
}
ObjectA objectA2 = (ObjectA) classes.get(index);
index = classes.indexOf(objectB);
if(index == -1){
throw new RuntimeException();
}
ObjectB objectB2 = (ObjectB) classes.get(index);
sessionContainer.close();
}
}
public void checkInputBuffersCount(){
if(TestBufferFactory._executionError!=null){
throw new RuntimeException(TestBufferFactory._executionError);
}
if(RandomAccessTestTransactionBuffer.getOpenBuffersCount() != 0){
RandomAccessTestTransactionBuffer.printOpenBuffers();
throw new RuntimeException();
}
}
public void verifyDataReadable(byte[] data, Vector verificationObjects, int minObjects, int maxObjects) throws IOException{
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
}
File file = File.createTempFile("incompleteTransaction", "");
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
JODBSessionContainer sessionContainer = (JODBSessionContainer) JodbMini.open(file);
List classes = sessionContainer.getAllObjects();
//sessionContainer.printFileMap();
if(classes.size() < minObjects || classes.size() > maxObjects){
throw new RuntimeException();
}
for (int i = 0; verificationObjects!=null && i < verificationObjects.size(); i++) {
if(classes.indexOf(verificationObjects.elementAt(i))==-1){
throw new RuntimeException();
}
}
sessionContainer.close();
file.deleteOnExit();
file.delete();
}
public void setTestBufferFactory(){
JODBConfig.setRandomAccessBufferFactory(new TestBufferFactory());
}
public void ensureTestFolderExists(){
File testFileDir = new File(_testPath+"/testData/");
boolean result = testFileDir.mkdirs();
System.err.println(""+testFileDir.getAbsolutePath()+" --------->"+result+" "+testFileDir.isDirectory()+" "+testFileDir.exists());
}
public JODBSessionContainer getContainerForFile(File file) throws IOException{
return (JODBSessionContainer) JodbMini.open(file);
}
public static class WriteIntersectingFactory extends TestBufferFactory{
public BUFFER_TYPE _typeToIntersect;
public int _operationsBeforeIntersect;
public boolean _throwException;
public byte[] _buffer;
public BUFFER_TYPE _bufferedType;
public int _counter;
public boolean _intersectEnabled;
//public boolean _excludeClientIntersect;
/**
* @param typeToIntersect
* @param length
*/
public WriteIntersectingFactory() {
}
public void intersectionInit(BUFFER_TYPE typeToIntersect, int operationsBeforeIntersect, boolean throwException){
_typeToIntersect = typeToIntersect;
_operationsBeforeIntersect = operationsBeforeIntersect;
_throwException = throwException;
_buffer = null;
_counter = 0;
_intersectEnabled = false;
_bufferedType = null;
}
public int getCounter() {
return _counter;
}
public byte[] getBuffer() {
return _buffer;
}
public void printUnclosedBuffers(){
Map<RandomAccessTestTransactionBuffer, Throwable> hashMap = RandomAccessTestTransactionBuffer._constructionKeepers;
Iterator<Throwable> iterator = hashMap.values().iterator();
while (iterator.hasNext()) {
Throwable element = (Throwable) iterator.next();
element.printStackTrace();
}
}
@Override
public void writeComplete0(IRandomAccessDataBuffer src, BUFFER_TYPE type, long start, long end) throws IOException
{
if (_intersectEnabled) {
if (_typeToIntersect == null || _typeToIntersect == type) {
_counter++;
if (_counter > _operationsBeforeIntersect) {
_bufferedType = type;
_buffer = ((RandomAccessTestTransactionBuffer) src).getAsByteArray();
if (_throwException) {
_intersectEnabled = false;
throw new IOException();
}
}
}
}
super.writeComplete0(src, type, start, end);
}
public void setIntersectEnabled(boolean intersectEnabled) {
_intersectEnabled = intersectEnabled;
}
}
public static class TestBufferFactory extends RandomAccessTestFileBufferFactory{
public static Throwable _executionError;
public boolean PRINT = false;
public WeakHashMap<IRandomAccessDataBuffer, IRandomAccessDataBuffer> _cache = new WeakHashMap<IRandomAccessDataBuffer, IRandomAccessDataBuffer>();
ITransactionBufferListener _transactionBufferListenerMAIN = new TransactionBufferListener(BUFFER_TYPE.MAIN);
ITransactionBufferListener _transactionBufferListenerNEW_DATA = new TransactionBufferListener(BUFFER_TYPE.NEW_DATA);
ITransactionBufferListener _transactionBufferListenerREPLACEMENTS = new TransactionBufferListener(BUFFER_TYPE.REPLACEMENTS);
ITransactionBufferListener _transactionBufferListenerROLLBACK = new TransactionBufferListener(BUFFER_TYPE.ROLLBACK);
ITransactionBufferListener _transactionBufferListenerCLIENT_TEMP = new TransactionBufferListener(BUFFER_TYPE.CLIENT_TEMP_FILE);
ITransactionBufferListener _transactionBufferListenerSERVER_TEMP = new TransactionBufferListener(BUFFER_TYPE.SERVER_TEMP_BUFFER);
@Override
protected IRandomAccessDataBuffer createBuffer(File name, BUFFER_TYPE type, boolean write) throws IOException
{
ITransactionBufferListener listener;
switch (type) {
case MAIN:
listener = _transactionBufferListenerMAIN;
break;
case NEW_DATA:
listener = _transactionBufferListenerNEW_DATA;
break;
case REPLACEMENTS:
listener = _transactionBufferListenerREPLACEMENTS;
break;
case ROLLBACK:
listener = _transactionBufferListenerREPLACEMENTS;
break;
case CLIENT_TEMP_FILE:
listener = _transactionBufferListenerCLIENT_TEMP;
break;
case SERVER_TEMP_BUFFER:
listener = _transactionBufferListenerSERVER_TEMP;
break;
default:
throw new RuntimeException();
}
IRandomAccessDataBuffer result = new RandomAccessTestTransactionBuffer(name,listener,write);
_cache.put(result,null);
return result;
}
public void readComplete0(IRandomAccessDataBuffer src, BUFFER_TYPE type, long start, long end) throws IOException{
if(PRINT)System.err.println("readComplete "+type+" "+start+ " "+end);
}
public void readStart0(IRandomAccessDataBuffer src, BUFFER_TYPE type, long position) throws IOException{
if(PRINT)System.err.println("readStart "+type+" "+position);
}
public void writeComplete0(IRandomAccessDataBuffer src, BUFFER_TYPE type, long start, long end) throws IOException{
if(PRINT)System.err.println("writeComplete "+type+" "+start+ " "+end);
}
public void writeStart0(IRandomAccessDataBuffer src, BUFFER_TYPE type, long position) throws IOException{
if(PRINT)System.err.println("readComplete "+type+" "+position);
}
public void printCache(){
System.err.println("TestBufferFactory **********START************");
Iterator<IRandomAccessDataBuffer> iterator = _cache.keySet().iterator();
while (iterator.hasNext()) {
IRandomAccessDataBuffer element = (IRandomAccessDataBuffer) iterator.next();
System.err.println("TestBufferFactory "+element.toString());
}
System.err.println("TestBufferFactory **********END************");
}
public class TransactionBufferListener implements ITransactionBufferListener{
BUFFER_TYPE type;
/**
* @param type
*/
public TransactionBufferListener(BUFFER_TYPE type) {
super();
this.type = type;
}
public void readComplete(IRandomAccessDataBuffer src, long start, long end) throws IOException{
readComplete0(src, type, start, end);
}
public void readStart(IRandomAccessDataBuffer src, long position) throws IOException{
readStart0(src, type, position);
}
public void writeComplete(IRandomAccessDataBuffer src, long start, long end) throws IOException{
writeComplete0(src, type, start, end);
}
public void writeStart(IRandomAccessDataBuffer src, long position) throws IOException{
writeStart0(src, type, position);
}
public void reportError(Throwable th) {
_executionError = th;
}
}
}
}