?? persistencetests.java
字號:
/**
* Copyright (c) 2003-2005 Craig Setera
* All Rights Reserved.
* Licensed under the Eclipse Public License - v 1.0
* For more information see http://www.eclipse.org/legal/epl-v10.html
*/
package eclipseme.core.persistence;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import org.w3c.dom.Document;
import eclipseme.core.internal.persistence.XMLPersistenceProvider;
import eclipseme.core.internal.utils.XMLUtils;
import eclipseme.core.model.AbstractCoreTestCase;
import eclipseme.core.model.SymbolDefinitionSet;
import eclipseme.core.model.SymbolDefinitionSetRegistry;
import eclipseme.core.model.device.DeviceRegistry;
import eclipseme.core.model.device.IDevice;
/**
* Type description
* <p />
* Copyright (c) 2003-2005 Craig Setera<br>
* All Rights Reserved.<br>
* Licensed under the Eclipse Public License - v 1.0<p/>
* <br>
* $Revision: 1.2 $
* <br>
* $Date: 2006/05/15 21:31:45 $
* <br>
* @author Craig Setera
*/
public class PersistenceTests extends AbstractCoreTestCase {
public void testDevicePersistence() throws Exception {
IDevice device = getTestDevice();
IPersistenceProvider readProvider = getReadProvider("device", device);
IDevice readDevice = (IDevice) readProvider.loadPersistable("device");
assertNotNull("Read device should not be null", readDevice);
assertEquals("Devices are not equal", device, readDevice);
}
public void testDeviceRegistryPersistence1() throws Exception {
DeviceRegistry registry = DeviceRegistry.singleton;
IDevice[] devices = getTestDevices();
for (int i = 0; i < devices.length; i++) {
registry.addDevice(devices[i]);
}
registry.setDefaultDevice(devices[0]);
XMLPersistenceProvider pprovider = new XMLPersistenceProvider("registry");
registry.storeUsing(pprovider);
IPersistenceProvider readProvider = getReadProvider(pprovider);
registry.loadUsing(readProvider);
assertEquals("Expected equal device count", devices.length, registry.getDeviceCount());
IDevice beforeDevice = devices[0];
IDevice afterDevice =
registry.getDevice(beforeDevice.getGroupName(), beforeDevice.getName());
assertNotNull("After device should not be null", afterDevice);
assertEquals("Devices should be equals", beforeDevice, afterDevice);
IDevice defaultDevice = registry.getDefaultDevice();
assertEquals("Default device should be the same", devices[0], defaultDevice);
}
public void testDeviceRegistryPersistence2() throws Exception {
// Start fresh
DeviceRegistry registry = DeviceRegistry.singleton;
registry.clear();
registry.store();
registry.load();
// Verify the registry is empty
assertEquals("Registry should be empty", 0, registry.getDeviceCount());
// Put some stuff into the registry
IDevice[] devices = getTestDevices();
for (int i = 0; i < devices.length; i++) {
registry.addDevice(devices[i]);
}
// Store, clear and reload to see how the storage is working
registry.store();
registry.clear();
registry.load();
assertEquals("Expected equal device count", devices.length, registry.getDeviceCount());
IDevice beforeDevice = devices[0];
IDevice afterDevice =
registry.getDevice(beforeDevice.getGroupName(), beforeDevice.getName());
assertNotNull("After device should not be null", afterDevice);
assertEquals("Devices should be equals", beforeDevice, afterDevice);
}
public void testSymbolDefinitionSetRegistryPersistence()
throws Exception
{
// Define a few symbol definition sets
SymbolDefinitionSet set1 = new SymbolDefinitionSet();
set1.setName("set1");
set1.define("set1a");
set1.define("set1b");
set1.define("set1c");
SymbolDefinitionSet set2 = new SymbolDefinitionSet();
set2.setName("set2");
set2.define("set2a");
set2.define("set2b");
set2.define("set2c");
SymbolDefinitionSetRegistry registry = SymbolDefinitionSetRegistry.singleton;
registry.clear();
// Verify the registry is empty
assertEquals("Registry should be empty", 0, registry.getAllSetDefinitions().length);
registry.store();
registry.load();
// Verify the registry is empty
assertEquals("Registry should be empty", 0, registry.getAllSetDefinitions().length);
registry.addDefinitionSet(set1);
registry.addDefinitionSet(set2);
// Store, clear and reload to see how the storage is working
registry.store();
registry.clear();
registry.load();
assertEquals("Expected equal set count", 2, registry.getAllDefinitionSetNames().length);
SymbolDefinitionSet set = registry.getSymbolDefinitionSet("set1");
assertNotNull("Definition set1 should not be null", set);
assertEquals("Definitions set1 should be equal after persistence", set1, set);
set = registry.getSymbolDefinitionSet("set2");
assertNotNull("Definition set2 should not be null", set);
assertEquals("Definitions set2 should be equal after persistence", set2, set);
}
private IPersistenceProvider getReadProvider(String rootName, IPersistable persistable)
throws Exception
{
XMLPersistenceProvider pprovider = new XMLPersistenceProvider("testRoot");
pprovider.storePersistable(rootName, persistable);
return getReadProvider(pprovider);
}
private IPersistenceProvider getReadProvider(XMLPersistenceProvider pprovider)
throws Exception
{
StringWriter writer = new StringWriter();
Document document = pprovider.getDocument();
XMLUtils.writeDocument(writer, document);
String result = writer.toString();
assertNotNull("Persistence output should not be null", result);
ByteArrayInputStream bis = new ByteArrayInputStream(result.getBytes());
document = XMLUtils.readDocument(bis);
return new XMLPersistenceProvider(document);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -