|
From: <bob...@us...> - 2003-12-31 04:06:46
|
Update of /cvsroot/ebxmlms/ebxmlms/src_junit/hk/hku/cecid/phoenix/test/message/handler
In directory sc8-pr-cvs1:/tmp/cvs-serv2011/src_junit/hk/hku/cecid/phoenix/test/message/handler
Added Files:
Tag: b0931
FilePersistenceHandlerTest.java
Log Message:
Add PersistenceHandler interface as the customize Persistence interface
Add RepositoryPersistenceHandler and ObjectStorePersistenceHandler,
which is the default persistence Handler for Repository and Object Store
Add PersistenceManager which will probably replace the DirectoryManager,
so that the persistence will not bound to File only.
Note that we haven't changed the logic on Hermes to use the PersistenceHandler
Interface yet...
Add a src_junit to add junit test case to this directory.
add test cases on testing the default persistence handler base class
Change build.xml to run the test cases.
--- NEW FILE: FilePersistenceHandlerTest.java ---
/*
* Copyright(c) 2002 Center for E-Commerce Infrastructure Development, The
* University of Hong Kong (HKU). All Rights Reserved.
*
* This software is licensed under the Academic Free License Version 1.0
*
* Academic Free License
* Version 1.0
*
* This Academic Free License applies to any software and associated
* documentation (the "Software") whose owner (the "Licensor") has placed the
* statement "Licensed under the Academic Free License Version 1.0" immediately
* after the copyright notice that applies to the Software.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of the Software (1) to use, copy, modify, merge, publish, perform,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, and (2) under patent
* claims owned or controlled by the Licensor that are embodied in the Software
* as furnished by the Licensor, to make, use, sell and offer for sale the
* Software and derivative works thereof, subject to the following conditions:
*
* - Redistributions of the Software in source code form must retain all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers.
* - Redistributions of the Software in executable form must reproduce all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers in the documentation and/or
* other materials provided with the distribution.
* - Neither the names of Licensor, nor the names of any contributors to the
* Software, nor any of their trademarks or service marks, may be used to
* endorse or promote products derived from this Software without express
* prior written permission of the Licensor.
*
* DISCLAIMERS: LICENSOR WARRANTS THAT THE COPYRIGHT IN AND TO THE SOFTWARE IS
* OWNED BY THE LICENSOR OR THAT THE SOFTWARE IS DISTRIBUTED BY LICENSOR UNDER
* A VALID CURRENT LICENSE. EXCEPT AS EXPRESSLY STATED IN THE IMMEDIATELY
* PRECEDING SENTENCE, THE SOFTWARE IS PROVIDED BY THE LICENSOR, CONTRIBUTORS
* AND COPYRIGHT OWNERS "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* LICENSOR, CONTRIBUTORS OR COPYRIGHT OWNERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE.
*
* This license is Copyright (C) 2002 Lawrence E. Rosen. All rights reserved.
* Permission is hereby granted to copy and distribute this license without
* modification. This license may not be modified without the express written
* permission of its copyright owner.
*/
/* =====
*
* $Header: /cvsroot/ebxmlms/ebxmlms/src_junit/hk/hku/cecid/phoenix/test/message/handler/Attic/FilePersistenceHandlerTest.java,v 1.1.2.1 2003/12/31 04:06:43 bobpykoon Exp $
*
* Code authored by:
*
* pykoon [2003-12-30]
*
* Code reviewed by:
*
* username [YYYY-MM-DD]
*
* Remarks:
*
* =====
*/
package hk.hku.cecid.phoenix.test.message.handler;
import hk.hku.cecid.phoenix.message.handler.FilePersistenceHandler;
import javax.activation.DataSource;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
/**
* The testing on File Persistence Handler
* @author pykoon
* @version $Revision: 1.1.2.1 $
*/
public class FilePersistenceHandlerTest extends TestCase {
/**
The path used on testing the FilePersistenceHandler
*/
private static final String TEST_PATH = "test/testFile";
/**
A sample content used to test on the FilePersistenceHandler
*/
private static final int[] FILE_CONTENT = new int[]{0, 1, 2, 3};
/**
the dataSource used during testing
*/
private DataSource dataSource;
/**
The file persistence handler used on testing
*/
private FilePersistenceHandler handler;
/**
construct the FilePersistenceHandlerTest
@param name the name, see TestCase on JUnit
*/
public FilePersistenceHandlerTest(String name) {
super(name);
}
/**
get the test suite
@return Test the test suite
*/
public static Test suite() {
return new TestSuite(FilePersistenceHandlerTest.class);
}
/**
set up the test case.
@throws Exception thrown when error occur on start up
*/
protected void setUp() throws Exception {
File testPathFile = new File(TEST_PATH);
if (!testPathFile.exists()) {
testPathFile.mkdir();
}
handler = new FilePersistenceHandler(new File(TEST_PATH));
}
/**
method called when the test case is tear down
@throws Exception thrown when error occur on tear down
*/
protected void tearDown() throws Exception {
File testPathFile = new File(TEST_PATH);
if (dataSource != null) {
handler.removeObject(dataSource.getName());
}
if (testPathFile.exists() && !testPathFile.delete()) {
testPathFile.deleteOnExit();
}
}
/**
create teh Test Data Source.
@return the DataSource created
@throws Exception thrown when error occur on creating data source
*/
private DataSource createTestDataSource() throws Exception {
DataSource dataSource = handler.createNewObject();
writeContent(dataSource);
return dataSource;
}
/**
write the content on DataSource
@param dataSource the data source to write the content
@throws Exception thrown when error occur on writing data source
*/
private void writeContent(DataSource dataSource) throws Exception {
OutputStream ostream = dataSource.getOutputStream();
for (int i = 0; i < FILE_CONTENT.length; i++) {
ostream.write(FILE_CONTENT[i]);
}
ostream.close();
}
/**
test to create a datasource and then remove from handler
*/
public void testCreateAndRemove() {
String name = null;
try {
dataSource = createTestDataSource();
name = dataSource.getName();
assertTrue(handler.getObject(name) != null);
handler.removeObject(name);
assertTrue(handler.getObject(name) == null);
} catch (Exception e) {
fail(e.toString());
}
}
/**
test to create a data source, write data and then see if it
is read back the same data.
*/
public void testWriteAndRead() {
String name = null;
InputStream istream = null;
try {
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());
}
}
}
try {
handler.removeObject(name);
} catch (Exception e) {
fail(e.toString());
}
}
}
|