0

I'm creating a unit test for a code that generates an archive based on a jar and I'm trying to compare the generated manifest file with an existing test resource using the code below:

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

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

assertTrue(smActual.equals(smExpected));

The problem is that the assert is always failing. Even if I compare the smExpected file with itself.

The manifest looks like the one below. Note it has two sections:

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

I did some debug and I'm getting a failure in the assertion bellow:

         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()));

But it passed with:

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

my environment is:

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
Cristiano
  • 1,414
  • 15
  • 22
  • That's very odd. I tried a small test, but I couldn't repro. I looked at the JDK source code, and I don't see anything funky in the `equals` implementation. [`Manifest`](http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/ab44843d5891/src/share/classes/java/util/jar/Manifest.java) compares the main `Attributes` and a `HashMap` of entries. [`Attributes`](http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/ab44843d5891/src/share/classes/java/util/jar/Attributes.java) is just a wrapper over another [HashMap], and it uses that for equality. Is there something else going on in your code? – Chris Nauroth Dec 30 '15 at 17:55
  • well, I'm using eclipse to run that code... I could note that when I use assertThat(smActual.getMainAttributes(), equalTo(smExpected.getMainAttributes())) it works... but comparing Attributes is working sometimes... – Cristiano Dec 30 '15 at 17:57

1 Answers1

0

Below program compares Manifest files, check your Manifest files or some configurations, or provide your test application and test zar file to simulate the issue.

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

    } 

}

Fairoz
  • 1,616
  • 13
  • 16
  • Thanks for the example. but you have used the manifestEntry of the zipFile in order to get the inputStream in the jar file. This have worked by luck? :) – Cristiano Jan 09 '16 at 14:23
  • I have no sure about the real reason but the problem is something related to how I've created the expected text file. I deleted the existent one then copied the generated manifest to test folder and renamed it to .txt. then it worked. :/ – Cristiano Jan 09 '16 at 14:25
  • Cristiano thanks for the info, if you could provide your test case that will be helpful to find the root cause. – Fairoz Jan 12 '16 at 04:20