aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2011-09-27 12:09:18 +0400
committerEvgeny Mandrikov <mandrikov@gmail.com>2011-09-27 12:46:01 +0400
commit24b35ccec51c57c47b301893e46c96c92cfdfe6d (patch)
tree55c9f93fdc74f3de6973381c90ccdcffe7692b10
parent438d36393d14196284cb4fe11e570c5e4b860d44 (diff)
downloadsonarqube-24b35ccec51c57c47b301893e46c96c92cfdfe6d.tar.gz
sonarqube-24b35ccec51c57c47b301893e46c96c92cfdfe6d.zip
CPD: add support for Java 7 - try with resource
-rw-r--r--sonar-duplications/src/main/java/org/sonar/duplications/java/JavaStatementBuilder.java1
-rw-r--r--sonar-duplications/src/test/java/org/sonar/duplications/java/JavaStatementBuilderTest.java27
2 files changed, 28 insertions, 0 deletions
diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/java/JavaStatementBuilder.java b/sonar-duplications/src/main/java/org/sonar/duplications/java/JavaStatementBuilder.java
index 66917fa87d1..c382bfdbdec 100644
--- a/sonar-duplications/src/main/java/org/sonar/duplications/java/JavaStatementBuilder.java
+++ b/sonar-duplications/src/main/java/org/sonar/duplications/java/JavaStatementBuilder.java
@@ -43,6 +43,7 @@ public final class JavaStatementBuilder {
.statement(from("else"))
.statement(from("for"), bridge("(", ")"))
.statement(from("while"), bridge("(", ")"))
+ .statement(from("try"), bridge("(", ")"))
.statement(from("case"), to(";", "{", "}"), forgetLastToken())
.statement(from("default"), to(";", "{", "}"), forgetLastToken())
.statement(to(";", "{", "}"), forgetLastToken())
diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaStatementBuilderTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaStatementBuilderTest.java
index e264233f5bc..46410d05154 100644
--- a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaStatementBuilderTest.java
+++ b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaStatementBuilderTest.java
@@ -289,6 +289,33 @@ public class JavaStatementBuilderTest {
assertThat(statements.get(3).getValue(), is("onException()"));
}
+ /**
+ * Java 7.
+ */
+ @Test
+ public void shouldHandleTryWithResource() {
+ List<Statement> statements;
+ statements = chunk("try (FileInputStream in = new FileInputStream()) {}");
+ assertThat(statements.size(), is(2));
+ assertThat(statements.get(0).getValue(), is("try(FileInputStreamin=newFileInputStream())"));
+ assertThat(statements.get(1).getValue(), is("{}"));
+
+ statements = chunk("try (FileInputStream in = new FileInputStream(); FileOutputStream out = new FileOutputStream()) {}");
+ assertThat(statements.size(), is(2));
+ assertThat(statements.get(0).getValue(), is("try(FileInputStreamin=newFileInputStream();FileOutputStreamout=newFileOutputStream())"));
+ assertThat(statements.get(1).getValue(), is("{}"));
+
+ statements = chunk("try (FileInputStream in = new FileInputStream(); FileOutputStream out = new FileOutputStream();) {}");
+ assertThat(statements.size(), is(2));
+ assertThat(statements.get(0).getValue(), is("try(FileInputStreamin=newFileInputStream();FileOutputStreamout=newFileOutputStream();)"));
+ assertThat(statements.get(1).getValue(), is("{}"));
+
+ statements = chunk("try (FileInputStream in = new FileInputStream()) { something(); }");
+ assertThat(statements.size(), is(2));
+ assertThat(statements.get(0).getValue(), is("try(FileInputStreamin=newFileInputStream())"));
+ assertThat(statements.get(1).getValue(), is("something()"));
+ }
+
@Test
public void realExamples() {
assertThat(chunk(DuplicationsTestUtil.findFile("/java/MessageResources.java")).size(), greaterThan(0));