aboutsummaryrefslogtreecommitdiffstats
path: root/test/net/java/sip/communicator/slick/fileaccess
diff options
context:
space:
mode:
authorBenoit Pradelle <pradelle@java.net>2007-10-11 16:20:19 +0000
committerBenoit Pradelle <pradelle@java.net>2007-10-11 16:20:19 +0000
commitacd777f36024605ec098953fd17bc4a5da4651a9 (patch)
tree8a8b4a8989ace01d6d8643c46085752c3e4416d6 /test/net/java/sip/communicator/slick/fileaccess
parentcf383ac1027068ee5a3d5cde350cf1ac76d327f3 (diff)
downloadjitsi-acd777f36024605ec098953fd17bc4a5da4651a9.zip
jitsi-acd777f36024605ec098953fd17bc4a5da4651a9.tar.gz
jitsi-acd777f36024605ec098953fd17bc4a5da4651a9.tar.bz2
Moved FailSafeTransactions to the fileaccess bundle.
Diffstat (limited to 'test/net/java/sip/communicator/slick/fileaccess')
-rw-r--r--test/net/java/sip/communicator/slick/fileaccess/FileAccessServiceLick.java1
-rw-r--r--test/net/java/sip/communicator/slick/fileaccess/TestFailSafeTransaction.java256
2 files changed, 257 insertions, 0 deletions
diff --git a/test/net/java/sip/communicator/slick/fileaccess/FileAccessServiceLick.java b/test/net/java/sip/communicator/slick/fileaccess/FileAccessServiceLick.java
index f80a387..906c9e2 100644
--- a/test/net/java/sip/communicator/slick/fileaccess/FileAccessServiceLick.java
+++ b/test/net/java/sip/communicator/slick/fileaccess/FileAccessServiceLick.java
@@ -39,6 +39,7 @@ public class FileAccessServiceLick extends TestSuite implements BundleActivator
properties.put("service.pid", getName());
addTestSuite(TestFileAccessService.class);
+ addTestSuite(TestFailSafeTransaction.class);
bundleContext.registerService(getClass().getName(), this, properties);
logger.debug("Successfully registered " + getClass().getName());
diff --git a/test/net/java/sip/communicator/slick/fileaccess/TestFailSafeTransaction.java b/test/net/java/sip/communicator/slick/fileaccess/TestFailSafeTransaction.java
new file mode 100644
index 0000000..3008299
--- /dev/null
+++ b/test/net/java/sip/communicator/slick/fileaccess/TestFailSafeTransaction.java
@@ -0,0 +1,256 @@
+/*
+ * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+
+package net.java.sip.communicator.slick.fileaccess;
+
+import java.io.*;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+import net.java.sip.communicator.service.fileaccess.*;
+import junit.framework.*;
+
+/**
+ * Tests for the fail safe transactions
+ *
+ * @author Benoit Pradelle
+ */
+public class TestFailSafeTransaction
+ extends TestCase
+{
+ /**
+ * The Service that we will be testing.
+ */
+ private FileAccessService fileAccessService = null;
+
+ /**
+ * Test data to write in the original file
+ */
+ private static final String origData = "this is a test for the fail safe "
+ + "transaction ability in SIP Communicator";
+
+ /**
+ * Test data to add to the file
+ */
+ private static final String addedData = " which is the greatest IM client "
+ + "in the world !";
+
+ /**
+ * Test data to never write in the file
+ */
+ private static final String wrongData = "all the file is damaged now !";
+
+ /**
+ * The base for the name of the temp file
+ */
+ private static String tempName = "wzsxedcrfv" + System.currentTimeMillis();
+
+ /**
+ * Standart constructor.
+ *
+ * @param name
+ */
+ public TestFailSafeTransaction(String name)
+ {
+ super(name);
+ BundleContext context = FileAccessServiceLick.bc;
+ ServiceReference ref = context
+ .getServiceReference(FileAccessService.class.getName());
+ this.fileAccessService = (FileAccessService) context.getService(ref);
+ }
+ /**
+ * Tests the commit operation
+ */
+ public void testCommit() {
+ try {
+ // setup a temp file
+ File temp = File.createTempFile(tempName + "a", null);
+ FileOutputStream out = new FileOutputStream(temp);
+
+ out.write(origData.getBytes());
+
+ // write a modification during a transaction
+ FailSafeTransaction trans = this.fileAccessService
+ .createFailSafeTransaction(temp);
+ trans.beginTransaction();
+
+ out.write(addedData.getBytes());
+
+ trans.commit();
+
+ out.close();
+
+ // test if the two writes are ok
+ // file length
+ assertEquals("the file hasn't the right size after a commit",
+ temp.length(),
+ origData.length() + addedData.length());
+
+ FileInputStream in = new FileInputStream(temp);
+ byte[] buffer = new byte[in.available()];
+ in.read(buffer);
+ in.close();
+ String content = new String(buffer);
+
+ // file content
+ assertEquals("the file content isn't correct",
+ origData + addedData,
+ content);
+
+ temp.delete();
+ } catch (Exception e) {
+ Assert.fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Tests the rollback operation
+ */
+ public void testRollback() {
+ try {
+ // setup a temp file
+ File temp = File.createTempFile(tempName + "b", null);
+ FileOutputStream out = new FileOutputStream(temp);
+
+ out.write(origData.getBytes());
+
+ // write a modification during a transaction
+ FailSafeTransaction trans = this.fileAccessService
+ .createFailSafeTransaction(temp);
+ trans.beginTransaction();
+
+ out.write(wrongData.getBytes());
+
+ trans.rollback();
+
+ out.close();
+
+ // test if the two writes are ok
+ // file length
+ assertEquals("the file hasn't the right size after a commit",
+ temp.length(),
+ origData.length());
+
+ FileInputStream in = new FileInputStream(temp);
+ byte[] buffer = new byte[in.available()];
+ in.read(buffer);
+ in.close();
+ String content = new String(buffer);
+
+ // file content
+ assertEquals("the file content isn't correct",
+ origData,
+ content);
+
+ temp.delete();
+ } catch (Exception e) {
+ Assert.fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Tests if the file is commited when we start a new transaction
+ */
+ public void testCommitOnReOpen() {
+ try {
+ // setup a temp file
+ File temp = File.createTempFile(tempName + "c", null);
+ FileOutputStream out = new FileOutputStream(temp);
+
+ out.write(origData.getBytes());
+
+ // write a modification during a transaction
+ FailSafeTransaction trans = this.fileAccessService
+ .createFailSafeTransaction(temp);
+ trans.beginTransaction();
+
+ out.write(addedData.getBytes());
+
+ // this transaction isn't closed, it should commit the changes
+ trans.beginTransaction();
+
+ // just to be sure to clean everything
+ // the rollback must rollback nothing
+ trans.rollback();
+
+ out.close();
+
+ // test if the two writes are ok
+ // file length
+ assertEquals("the file hasn't the right size after a commit",
+ temp.length(),
+ origData.length() + addedData.length());
+
+ FileInputStream in = new FileInputStream(temp);
+ byte[] buffer = new byte[in.available()];
+ in.read(buffer);
+ in.close();
+ String content = new String(buffer);
+
+ // file content
+ assertEquals("the file content isn't correct",
+ origData + addedData,
+ content);
+
+ temp.delete();
+ } catch (Exception e) {
+ Assert.fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Tests if the file is rollback-ed if the transaction is never closed
+ */
+ public void testRollbackOnFailure() {
+ try {
+ // setup a temp file
+ File temp = File.createTempFile(tempName + "d", null);
+ FileOutputStream out = new FileOutputStream(temp);
+
+ out.write(origData.getBytes());
+
+ // write a modification during a transaction
+ FailSafeTransaction trans = this.fileAccessService
+ .createFailSafeTransaction(temp);
+ FailSafeTransaction trans2 = this.fileAccessService
+ .createFailSafeTransaction(temp);
+ trans.beginTransaction();
+
+ out.write(wrongData.getBytes());
+
+ // we suppose here that SC crashed without closing properly the
+ // transaction. When it restarts, the modification must have been
+ // rollback-ed
+
+ trans2.restoreFile();
+
+ out.close();
+
+ // test if the two writes are ok
+ // file length
+ assertEquals("the file hasn't the right size after a commit",
+ temp.length(),
+ origData.length());
+
+ FileInputStream in = new FileInputStream(temp);
+ byte[] buffer = new byte[in.available()];
+ in.read(buffer);
+ in.close();
+ String content = new String(buffer);
+
+ // file content
+ assertEquals("the file content isn't correct",
+ origData,
+ content);
+
+ temp.delete();
+ } catch (Exception e) {
+ Assert.fail(e.getMessage());
+ }
+ }
+}