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

时间:2015-12-30 01:09:38

标签: java java-7 manifest.mf

我正在为基于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

1 个答案:

答案 0 :(得分:0)

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

public class Manifest123 {

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

    } 

}