diff options
Diffstat (limited to 'sonar-duplications')
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)); |