From c8bc216c344945d29d01dab48b3425d5ad50383f Mon Sep 17 00:00:00 2001 From: JSpiner Date: Sun, 10 Oct 2021 23:42:18 +0900 Subject: [PATCH 1/5] Create failing test code. - `getDebugFileName` only checks whether '.js' is in the file name. So the 'john.jspiner' case throws an error. --- .../djn/jscodegen/CodeFileGeneratorTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 directjngine/src/test/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGeneratorTest.java diff --git a/directjngine/src/test/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGeneratorTest.java b/directjngine/src/test/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGeneratorTest.java new file mode 100644 index 0000000..0c3df85 --- /dev/null +++ b/directjngine/src/test/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGeneratorTest.java @@ -0,0 +1,34 @@ +package com.softwarementors.extjs.djn.jscodegen; + +import org.testng.annotations.Test; + +import java.lang.reflect.Method; + +import static org.testng.Assert.*; + +public class CodeFileGeneratorTest { + + private String getDebugFileName(String file) { + try { + Method method = CodeFileGenerator.class.getDeclaredMethod("getDebugFileName", String.class); + method.setAccessible(true); + Object result = method.invoke(null, file); + return (String) result; + } catch (Exception e) { + e.printStackTrace(); + return ""; + } + } + + @Test + public void getDebugFileNameTest() { + assertEquals( + "john.jspiner", + getDebugFileName("john.jspiner") + ); + assertEquals( + "app-debug.js", + getDebugFileName("app.js") + ); + } +} \ No newline at end of file From 790d4f6a47d2052aff7e8b15f246e8140446321c Mon Sep 17 00:00:00 2001 From: JSpiner Date: Sun, 10 Oct 2021 23:44:02 +0900 Subject: [PATCH 2/5] Fix `getDebugFileName` file regex logic. - The file extension must be at the end of the full path. --- .../extjs/djn/jscodegen/CodeFileGenerator.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/directjngine/src/main/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGenerator.java b/directjngine/src/main/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGenerator.java index e79c2f4..939dbd5 100644 --- a/directjngine/src/main/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGenerator.java +++ b/directjngine/src/main/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGenerator.java @@ -166,9 +166,13 @@ private static boolean fileContentEquals(File file, String code) throws IOExcept } return false; } - + private static String getDebugFileName( String file ) { - String result = file.replace( ".js", "-debug.js"); - return result; + int extensionIndex = file.lastIndexOf( ".js" ); + + if ( extensionIndex != -1 && file.endsWith(".js") ) { + return file.substring(0, extensionIndex) + "-debug.js"; + } + return file; } } From 9eb52237ad55c2e52c8875bf11bac240004368f7 Mon Sep 17 00:00:00 2001 From: JSpiner Date: Sun, 17 Oct 2021 17:12:59 +0900 Subject: [PATCH 3/5] Use `$` regex character instead of check is ends with file extension. - Thanks to @ataylor284 --- .../extjs/djn/jscodegen/CodeFileGenerator.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/directjngine/src/main/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGenerator.java b/directjngine/src/main/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGenerator.java index 939dbd5..6de0505 100644 --- a/directjngine/src/main/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGenerator.java +++ b/directjngine/src/main/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGenerator.java @@ -168,11 +168,6 @@ private static boolean fileContentEquals(File file, String code) throws IOExcept } private static String getDebugFileName( String file ) { - int extensionIndex = file.lastIndexOf( ".js" ); - - if ( extensionIndex != -1 && file.endsWith(".js") ) { - return file.substring(0, extensionIndex) + "-debug.js"; - } - return file; + return file.replaceFirst( ".js$" , "-debug.js" ); } } From 68d396981e8ed53491869938a65db60e95b7f74d Mon Sep 17 00:00:00 2001 From: JSpiner Date: Sun, 17 Oct 2021 17:17:46 +0900 Subject: [PATCH 4/5] Change `getDebugFileName` to protected function for testing. - Remove reflection test. - Thanks to @ataylor284 --- .../djn/jscodegen/CodeFileGenerator.java | 2 +- .../djn/jscodegen/CodeFileGeneratorTest.java | 20 +++---------------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/directjngine/src/main/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGenerator.java b/directjngine/src/main/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGenerator.java index 6de0505..d04fcc1 100644 --- a/directjngine/src/main/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGenerator.java +++ b/directjngine/src/main/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGenerator.java @@ -167,7 +167,7 @@ private static boolean fileContentEquals(File file, String code) throws IOExcept return false; } - private static String getDebugFileName( String file ) { + protected static String getDebugFileName( String file ) { return file.replaceFirst( ".js$" , "-debug.js" ); } } diff --git a/directjngine/src/test/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGeneratorTest.java b/directjngine/src/test/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGeneratorTest.java index 0c3df85..2da0975 100644 --- a/directjngine/src/test/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGeneratorTest.java +++ b/directjngine/src/test/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGeneratorTest.java @@ -2,33 +2,19 @@ import org.testng.annotations.Test; -import java.lang.reflect.Method; - -import static org.testng.Assert.*; +import static org.testng.Assert.assertEquals; public class CodeFileGeneratorTest { - private String getDebugFileName(String file) { - try { - Method method = CodeFileGenerator.class.getDeclaredMethod("getDebugFileName", String.class); - method.setAccessible(true); - Object result = method.invoke(null, file); - return (String) result; - } catch (Exception e) { - e.printStackTrace(); - return ""; - } - } - @Test public void getDebugFileNameTest() { assertEquals( "john.jspiner", - getDebugFileName("john.jspiner") + CodeFileGenerator.getDebugFileName("john.jspiner") ); assertEquals( "app-debug.js", - getDebugFileName("app.js") + CodeFileGenerator.getDebugFileName("app.js") ); } } \ No newline at end of file From d7a1d5cf8b405f42ecbeefa4931ce31565363fe6 Mon Sep 17 00:00:00 2001 From: JSpiner Date: Sun, 17 Oct 2021 17:17:08 +0900 Subject: [PATCH 5/5] Add license header to file. - And reformat code. - Thanks to @dbradicich --- .../djn/jscodegen/CodeFileGeneratorTest.java | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/directjngine/src/test/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGeneratorTest.java b/directjngine/src/test/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGeneratorTest.java index 2da0975..e875eed 100644 --- a/directjngine/src/test/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGeneratorTest.java +++ b/directjngine/src/test/java/com/softwarementors/extjs/djn/jscodegen/CodeFileGeneratorTest.java @@ -1,3 +1,27 @@ +/* + * Copyright 2018-present Sonatype, Inc. + * + * This file is part of DirectJNgine. + * + * DirectJNgine is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License. + * + * Commercial use is permitted to the extent that the code/component(s) + * do NOT become part of another Open Source or Commercially developed + * licensed development library or toolkit without explicit permission. + * + * DirectJNgine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with DirectJNgine. If not, see . + * + * This software uses the ExtJs library (http://extjs.com), which is + * distributed under the GPL v3 license (see http://extjs.com/license). + */ package com.softwarementors.extjs.djn.jscodegen; import org.testng.annotations.Test; @@ -8,13 +32,7 @@ public class CodeFileGeneratorTest { @Test public void getDebugFileNameTest() { - assertEquals( - "john.jspiner", - CodeFileGenerator.getDebugFileName("john.jspiner") - ); - assertEquals( - "app-debug.js", - CodeFileGenerator.getDebugFileName("app.js") - ); + assertEquals( "john.jspiner" , CodeFileGenerator.getDebugFileName( "john.jspiner" ) ); + assertEquals( "app-debug.js" , CodeFileGenerator.getDebugFileName( "app.js" ) ); } } \ No newline at end of file