使用 java.util.jar.Manifest 比较包含多个部分的清单文件的安全方法?

A safe way to compare Manifest files containing multiple sections using java.util.jar.Manifest?

我正在为基于 jar 生成存档的代码创建单元测试,我正在尝试使用以下代码将生成的清单文件与现有测试资源进行比较:

Manifest smActual = new Manifest(jar.getInputStream(   
         dpzip.getEntry(Constants.MANIFEST_LOCATION));

Manifest smExpected = new Manifest(
                new FileInputStream(expected.toFile()))

assertTrue(smActual.equals(smExpected));

问题是断言总是失败。即使我将 smExpected 文件与自身进行比较。

清单如下所示。请注意它有两个部分:

Manifest-Version: 1.0
Package-Name: it-project--normal
Package-Version: 0.1.0

Name: plugins/dependency-2.4.0.jar
Bundle-Version: 2.4.0
Bundle-SymbolicName: org.dependency

Name: plugins/anotherBundle.jar
Bundle-SymbolicName: org.anotherBundle
Bundle-Version: 1.0.0

我做了一些调试,但在下面的断言中遇到了失败:

         Attributes att1 = smExpected
           .getAttributes("plugins/dependency-2.4.0.jar");
         Attributes att2 = smActual
           .getAttributes("plugins/dependency-2.4.0.jar");
         assertTrue(att1.values().equals(att2.values()));

但它通过了:

assertThat(smActual.getMainAttributes(), equalTo(smExpected.getMainAttributes()));

我的环境是:

Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed  mode)
Linux ubuntu 3.13.0-74-generic #118-Ubuntu SMP Thu Dec 17 22:52:10 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

以下程序比较Manifest文件,检查您的Manifest文件或一些配置,或提供您的测试应用程序和测试zar文件来模拟问题。

public class 清单 123 {

    public static void main(String[] args) { 
            JarFile jar = null; 
            try { 
                    jar = new JarFile("plugin.jar"); 
            } catch (IOException e) { 
                    e.printStackTrace(); 
            } 
            ZipFile zipFile = null; 
            try { 
                    zipFile = new ZipFile("plugin.jar"); 
            } catch (IOException e) { 
                    e.printStackTrace(); 
            } 

            final ZipEntry manifestEntry = zipFile.getEntry("META-INF/MANIFEST.MF"); 

            Manifest smActual = null; 
            Manifest smExpected = null; 
            try { 
                    smActual = new Manifest(jar.getInputStream(manifestEntry)); 
                    smExpected = new Manifest(new FileInputStream("META-INF/MANIFEST.MF")); 
            } catch (IOException e) { 
                    e.printStackTrace(); 
            } 


            if(smActual.equals(smExpected)) { 
                    System.out.println("Yes Equal"); 
            } else { 
                    System.out.println("They are not equal"); 
            } 

    } 

}