How to create an in-memory JarFile?
I'm trying to write a function like:
public Map<String, Document> getTestXml(JarFile jarFile) {
Map<String, Document> result = Maps.newHashMap();
Enumeration<JarEntry> jarEntries = jarFile.getEntries();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = jarEntries.nextElement();
String name = jarEntry.getName();
if (name.endsWith(".class") && !name.contains("$")) {
String testClassName = name.replace(".class", "").replace("/", ".");
String testXmlFilename = "TEST-" + testClassName + ".xml";
InputStream testXmlInputStream = testJarFile.getInputStream(
testJarFile.getJarEntry(testXmlFilename));
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document testXmlDocument = documentBuilder.parse(testXmlInputStream);
result.put(testClassName, testXmlDocument);
}
}
return result;
}
And I would like to write a unit test that doesn't actually create a JarFile on the file system. I've tried to look for how t开发者_开发技巧o create a File object in memory, but haven't found anything like that. Anyone have any suggestions?
Instead of a JarFile, use a JarInputStream. For testing, hook the JarInputStream up to a ByteArrayInputStream loaded with in-memory jar data, and in normal operation hook it up to the input stream from a file.
File() objects all live within some file system name space. Which gives you two basic choices:
1). If you're using an O/S with a tempfs file system, create it there. 2). Use File.createTempFile() and set the delete-on-exit attribute.
The usual approach of creating a sub-class ("public MemoryFile extends File" ...) doesn't work because a File() object doesn't contain the methods for doing actual I/O, just for holding the name of the object and doing a few file system operations.
You need to look at ByteArrayOutputStream and ByteArrayInputStream. Those are the in memory stream objects in Java. Use those and nothing will get written to disk.
You can use EasyMock to create a mock object of class JarFile. For the mock object you specify which methods are called in the test and what the return values are without the need to actually create a JAR file on the file system.
Then call your getTestXml() method with your mock JarFile instance.
It needs some time to get used to it, but then you will see it's worth the effort.
Update The given source code doesn't compile, so here is a compilable version:
public class JarFileUser {
public Map<String, Document> getTestXml(JarFile jarFile) throws IOException, ParserConfigurationException, SAXException {
Map<String, Document> result = new HashMap<String, Document>();
Enumeration<JarEntry> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = jarEntries.nextElement();
String name = jarEntry.getName();
if (name.endsWith(".class") && !name.contains("$")) {
String testClassName = name.replace(".class", "").replace("/", ".");
String testXmlFilename = "TEST-" + testClassName + ".xml";
InputStream testXmlInputStream = jarFile.getInputStream(jarFile.getJarEntry(testXmlFilename));
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document testXmlDocument = documentBuilder.parse(testXmlInputStream);
result.put(testClassName, testXmlDocument);
}
}
return result;
}
}
Here is a test with EasyMock:
public class JarFileUserTest {
private JarFile mockJarFile;
private Enumeration<JarEntry> mockJarEntries;
private JarFileUser jarFileUser;
private JarEntry first;
private JarEntry second;
private JarEntry firstXml;
@Before
public void setUp() throws Exception {
jarFileUser = new JarFileUser();
// Create a mock for the JarFile parameter
mockJarFile = createMock(JarFile.class);
// User Vector to provide an Enumeration of JarEntry-Instances
Vector<JarEntry> entries = new Vector<JarEntry>();
first = createMock(JarEntry.class);
second = createMock(JarEntry.class);
entries.add(first);
entries.add(second);
expect(first.getName()).andReturn("mocktest.JarFileUser.class");
expect(second.getName()).andReturn("mocktest.Ignore$Me.class");
mockJarEntries = entries.elements();
expect(mockJarFile.entries()).andReturn(mockJarEntries);
// JarEntry for the XML file
firstXml = createMock(JarEntry.class);
expect(mockJarFile.getJarEntry("TEST-mocktest.JarFileUser.xml")).andReturn(firstXml);
// XML contents
ByteArrayInputStream is = new ByteArrayInputStream("<test>This is a test.</test>".getBytes("UTF-8"));
expect(mockJarFile.getInputStream(firstXml)).andReturn(is);
replay(mockJarFile);
replay(first);
replay(second);
replay(firstXml);
}
@Test
public void testGetTestXml() throws IOException, ParserConfigurationException, SAXException {
Map<String, Document> map = jarFileUser.getTestXml(mockJarFile);
verify(mockJarFile);
verify(first);
verify(second);
verify(firstXml);
assertEquals(1, map.size());
Document doc = map.get("mocktest.JarFileUser");
assertNotNull(doc);
final Element root = (Element) doc.getDocumentElement();
assertNotNull(root);
assertEquals("test", root.getNodeName());
assertEquals("This is a test.", root.getTextContent());
}
}
Note on additional libraries JarFile is a class and not an interface so according to the EasyMock installation docs you should have Objenesis and cglib in your classpath.
精彩评论