* Tests the serialization of File based resource keys
*/
public void testSerialize() throws Exception
{
final FileResourceLoader fileResourceLoader = new FileResourceLoader();
final ResourceManager manager = new ResourceManager();
manager.registerDefaults();
ResourceKey key = null;
Map<ParameterKey, Object> factoryParameters = new HashMap<ParameterKey, Object>();
String serializedVersion = null;
// Test with null parameter
try
{
serializedVersion = fileResourceLoader.serialize(null, key);
fail("Serialization with a null paramter should throw a NullPointerException"); //$NON-NLS-1$
}
catch (NullPointerException npe)
{
// success
}
// Test with a resource key instead of a file key
try
{
key = manager.createKey("res://org/pentaho/reporting/libraries/resourceloader/test1.properties"); //$NON-NLS-1$
serializedVersion = fileResourceLoader.serialize(key, key);
fail("The resource key should not handles by the file resource loader"); //$NON-NLS-1$
}
catch (IllegalArgumentException iae)
{
// success
}
// Create a key from the temp file
key = manager.createKey(tempFile);
serializedVersion = fileResourceLoader.serialize(key, key);
assertNotNull("The returned key should not be null", key); //$NON-NLS-1$
assertTrue("Serialized verison does not start with the correct header", serializedVersion //$NON-NLS-1$
.startsWith(STRING_SERIALIZATION_PREFIX));
assertTrue("Serialized version does not contain the correct schema information", serializedVersion //$NON-NLS-1$
.startsWith(STRING_SERIALIZATION_PREFIX + fileResourceLoader.getClass().getName() + ';'));
assertTrue("Serialized version should contain the filename", serializedVersion.endsWith(tempFile.getName())); //$NON-NLS-1$
// Create a key as a relative path from the above key
key = manager.deriveKey(key, tempRelativeFilename);
assertNotNull(key);
serializedVersion = fileResourceLoader.serialize(key, key);
assertNotNull(serializedVersion);
assertTrue("Serialized verison does not start with the correct header", serializedVersion //$NON-NLS-1$
.startsWith(STRING_SERIALIZATION_PREFIX));
assertTrue("Serialized version does not contain the correct schema information", serializedVersion //$NON-NLS-1$
.startsWith(STRING_SERIALIZATION_PREFIX + fileResourceLoader.getClass().getName() + ';'));
assertTrue(
"Serialized version should contain the filename", serializedVersion.endsWith(tempSubFile.getCanonicalPath())); //$NON-NLS-1$
// Create a key with factory parameters
factoryParameters.put(new FactoryParameterKey("this"), "that");
factoryParameters.put(new FactoryParameterKey("null"), null);
key = manager.createKey(tempFile, factoryParameters);
serializedVersion = fileResourceLoader.serialize(key, key);
assertNotNull("The returned key should not be null", key); //$NON-NLS-1$
assertTrue("Serialized verison does not start with the correct header", serializedVersion //$NON-NLS-1$
.startsWith(STRING_SERIALIZATION_PREFIX));