]> source.dussan.org Git - sonarqube.git/commitdiff
CPD: add support for Java 7 - try with resource
authorEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 27 Sep 2011 08:09:18 +0000 (12:09 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 27 Sep 2011 08:59:48 +0000 (12:59 +0400)
sonar-duplications/src/main/java/org/sonar/duplications/java/JavaStatementBuilder.java
sonar-duplications/src/test/java/org/sonar/duplications/java/JavaStatementBuilderTest.java

index 66917fa87d16535ec491336190ef9e65b491645b..c382bfdbdecc6c2aba40f09c4c2c6cf53763f8e4 100644 (file)
@@ -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())
index e264233f5bc4e76f3648a5f1d3424c358ea6fdce..46410d0515494e4441fec0e4ea81ad57ca4d39c2 100644 (file)
@@ -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));