Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java.util.jar.Manifest比较包含多个部分的清单文件的安全方法?_Java_Java 7_Manifest.mf - Fatal编程技术网

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

使用java.util.jar.Manifest比较包含多个部分的清单文件的安全方法?,java,java-7,manifest.mf,Java,Java 7,Manifest.mf,我正在为基于jar生成存档的代码创建单元测试,并尝试使用以下代码将生成的清单文件与现有测试资源进行比较: Manifest smActual = new Manifest(jar.getInputStream( dpzip.getEntry(Constants.MANIFEST_LOCATION)); Manifest smExpected = new Manifest( new FileInputStream(expected.toFi

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

下面的程序比较清单文件,检查清单文件或某些配置,或者提供测试应用程序和测试zar文件来模拟问题

公共类声明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"); 
            } 

    } 

}

这很奇怪。我试过一个小测试,但我无法重新测试。我查看了JDK源代码,但在
equals
实现中没有看到任何奇怪的东西。比较条目的主
属性
哈希映射
。只是另一个[HashMap]上的包装器,它使用它来表示相等。你的代码中还有其他东西吗?嗯,我正在使用eclipse来运行代码。。。我可以注意到,当我使用assertThat(smActual.getMainAttributes()、equalTo(smExpected.getMainAttributes())时,它是有效的。。。但是比较属性有时是有效的…谢谢你的例子。但是您已经使用了zipFile的manifestEntry来获取jar文件中的inputStream。这是幸运的吗?:)我不确定真正的原因,但问题在于我是如何创建预期的文本文件的。我删除了现有的清单,然后将生成的清单复制到测试文件夹,并将其重命名为.txt。然后它成功了/Cristiano感谢您提供的信息,如果您能提供有助于找到根本原因的测试用例的话。