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