From dc14cc0586328ab9a7615ddf2ca4f404d3a4c861 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 10 Feb 2025 11:31:34 +0100 Subject: [PATCH] Modernize aaa-cli-jar Use more modern path resolution, as pointed out by modernizer. Change-Id: I43373319da84216e03c213a41fbfb3ef56761f34 Signed-off-by: Robert Varga --- aaa-cli-jar/pom.xml | 4 --- .../aaa/cli/jar/AbstractMain.java | 3 +- .../aaa/cli/jar/AbstractMainTest.java | 17 ++++++----- .../opendaylight/aaa/cli/jar/FilesUtils.java | 9 +++--- .../org/opendaylight/aaa/cli/jar/MainIT.java | 28 ++++++++----------- .../aaa/cli/jar/MainIntegrationTest.java | 22 ++++++++------- .../StandaloneCommandLineInterfaceTest.java | 8 +++--- 7 files changed, 43 insertions(+), 48 deletions(-) diff --git a/aaa-cli-jar/pom.xml b/aaa-cli-jar/pom.xml index d9c4db423..f2adb888a 100644 --- a/aaa-cli-jar/pom.xml +++ b/aaa-cli-jar/pom.xml @@ -20,10 +20,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html INTERNAL ODL :: aaa :: ${project.artifactId} jar - - false - - com.google.guava diff --git a/aaa-cli-jar/src/main/java/org/opendaylight/aaa/cli/jar/AbstractMain.java b/aaa-cli-jar/src/main/java/org/opendaylight/aaa/cli/jar/AbstractMain.java index 0b0957f0b..f316a4e84 100644 --- a/aaa-cli-jar/src/main/java/org/opendaylight/aaa/cli/jar/AbstractMain.java +++ b/aaa-cli-jar/src/main/java/org/opendaylight/aaa/cli/jar/AbstractMain.java @@ -9,6 +9,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.List; import joptsimple.OptionParser; import joptsimple.OptionSet; @@ -130,7 +131,7 @@ private static OptionParser getOptionParser() { parser.acceptsAll(List.of(OPTION_HELP, "?"), "Show help").forHelp(); parser.accepts(OPTION_DB_DIR, "databaseDirectory").withRequiredArg().ofType(File.class) - .defaultsTo(new File(".")).describedAs("path"); + .defaultsTo(Path.of(".").toFile()).describedAs("path"); parser.acceptsAll(List.of(OPTION_LIST_USERS, "listUsers"), "List all existing users"); parser.acceptsAll(List.of(OPTION_NEW_USER, "newUser"), "New user to create").withRequiredArg(); parser.acceptsAll(List.of(OPTION_CHANGE_USER, "changeUser"), "Existing user name to change password") diff --git a/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/AbstractMainTest.java b/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/AbstractMainTest.java index 1f1923a26..0fbc38093 100644 --- a/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/AbstractMainTest.java +++ b/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/AbstractMainTest.java @@ -16,6 +16,7 @@ import static org.mockito.Mockito.verify; import java.io.File; +import java.nio.file.Path; import java.util.List; import org.junit.Test; import org.opendaylight.yangtools.testutils.mockito.MoreAnswers; @@ -26,6 +27,8 @@ * @author Michael Vorburger.ch */ public class AbstractMainTest { + private static final File DOT_FILE = Path.of(".").toFile(); + private static AbstractMain mockedMain() { return mock(AbstractMain.class, MoreAnswers.realOrException()); } @@ -94,7 +97,7 @@ public void ifPasswordsThenEitherCreateOrChangeUser() throws Exception { public void changeUserAndPassword() throws Exception { AbstractMain main = spy(AbstractMain.class); assertEquals(0, main.parseArguments(new String[] { "-X", "-cu", "user", "-p", "newpass" })); - verify(main).setDbDirectory(new File(".")); + verify(main).setDbDirectory(DOT_FILE); verify(main).resetPasswords(List.of("user"), List.of("newpass")); } @@ -102,7 +105,7 @@ public void changeUserAndPassword() throws Exception { public void addNewUser() throws Exception { AbstractMain main = spy(AbstractMain.class); assertEquals(0, main.parseArguments(new String[] { "-X", "-nu", "user", "-p", "newpass" })); - verify(main).setDbDirectory(new File(".")); + verify(main).setDbDirectory(DOT_FILE); verify(main).addNewUsers(List.of("user"), List.of("newpass"), false); } @@ -110,7 +113,7 @@ public void addNewUser() throws Exception { public void addNewAdminUser() throws Exception { AbstractMain main = spy(AbstractMain.class); assertEquals(0, main.parseArguments(new String[] { "-X", "-nu", "user", "-p", "newpass", "-a" })); - verify(main).setDbDirectory(new File(".")); + verify(main).setDbDirectory(DOT_FILE); verify(main).addNewUsers(List.of("user"), List.of("newpass"), true); } @@ -119,7 +122,7 @@ public void changeUserAndPasswordInNonDefaultDatabase() throws Exception { AbstractMain main = spy(AbstractMain.class); assertEquals(0, main.parseArguments(new String[] { "-X", "--dbd", "altDbDir", "-cu", "user", "-p", "newpass" })); - verify(main).setDbDirectory(new File("altDbDir")); + verify(main).setDbDirectory(Path.of("altDbDir").toFile()); verify(main).resetPasswords(List.of("user"), List.of("newpass")); } @@ -147,7 +150,7 @@ public void cantAddAndChangeUsersTogether() throws Exception { public void deleteSingleUser() throws Exception { AbstractMain main = spy(AbstractMain.class); assertEquals(0, main.parseArguments(new String[] { "-X", "-du", "duser" })); - verify(main).setDbDirectory(new File(".")); + verify(main).setDbDirectory(DOT_FILE); verify(main).deleteUsers(List.of("duser")); } @@ -155,7 +158,7 @@ public void deleteSingleUser() throws Exception { public void verify1User1Password() throws Exception { AbstractMain main = spy(AbstractMain.class); assertEquals(0, main.parseArguments(new String[] { "-X", "-vu", "user", "-p", "pass" })); - verify(main).setDbDirectory(new File(".")); + verify(main).setDbDirectory(DOT_FILE); verify(main).verifyUsers(List.of("user"), List.of("pass")); } @@ -164,7 +167,7 @@ public void verify2User2Password() throws Exception { AbstractMain main = spy(AbstractMain.class); assertEquals(0, main.parseArguments(new String[] { "-X", "-vu", "user1", "-p", "pass1", "-vu", "user2", "-p", "pass2" })); - verify(main).setDbDirectory(new File(".")); + verify(main).setDbDirectory(DOT_FILE); verify(main).verifyUsers(List.of("user1", "user2"), List.of("pass1", "pass2")); } diff --git a/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/FilesUtils.java b/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/FilesUtils.java index 4e1c36d0a..d64695d97 100644 --- a/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/FilesUtils.java +++ b/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/FilesUtils.java @@ -11,7 +11,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Comparator; /** @@ -24,10 +23,10 @@ private FilesUtils() { // Hidden on purpose } - public static void delete(final String directory) throws IOException { - final Path path = Paths.get(directory); - if (Files.exists(path)) { - Files.walk(path).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); + public static void delete(final Path directory) throws IOException { + if (Files.exists(directory)) { + Files.walk(directory).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); } } + } diff --git a/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/MainIT.java b/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/MainIT.java index 1d86ca9ed..2c5ae9257 100644 --- a/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/MainIT.java +++ b/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/MainIT.java @@ -10,10 +10,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.junit.Test; @@ -27,8 +26,7 @@ * @author Michael Vorburger */ public class MainIT { - - private static final String DIR = "target/" + MainIT.class.getSimpleName(); + private static final Path DIR = Path.of("target", MainIT.class.getSimpleName()); @Test public void integrationTestBuildJAR() throws Exception { @@ -43,13 +41,13 @@ public void integrationTestBuildJAR() throws Exception { } private static void runFatJAR(final String... arguments) throws IOException, InterruptedException { - List fullArguments = new ArrayList<>(Arrays.asList( - findJava().getAbsolutePath(), + final var fullArguments = new ArrayList<>(List.of( + Path.of(System.getProperty("java.home")).resolve("bin").resolve("java").toAbsolutePath().toString(), "-jar", - findExecutableFatJAR().getAbsolutePath(), + findExecutableFatJAR().toAbsolutePath().toString(), "--dbd", - DIR)); - fullArguments.addAll(Arrays.asList(arguments)); + DIR.toString())); + fullArguments.addAll(List.of(arguments)); // If Output piping to LOG instead of inheritIO() etc. is needed, then // consider using https://github.com/vorburger/MariaDB4j/tree/master/mariaDB4j-core/src/main/java/ch/vorburger/exec @@ -61,18 +59,14 @@ private static void runFatJAR(final String... arguments) throws IOException, Int assertEquals(0, process.exitValue()); } - private static File findExecutableFatJAR() { - File targetDirectory = new File(".", "target"); - File[] jarFiles = targetDirectory.listFiles((dir, name) -> name.startsWith("aaa-cli-jar-") + private static Path findExecutableFatJAR() { + final var targetDirectory = Path.of(".", "target").toFile(); + final var jarFiles = targetDirectory.listFiles((dir, name) -> name.startsWith("aaa-cli-jar-") && name.endsWith(".jar") && !name.contains("-javadoc") && !name.contains("-sources")); assertNotNull("*jar-with-dependencies.jar files in " + targetDirectory, jarFiles); assertEquals("*jar-with-dependencies.jar files in " + targetDirectory, 1, jarFiles.length); - return jarFiles[0]; - } - - private static File findJava() { - return new File(new File(new File(System.getProperty("java.home")), "bin"), "java"); + return jarFiles[0].toPath(); } } diff --git a/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/MainIntegrationTest.java b/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/MainIntegrationTest.java index a9461bd8e..3cf1ade97 100644 --- a/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/MainIntegrationTest.java +++ b/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/MainIntegrationTest.java @@ -9,6 +9,7 @@ import static org.junit.Assert.assertEquals; +import java.nio.file.Path; import org.junit.Test; /** @@ -19,32 +20,33 @@ * @author Michael Vorburger.ch */ public class MainIntegrationTest { - private static final String DIR = "target/" + MainIntegrationTest.class.getSimpleName(); + private static final Path DIR = Path.of("target", MainIntegrationTest.class.getSimpleName()); + private static final String DIR_STR = DIR.toString(); @Test public void testCLI() throws Exception { FilesUtils.delete(DIR); assertEquals(0, new Main() - .parseArguments(new String[] { "-X", "--dbd", DIR, "-a", "-nu", "newuser", "-p", "firstpass" })); + .parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-a", "-nu", "newuser", "-p", "firstpass" })); assertEquals(0, new Main() - .parseArguments(new String[] { "-X", "--dbd", DIR, "-vu", "newuser", "-p", "firstpass" })); + .parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-vu", "newuser", "-p", "firstpass" })); assertEquals(-7, new Main() - .parseArguments(new String[] { "-X", "--dbd", DIR, "-vu", "newuser", "-p", "wrongpass" })); + .parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-vu", "newuser", "-p", "wrongpass" })); assertEquals(0, new Main() - .parseArguments(new String[] { "-X", "--dbd", DIR, "-cu", "newuser", "-p", "newpass" })); + .parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-cu", "newuser", "-p", "newpass" })); assertEquals(-7, new Main() - .parseArguments(new String[] { "-X", "--dbd", DIR, "-vu", "newuser", "-p", "firstpass" })); + .parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-vu", "newuser", "-p", "firstpass" })); assertEquals(0, new Main() - .parseArguments(new String[] { "-X", "--dbd", DIR, "-vu", "newuser", "-p", "newpass" })); + .parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-vu", "newuser", "-p", "newpass" })); assertEquals(0, new Main() - .parseArguments(new String[] { "-X", "--dbd", DIR, "-du", "newuser" })); + .parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-du", "newuser" })); assertEquals(-7, new Main() - .parseArguments(new String[] { "-X", "--dbd", DIR, "-vu", "newuser", "-p", "newpass" })); + .parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-vu", "newuser", "-p", "newpass" })); } @Test public void testMismatchUsersPasswords() throws Exception { - assertEquals(-3, new Main().parseArguments(new String[] { "-X", "--dbd", DIR, + assertEquals(-3, new Main().parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-vu", "newuser1", "-p", "newpass1", "-vu", "newuser2" /* No 2nd -p */ })); } diff --git a/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/StandaloneCommandLineInterfaceTest.java b/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/StandaloneCommandLineInterfaceTest.java index d6ae43307..7f071a337 100644 --- a/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/StandaloneCommandLineInterfaceTest.java +++ b/aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/StandaloneCommandLineInterfaceTest.java @@ -13,7 +13,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import java.io.File; +import java.nio.file.Path; import java.util.List; import org.junit.Before; import org.junit.Test; @@ -24,14 +24,14 @@ * @author Michael Vorburger.ch */ public class StandaloneCommandLineInterfaceTest { - private static final String DIR = "target/" + StandaloneCommandLineInterfaceTest.class.getSimpleName(); + private static final Path DIR = Path.of("target", StandaloneCommandLineInterfaceTest.class.getSimpleName()); - StandaloneCommandLineInterface cli; + private StandaloneCommandLineInterface cli; @Before public void before() throws Exception { FilesUtils.delete(DIR); - cli = new StandaloneCommandLineInterface(new File(DIR)); + cli = new StandaloneCommandLineInterface(DIR.toFile()); } @Test