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