1
votes

I currently have a strange problem. I read a file using the following function, create an object from it and store the file content as a byte array in the object as well.

public static Iodd readFile(File file) throws IOException {
    if (!file.getName().endsWith(".xml"))
        throw new InvalidObjectException("Not a valid IODD XML file");

    String s = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
    Iodd i = new XmlMapper().readValue(s, Iodd.class);

    i.setFile(Files.readAllBytes(file.toPath()));

    return i;
}

In the file there is a CRC32 checksum. During the runtime of the program I can reproduce the checksum without problems using the java.util.zip.CRC32 class. But if I want to test the same way using a JUnit test now, I get a completely different checksum.

The test file you can find here. Please use the the XML-file without country code.

Am I missing something?

I have already checked if the byte arrays are different in the JUnit test and during runtime but everything matches. For testing purpose I created a MD5 checksum from the file content and check it with the MD5 from the JUnit test but they are equal.

Function for interest

This is the function which calculates the CRC32 checksum. The method removeStamp removes the crc-value from the stamp tag.

public static boolean isValidInternal(final byte[] data, final long checkCrc) {
    CRC32 c = new CRC32();
    c.update(removeStamp(data));

    return (c.getValue() == checkCrc);
}

Update 1

I add a new class with a main and add an output to the crc calculation method.

public class App {
    private final static String TESTFILE = "testfile-iodd-tv7105.xml";

    public static void main(String[] args) throws IOException {
        File f = new File(Iodd.class.getClassLoader().getResource(TESTFILE).getFile());
        Iodd i = Iodd.readFile(f);
        System.out.println(i.isValid());
    }
}

Output from the IntelliJ Run:
checkCrc: 1195433981
CRC32: 1195433981
true

Output from the Maven test command in IntelliJ:
checkCrc: 1195433981
CRC32: 2019633554

1
can you put a reproducer on github ?özkan pakdil
if you still want to have it in github, just let me know. iversion.informatik.htw-dresden.de/smartproductionsystems/…BlackRose01
I ran in my local from intellij unit test and wrote a main in iodd and ran looks like result is the same checkCrc:1195433981 c.getValue():557217516 from main and unit testsözkan pakdil
I see... I add my answer as Update 1BlackRose01

1 Answers

1
votes

Thank you. I hope the problem is solved now. mvn test runs without problems from IntelliJ and Bash.

The solution was that I set the following configurations for the maven-surefire-plugin in the pom.xml:

  • reuseForks: false
  • forkCount: 0
  • useSystemClassLoader: false (surpress warning that this option is not used when reuseForks option is false)
<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.1</version>
    <configuration>
        <reuseForks>false</reuseForks>
        <forkCount>0</forkCount>
        <useSystemClassLoader>false</useSystemClassLoader>
    </configuration>
</plugin>