View Javadoc
1   /*
2   LocalFinalVariableName
3   format = [A-Z]+
4   tokens = (default)VARIABLE_DEF, PARAMETER_DEF, RESOURCE
5   
6   
7   */
8   
9   package com.puppycrawl.tools.checkstyle.checks.naming.localfinalvariablename;
10  
11  import java.io.BufferedReader;
12  import java.io.File;
13  import java.io.FileInputStream;
14  import java.io.InputStreamReader;
15  import java.io.IOException;
16  import java.nio.charset.StandardCharsets;
17  import java.util.Arrays;
18  import java.util.PropertyResourceBundle;
19  import java.util.zip.ZipFile;
20  
21  /**
22   * Contains test cases regarding checking local
23   * final variable name in try-with-resources statement:
24   *
25   * @author Valeria Vasylieva
26   **/
27  public class InputLocalFinalVariableNameTryResources {
28  
29      void method() throws Exception {
30          final String fileName = "Test";
31          final BufferedReader br = new BufferedReader(new InputStreamReader( // violation
32                  new FileInputStream(fileName), StandardCharsets.UTF_8));
33          try {
34          } finally {
35              br.close();
36          }
37      }
38  
39      void method2() throws Exception {
40          final String fileName = "Test";
41          try (BufferedReader br = new BufferedReader(new InputStreamReader( // violation
42                  new FileInputStream(fileName), StandardCharsets.UTF_8))) {
43          } finally {
44  
45          }
46      }
47  
48      void method3() throws Exception {
49          final String fileName = "Test";
50          try (final BufferedReader BR = new BufferedReader(new InputStreamReader(
51                  new FileInputStream(fileName), StandardCharsets.UTF_8))) {
52          } finally {
53  
54          }
55      }
56  
57      void method4() throws Exception {
58          final String fileName = "Test";
59          try (BufferedReader BR = new BufferedReader(new InputStreamReader(
60                  new FileInputStream(fileName), StandardCharsets.UTF_8));
61               ZipFile zf = new ZipFile(fileName)) { // violation
62          } finally {
63  
64          }
65      }
66  
67      void method5() throws Exception {
68          final String fileName = "Test";
69          try (BufferedReader BR = new BufferedReader(new InputStreamReader(
70                  new FileInputStream(fileName), StandardCharsets.UTF_8));
71               ZipFile ZF = new ZipFile(fileName)) {
72          } finally {
73  
74          }
75      }
76  
77      void method6() throws Exception {
78          String srcDir = System.getProperty("test.src", ".");
79          try (FileInputStream fis8859_1 = new FileInputStream( // violation
80                  new File(srcDir, "Bug.properties"));
81               FileInputStream fisUTF8 = new FileInputStream(new File(srcDir, "Bug_Utf8.properties"));
82               InputStreamReader isrutf8 = new InputStreamReader(fisUTF8, "UTF-8")) { // violation
83              PropertyResourceBundle bundleUtf8 = new PropertyResourceBundle(isrutf8);
84              PropertyResourceBundle bundle = new PropertyResourceBundle(fis8859_1);
85              String[] arrayUtf8 = {"1", "2", "3"};
86              String[] array = {"key1", "key2"};
87              if (!Arrays.equals(arrayUtf8, array)) {
88                  throw new RuntimeException("Error message");
89              }
90          } catch (IOException ioe) {
91              throw new RuntimeException(ioe);
92          }
93      }
94  }