3

我正在尝试编写如下函数:

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;
}

而且我想编写一个实际上不会在文件系统上创建 JarFile 的单元测试。我试图寻找如何在内存中创建 File 对象,但没有找到类似的东西。有人有什么建议吗?

4

4 回答 4

4

使用 JarInputStream 代替 JarFile。为了测试,将 JarInputStream 连接到加载了内存中 jar 数据的 ByteArrayInputStream,并在正常操作中将其连接到来自文件的输入流。

于 2011-07-05T23:15:10.063 回答
1

File() 对象都存在于某个文件系统名称空间中。这为您提供了两个基本选择:

1)。如果您使用的是带有 tempfs 文件系统的操作系统,请在此处创建它。2)。使用 File.createTempFile() 并设置 delete-on-exit 属性。

创建子类的常用方法(“public MemoryFile extends File”...)不起作用,因为 File() 对象不包含进行实际 I/O 的方法,只是为了保存对象并执行一些文件系统操作。

于 2011-07-05T23:17:54.870 回答
0

您可以使用EasyMock创建类 JarFile 的模拟对象。对于模拟对象,您可以指定在测试中调用哪些方法以及返回值是什么,而无需在文件系统上实际创建 JAR 文件。

然后使用您的模拟 JarFile 实例调用您的 getTestXml() 方法。

它需要一些时间来适应它,但是你会发现它是值得的。

更新 给定的源代码无法编译,所以这是一个可编译的版本:

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;
  }
}

这是使用 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());
  }

}

注意附加库 JarFile 是一个类而不是一个接口,因此根据EasyMock 安装文档,您的类路径中应该有Objenesiscglib

于 2011-07-05T23:09:30.023 回答
0

您需要查看ByteArrayOutputStreamByteArrayInputStream。这些是 Java 中的内存流对象。使用这些,什么都不会写入磁盘。

于 2011-07-05T23:16:28.933 回答