From 15f867717bda5bdf13ca4b1a67c355015bd5a362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francesco=20Chicchiricc=C3=B2?= Date: Mon, 30 Dec 2024 14:46:14 +0100 Subject: [PATCH] Comply --- pom.xml | 1 + .../bundles/csvdir/database/FileSystem.java | 3 +- .../bundles/csvdir/database/FileToDB.java | 3 +- .../connid/bundles/csvdir/AbstractTest.java | 127 ++++++++---------- 4 files changed, 59 insertions(+), 75 deletions(-) diff --git a/pom.xml b/pom.xml index cadb108..5bfc656 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,7 @@ 2.7.4 ${project.build.directory}/test-source-path + 11 UTF-8 diff --git a/src/main/java/net/tirasa/connid/bundles/csvdir/database/FileSystem.java b/src/main/java/net/tirasa/connid/bundles/csvdir/database/FileSystem.java index 8baf486..8635787 100644 --- a/src/main/java/net/tirasa/connid/bundles/csvdir/database/FileSystem.java +++ b/src/main/java/net/tirasa/connid/bundles/csvdir/database/FileSystem.java @@ -17,6 +17,7 @@ import java.io.File; import java.io.FileFilter; +import java.nio.file.Path; import net.tirasa.connid.bundles.csvdir.CSVDirConfiguration; public class FileSystem { @@ -29,7 +30,7 @@ public class FileSystem { public FileSystem(final CSVDirConfiguration conf) { this.conf = conf; - this.sourcePath = new File(conf.getSourcePath()); + this.sourcePath = Path.of(conf.getSourcePath()).toFile(); this.fileFilter = new FileFilter() { @Override diff --git a/src/main/java/net/tirasa/connid/bundles/csvdir/database/FileToDB.java b/src/main/java/net/tirasa/connid/bundles/csvdir/database/FileToDB.java index 02cc212..5d853da 100644 --- a/src/main/java/net/tirasa/connid/bundles/csvdir/database/FileToDB.java +++ b/src/main/java/net/tirasa/connid/bundles/csvdir/database/FileToDB.java @@ -16,6 +16,7 @@ package net.tirasa.connid.bundles.csvdir.database; import java.io.File; +import java.nio.file.Path; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -50,7 +51,7 @@ public FileToDB(final CSVDirConnection conn) { public String createDbForCreate() { File file = fileSystem.getLastModifiedCsvFile(); if (file == null) { - file = new File(DEFAULT_PREFIX + Utilities.randomNumber() + ".csv"); + file = Path.of(DEFAULT_PREFIX + Utilities.randomNumber() + ".csv").toFile(); } return bindFileTable(file); } diff --git a/src/test/java/net/tirasa/connid/bundles/csvdir/AbstractTest.java b/src/test/java/net/tirasa/connid/bundles/csvdir/AbstractTest.java index f69761d..fb4431d 100644 --- a/src/test/java/net/tirasa/connid/bundles/csvdir/AbstractTest.java +++ b/src/test/java/net/tirasa/connid/bundles/csvdir/AbstractTest.java @@ -21,14 +21,14 @@ import java.io.BufferedWriter; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; import java.util.HashSet; import java.util.List; import java.util.Properties; @@ -39,8 +39,6 @@ import org.identityconnectors.framework.common.objects.Attribute; import org.identityconnectors.framework.common.objects.AttributeBuilder; import org.identityconnectors.framework.common.objects.Name; -import org.identityconnectors.framework.impl.api.APIConfigurationImpl; -import org.identityconnectors.framework.impl.api.local.JavaClassProperties; import org.identityconnectors.test.common.TestHelpers; import org.junit.jupiter.api.BeforeAll; @@ -56,7 +54,7 @@ public static void readTestProperties() { try { InputStream propStream = AbstractTest.class.getResourceAsStream("/test.properties"); props.load(propStream); - testSourceDir = new File(props.getProperty("testSourcePath")); + testSourceDir = Path.of(props.getProperty("testSourcePath")).toFile(); } catch (Exception e) { fail("Could not load test.properties: " + e.getMessage()); } @@ -64,7 +62,7 @@ public static void readTestProperties() { assertTrue(testSourceDir.exists() ? testSourceDir.isDirectory() : testSourceDir.mkdir()); } - protected CSVDirConfiguration createConfiguration(final String mask) { + protected static CSVDirConfiguration createConfiguration(final String mask) { // create the connector configuration.. final CSVDirConfiguration config = new CSVDirConfiguration(); config.setFileMask(mask); @@ -89,7 +87,7 @@ protected CSVDirConfiguration createConfiguration(final String mask) { return config; } - protected CSVDirConfiguration createMultiOCsConfiguration(final String mask) { + protected static CSVDirConfiguration createMultiOCsConfiguration(final String mask) { // create the connector configuration.. final CSVDirConfiguration config = new CSVDirConfiguration(); config.setFileMask(mask); @@ -117,44 +115,36 @@ protected CSVDirConfiguration createMultiOCsConfiguration(final String mask) { return config; } - protected ConnectorFacade createMultiOCsFacade(final String mask) { + protected static ConnectorFacade createMultiOCsFacade(final String mask) { final ConnectorFacadeFactory factory = ConnectorFacadeFactory.getInstance(); final CSVDirConfiguration cfg = createMultiOCsConfiguration(mask); final APIConfiguration impl = TestHelpers.createTestConfiguration(CSVDirConnector.class, cfg); - // TODO: remove the line below when using ConnId >= 1.4.0.1 - ((APIConfigurationImpl) impl). - setConfigurationProperties(JavaClassProperties.createConfigurationProperties(cfg)); return factory.newInstance(impl); } - protected ConnectorFacade createFacade(final String mask) { + protected static ConnectorFacade createFacade(final String mask) { final ConnectorFacadeFactory factory = ConnectorFacadeFactory.getInstance(); final CSVDirConfiguration cfg = createConfiguration(mask); final APIConfiguration impl = TestHelpers.createTestConfiguration(CSVDirConnector.class, cfg); - // TODO: remove the line below when using ConnId >= 1.4.0.1 - ((APIConfigurationImpl) impl). - setConfigurationProperties(JavaClassProperties.createConfigurationProperties(cfg)); return factory.newInstance(impl); } - protected File createFile(final String name, final List testAccounts) - throws IOException { - + protected static File createFile(final String name, final List testAccounts) throws IOException { final File file = File.createTempFile(name, ".csv", testSourceDir); file.deleteOnExit(); - final PrintWriter wrt = writeOutFileData(file); - writeOutEachUser(wrt, testAccounts); - wrt.close(); + try (PrintWriter wrt = writeOutFileData(file.toPath())) { + writeOutEachUser(wrt, testAccounts); + } return file; } - private void writeOutEachUser( + private static void writeOutEachUser( final PrintWriter wrt, final List testAccounts) { @@ -171,65 +161,56 @@ private void writeOutEachUser( } } - protected File createSampleFile(final String name, final int THOUSANDS) - throws IOException { - + protected static File createSampleFile(final String name, final int thousands) throws IOException { final File file = File.createTempFile(name, ".csv", testSourceDir); file.deleteOnExit(); - final PrintWriter wrt = writeOutFileData(file); - - if (IGNORE_HEADER) { - wrt.println(TestAccountsValue.HEADER.toLine( - TestAccountsValue.FIELD_DELIMITER, - TestAccountsValue.TEXT_QUALIFIER)); + try (PrintWriter wrt = writeOutFileData(file.toPath())) { + if (IGNORE_HEADER) { + wrt.println(TestAccountsValue.HEADER.toLine( + TestAccountsValue.FIELD_DELIMITER, + TestAccountsValue.TEXT_QUALIFIER)); + } + + for (int i = 0; i < thousands; i++) { + TestAccount account = new TestAccount( + "accountid" + i, + "firstname", + "lastname", + "email", + "changeNumber", + "password", + "no"); + + wrt.println(account.toLine( + TestAccountsValue.FIELD_DELIMITER, + TestAccountsValue.TEXT_QUALIFIER)); + } } - - for (int i = 0; i < THOUSANDS; i++) { - TestAccount account = new TestAccount( - "accountid" + i, - "firstname", - "lastname", - "email", - "changeNumber", - "password", - "no"); - - wrt.println(account.toLine( - TestAccountsValue.FIELD_DELIMITER, - TestAccountsValue.TEXT_QUALIFIER)); - } - wrt.close(); return file; } - private PrintWriter writeOutFileData(final File file) - throws FileNotFoundException { - return new PrintWriter(new OutputStreamWriter( - new FileOutputStream(file), getUTF8Charset())); - } - - private Charset getUTF8Charset() { - return Charset.forName("UTF-8"); + private static PrintWriter writeOutFileData(final Path file) throws IOException { + return new PrintWriter(new OutputStreamWriter(Files.newOutputStream(file), StandardCharsets.UTF_8)); } - protected File updateFile(final File file, final List testAccounts) - throws IOException { - - final BufferedWriter bufferedWriter = new BufferedWriter( - new FileWriter(file, true)); - for (TestAccount user : testAccounts) { - bufferedWriter.write(user.toLine(TestAccountsValue.FIELD_DELIMITER, - TestAccountsValue.TEXT_QUALIFIER)); - bufferedWriter.newLine(); + protected static File updateFile(final File file, final List testAccounts) throws IOException { + try (BufferedWriter bufferedWriter = Files.newBufferedWriter( + file.toPath(), StandardOpenOption.APPEND, StandardOpenOption.WRITE)) { + + for (TestAccount user : testAccounts) { + bufferedWriter.write(user.toLine( + TestAccountsValue.FIELD_DELIMITER, + TestAccountsValue.TEXT_QUALIFIER)); + bufferedWriter.newLine(); + } + bufferedWriter.flush(); } - bufferedWriter.flush(); - bufferedWriter.close(); return file; } - protected Set buildTestAttributes(final Name name) { - final Set attributes = new HashSet(); + protected static Set buildTestAttributes(final Name name) { + final Set attributes = new HashSet<>(); attributes.add(name); attributes.add(AttributeBuilder.build(TestAccountsValue.FIRSTNAME, "pmassi")); @@ -242,17 +223,17 @@ protected Set buildTestAttributes(final Name name) { return attributes; } - protected Set setAccountId(final Set attributes) { + protected static Set setAccountId(final Set attributes) { attributes.add(AttributeBuilder.build(TestAccountsValue.ACCOUNTID, "___mperro123")); return attributes; } - protected Set setEmployee(final Set attributes) { + protected static Set setEmployee(final Set attributes) { attributes.add(AttributeBuilder.build("OC", "_EMPLOYEE_")); return attributes; } - protected Set setManager(final Set attributes) { + protected static Set setManager(final Set attributes) { attributes.add(AttributeBuilder.build("OC", "_MANAGER_")); return attributes; }