Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Commit

Permalink
FileDifferenceProvider: calculate md5 + enable test case which was fa…
Browse files Browse the repository at this point in the history
…iling previously due to missing md5 check
  • Loading branch information
epeee committed May 28, 2017
1 parent 64a59b9 commit 616ae01
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.mockito.release.internal.comparison.file;

import org.mockito.release.internal.util.Md5;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -33,7 +35,9 @@ public CompareResult getDifference(File dirA, File dirB) {
onlyB.add(dirBFiles.get(j));
j++;
} else {
boolean sameMd5 = true;
String md5A = Md5.calculate(dirAFiles.get(i));
String md5B = Md5.calculate(dirBFiles.get(j));
boolean sameMd5 = md5A.equals(md5B);
// TODO md5 check

if (dirAFiles.get(i).length() == dirBFiles.get(j).length() && sameMd5) {
Expand Down
48 changes: 48 additions & 0 deletions src/main/groovy/org/mockito/release/internal/util/Md5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.mockito.release.internal.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
* Md5 utilities
*/
public class Md5 {

/**
* Calculates the md5 of a given file.
* @param file the file to calculate the md5 for
* @return the resulting md5
*/
public static String calculate(File file) {
InputStream is = null;
try {
MessageDigest md = MessageDigest.getInstance("md5");
is = new FileInputStream(file);

int bytesRead = 0;
byte[] data = new byte[1024];
while ((bytesRead = is.read(data)) != -1) {
md.update(data, 0, bytesRead);
}
return new BigInteger(1, md.digest()).toString(16);
} catch (java.io.IOException e) {
throw new RuntimeException("error while generating md5 for file " + file, e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("error while generating md5 for file " + file, e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.mockito.release.internal.comparison.file

import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Ignore
import spock.lang.Specification


Expand Down Expand Up @@ -104,7 +103,6 @@ class FileDifferenceProviderTest extends Specification {
result.bothButDifferent == [dirADifferentFile, dirBDifferentFile]
}

@Ignore
def "both but different (same length)"() {
given:
createSomeSameContent()
Expand All @@ -123,6 +121,19 @@ class FileDifferenceProviderTest extends Specification {
result.bothButDifferent == [dirADifferentFile, dirBDifferentFile]
}
def "same files and same content"() {
given:
createSomeSameContent()
when:
CompareResult result = new FileDifferenceProvider().getDifference(dirA, dirB);
then:
result.onlyA.isEmpty()
result.onlyB.isEmpty()
result.bothButDifferent.isEmpty()
}
private void createSomeSameContent() {
File dirAFile = new File(dirA, 'newFile')
dirAFile << "someContent"
Expand Down

0 comments on commit 616ae01

Please sign in to comment.