From f2ae38b1e8901b60662e9d2f6937bb204d04b2a1 Mon Sep 17 00:00:00 2001 From: Hiroyuki Sato Date: Sun, 12 May 2024 11:17:50 +0900 Subject: [PATCH 1/2] Checkstyle with checkstyle 9.3 --- build.gradle | 11 + config/checkstyle/README.md | 11 + config/checkstyle/checkstyle-suppressions.xml | 14 + config/checkstyle/checkstyle.xml | 368 ++++++++++++++++++ config/checkstyle/google_checks.xml | 364 +++++++++++++++++ gradle/libs.versions.toml | 2 + 6 files changed, 770 insertions(+) create mode 100644 config/checkstyle/README.md create mode 100644 config/checkstyle/checkstyle-suppressions.xml create mode 100644 config/checkstyle/checkstyle.xml create mode 100644 config/checkstyle/google_checks.xml diff --git a/build.gradle b/build.gradle index bacc8ac..0cdf5df 100644 --- a/build.gradle +++ b/build.gradle @@ -3,6 +3,7 @@ plugins { id "maven-publish" id "signing" id "org.embulk.embulk-plugins" version "0.7.0" + id "checkstyle" } repositories { mavenCentral() @@ -174,3 +175,13 @@ test { outputs.upToDateWhen { false } } } + +checkstyle { + toolVersion = libs.versions.checkstyle.get() + configFile = file("${rootProject.projectDir}/config/checkstyle/checkstyle.xml") + configProperties = [ + "org.checkstyle.google.suppressionfilter.config": file("${rootProject.projectDir}/config/checkstyle/checkstyle-suppressions.xml"), + ] + ignoreFailures = false + maxWarnings = 0 +} diff --git a/config/checkstyle/README.md b/config/checkstyle/README.md new file mode 100644 index 0000000..403c736 --- /dev/null +++ b/config/checkstyle/README.md @@ -0,0 +1,11 @@ +Checkstyle for the Embulk project +================================== + +* google_check.xml: Downloaded from: https://github.com/checkstyle/checkstyle/blob/checkstyle-9.3/src/main/resources/google_checks.xml + * Commit: 5c1903792f8432243cc8ae5cd79a03a004d3c09c +* checkstyle.xml: Customized from google_check.xml. + * To enable suppressions through checkstyle-suppressions.xml. + * To enable suppressions with @SuppressWarnings. + * To indent with 4-column spaces. + * To limit columns to 180 characters. + * To reject unused imports. diff --git a/config/checkstyle/checkstyle-suppressions.xml b/config/checkstyle/checkstyle-suppressions.xml new file mode 100644 index 0000000..aefd4d6 --- /dev/null +++ b/config/checkstyle/checkstyle-suppressions.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml new file mode 100644 index 0000000..c70b0c4 --- /dev/null +++ b/config/checkstyle/checkstyle.xml @@ -0,0 +1,368 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/config/checkstyle/google_checks.xml b/config/checkstyle/google_checks.xml new file mode 100644 index 0000000..515a844 --- /dev/null +++ b/config/checkstyle/google_checks.xml @@ -0,0 +1,364 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ab59dc9..47bab06 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -14,6 +14,8 @@ jackson = "2.16.2" junit4 = "4.13.2" +checkstyle = "9.3" + [libraries] embulk-core = { group = "org.embulk", name = "embulk-core", version.ref = "embulk-core" } From 714c3ccad6bfd33e7490f59446f5e0cc1002b028 Mon Sep 17 00:00:00 2001 From: Hiroyuki Sato Date: Sun, 12 May 2024 11:24:42 +0900 Subject: [PATCH 2/2] Fix checkstyle error. Reformat codes and Optimize imports --- .../output/CommandFileOutputPlugin.java | 98 ++++++++----------- .../output/TestCommandFileOutputPlugin.java | 18 ++-- 2 files changed, 47 insertions(+), 69 deletions(-) diff --git a/src/main/java/org/embulk/output/CommandFileOutputPlugin.java b/src/main/java/org/embulk/output/CommandFileOutputPlugin.java index f98293e..65a21d6 100644 --- a/src/main/java/org/embulk/output/CommandFileOutputPlugin.java +++ b/src/main/java/org/embulk/output/CommandFileOutputPlugin.java @@ -1,40 +1,37 @@ package org.embulk.output; -import java.util.List; +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.io.OutputStream; -import java.io.FilterOutputStream; -import java.io.IOException; -import org.slf4j.Logger; -import org.embulk.config.TaskReport; -import org.embulk.util.config.Config; +import java.util.List; import org.embulk.config.ConfigDiff; import org.embulk.config.ConfigSource; -import org.embulk.util.config.ConfigMapper; -import org.embulk.util.config.ConfigMapperFactory; -import org.embulk.util.config.Task; -import org.embulk.util.config.TaskMapper; +import org.embulk.config.TaskReport; import org.embulk.config.TaskSource; import org.embulk.spi.Buffer; import org.embulk.spi.FileOutputPlugin; import org.embulk.spi.TransactionalFileOutput; +import org.embulk.util.config.Config; +import org.embulk.util.config.ConfigMapper; +import org.embulk.util.config.ConfigMapperFactory; +import org.embulk.util.config.Task; +import org.embulk.util.config.TaskMapper; +import org.slf4j.Logger; public class CommandFileOutputPlugin - implements FileOutputPlugin -{ + implements FileOutputPlugin { public interface PluginTask - extends Task - { + extends Task { @Config("command") public String getCommand(); } @Override public ConfigDiff transaction(ConfigSource config, int taskCount, - FileOutputPlugin.Control control) - { + FileOutputPlugin.Control control) { final ConfigMapper configMapper = CONFIG_MAPPER_FACTORY.createConfigMapper(); final PluginTask task = configMapper.map(config, PluginTask.class); @@ -44,23 +41,20 @@ public ConfigDiff transaction(ConfigSource config, int taskCount, @Override public ConfigDiff resume(TaskSource taskSource, - int taskCount, - FileOutputPlugin.Control control) - { + int taskCount, + FileOutputPlugin.Control control) { control.run(taskSource); return CONFIG_MAPPER_FACTORY.newConfigDiff(); } @Override public void cleanup(TaskSource taskSource, - int taskCount, - List successTaskReports) - { + int taskCount, + List successTaskReports) { } @Override - public TransactionalFileOutput open(TaskSource taskSource, final int taskIndex) - { + public TransactionalFileOutput open(TaskSource taskSource, final int taskIndex) { final TaskMapper taskMapper = CONFIG_MAPPER_FACTORY.createTaskMapper(); final PluginTask task = taskMapper.map(taskSource, PluginTask.class); @@ -73,10 +67,9 @@ public TransactionalFileOutput open(TaskSource taskSource, final int taskIndex) return new PluginFileOutput(cmdline, taskIndex); } - static List buildShell() - { + static List buildShell() { String osName = System.getProperty("os.name"); - if(osName.indexOf("Windows") >= 0) { + if (osName.indexOf("Windows") >= 0) { return Collections.unmodifiableList(Arrays.asList("PowerShell.exe", "-Command")); } else { return Collections.unmodifiableList(Arrays.asList("sh", "-c")); @@ -84,25 +77,21 @@ static List buildShell() } private static class ProcessWaitOutputStream - extends FilterOutputStream - { + extends FilterOutputStream { private Process process; - public ProcessWaitOutputStream(OutputStream out, Process process) - { + public ProcessWaitOutputStream(OutputStream out, Process process) { super(out); this.process = process; } @Override - public void close() throws IOException - { + public void close() throws IOException { super.close(); waitFor(); } - private synchronized void waitFor() throws IOException - { + private synchronized void waitFor() throws IOException { if (process != null) { int code; try { @@ -113,38 +102,34 @@ private synchronized void waitFor() throws IOException process = null; if (code != 0) { throw new IOException(String.format( - "Command finished with non-zero exit code. Exit code is %d.", code)); + "Command finished with non-zero exit code. Exit code is %d.", code)); } } } } public class PluginFileOutput - implements TransactionalFileOutput - { + implements TransactionalFileOutput { private final List cmdline; private final int taskIndex; private int seqId; private ProcessWaitOutputStream currentProcess; - public PluginFileOutput(List cmdline, int taskIndex) - { + public PluginFileOutput(List cmdline, int taskIndex) { this.cmdline = cmdline; this.taskIndex = taskIndex; this.seqId = 0; this.currentProcess = null; } - public void nextFile() - { + public void nextFile() { closeCurrentProcess(); Process proc = startProcess(cmdline, taskIndex, seqId); currentProcess = new ProcessWaitOutputStream(proc.getOutputStream(), proc); seqId++; } - public void add(Buffer buffer) - { + public void add(Buffer buffer) { try { currentProcess.write(buffer.array(), buffer.offset(), buffer.limit()); } catch (IOException ex) { @@ -154,27 +139,22 @@ public void add(Buffer buffer) } } - public void finish() - { + public void finish() { closeCurrentProcess(); } - public void close() - { + public void close() { closeCurrentProcess(); } - public void abort() - { + public void abort() { } - public TaskReport commit() - { + public TaskReport commit() { return CONFIG_MAPPER_FACTORY.newTaskReport(); } - private void closeCurrentProcess() - { + private void closeCurrentProcess() { try { if (currentProcess != null) { currentProcess.close(); @@ -185,11 +165,10 @@ private void closeCurrentProcess() } } - private Process startProcess(List cmdline, int taskIndex, int seqId) - { + private Process startProcess(List cmdline, int taskIndex, int seqId) { ProcessBuilder builder = new ProcessBuilder(cmdline.toArray(new String[cmdline.size()])) - .redirectError(ProcessBuilder.Redirect.INHERIT) - .redirectOutput(ProcessBuilder.Redirect.INHERIT); + .redirectError(ProcessBuilder.Redirect.INHERIT) + .redirectOutput(ProcessBuilder.Redirect.INHERIT); builder.environment().put("INDEX", Integer.toString(taskIndex)); builder.environment().put("SEQID", Integer.toString(seqId)); // TODO transaction_time, etc @@ -201,6 +180,7 @@ private Process startProcess(List cmdline, int taskIndex, int seqId) } } } + private static final Logger logger = org.slf4j.LoggerFactory.getLogger(CommandFileOutputPlugin.class); private static final ConfigMapperFactory CONFIG_MAPPER_FACTORY = ConfigMapperFactory.builder().addDefaultModules().build(); diff --git a/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java b/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java index 4cfc9d9..86b2bc0 100644 --- a/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java +++ b/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java @@ -1,26 +1,24 @@ package org.embulk.output; +import static org.embulk.output.CommandFileOutputPlugin.buildShell; +import static org.junit.Assert.assertEquals; + import java.util.Arrays; import java.util.Collections; +import org.embulk.test.EmbulkTestRuntime; import org.junit.Rule; import org.junit.Test; -import org.embulk.test.EmbulkTestRuntime; -import static org.embulk.output.CommandFileOutputPlugin.buildShell; - -import static org.junit.Assert.assertEquals; -public class TestCommandFileOutputPlugin -{ +public class TestCommandFileOutputPlugin { @Rule public EmbulkTestRuntime runtime = new EmbulkTestRuntime(); @Test public void testShell() { if (System.getProperty("os.name").indexOf("Windows") >= 0) { - assertEquals(Collections.unmodifiableList(Arrays.asList("PowerShell.exe", "-Command")) -, buildShell()); - } - else { + assertEquals(Collections.unmodifiableList(Arrays.asList("PowerShell.exe", "-Command")), + buildShell()); + } else { assertEquals(Collections.unmodifiableList(Arrays.asList("sh", "-c")), buildShell()); } }