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          // violation below 'Name 'br' must match pattern'
32          final BufferedReader br = new BufferedReader(new InputStreamReader(
33                  new FileInputStream(fileName), StandardCharsets.UTF_8));
34          try {
35          } finally {
36              br.close();
37          }
38      }
39  
40      void method2() throws Exception {
41          final String fileName = "Test";
42          // violation below 'Name 'br' must match pattern'
43          try (BufferedReader br = new BufferedReader(new InputStreamReader(
44                  new FileInputStream(fileName), StandardCharsets.UTF_8))) {
45          } finally {
46  
47          }
48      }
49  
50      void method3() throws Exception {
51          final String fileName = "Test";
52          try (final BufferedReader BR = new BufferedReader(new InputStreamReader(
53                  new FileInputStream(fileName), StandardCharsets.UTF_8))) {
54          } finally {
55  
56          }
57      }
58  
59      void method4() 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)) { // violation 'Name 'zf' must match pattern'
64          } finally {
65  
66          }
67      }
68  
69      void method5() throws Exception {
70          final String fileName = "Test";
71          try (BufferedReader BR = new BufferedReader(new InputStreamReader(
72                  new FileInputStream(fileName), StandardCharsets.UTF_8));
73               ZipFile ZF = new ZipFile(fileName)) {
74          } finally {
75  
76          }
77      }
78  
79      void method6() throws Exception {
80          String srcDir = System.getProperty("test.src", ".");
81          // violation below 'Name 'fis8859_1' must match pattern'
82          try (FileInputStream fis8859_1 = new FileInputStream(
83                  new File(srcDir, "Bug.properties"));
84               FileInputStream fisUTF8 = new FileInputStream(new File(srcDir, "Bug_Utf8.properties"));
85               // violation below 'Name 'isrutf8' must match pattern'
86               InputStreamReader isrutf8 = new InputStreamReader(fisUTF8, "UTF-8")) {
87              PropertyResourceBundle bundleUtf8 = new PropertyResourceBundle(isrutf8);
88              PropertyResourceBundle bundle = new PropertyResourceBundle(fis8859_1);
89              String[] arrayUtf8 = {"1", "2", "3"};
90              String[] array = {"key1", "key2"};
91              if (!Arrays.equals(arrayUtf8, array)) {
92                  throw new RuntimeException("Error message");
93              }
94          } catch (IOException ioe) {
95              throw new RuntimeException(ioe);
96          }
97      }
98  }