Skip to content

Commit

Permalink
Fail if duplicate features are detected. Closes cucumber#165.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Feb 6, 2012
1 parent 8a9ede2 commit 894cf1c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## In Git

* [Core] Fail if duplicate features are detected ([#165](https://github.com/cucumber/cucumber-jvm/issues/165) Aslak Hellesøy)

## [1.0.0.RC14](https://github.com/cucumber/cucumber-jvm/compare/v1.0.0.RC13...v1.0.0.RC14)

* [Core] HTML formatter produces invalid page if no features ([#191](https://github.com/cucumber/cucumber-jvm/issues/191) Paolo Ambrosio)
Expand Down
26 changes: 26 additions & 0 deletions core/src/main/java/cucumber/runtime/FeatureBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,29 @@

import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FeatureBuilder implements Formatter {
private static final Charset UTF8 = Charset.forName("UTF-8");
private final List<CucumberFeature> cucumberFeatures;
private final MessageDigest md5;
private CucumberFeature currentCucumberFeature;
private String uri;
private Map<String, String> pathsByChecksum = new HashMap<String, String>();

public FeatureBuilder(List<CucumberFeature> cucumberFeatures) {
this.cucumberFeatures = cucumberFeatures;
try {
this.md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new CucumberException(e);
}
}

@Override
Expand Down Expand Up @@ -86,6 +100,14 @@ public void parse(Resource resource, List<Object> filters) {
}
Parser parser = new Parser(formatter);
String gherkin = read(resource);

String checksum = checksum(gherkin);
String path = pathsByChecksum.get(checksum);
if(path != null) {
throw new CucumberException(String.format("Found the same source in %s and %s", path, resource.getPath()));
}
pathsByChecksum.put(checksum, resource.getPath());

parser.parse(gherkin, resource.getPath(), 0);
I18n i18n = parser.getI18nLanguage();
if (currentCucumberFeature != null) {
Expand All @@ -95,6 +117,10 @@ public void parse(Resource resource, List<Object> filters) {
}
}

private String checksum(String gherkin) {
return new BigInteger(1, md5.digest(gherkin.getBytes(UTF8))).toString(16);
}

private String read(Resource resource) {
try {
return FixJava.readReader(new InputStreamReader(resource.getInputStream(), "UTF-8"));
Expand Down
30 changes: 30 additions & 0 deletions core/src/test/java/cucumber/runtime/FeatureBuilderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cucumber.runtime;

import cucumber.io.Resource;
import cucumber.runtime.model.CucumberFeature;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.util.Collections.emptyList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class FeatureBuilderTest {
@Test(expected = CucumberException.class)
public void detects_duplicate_features() throws IOException {
List<CucumberFeature> fearures = new ArrayList<CucumberFeature>();
FeatureBuilder builder = new FeatureBuilder(fearures);
Resource resource = mock(Resource.class);
when(resource.getPath()).thenReturn("foo.feature");
ByteArrayInputStream firstFeature = new ByteArrayInputStream("Feature: foo".getBytes("UTF-8"));
ByteArrayInputStream secondFeature = new ByteArrayInputStream("Feature: foo".getBytes("UTF-8"));
when(resource.getInputStream()).thenReturn(firstFeature, secondFeature);
builder.parse(resource, emptyList());
builder.parse(resource, emptyList());
}

}

0 comments on commit 894cf1c

Please sign in to comment.