super.setUp();
}
public void testSerialize() throws Exception
{
final ResourceLoader resourceLoader = new ClassloaderResourceLoader();
final ResourceManager manager = new ResourceManager();
manager.registerDefaults();
// Test failure - null key
try
{
resourceLoader.serialize(null, null);
fail("Serialization of a null key should throw an exception");
}
catch (NullPointerException npe)
{
// success
}
// Test failure - not a Classloader resource key
try
{
final File tempFile = File.createTempFile("junit-test", ".tmp");
final ResourceKey tempKey = manager.createKey(tempFile);
resourceLoader.serialize(null, tempKey);
fail("The Classloader Resource Loader should fail when handling a non-classloader resource key");
}
catch(IllegalArgumentException iae)
{
// success
}
// Create key
final String key1source = "res://org/pentaho/reporting/libraries/resourceloader/test1.properties";
final ResourceKey key1 = manager.createKey(key1source);
assertNotNull(key1);
// Serialize the key
final String serKey1 = resourceLoader.serialize(null, key1);
assertNotNull("The returned key should not be null", serKey1); //$NON-NLS-1$
assertTrue("Serialized verison does not start with the correct header", serKey1.startsWith(STRING_SERIALIZATION_PREFIX));
assertTrue("Serialized version does not contain the correct schema information", serKey1.startsWith(DESERIALIZE_PREFIX));
assertTrue("Serialized version should contain the identifier intact", serKey1.endsWith(key1.getIdentifier().toString()));
// Serialize a key created from a derived key
final String key2source = "test2.properties";
final ResourceKey key2 = manager.deriveKey(key1, key2source);
assertNotNull(key2);
final String serKey2 = resourceLoader.serialize(null, key2);
assertNotNull("The returned key should not be null", serKey2); //$NON-NLS-1$
assertTrue("Serialized verison does not start with the correct header", serKey2.startsWith(STRING_SERIALIZATION_PREFIX));
assertTrue("Serialized version does not contain the correct schema information", serKey2.startsWith(DESERIALIZE_PREFIX));
assertTrue("Serialized version should contain the identifier intact", serKey2.endsWith(";res://org/pentaho/reporting/libraries/resourceloader/test2.properties"));
// Serialize a key with factory parameters
final Map<ParameterKey, Object> factoryParams = new HashMap<ParameterKey, Object>();
factoryParams.put(new FactoryParameterKey("this"), "that");
factoryParams.put(new FactoryParameterKey("null"), null);
final ResourceKey key3 = manager.createKey(key1source, factoryParams);
assertNotNull(key3);
final String serKey3 = resourceLoader.serialize(null, key3);
assertEquals("resourcekey:org.pentaho.reporting.libraries.resourceloader" +
".loader.resource.ClassloaderResourceLoader;" +
"res://org/pentaho/reporting/libraries/resourceloader/" +
"test1.properties;\"\"\"f:this=that\"\":\"\"f:null=\"\"\"", serKey3);