📄 namedobjectstoretest.java
字号:
/*
* Copyright (C) 2003-2007 Funambol
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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.funambol.storage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import jmunit.framework.cldc10.AssertionFailedException;
import jmunit.framework.cldc10.TestCase;
import jmunit.framework.cldc10.TestSuite;
import com.funambol.util.Log;
/**
* Test case for the NamedObjectStore class
*/
public class NamedObjectStoreTest extends TestCase {
private NamedObjectStore os = null;
private static final String storename1 = "Test1";
private static final String storename2 = "Test2";
private TestContainer container;
public NamedObjectStoreTest() {
super(8, "NamedObjectStoreTest");
try {
RecordStore.deleteRecordStore(storename1);
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
try {
RecordStore.deleteRecordStore(storename2);
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
container = null;
Log.setLogLevel(Log.DEBUG);
}
/**
* Set up the tests
*/
public void setUp() {
container = new TestContainer(new TestClass("TestClass element"));
os = new NamedObjectStore();
}
/**
* Clean up resources
*/
public void tearDown() {
container = null;
try {
os.close();
}
catch (Exception e){
e.printStackTrace();
}
os = null;
}
/**
* Run the tests
*/
public void test(int testNumber) throws Throwable {
switch(testNumber) {
case 0: testCreate(); break;
case 1: testOpen(); break;
case 2: testStore(); break;
case 3: testRetrieveExistent(); break;
case 4: testRetrieveNotExistent(); break;
case 5: testStoreVector(); break;
case 6: testRetrieveVector(); break;
default: break;
}
}
/**
* Test #0: Create the two record store.
*/
public void testCreate() throws Exception {
assertTrue(os.create(storename1));
assertTrue(os.create(storename2));
}
/**
* Test #1: Tests on open.
*/
public void testOpen() throws Exception {
boolean ret = os.open(storename1);
assertTrue(ret);
ret = os.open(storename1);
assertFalse(ret);
ret = os.open(storename2);
assertTrue(ret);
os.close();
ret = os.open(storename2);
assertTrue(ret);
os.close();
try {
os.open("NonExiststentStore");
throw new Exception("Open cannot create new RecordStore");
}
catch (RecordStoreException e){
Log.info(
new StringBuffer("Okay, exception catched: ")
.append(e.getMessage()).toString());
}
}
/**
* Test #2: Test to store some serializable objects.
*/
private void testStore() throws Exception {
assertTrue(os.open(storename1));
for (int i=1; i<=3; i++) {
TestClass tc = new TestClass("TestClass_"+i);
os.store("Test"+i, tc);
TestClass tcCompare = (TestClass) os.retrieve("Test"+i, tc);
assertEquals(tc.toString(), tcCompare.toString());
}
os.close();
}
/**
* Test #3: Test to retrieve the same objects
*/
private void testRetrieveExistent() throws Exception {
os.open(storename1);
TestClass tc = new TestClass();
for (int i=1; i<=3; i++) {
os.retrieve("Test"+i, tc);
Log.info(tc.toString());
TestClass comparator = new TestClass("TestClass_"+i);
assertEquals(comparator.toString(), tc.toString());
}
os.close();
}
/**
* Test #4: Try to get a not existend object
*/
public void testRetrieveNotExistent() throws Exception {
os.open(storename1);
TestClass tc = new TestClass();
try {
os.retrieve("NonExistentTest", tc);
throw new Exception("Test failed");
}
catch (RecordStoreException e) {
Log.info(
new StringBuffer("Okay, RecordStoreException catched: ")
.append(e.getMessage()).toString() );
}
finally{
os.close();
}
}
/**
* Test #5: Test to store a Vector
*/
public void testStoreVector() throws Exception {
os.open(storename2);
os.store("TestContainer", container);
// os left open intentionally
}
/**
* Test #6: Test to retieve a Vector
*/
public void testRetrieveVector() throws Exception {
os.open(storename2);
TestContainer c = new TestContainer();
os.retrieve("TestContainer", c);
assertTrue(container.equals(c));
}
/**
* Test #7: Test to delete a Vector
*/
public void testDeleteVector() throws Exception {
os.open(storename2);
assertTrue(os.remove("TestContainer"));
os.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -