
org.callbackparams.support.ResourceMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of callbackparams Show documentation
Show all versions of callbackparams Show documentation
CallbackParams is a JUnit-extension for writing parameterized tests.
It unlocks new innovative patterns that offer elegant solutions to many
obstacles that are traditionally associated with parameterized testing.
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.callbackparams.support;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
* @author Henrik Kaipe
*/
public class ResourceMap {
private static final String tmpResourceDirProperty =
"callbackparams.temp.resources";
private static final File tmpResourceDir = setupTmpResourceDir();
private Map map = new HashMap();
public void put(Class rebytedClass, byte[] byteCode) {
map.put(asClassPathResourcePath(rebytedClass), byteCode);
}
public InputStream getAsStream(String name) {
Object resource = map.get(name);
if (null == resource) {
return null;
} else if (resource instanceof byte[]) {
return new ByteArrayInputStream((byte[])resource);
} else if (resource instanceof URL) {
try {
return ((URL) resource).openStream();
} catch (IOException ex) {
throw new Error(ex);
}
} else {
throw new Error("Unexpected resource-type: " + resource.getClass());
}
}
public URL getURL(String name) {
Object resource = map.get(name);
if (resource instanceof byte[]) {
resource = createTmpResourceFile((byte[])resource);
map.put(name, resource);
}
return (URL) resource;
}
private static String asClassPathResourcePath(Class classResource) {
return classResource.getName().replace('.', '/') + ".class";
}
private static URL createTmpResourceFile(byte[] b) {
FileOutputStream fos = null;
try {
String prefix = "org.callbackparams$";
String suffix = "class$rebyted";
File resourceFile = null == tmpResourceDir
? File.createTempFile(prefix, suffix)
: File.createTempFile(prefix, suffix, tmpResourceDir);
resourceFile.deleteOnExit();
fos = new FileOutputStream(resourceFile);
fos.write(b);
return resourceFile.toURI().toURL();
} catch (IOException x) {
throw new Error(x);
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException ignore) {
/* Never mind ... */
}
}
}
}
private static File setupTmpResourceDir() {
String tmpResourceValue = System.getProperty(tmpResourceDirProperty);
if (null == tmpResourceValue) {
return null;
}
File resourceDir = new File(tmpResourceValue.trim());
if (resourceDir.isDirectory() || resourceDir.mkdirs()) {
return resourceDir;
} else {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy