diff options
Diffstat (limited to 'src/test')
2 files changed, 104 insertions, 0 deletions
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/LocalFinalVariableNameCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/LocalFinalVariableNameCheckTest.java index e0ddbfec9..c9d6c0fdc 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/LocalFinalVariableNameCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/LocalFinalVariableNameCheckTest.java @@ -95,7 +95,26 @@ public class LocalFinalVariableNameCheckTest final int[] expected = { TokenTypes.VARIABLE_DEF, TokenTypes.PARAMETER_DEF, + TokenTypes.RESOURCE, }; assertArrayEquals(expected, actual); } + + @Test + public void testTryWithResources() throws Exception { + final DefaultConfiguration checkConfig = + createCheckConfig(LocalFinalVariableNameCheck.class); + checkConfig.addAttribute("format", "[A-Z]+"); + + final String pattern = "[A-Z]+"; + + final String[] expected = { + "23:30: " + getCheckMessage(MSG_INVALID_PATTERN, "br", pattern), + "33:29: " + getCheckMessage(MSG_INVALID_PATTERN, "br", pattern), + "53:22: " + getCheckMessage(MSG_INVALID_PATTERN, "zf", pattern), + "71:30: " + getCheckMessage(MSG_INVALID_PATTERN, "fis8859_1", pattern), + "73:32: " + getCheckMessage(MSG_INVALID_PATTERN, "isrutf8", pattern), + }; + verify(checkConfig, getPath("InputLocalFinalVariableNameTryResources.java"), expected); + } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/InputLocalFinalVariableNameTryResources.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/InputLocalFinalVariableNameTryResources.java new file mode 100644 index 000000000..f9ab90a51 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/InputLocalFinalVariableNameTryResources.java @@ -0,0 +1,85 @@ +package com.puppycrawl.tools.checkstyle.checks.naming.localfinalvariablename; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.PropertyResourceBundle; +import java.util.zip.ZipFile; + +/** + * Contains test cases regarding checking local + * final variable name in try-with-resources statement: + * + * @author Valeria Vasylieva + **/ +public class InputLocalFinalVariableNameTryResources { + + void method() throws Exception { + final String fileName = "Test"; + final BufferedReader br = new BufferedReader(new InputStreamReader( + new FileInputStream(fileName), StandardCharsets.UTF_8)); + try { + } finally { + br.close(); + } + } + + void method2() throws Exception { + final String fileName = "Test"; + try (BufferedReader br = new BufferedReader(new InputStreamReader( + new FileInputStream(fileName), StandardCharsets.UTF_8))) { + } finally { + + } + } + + void method3() throws Exception { + final String fileName = "Test"; + try (final BufferedReader BR = new BufferedReader(new InputStreamReader( + new FileInputStream(fileName), StandardCharsets.UTF_8))) { + } finally { + + } + } + + void method4() throws Exception { + final String fileName = "Test"; + try (BufferedReader BR = new BufferedReader(new InputStreamReader( + new FileInputStream(fileName), StandardCharsets.UTF_8)); + ZipFile zf = new ZipFile(fileName)) { + } finally { + + } + } + + void method5() throws Exception { + final String fileName = "Test"; + try (BufferedReader BR = new BufferedReader(new InputStreamReader( + new FileInputStream(fileName), StandardCharsets.UTF_8)); + ZipFile ZF = new ZipFile(fileName)) { + } finally { + + } + } + + void method6() throws Exception { + String srcDir = System.getProperty("test.src", "."); + try (FileInputStream fis8859_1 = new FileInputStream(new File(srcDir, "Bug6204853.properties")); + FileInputStream fisUTF8 = new FileInputStream(new File(srcDir, "Bug6204853_Utf8.properties")); + InputStreamReader isrutf8 = new InputStreamReader(fisUTF8, "UTF-8")) { + PropertyResourceBundle bundleUtf8 = new PropertyResourceBundle(isrutf8); + PropertyResourceBundle bundle = new PropertyResourceBundle(fis8859_1); + String[] arrayUtf8 = {"1", "2", "3"}; + String[] array = {"key1", "key2"}; + if (!Arrays.equals(arrayUtf8, array)) { + throw new RuntimeException("Error message"); + } + } catch (IOException ioe) { + throw new RuntimeException(ioe); + } + } +} |