|
From: <bob...@us...> - 2004-01-16 04:26:38
|
Update of /cvsroot/ebxmlms/ebxmlms/src_junit/hk/hku/cecid/phoenix/test/message/handler
In directory sc8-pr-cvs1:/tmp/cvs-serv4371/src_junit/hk/hku/cecid/phoenix/test/message/handler
Modified Files:
FilePersistenceHandlerTest.java
Log Message:
Add BackupablePersistenceHandler and ArchivablePersistenceHandler
for backup and archive interfaces.
make persistence support in backup, restore and archive.
Index: FilePersistenceHandlerTest.java
===================================================================
RCS file: /cvsroot/ebxmlms/ebxmlms/src_junit/hk/hku/cecid/phoenix/test/message/handler/FilePersistenceHandlerTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** FilePersistenceHandlerTest.java 5 Jan 2004 09:57:43 -0000 1.2
--- FilePersistenceHandlerTest.java 16 Jan 2004 04:26:35 -0000 1.3
***************
*** 73,80 ****
--- 73,86 ----
import junit.framework.TestCase;
import junit.framework.TestSuite;
+
+ import java.io.ByteArrayInputStream;
+ import java.io.ByteArrayOutputStream;
import java.io.File;
+ import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
+ import java.util.zip.ZipInputStream;
+ import java.util.zip.ZipOutputStream;
/**
* The testing on File Persistence Handler
***************
*** 88,91 ****
--- 94,102 ----
*/
protected static final String TEST_PATH = "test/testFile";
+
+ /**
+ * The path used on testing the archive on FilePersistenceHandler
+ */
+ protected static final String TEST_ARCHIVE_PATH = "test/archive";
/**
***************
*** 125,129 ****
--- 136,146 ----
testPathFile.mkdir();
}
+ File testArchivePathFile = new File(TEST_ARCHIVE_PATH);
+ removeAllFiles(testArchivePathFile);
+ if (!testArchivePathFile.exists()) {
+ testArchivePathFile.mkdir();
+ }
handler = createFilePersistenceHandler();
+ handler.setBackupDirectory("temp");
}
***************
*** 143,146 ****
--- 160,168 ----
testPathFile.deleteOnExit();
}
+ File testArchivePathFile = new File(TEST_ARCHIVE_PATH);
+ removeAllFiles(testArchivePathFile);
+ if (testArchivePathFile.exists() && !testArchivePathFile.delete()) {
+ testArchivePathFile.deleteOnExit();
+ }
}
***************
*** 169,172 ****
--- 191,217 ----
return dataSource;
}
+
+ protected void verifyTestDataSource(String name) throws Exception {
+ DataSource gotDataSource = handler.getObject(name);
+ assertTrue(gotDataSource != null);
+ InputStream istream = null;
+ try {
+ istream = gotDataSource.getInputStream();
+ verifyTestDataSourceFromInputStream(istream);
+ } catch (IOException e) {
+ fail(e.toString());
+ } finally {
+ istream.close();
+ }
+ }
+
+ protected void verifyTestDataSourceFromInputStream(InputStream istream)
+ throws IOException {
+ for (int i = 0; i < FILE_CONTENT.length; i++) {
+ int read = istream.read();
+ assertTrue(read == FILE_CONTENT[i]);
+ }
+ assertTrue(-1 == istream.read());
+ }
/**
***************
*** 205,228 ****
public void testWriteAndRead() {
String name = null;
InputStream istream = null;
try {
DataSource dataSource = createTestDataSource();
name = dataSource.getName();
! DataSource gotDataSource = handler.getObject(name);
! assertTrue(gotDataSource != null);
! istream = gotDataSource.getInputStream();
! for (int i = 0; i < FILE_CONTENT.length; i++) {
! int read = istream.read();
! assertTrue(read == FILE_CONTENT[i]);
}
! assertTrue(-1 == istream.read());
} catch (Exception e) {
fail(e.toString());
} finally {
! if (istream != null) {
try {
! istream.close();
! } catch (IOException e) {
! fail("Fail on closing : " + e.toString());
}
}
--- 250,330 ----
public void testWriteAndRead() {
String name = null;
+ try {
+ DataSource dataSource = createTestDataSource();
+ name = dataSource.getName();
+ verifyTestDataSource(name);
+ } catch (Exception e) {
+ fail(e.toString());
+ }
+ try {
+ handler.removeObject(name);
+ } catch (Exception e) {
+ fail(e.toString());
+ }
+ }
+
+ /**
+ * test the backup and restore function.
+ */
+ public void testBackupRestore() {
+ String name = null;
InputStream istream = null;
+ InputStream bainstream = null;
+ ByteArrayOutputStream baostream = null;
+ ZipOutputStream zos = null;
+ /*
+ * Create a test data source and then badckup to a byte array
+ */
try {
DataSource dataSource = createTestDataSource();
name = dataSource.getName();
! baostream = new ByteArrayOutputStream();
! zos = new ZipOutputStream(baostream);
! handler.backup(zos);
! } catch (Exception e) {
! fail(e.toString());
! } finally {
! if (zos != null) {
! try {
! zos.close();
! } catch (Exception e) {
! fail(e.toString());
! }
}
! }
! byte[] result = baostream.toByteArray();
! try {
! baostream.close();
! } catch (Exception e) {
! fail(e.toString());
! }
! ZipInputStream zis = null;
! /*
! * remove the object and then restore the handler to see whether
! * it can be get back the object.
! */
! try {
! verifyTestDataSource(name);
! handler.removeObject(name);
! assertTrue(handler.getObject(name) == null);
! bainstream = new ByteArrayInputStream(result);
! zis = new ZipInputStream(bainstream);
! handler.restore(zis);
! assertTrue(handler.getObject(name) != null);
} catch (Exception e) {
fail(e.toString());
} finally {
! if (bainstream != null) {
try {
! bainstream.close();
! } catch (Exception e) {
! fail(e.toString());
! }
! }
! if (zis != null) {
! try {
! zis.close();
! } catch (Exception e) {
! fail(e.toString());
}
}
***************
*** 234,236 ****
--- 336,362 ----
}
}
+
+ public void testArchive() {
+ InputStream istream = null;
+ try {
+ DataSource dataSource = createTestDataSource();
+ File dir = new File(TEST_ARCHIVE_PATH);
+ String name = dataSource.getName();
+ handler.archive(name, dir);
+ File archivedFile = new File(dir, name);
+ assertTrue(archivedFile.isFile());
+ istream = new FileInputStream(archivedFile);
+ verifyTestDataSourceFromInputStream(istream);
+ } catch (Exception e) {
+ fail(e.toString());
+ } finally {
+ if (istream != null) {
+ try {
+ istream.close();
+ } catch (IOException e) {
+ fail(e.toString());
+ }
+ }
+ }
+ }
}
|