[Joafip-svn] SF.net SVN: joafip:[3017] trunk
Brought to you by:
luc_peuvrier
|
From: <luc...@us...> - 2012-04-18 08:20:13
|
Revision: 3017
http://joafip.svn.sourceforge.net/joafip/?rev=3017&view=rev
Author: luc_peuvrier
Date: 2012-04-18 08:20:01 +0000 (Wed, 18 Apr 2012)
Log Message:
-----------
changes for heap element manager tests
Modified Paths:
--------------
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapElementManager.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapElementManager.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestStoreRestore.java
trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/AbstractFileStorable.java
trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/IFileStorable.java
trunk/joafip-testsuite/src/main/java/net/sf/joafip/InErrorTests.java
trunk/joafip-testsuite/src/main/java/net/sf/joafip/heapfile/record/service/RecordServiceTests.java
Added Paths:
-----------
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/entity/RecordForTest.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManagerWithHeapRecord.java
Removed Paths:
-------------
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManager.java
Modified: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapElementManager.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapElementManager.java 2012-04-18 03:29:16 UTC (rev 3016)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapElementManager.java 2012-04-18 08:20:01 UTC (rev 3017)
@@ -996,7 +996,8 @@
}
@Override
- public void setHeapHeader(final HeapHeader heapHeader) throws HeapException {
+ public void setHeapHeader(final IFileStorable heapHeader)
+ throws HeapException {
this.header = heapHeader;
}
Modified: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapElementManager.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapElementManager.java 2012-04-18 03:29:16 UTC (rev 3016)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapElementManager.java 2012-04-18 08:20:01 UTC (rev 3017)
@@ -124,7 +124,7 @@
* @param heapHeader
* @throws HeapException
*/
- void setHeapHeader(HeapHeader heapHeader) throws HeapException;
+ void setHeapHeader(IFileStorable heapHeader) throws HeapException;
/**
*
Added: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/entity/RecordForTest.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/entity/RecordForTest.java (rev 0)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/entity/RecordForTest.java 2012-04-18 08:20:01 UTC (rev 3017)
@@ -0,0 +1,114 @@
+package net.sf.joafip.heapfile.record.entity;
+
+/*
+ * Copyright 2012 Luc Peuvrier
+ * All rights reserved.
+ *
+ * This file is a part of JOAFIP.
+ *
+ * JOAFIP is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License.
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
+ * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * JOAFIP is distributed in the hope that it will be useful, but
+ * unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import net.sf.joafip.heapfile.record.service.IHeapElementManager;
+import net.sf.joafip.kvstore.entity.AbstractFileStorable;
+import net.sf.joafip.kvstore.service.HeapException;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ */
+public class RecordForTest extends AbstractFileStorable {
+
+ private final int recordSize;
+
+ private int value;
+
+ public RecordForTest(final IHeapElementManager heapElementManager,
+ final long positionInFile, final int recordSize) {
+ super(heapElementManager.getFileForStorable(), positionInFile);
+ this.recordSize = recordSize;
+ }
+
+ @Override
+ public long getPreviousRecordPositionInFile() throws HeapException {
+ throw new HeapException("unsupported");
+ }
+
+ @Override
+ public int getRecordSize() throws HeapException {
+ return recordSize;
+ }
+
+ @Override
+ protected int toMarshallSize() throws HeapException {
+ return 8;// integer vale and integer crc32
+ }
+
+ @Override
+ protected void valueChangedAction() throws HeapException {
+ // no implementation
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(final int value) throws HeapException {
+ this.value = value;
+ setValueIsChangedValueToSave();
+ }
+
+ @Override
+ protected void unmarshallImpl() throws HeapException {
+ readFileToIoBuffer(8);
+ value = readInteger();
+ readAndCheckCrc32();
+ }
+
+ @Override
+ protected void marshallImpl() throws HeapException {
+ writeInteger(value);
+ writeCrc32();
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = super.hashCode();
+ result = prime * result + recordSize;
+ result = prime * result + value;
+ return result;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj)
+ return true;
+ if (!super.equals(obj))
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ RecordForTest other = (RecordForTest) obj;
+ if (recordSize != other.recordSize)
+ return false;
+ if (value != other.value)
+ return false;
+ return true;
+ }
+}
Deleted: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManager.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManager.java 2012-04-18 03:29:16 UTC (rev 3016)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManager.java 2012-04-18 08:20:01 UTC (rev 3017)
@@ -1,498 +0,0 @@
-/*
- * Copyright 2007 Luc Peuvrier
- *
- * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
- * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.gnu.org/licenses/lgpl.html
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package net.sf.joafip.heapfile.record.service;
-
-import java.io.File;
-
-import net.sf.joafip.AbstractCommonDeleteFileTestCase;
-import net.sf.joafip.DoNotTransform;
-import net.sf.joafip.NotStorableClass;
-import net.sf.joafip.TestConstant;
-import net.sf.joafip.TestException;
-import net.sf.joafip.heapfile.record.entity.HeapHeader;
-import net.sf.joafip.heapfile.record.entity.HeapRecord;
-import net.sf.joafip.kvstore.entity.IFileStorable;
-import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
-import net.sf.joafip.kvstore.service.FileForStorable;
-import net.sf.joafip.kvstore.service.HeapException;
-import net.sf.joafip.redblacktree.entity.IRBTNode;
-import net.sf.joafip.redblacktree.service.RBTException;
-
-@NotStorableClass
-@DoNotTransform
-public class TestHeapElementManager extends AbstractCommonDeleteFileTestCase
- implements IHeapRecordFactory {// NOPMD
-
- private FileForStorable fileForStorable; // NOPMD
-
- private FileForStorable backupFileForStorable; // NOPMD
-
- private HeapElementManager heapElementManager; // NOPMD
-
- /** first record position */
- private long record1pos; // NOPMD
-
- /** second record position */
- private long record2pos; // NOPMD
-
- /** first record appended */
- private HeapRecord heapRecordAppened1; // NOPMD
-
- /** second record appended */
- private HeapRecord heapRecordAppened2; // NOPMD
-
- public TestHeapElementManager() throws TestException {
- super();
- }
-
- public TestHeapElementManager(final String name) throws TestException {
- super(name);
- }
-
- @Override
- protected void setUp() throws Exception {// NOPMD override test case
- super.setUp();
- final String runtimePath = TestConstant.getWinRamDiskRuntimeDir();
-
- final File dataFile = new File(runtimePath + File.separator
- + "test.dat");
-
- final File backupFile = new File(runtimePath + File.separator
- + "backup.dat");
-
- final File stateDataBackupFile;
- stateDataBackupFile = new File(runtimePath + File.separator
- + "backup.flag");
-
- final File stateDataFile;
- stateDataFile = new File(runtimePath + File.separator + "data.flag");
- fileForStorable = new FileForStorable(dataFile, 1, 0);
- backupFileForStorable = new FileForStorable(backupFile, 1, 0);
- final File globalStateFile;
- globalStateFile = new File(runtimePath + File.separator + "global.flag");
- final File openFileTraceFile = new File(runtimePath + File.separator
- + "trace.txt");
- heapElementManager = new HeapElementManager(this, fileForStorable,
- stateDataFile, backupFileForStorable, stateDataBackupFile,
- globalStateFile, false, false, 1, 0, openFileTraceFile);
- heapElementManager.setHeapHeader(new HeapHeader(heapElementManager));
- }
-
- @Override
- protected void tearDown() throws Exception {// NOPMD override test case
- try {
- if (fileForStorable.isOpened()) {
- fileForStorable.close();
- }
- } catch (Throwable throwable) {// NOPMD ignore all errors
- }
- try {
- if (backupFileForStorable.isOpened()) {
- backupFileForStorable.close();
- }
- } catch (Throwable throwable) {// NOPMD ignore all errors
- }
- fileForStorable = null;// NOPMD
- backupFileForStorable = null;// NOPMD
- heapElementManager = null;// NOPMD
- heapRecordAppened1 = null;// NOPMD
- heapRecordAppened2 = null;// NOPMD
- super.tearDown();
- }
-
- /**
- * test creation of the heap header in empty heap file
- *
- * @throws HeapException
- *
- */
- public void testEmptyHeap() throws HeapException {// NOPMD assertion in
- // method called
-
- /* new heap header creation and write with check */
- final HeapHeader heapHeader1 = appendHeaderInEmptyFile();
-
- /* check the wrote header */
- heapElementManager.startService();
- heapElementManager.openTransaction();
- getHeapHeaderAndCheckPosition(heapHeader1);
- heapElementManager.closeTransaction();
- heapElementManager.stopService();
- }
-
- /**
- * test header modification saved in heap file
- *
- * @throws HeapException
- * @throws RBTException
- *
- */
- public void testChangeHeader() throws HeapException, RBTException {
- /* new heap header creation and write with check */
- final HeapHeader heapHeader1 = appendHeaderInEmptyFile();
-
- /* then read header */
- heapElementManager.startService();
- heapElementManager.openTransaction();
- final HeapHeader heapHeader2 = getHeapHeaderAndCheckPosition(heapHeader1);
- /* add heap record */
- final HeapRecord heapRecord = new HeapRecord(heapElementManager,
- /**/HeapHeader.HEAP_HEADER_SIZE/* position in file */);
- // heapHeader2.setFreeRootNodeFilePosition(new HeapFreeNode(
- // heapElementManager, heapRecord).getPositionInFile());
- heapHeader2.setFreeRootNodeFilePosition(heapRecord.getPositionInFile());
- // heapHeader2.setIdRootNodeFilePosition(new HeapIdNode(
- // heapElementManager, heapRecord).getPositionInFile());
- heapHeader2.setIdRootNodeFilePosition(heapRecord.getPositionInFile());
- /* write modifications */
- heapElementManager.closeTransaction();
-
- /* read the last modification */
- heapElementManager.openTransaction();
- final HeapHeader heapHeader3 = getHeapHeaderAndCheckPosition(heapHeader2);
-
- /* check modifications */
- assertEquals("FreeRootNodeFilePosition must be "
- + HeapHeader.HEAP_HEADER_SIZE, HeapHeader.HEAP_HEADER_SIZE,
- heapHeader3.getFreeRootNodeFilePosition());
-
- assertEquals("IdRootNodeFilePosition must be "
- + HeapHeader.HEAP_HEADER_SIZE, HeapHeader.HEAP_HEADER_SIZE,
- heapHeader3.getIdRootNodeFilePosition());
-
- heapElementManager.closeTransaction();
- heapElementManager.stopService();
- }
-
- /**
- * test append done correctly
- *
- * @throws HeapException
- *
- */
- public void testAppendRecord() throws HeapException {
- appendRecord();
- heapElementManager.startService();
- heapElementManager.openTransaction();
-
- final int dataSize = 100;
- final HeapRecord heapRecord1 = readHeapFileDataRecord(record1pos);
- assertEquals("bad record position", record1pos,
- heapRecord1.getPositionInFile());
- assertEquals("bad record data size", dataSize, heapRecord1
- .getDataAssociatedSize().intValue());
- final byte[] data1 = heapRecord1.getDataAssociated();
- for (int index = 0; index < dataSize; index++) {
- assertEquals("bad record data", index, data1[index]);
- }
- logger.debug("heap record1\n" + heapRecord1.toString());
- assertEquals("read must be equals appened", heapRecordAppened1,
- heapRecord1);
-
- final HeapRecord heapRecord2 = readHeapFileDataRecord(record2pos);
- assertEquals("bad record position", record2pos,
- heapRecord2.getPositionInFile());
- assertEquals("bad record data size", dataSize, heapRecord2
- .getDataAssociatedSize().intValue());
- final byte[] data2 = heapRecord2.getDataAssociated();
- assertNotSame("heap record 1 and 2 must not have the same data", data1,
- data2);
- for (int index = 0; index < 100; index++) {
- assertEquals("bad record data", index, data2[index]);
- }
- logger.debug("heap record2\n" + heapRecord2.toString());
- assertEquals("read must be equals appened", heapRecordAppened2,
- heapRecord2);
-
- heapElementManager.closeTransaction();
- heapElementManager.stopService();
- }
-
- public void testUpdateDataRecord() throws HeapException {
- appendRecord();
- final long pos = record1pos;
-
- heapElementManager.startService();
- heapElementManager.openTransaction();
- HeapRecord heapRecord1 = readHeapFileDataRecord(pos);
- byte[] data1 = heapRecord1.getDataAssociated();
- for (int index = 0; index < data1.length; index++) {
- data1[index] = (byte) (data1.length - index);
- }
- heapRecord1.setDataAssociated(data1);
- heapElementManager.closeTransaction();
-
- heapElementManager.openTransaction();
- heapRecord1 = readHeapFileDataRecord(pos);
- data1 = heapRecord1.getDataAssociated();
- for (int index = 0; index < data1.length; index++) {
- assertEquals("bad record data", data1.length - index, data1[index]);
- }
- heapElementManager.closeTransaction();
- heapElementManager.stopService();
-
- }
-
- public void testUpdateNodeRecord() throws HeapException, RBTException {
- appendRecord();
- final long pos = record1pos;
-
- heapElementManager.startService();
-
- heapElementManager.openTransaction();
- HeapRecord heapRecord1 = readHeapFileDataRecord(pos);
-
- try {
- assert false;
- } catch (AssertionError error) {
- try {
- heapRecord1.getFreeNode();
- fail("must not obtain free node");// NOPMD
- } catch (AssertionError error2) {// NOPMD nothing to do when
- // exception thrown
- // expected
- }
- }
- IRBTNode<?> node = heapRecord1.getIdNode();
- boolean color;
- if (node.isColorSetted()) {
- color = node.getColor();
- } else {
- color = true;
- }
- node.setColor(!color);
- assertTrue("value must have changed",
- heapRecord1.isValueChangedToSave());
- heapElementManager.closeTransaction();
-
- heapElementManager.openTransaction();
- heapRecord1 = readHeapFileDataRecord(pos);
- node = heapRecord1.getIdNode();
- assertEquals("color must have changed", !color, node.getColor());
- assertFalse("not modified record", heapRecord1.isValueChangedToSave());
- heapElementManager.closeTransaction();
-
- heapElementManager.openTransaction();
- heapRecord1 = readHeapFileDataRecord(pos);
- heapRecord1.freeRecord();
- try {
- assert false;
- } catch (AssertionError error) {
- try {
- heapRecord1.getIdNode();
- fail("must not obtain id node");// NOPMD
- } catch (AssertionError error2) {// NOPMD nothing to do when
- // exception thrown
- // expected
- }
- }
- node = heapRecord1.getFreeNode();
- if (node.isColorSetted()) {
- color = node.getColor();
- } else {
- color = true;
- }
- node.setColor(!color);
- heapElementManager.closeTransaction();
-
- heapElementManager.openTransaction();
- heapRecord1 = readHeapFileDataRecord(pos);
- node = heapRecord1.getFreeNode();
- assertEquals("color must have changed", !color, node.getColor());
- heapElementManager.closeTransaction();
- heapElementManager.stopService();
- }
-
- public void testChangeManageDataMode() throws HeapException {
- appendRecord();
- final long pos = record1pos;
- heapElementManager.startService();
- heapElementManager.openTransaction();
- /*
- * read record 1 whithout data managing and check state
- */
- HeapRecord heapRecord1 = readHeapFileDataRecord(
- /**/pos/* position in file */);
- assertFalse("record1 must be data record", heapRecord1.isFreeRecord());
- assertFalse("record1 must be in state just created",
- heapRecord1.isJustCreated());
- assertTrue("record1 has been read", heapRecord1.isHeaderRead());
- assertFalse("record1 value must not changed state",
- heapRecord1.isValueChangedToSave());
-
- /*
- * set record 1 value changer, check if added to be wrote
- */
- heapRecord1.setValueIsChangedValueToSave();
- assertNotNull("must be in cache",
- heapElementManager.getHeapFileRecordInWriteCache(pos));
-
- // heapRecord1.createDataArea();
-
- heapElementManager.closeTransaction();
- heapElementManager.openTransaction();
-
- /*
- * change to manage data mode
- */
- heapRecord1 = readHeapFileDataRecord(
- /**/pos/* position in file */);
- assertFalse("record1 must be data record", heapRecord1.isFreeRecord());
- assertFalse("must not be just created", heapRecord1.isJustCreated());
- assertTrue("must has been read", heapRecord1.isHeaderRead());
- assertFalse("value changed must not change",
- heapRecord1.isValueChangedToSave());
- assertNotNull("must be in cache",
- heapElementManager.getHeapFileRecordInReadCache(pos));
-
- heapElementManager.closeTransaction();
- heapElementManager.stopService();
- }
-
- /**
- * append heap header in heap empty file
- *
- * @return appened heap header
- * @throws HeapException
- *
- */
- private HeapHeader appendHeaderInEmptyFile() throws HeapException {
- heapElementManager.startService();
- heapElementManager.openTransaction();
- final HeapHeader heapHeader1 = getHeapHeaderAndCheckPosition(null);
- assertEquals("FreeRootNodeFilePosition must be -1", -1,
- heapHeader1.getFreeRootNodeFilePosition());
- assertEquals("IdRootNodeFilePosition must be -1", -1,
- heapHeader1.getIdRootNodeFilePosition());
- // assertTrue("created header must be to save", heapHeader1
- // .isValueChangedToSave());
- assertFalse("created header must be saved",
- heapHeader1.isValueChangedToSave());
- heapElementManager.closeTransaction();
- heapElementManager.stopService();
- assertFalse("saving implies no more value changed",
- heapHeader1.isValueChangedToSave());
- assertFalse("just created must be false", heapHeader1.isJustCreated());
- return heapHeader1;
- }
-
- /**
- * append two records
- *
- * @throws HeapException
- */
- private void appendRecord() throws HeapException {
- appendHeaderInEmptyFile();
- heapElementManager.startService();
- heapElementManager.openTransaction();
- record1pos = HeapHeader.HEAP_HEADER_SIZE;
- final int dataSize = 100;
- final DataRecordIdentifier dataRecordIdentifier = DataRecordIdentifier.ZERO;
- heapRecordAppened1 = new HeapRecord(heapElementManager, record1pos,
- -1L/* prev pos */, dataRecordIdentifier/* id */,
- false/* free */, dataSize, dataSize
- + HeapRecord.MAX_RECORD_HEADER_SIZE + 4/*
- * area size: +
- * 4 for crc32
- */);
- // heapRecordAppened1 = heapElementManager.newHeapFileRecord(record1pos,
- // -1L/* prev pos */, dataRecordIdentifier/* id */,
- // false/* free */, dataSize, dataSize
- // + HeapRecord.MAX_RECORD_HEADER_SIZE + 4/*
- // * area size: +
- // * 4 for crc32
- // */);
- heapElementManager.appendHeapFileRecord(heapRecordAppened1);
- try {
- assert false;
- } catch (AssertionError error) {
- try {
- heapRecordAppened1.getDataAssociated();
- fail("there is no data associated to record");
- } catch (AssertionError error2) {// NOPMD
- // expected error
- }
- }
-
- byte[] testData = new byte[dataSize];
- for (int index = 0; index < dataSize; index++) {
- testData[index] = (byte) index;
- }
- heapRecordAppened1.setDataAssociated(testData);
-
- heapElementManager.closeTransaction();
- logger.debug("appened heap record1\n" + heapRecordAppened1.toString());
-
- heapElementManager.openTransaction();
-
- record2pos = record1pos + heapRecordAppened1.getRecordSize();
- heapRecordAppened2 = new HeapRecord(heapElementManager, record2pos,
- record1pos, dataRecordIdentifier/* id */, false/* free */,
- dataSize, dataSize + HeapRecord.MAX_RECORD_HEADER_SIZE + 4/*
- * 4 for
- * crc32
- */);
- // heapRecordAppened2 = heapElementManager.newHeapFileRecord(record2pos,
- // record1pos, dataRecordIdentifier/* id */, false/* free */,
- // dataSize, dataSize + HeapRecord.MAX_RECORD_HEADER_SIZE + 4/*
- // * 4 for
- // * crc32
- // */);
- heapElementManager.appendHeapFileRecord(heapRecordAppened2);
- heapRecordAppened2.setDataAssociated(testData);
- heapElementManager.closeTransaction();
- heapElementManager.stopService();
- logger.debug("appened heap record2\n" + heapRecordAppened2.toString());
- }
-
- /**
- * get heap header and check its position
- *
- * @param heapHeaderReference
- *
- * @return heap header
- * @throws HeapException
- */
- private HeapHeader getHeapHeaderAndCheckPosition(
- final HeapHeader heapHeaderReference) throws HeapException {
- final HeapHeader heapHeader = (HeapHeader) heapElementManager
- .getHeapHeader();
- assertEquals("header must be at file beginning", 0,
- heapHeader.getPositionInFile());
- if (heapHeaderReference != null) {
- // header is now the same object
- // assertNotSame("new header must not be same of creation one",
- // heapHeaderReference, heapHeader);
- assertEquals("wrote and read must be equals", heapHeaderReference,
- heapHeader);
- }
- return heapHeader;
- }
-
- private HeapRecord readHeapFileDataRecord(final long positionInFile)
- throws HeapException {
- return (HeapRecord) heapElementManager
- .readHeapFileDataRecord(positionInFile);
- }
-
- @Override
- public IFileStorable createHeapRecord(
- final HeapElementManager heapElementManager,
- final long positionInFile) throws HeapException {
- return new HeapRecord(heapElementManager, positionInFile);
- }
-}
Copied: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManagerWithHeapRecord.java (from rev 3016, trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManager.java)
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManagerWithHeapRecord.java (rev 0)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManagerWithHeapRecord.java 2012-04-18 08:20:01 UTC (rev 3017)
@@ -0,0 +1,499 @@
+/*
+ * Copyright 2007 Luc Peuvrier
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
+ * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.sf.joafip.heapfile.record.service;
+
+import java.io.File;
+
+import net.sf.joafip.AbstractCommonDeleteFileTestCase;
+import net.sf.joafip.DoNotTransform;
+import net.sf.joafip.NotStorableClass;
+import net.sf.joafip.TestConstant;
+import net.sf.joafip.TestException;
+import net.sf.joafip.heapfile.record.entity.HeapHeader;
+import net.sf.joafip.heapfile.record.entity.HeapRecord;
+import net.sf.joafip.kvstore.entity.IFileStorable;
+import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
+import net.sf.joafip.kvstore.service.FileForStorable;
+import net.sf.joafip.kvstore.service.HeapException;
+import net.sf.joafip.redblacktree.entity.IRBTNode;
+import net.sf.joafip.redblacktree.service.RBTException;
+
+@NotStorableClass
+@DoNotTransform
+public class TestHeapElementManagerWithHeapRecord extends
+ AbstractCommonDeleteFileTestCase implements IHeapRecordFactory {// NOPMD
+
+ private FileForStorable fileForStorable; // NOPMD
+
+ private FileForStorable backupFileForStorable; // NOPMD
+
+ private HeapElementManager heapElementManager; // NOPMD
+
+ /** first record position */
+ private long record1pos; // NOPMD
+
+ /** second record position */
+ private long record2pos; // NOPMD
+
+ /** first record appended */
+ private HeapRecord heapRecordAppened1; // NOPMD
+
+ /** second record appended */
+ private HeapRecord heapRecordAppened2; // NOPMD
+
+ public TestHeapElementManagerWithHeapRecord() throws TestException {
+ super();
+ }
+
+ public TestHeapElementManagerWithHeapRecord(final String name)
+ throws TestException {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {// NOPMD override test case
+ super.setUp();
+ final String runtimePath = TestConstant.getWinRamDiskRuntimeDir();
+
+ final File dataFile = new File(runtimePath + File.separator
+ + "test.dat");
+
+ final File backupFile = new File(runtimePath + File.separator
+ + "backup.dat");
+
+ final File stateDataBackupFile;
+ stateDataBackupFile = new File(runtimePath + File.separator
+ + "backup.flag");
+
+ final File stateDataFile;
+ stateDataFile = new File(runtimePath + File.separator + "data.flag");
+ fileForStorable = new FileForStorable(dataFile, 1, 0);
+ backupFileForStorable = new FileForStorable(backupFile, 1, 0);
+ final File globalStateFile;
+ globalStateFile = new File(runtimePath + File.separator + "global.flag");
+ final File openFileTraceFile = new File(runtimePath + File.separator
+ + "trace.txt");
+ heapElementManager = new HeapElementManager(this, fileForStorable,
+ stateDataFile, backupFileForStorable, stateDataBackupFile,
+ globalStateFile, false, false, 1, 0, openFileTraceFile);
+ heapElementManager.setHeapHeader(new HeapHeader(heapElementManager));
+ }
+
+ @Override
+ protected void tearDown() throws Exception {// NOPMD override test case
+ try {
+ if (fileForStorable.isOpened()) {
+ fileForStorable.close();
+ }
+ } catch (Throwable throwable) {// NOPMD ignore all errors
+ }
+ try {
+ if (backupFileForStorable.isOpened()) {
+ backupFileForStorable.close();
+ }
+ } catch (Throwable throwable) {// NOPMD ignore all errors
+ }
+ fileForStorable = null;// NOPMD
+ backupFileForStorable = null;// NOPMD
+ heapElementManager = null;// NOPMD
+ heapRecordAppened1 = null;// NOPMD
+ heapRecordAppened2 = null;// NOPMD
+ super.tearDown();
+ }
+
+ /**
+ * test creation of the heap header in empty heap file
+ *
+ * @throws HeapException
+ *
+ */
+ public void testEmptyHeap() throws HeapException {// NOPMD assertion in
+ // method called
+
+ /* new heap header creation and write with check */
+ final HeapHeader heapHeader1 = appendHeaderInEmptyFile();
+
+ /* check the wrote header */
+ heapElementManager.startService();
+ heapElementManager.openTransaction();
+ getHeapHeaderAndCheckPosition(heapHeader1);
+ heapElementManager.closeTransaction();
+ heapElementManager.stopService();
+ }
+
+ /**
+ * test header modification saved in heap file
+ *
+ * @throws HeapException
+ * @throws RBTException
+ *
+ */
+ public void testChangeHeader() throws HeapException, RBTException {
+ /* new heap header creation and write with check */
+ final HeapHeader heapHeader1 = appendHeaderInEmptyFile();
+
+ /* then read header */
+ heapElementManager.startService();
+ heapElementManager.openTransaction();
+ final HeapHeader heapHeader2 = getHeapHeaderAndCheckPosition(heapHeader1);
+ /* add heap record */
+ final HeapRecord heapRecord = new HeapRecord(heapElementManager,
+ /**/HeapHeader.HEAP_HEADER_SIZE/* position in file */);
+ // heapHeader2.setFreeRootNodeFilePosition(new HeapFreeNode(
+ // heapElementManager, heapRecord).getPositionInFile());
+ heapHeader2.setFreeRootNodeFilePosition(heapRecord.getPositionInFile());
+ // heapHeader2.setIdRootNodeFilePosition(new HeapIdNode(
+ // heapElementManager, heapRecord).getPositionInFile());
+ heapHeader2.setIdRootNodeFilePosition(heapRecord.getPositionInFile());
+ /* write modifications */
+ heapElementManager.closeTransaction();
+
+ /* read the last modification */
+ heapElementManager.openTransaction();
+ final HeapHeader heapHeader3 = getHeapHeaderAndCheckPosition(heapHeader2);
+
+ /* check modifications */
+ assertEquals("FreeRootNodeFilePosition must be "
+ + HeapHeader.HEAP_HEADER_SIZE, HeapHeader.HEAP_HEADER_SIZE,
+ heapHeader3.getFreeRootNodeFilePosition());
+
+ assertEquals("IdRootNodeFilePosition must be "
+ + HeapHeader.HEAP_HEADER_SIZE, HeapHeader.HEAP_HEADER_SIZE,
+ heapHeader3.getIdRootNodeFilePosition());
+
+ heapElementManager.closeTransaction();
+ heapElementManager.stopService();
+ }
+
+ /**
+ * test append done correctly
+ *
+ * @throws HeapException
+ *
+ */
+ public void testAppendRecord() throws HeapException {
+ appendRecord();
+ heapElementManager.startService();
+ heapElementManager.openTransaction();
+
+ final int dataSize = 100;
+ final HeapRecord heapRecord1 = readHeapFileDataRecord(record1pos);
+ assertEquals("bad record position", record1pos,
+ heapRecord1.getPositionInFile());
+ assertEquals("bad record data size", dataSize, heapRecord1
+ .getDataAssociatedSize().intValue());
+ final byte[] data1 = heapRecord1.getDataAssociated();
+ for (int index = 0; index < dataSize; index++) {
+ assertEquals("bad record data", index, data1[index]);
+ }
+ logger.debug("heap record1\n" + heapRecord1.toString());
+ assertEquals("read must be equals appened", heapRecordAppened1,
+ heapRecord1);
+
+ final HeapRecord heapRecord2 = readHeapFileDataRecord(record2pos);
+ assertEquals("bad record position", record2pos,
+ heapRecord2.getPositionInFile());
+ assertEquals("bad record data size", dataSize, heapRecord2
+ .getDataAssociatedSize().intValue());
+ final byte[] data2 = heapRecord2.getDataAssociated();
+ assertNotSame("heap record 1 and 2 must not have the same data", data1,
+ data2);
+ for (int index = 0; index < 100; index++) {
+ assertEquals("bad record data", index, data2[index]);
+ }
+ logger.debug("heap record2\n" + heapRecord2.toString());
+ assertEquals("read must be equals appened", heapRecordAppened2,
+ heapRecord2);
+
+ heapElementManager.closeTransaction();
+ heapElementManager.stopService();
+ }
+
+ public void testUpdateDataRecord() throws HeapException {
+ appendRecord();
+ final long pos = record1pos;
+
+ heapElementManager.startService();
+ heapElementManager.openTransaction();
+ HeapRecord heapRecord1 = readHeapFileDataRecord(pos);
+ byte[] data1 = heapRecord1.getDataAssociated();
+ for (int index = 0; index < data1.length; index++) {
+ data1[index] = (byte) (data1.length - index);
+ }
+ heapRecord1.setDataAssociated(data1);
+ heapElementManager.closeTransaction();
+
+ heapElementManager.openTransaction();
+ heapRecord1 = readHeapFileDataRecord(pos);
+ data1 = heapRecord1.getDataAssociated();
+ for (int index = 0; index < data1.length; index++) {
+ assertEquals("bad record data", data1.length - index, data1[index]);
+ }
+ heapElementManager.closeTransaction();
+ heapElementManager.stopService();
+
+ }
+
+ public void testUpdateNodeRecord() throws HeapException, RBTException {
+ appendRecord();
+ final long pos = record1pos;
+
+ heapElementManager.startService();
+
+ heapElementManager.openTransaction();
+ HeapRecord heapRecord1 = readHeapFileDataRecord(pos);
+
+ try {
+ assert false;
+ } catch (AssertionError error) {
+ try {
+ heapRecord1.getFreeNode();
+ fail("must not obtain free node");// NOPMD
+ } catch (AssertionError error2) {// NOPMD nothing to do when
+ // exception thrown
+ // expected
+ }
+ }
+ IRBTNode<?> node = heapRecord1.getIdNode();
+ boolean color;
+ if (node.isColorSetted()) {
+ color = node.getColor();
+ } else {
+ color = true;
+ }
+ node.setColor(!color);
+ assertTrue("value must have changed",
+ heapRecord1.isValueChangedToSave());
+ heapElementManager.closeTransaction();
+
+ heapElementManager.openTransaction();
+ heapRecord1 = readHeapFileDataRecord(pos);
+ node = heapRecord1.getIdNode();
+ assertEquals("color must have changed", !color, node.getColor());
+ assertFalse("not modified record", heapRecord1.isValueChangedToSave());
+ heapElementManager.closeTransaction();
+
+ heapElementManager.openTransaction();
+ heapRecord1 = readHeapFileDataRecord(pos);
+ heapRecord1.freeRecord();
+ try {
+ assert false;
+ } catch (AssertionError error) {
+ try {
+ heapRecord1.getIdNode();
+ fail("must not obtain id node");// NOPMD
+ } catch (AssertionError error2) {// NOPMD nothing to do when
+ // exception thrown
+ // expected
+ }
+ }
+ node = heapRecord1.getFreeNode();
+ if (node.isColorSetted()) {
+ color = node.getColor();
+ } else {
+ color = true;
+ }
+ node.setColor(!color);
+ heapElementManager.closeTransaction();
+
+ heapElementManager.openTransaction();
+ heapRecord1 = readHeapFileDataRecord(pos);
+ node = heapRecord1.getFreeNode();
+ assertEquals("color must have changed", !color, node.getColor());
+ heapElementManager.closeTransaction();
+ heapElementManager.stopService();
+ }
+
+ public void testChangeManageDataMode() throws HeapException {
+ appendRecord();
+ final long pos = record1pos;
+ heapElementManager.startService();
+ heapElementManager.openTransaction();
+ /*
+ * read record 1 whithout data managing and check state
+ */
+ HeapRecord heapRecord1 = readHeapFileDataRecord(
+ /**/pos/* position in file */);
+ assertFalse("record1 must be data record", heapRecord1.isFreeRecord());
+ assertFalse("record1 must be in state just created",
+ heapRecord1.isJustCreated());
+ assertTrue("record1 has been read", heapRecord1.isHeaderRead());
+ assertFalse("record1 value must not changed state",
+ heapRecord1.isValueChangedToSave());
+
+ /*
+ * set record 1 value changer, check if added to be wrote
+ */
+ heapRecord1.setValueIsChangedValueToSave();
+ assertNotNull("must be in cache",
+ heapElementManager.getHeapFileRecordInWriteCache(pos));
+
+ // heapRecord1.createDataArea();
+
+ heapElementManager.closeTransaction();
+ heapElementManager.openTransaction();
+
+ /*
+ * change to manage data mode
+ */
+ heapRecord1 = readHeapFileDataRecord(
+ /**/pos/* position in file */);
+ assertFalse("record1 must be data record", heapRecord1.isFreeRecord());
+ assertFalse("must not be just created", heapRecord1.isJustCreated());
+ assertTrue("must has been read", heapRecord1.isHeaderRead());
+ assertFalse("value changed must not change",
+ heapRecord1.isValueChangedToSave());
+ assertNotNull("must be in cache",
+ heapElementManager.getHeapFileRecordInReadCache(pos));
+
+ heapElementManager.closeTransaction();
+ heapElementManager.stopService();
+ }
+
+ /**
+ * append heap header in heap empty file
+ *
+ * @return appened heap header
+ * @throws HeapException
+ *
+ */
+ private HeapHeader appendHeaderInEmptyFile() throws HeapException {
+ heapElementManager.startService();
+ heapElementManager.openTransaction();
+ final HeapHeader heapHeader1 = getHeapHeaderAndCheckPosition(null);
+ assertEquals("FreeRootNodeFilePosition must be -1", -1,
+ heapHeader1.getFreeRootNodeFilePosition());
+ assertEquals("IdRootNodeFilePosition must be -1", -1,
+ heapHeader1.getIdRootNodeFilePosition());
+ // assertTrue("created header must be to save", heapHeader1
+ // .isValueChangedToSave());
+ assertFalse("created header must be saved",
+ heapHeader1.isValueChangedToSave());
+ heapElementManager.closeTransaction();
+ heapElementManager.stopService();
+ assertFalse("saving implies no more value changed",
+ heapHeader1.isValueChangedToSave());
+ assertFalse("just created must be false", heapHeader1.isJustCreated());
+ return heapHeader1;
+ }
+
+ /**
+ * append two records
+ *
+ * @throws HeapException
+ */
+ private void appendRecord() throws HeapException {
+ appendHeaderInEmptyFile();
+ heapElementManager.startService();
+ heapElementManager.openTransaction();
+ record1pos = HeapHeader.HEAP_HEADER_SIZE;
+ final int dataSize = 100;
+ final DataRecordIdentifier dataRecordIdentifier = DataRecordIdentifier.ZERO;
+ heapRecordAppened1 = new HeapRecord(heapElementManager, record1pos,
+ -1L/* prev pos */, dataRecordIdentifier/* id */,
+ false/* free */, dataSize, dataSize
+ + HeapRecord.MAX_RECORD_HEADER_SIZE + 4/*
+ * area size: +
+ * 4 for crc32
+ */);
+ // heapRecordAppened1 = heapElementManager.newHeapFileRecord(record1pos,
+ // -1L/* prev pos */, dataRecordIdentifier/* id */,
+ // false/* free */, dataSize, dataSize
+ // + HeapRecord.MAX_RECORD_HEADER_SIZE + 4/*
+ // * area size: +
+ // * 4 for crc32
+ // */);
+ heapElementManager.appendHeapFileRecord(heapRecordAppened1);
+ try {
+ assert false;
+ } catch (AssertionError error) {
+ try {
+ heapRecordAppened1.getDataAssociated();
+ fail("there is no data associated to record");
+ } catch (AssertionError error2) {// NOPMD
+ // expected error
+ }
+ }
+
+ byte[] testData = new byte[dataSize];
+ for (int index = 0; index < dataSize; index++) {
+ testData[index] = (byte) index;
+ }
+ heapRecordAppened1.setDataAssociated(testData);
+
+ heapElementManager.closeTransaction();
+ logger.debug("appened heap record1\n" + heapRecordAppened1.toString());
+
+ heapElementManager.openTransaction();
+
+ record2pos = record1pos + heapRecordAppened1.getRecordSize();
+ heapRecordAppened2 = new HeapRecord(heapElementManager, record2pos,
+ record1pos, dataRecordIdentifier/* id */, false/* free */,
+ dataSize, dataSize + HeapRecord.MAX_RECORD_HEADER_SIZE + 4/*
+ * 4 for
+ * crc32
+ */);
+ // heapRecordAppened2 = heapElementManager.newHeapFileRecord(record2pos,
+ // record1pos, dataRecordIdentifier/* id */, false/* free */,
+ // dataSize, dataSize + HeapRecord.MAX_RECORD_HEADER_SIZE + 4/*
+ // * 4 for
+ // * crc32
+ // */);
+ heapElementManager.appendHeapFileRecord(heapRecordAppened2);
+ heapRecordAppened2.setDataAssociated(testData);
+ heapElementManager.closeTransaction();
+ heapElementManager.stopService();
+ logger.debug("appened heap record2\n" + heapRecordAppened2.toString());
+ }
+
+ /**
+ * get heap header and check its position
+ *
+ * @param heapHeaderReference
+ *
+ * @return heap header
+ * @throws HeapException
+ */
+ private HeapHeader getHeapHeaderAndCheckPosition(
+ final HeapHeader heapHeaderReference) throws HeapException {
+ final HeapHeader heapHeader = (HeapHeader) heapElementManager
+ .getHeapHeader();
+ assertEquals("header must be at file beginning", 0,
+ heapHeader.getPositionInFile());
+ if (heapHeaderReference != null) {
+ // header is now the same object
+ // assertNotSame("new header must not be same of creation one",
+ // heapHeaderReference, heapHeader);
+ assertEquals("wrote and read must be equals", heapHeaderReference,
+ heapHeader);
+ }
+ return heapHeader;
+ }
+
+ private HeapRecord readHeapFileDataRecord(final long positionInFile)
+ throws HeapException {
+ return (HeapRecord) heapElementManager
+ .readHeapFileDataRecord(positionInFile);
+ }
+
+ @Override
+ public IFileStorable createHeapRecord(
+ final HeapElementManager heapElementManager,
+ final long positionInFile) throws HeapException {
+ return new HeapRecord(heapElementManager, positionInFile);
+ }
+}
Modified: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestStoreRestore.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestStoreRestore.java 2012-04-18 03:29:16 UTC (rev 3016)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestStoreRestore.java 2012-04-18 08:20:01 UTC (rev 3017)
@@ -34,6 +34,12 @@
import net.sf.joafip.kvstore.service.HeapException;
import net.sf.joafip.redblacktree.service.RBTException;
+/**
+ * test {@link HeapRecord} store and restore
+ *
+ * @author luc peuvrier
+ *
+ */
@NotStorableClass
@DoNotTransform
public class TestStoreRestore extends AbstractCommonDeleteFileTestCase
@@ -251,7 +257,8 @@
}
@Override
- public void setHeapHeader(final HeapHeader heapHeader) throws HeapException {
+ public void setHeapHeader(final IFileStorable heapHeader)
+ throws HeapException {
// do nothing
}
Modified: trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/AbstractFileStorable.java
===================================================================
--- trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/AbstractFileStorable.java 2012-04-18 03:29:16 UTC (rev 3016)
+++ trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/AbstractFileStorable.java 2012-04-18 08:20:01 UTC (rev 3017)
@@ -153,10 +153,7 @@
*/
protected abstract void marshallImpl() throws HeapException;
- /**
- *
- * @return true if just created ( not read from file and not setted )
- */
+ @Override
public boolean isJustCreated() {
return justCreated;
}
Modified: trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/IFileStorable.java
===================================================================
--- trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/IFileStorable.java 2012-04-18 03:29:16 UTC (rev 3016)
+++ trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/IFileStorable.java 2012-04-18 08:20:01 UTC (rev 3017)
@@ -76,10 +76,17 @@
boolean isValueChangedToSave();
/**
+ *
+ * @return true if just created ( not read from file and not setted )
+ */
+ boolean isJustCreated();
+
+ /**
* get the record size
*
* @return the record size
* @throws HeapException
*/
int getRecordSize() throws HeapException;
+
}
Modified: trunk/joafip-testsuite/src/main/java/net/sf/joafip/InErrorTests.java
===================================================================
--- trunk/joafip-testsuite/src/main/java/net/sf/joafip/InErrorTests.java 2012-04-18 03:29:16 UTC (rev 3016)
+++ trunk/joafip-testsuite/src/main/java/net/sf/joafip/InErrorTests.java 2012-04-18 08:20:01 UTC (rev 3017)
@@ -24,8 +24,8 @@
import junit.framework.Test;
import junit.framework.TestSuite;
-import net.sf.joafip.java.util.TestSunTreeMap;
-import net.sf.joafip.pmap.TestFileTreeMap;
+import net.sf.joafip.heapfile.record.service.TestHeapElementManagerWithHeapRecord;
+import net.sf.joafip.heapfile.record.service.TestHeapRecordManage;
/**
*
@@ -42,8 +42,8 @@
public static Test suite() {
final TestSuite suite = new TestSuite("in error Tests");
// $JUnit-BEGIN$
- suite.addTestSuite(TestSunTreeMap.class);
- suite.addTestSuite(TestFileTreeMap.class);
+ suite.addTestSuite(TestHeapElementManagerWithHeapRecord.class);
+ suite.addTestSuite(TestHeapRecordManage.class);
// suite.addTest(xxxx.suite());
// $JUnit-END$
return suite;
Modified: trunk/joafip-testsuite/src/main/java/net/sf/joafip/heapfile/record/service/RecordServiceTests.java
===================================================================
--- trunk/joafip-testsuite/src/main/java/net/sf/joafip/heapfile/record/service/RecordServiceTests.java 2012-04-18 03:29:16 UTC (rev 3016)
+++ trunk/joafip-testsuite/src/main/java/net/sf/joafip/heapfile/record/service/RecordServiceTests.java 2012-04-18 08:20:01 UTC (rev 3017)
@@ -27,7 +27,7 @@
final TestSuite suite = new TestSuite(
"Test for heapfile.record.service");
// $JUnit-BEGIN$
- suite.addTestSuite(TestHeapElementManager.class);
+ suite.addTestSuite(TestHeapElementManagerWithHeapRecord.class);
suite.addTestSuite(TestHeapRecordManage.class);
suite.addTestSuite(TestStoreRestore.class);
// $JUnit-END$
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|