From 341ba349d3cef65b0b168b341ecd585bf7d5752e Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Tue, 18 Jan 2022 15:36:01 +0900 Subject: [PATCH] Reorganize code structures - Add test utility project Signed-off-by: Hiroshi Miura --- build.gradle | 14 ++ .../main/java/org/dict/zip/cli/DictZipUtils.java | 4 +- .../test/java/org/dict/zip/cli/DictDataTest.java | 6 +- .../test/java/tokyo/northside/io/FileUtils2.java | 91 ------------- .../src/test/java/tokyo/northside/io/IOUtils2.java | 144 --------------------- .../test/java/org/dict/zip/DictZipFileTest.java | 15 ++- .../java/org/dict/zip/DictZipFileUtilsTest.java | 51 ++++++++ gradle.properties | 2 +- .../main}/java/tokyo/northside/io/FileUtils2.java | 0 .../main}/java/tokyo/northside/io/IOUtils2.java | 0 .../test/java/tokyo/northside}/FileUtils2Test.java | 44 +------ .../src/test/resources/test_util.txt | 0 .../src/test/resources/test_util1.txt | 0 .../src/test/resources/test_util2.txt | 0 settings.gradle | 2 +- 15 files changed, 82 insertions(+), 291 deletions(-) delete mode 100644 dictzip-cli/src/test/java/tokyo/northside/io/FileUtils2.java delete mode 100644 dictzip-cli/src/test/java/tokyo/northside/io/IOUtils2.java create mode 100644 dictzip-lib/src/test/java/org/dict/zip/DictZipFileUtilsTest.java rename {dictzip-lib/src/test => northside-io/src/main}/java/tokyo/northside/io/FileUtils2.java (100%) rename {dictzip-lib/src/test => northside-io/src/main}/java/tokyo/northside/io/IOUtils2.java (100%) rename {dictzip-lib/src/test/java/org/dict/zip => northside-io/src/test/java/tokyo/northside}/FileUtils2Test.java (77%) rename {dictzip-lib => northside-io}/src/test/resources/test_util.txt (100%) rename {dictzip-lib => northside-io}/src/test/resources/test_util1.txt (100%) rename {dictzip-lib => northside-io}/src/test/resources/test_util2.txt (100%) diff --git a/build.gradle b/build.gradle index 25d8979..39d47fb 100644 --- a/build.gradle +++ b/build.gradle @@ -66,9 +66,22 @@ subprojects { } } +project(':northside-io') { + apply plugin: 'java-library' + version = rootProject.version + dependencies { + implementation 'commons-io:commons-io:2.11.0' + } +} + project(':dictzip-lib') { apply plugin: 'java-library' version = rootProject.version + + dependencies { + testImplementation project(':northside-io') + } + publishing { publications { mavenJava(MavenPublication) { @@ -140,6 +153,7 @@ project(':dictzip-cli') { dependencies { implementation project(':dictzip-lib') implementation 'gnu.getopt:java-getopt:1.0.13' + testImplementation project(':northside-io') } task mandoc(type: Copy) { from "doc/dictzip.1.in" diff --git a/dictzip-cli/src/main/java/org/dict/zip/cli/DictZipUtils.java b/dictzip-cli/src/main/java/org/dict/zip/cli/DictZipUtils.java index 11b1628..3914685 100644 --- a/dictzip-cli/src/main/java/org/dict/zip/cli/DictZipUtils.java +++ b/dictzip-cli/src/main/java/org/dict/zip/cli/DictZipUtils.java @@ -32,7 +32,7 @@ public final class DictZipUtils { * @param name input filename. * @return output filename. */ - protected static String uncompressedFileName(final String name) { + static String uncompressedFileName(final String name) { String result; if (name.endsWith(".dz") || name.endsWith(".gz")) { result = name.substring(0, name.length() - 3); @@ -47,7 +47,7 @@ public final class DictZipUtils { * @param name input file name. * @return output filename. */ - protected static String compressedFileName(final String name) { + static String compressedFileName(final String name) { return name + ".dz"; } diff --git a/dictzip-cli/src/test/java/org/dict/zip/cli/DictDataTest.java b/dictzip-cli/src/test/java/org/dict/zip/cli/DictDataTest.java index 9352911..974c5b8 100644 --- a/dictzip-cli/src/test/java/org/dict/zip/cli/DictDataTest.java +++ b/dictzip-cli/src/test/java/org/dict/zip/cli/DictDataTest.java @@ -51,10 +51,11 @@ public class DictDataTest { /** * Test of doZip method, of class DictData. + * @param tempDir JUnit5 temporary directory support * @throws java.lang.Exception if file operation failed. */ @Test - public void testDoZip(@TempDir Path tempDir) throws Exception { + public void testDoZip(@TempDir final Path tempDir) throws Exception { Path testFile = Paths.get(Objects.requireNonNull( this.getClass().getResource("/test_dozip.dict")).toURI()); Path zippedFile = tempDir.resolve("test_dozip.dict.dz"); @@ -72,10 +73,11 @@ public class DictDataTest { /** * Test of doUnzip method, of class DictData. + * @param tempDir JUnit5 temporary directory support * @throws java.lang.Exception if file operation failed. */ @Test - public void testDoUnzip(@TempDir Path tempDir) throws Exception { + public void testDoUnzip(@TempDir final Path tempDir) throws Exception { String dzFile = this.getClass().getResource("/test.dict.dz").getFile(); Path decompressed = tempDir.resolve("test.dict"); long start = 0L; diff --git a/dictzip-cli/src/test/java/tokyo/northside/io/FileUtils2.java b/dictzip-cli/src/test/java/tokyo/northside/io/FileUtils2.java deleted file mode 100644 index 4ceda97..0000000 --- a/dictzip-cli/src/test/java/tokyo/northside/io/FileUtils2.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * FileUtils library. - * - * Copyright (C) 2016 Hiroshi Miura - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package tokyo.northside.io; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; - -import org.apache.commons.io.FileUtils; - - -/** - * General File manipulation utility. - *

- * This class provides static utility methods for input/output operations. - *

- *

- * The methods in this class that read a file are buffered internally. - * The default buffer size of 4K has been shown to be efficient in tests. - *

- * Created by Hiroshi Miura on 16/04/09. - * - * @author Hiroshi Miura - */ -public final class FileUtils2 extends FileUtils { - - /** - * Compare file contents in range. Both files must be files (not directories) and exist. - * - * @param first first file - * @param second second file - * @param off compare from offset - * @param len comparison length - * @return boolean true if files are equal, otherwise false - * @throws IOException error in function - */ - public static boolean contentEquals(final File first, final File second, final long off, - final long len) throws IOException { - boolean result = false; - - if (len < 1) { - throw new IllegalArgumentException(); - } - if (off < 0) { - throw new IllegalArgumentException(); - } - - if ((first.exists()) && (second.exists()) - && (first.isFile()) && (second.isFile())) { - if (first.getCanonicalPath().equals(second.getCanonicalPath())) { - result = true; - } else { - FileInputStream firstInput = null; - FileInputStream secondInput = null; - - try { - firstInput = new FileInputStream(first); - secondInput = new FileInputStream(second); - result = IOUtils2.contentEquals(firstInput, secondInput, off, len); - } catch (RuntimeException e) { - throw e; - } catch (IOException ioe) { - throw ioe; - } finally { - org.apache.commons.io.IOUtils.closeQuietly(firstInput); - org.apache.commons.io.IOUtils.closeQuietly(secondInput); - } - } - } - return result; - } - -} diff --git a/dictzip-cli/src/test/java/tokyo/northside/io/IOUtils2.java b/dictzip-cli/src/test/java/tokyo/northside/io/IOUtils2.java deleted file mode 100644 index 2b9e563..0000000 --- a/dictzip-cli/src/test/java/tokyo/northside/io/IOUtils2.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * FileUtils library. - * - * Copyright (C) 2016 Hiroshi Miura - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package tokyo.northside.io; - -import java.io.InputStream; -import java.io.IOException; -import java.util.Arrays; - -import org.apache.commons.io.IOUtils; - -/** - * General IO stream manipulation utility. - *

- * This class provides static utility methods for input/output operations. - *

- *

- * The methods in this class that read a stream are buffered internally. - * This means that there is no cause to use a BufferedInputStream - * or BufferedReader. The default buffer size of 4K has been shown - * to be efficient in tests. - *

- * Wherever possible, the methods in this class do not flush or close - * the stream. This is to avoid making non-portable assumptions about the - * streams' origin and further use. Thus the caller is still responsible for - * closing streams after use. - *

- * Created by Hiroshi Miura on 16/04/09. - * - * @author Hiroshi Miura - */ -public final class IOUtils2 extends IOUtils { - - private static final int BUF_LEN = 4096; - - /** - * Compare the contents of two Streams to determine if they are equal or not. - * - * @param first first input stream. - * @param second second input stream. - * @param off compare from offset - * @param len comparison length - * @return boolean true if content of input streams are equal, true if streams are equal, - * otherwise false. - * @throws IOException when I/O error occurred. - */ - public static boolean contentEquals(final InputStream first, final InputStream second, - final long off, final long len) throws IOException { - boolean result; - - if (len < 1) { - throw new IllegalArgumentException(); - } - if (off < 0) { - throw new IllegalArgumentException(); - } - if (first.equals(second)) { - return false; - } - - try { - byte[] firstBytes = new byte[BUF_LEN]; - byte[] secondBytes = new byte[BUF_LEN]; - - if (off > 0) { - long totalSkipped = 0; - while (totalSkipped < off) { - long skipped = first.skip(off - totalSkipped); - if (skipped == 0) { - throw new IOException("Cannot seek offset bytes."); - } - totalSkipped += skipped; - } - totalSkipped = 0; - while (totalSkipped < off) { - long skipped = second.skip(off - totalSkipped); - if (skipped == 0) { - throw new IOException("Cannot seek offset bytes."); - } - totalSkipped += skipped; - } - } - - long readLengthTotal = 0; - result = true; - while (readLengthTotal < len) { - int readLength = BUF_LEN; - if (len - readLengthTotal < (long) BUF_LEN) { - readLength = (int) (len - readLengthTotal); - } - int lenFirst = first.read(firstBytes, 0, readLength); - int lenSecond = second.read(secondBytes, 0, readLength); - if (lenFirst != lenSecond) { - result = false; - break; - } - if ((lenFirst < 0) && (lenSecond < 0)) { - result = true; - break; - } - readLengthTotal += lenFirst; - if (lenFirst < firstBytes.length) { - byte[] a = Arrays.copyOfRange(firstBytes, 0, lenFirst); - byte[] b = Arrays.copyOfRange(secondBytes, 0, lenSecond); - if (!Arrays.equals(a, b)) { - result = false; - break; - } - } else if (!Arrays.equals(firstBytes, secondBytes)) { - result = false; - break; - } - } - } catch (RuntimeException e) { - throw e; - } catch (IOException ioe) { - throw ioe; - } - return result; - } - - /** - * Static utility should not be instantiated. - */ - private IOUtils2() { - } -} diff --git a/dictzip-lib/src/test/java/org/dict/zip/DictZipFileTest.java b/dictzip-lib/src/test/java/org/dict/zip/DictZipFileTest.java index 5e13ac1..b9bb8e8 100644 --- a/dictzip-lib/src/test/java/org/dict/zip/DictZipFileTest.java +++ b/dictzip-lib/src/test/java/org/dict/zip/DictZipFileTest.java @@ -142,11 +142,11 @@ public class DictZipFileTest { dout.finish(); } // check archive + String[] command = {"/usr/bin/dictzip", "-d", "-c", "-s", null, "-e", "10", zippedPath.toAbsolutePath().toString()}; for (int i = 0; i < positions.length; i++) { System.out.printf("seek position: %d%n", positions[i]); - Process process = Runtime.getRuntime().exec( - String.format("dictzip -d -c -k -s %d -e %d %s", - positions[i], 10, zippedPath.toAbsolutePath())); + command[4] = Integer.toString(positions[i]); + Process process = Runtime.getRuntime().exec(command); int b = process.getInputStream().read(); int returnCode = process.waitFor(); assertEquals(0, returnCode); @@ -196,7 +196,8 @@ public class DictZipFileTest { } } // create dictzip archive with dictzip command - Process process = Runtime.getRuntime().exec(String.format("dictzip %s", outTextPath.toAbsolutePath())); + String[] command = {"/usr/bin/dictzip", outTextPath.toAbsolutePath().toString()}; + Process process = Runtime.getRuntime().exec(command); int returnCode = process.waitFor(); assertEquals(0, returnCode); File zippedFile = tempDir.resolve("DictZipText.txt.dz").toFile(); @@ -266,11 +267,11 @@ public class DictZipFileTest { dout.finish(); } // check archive + String[] command = {"/usr/bin/dictzip", "-d", "-c", "-s", null, "-e", "10", zippedPath.toAbsolutePath().toString()}; for (int i = 0; i < positions.length; i++) { System.out.printf("seek position: %d%n", positions[i]); - Process process = Runtime.getRuntime().exec( - String.format("dictzip -d -c -k -s %d -e %d %s", - positions[i], 10, zippedPath.toAbsolutePath())); + command[4] = Integer.toString(positions[i]); + Process process = Runtime.getRuntime().exec(command); int b = process.getInputStream().read(); int returnCode = process.waitFor(); assertEquals(0, returnCode); diff --git a/dictzip-lib/src/test/java/org/dict/zip/DictZipFileUtilsTest.java b/dictzip-lib/src/test/java/org/dict/zip/DictZipFileUtilsTest.java new file mode 100644 index 0000000..95d68cc --- /dev/null +++ b/dictzip-lib/src/test/java/org/dict/zip/DictZipFileUtilsTest.java @@ -0,0 +1,51 @@ +package org.dict.zip; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class DictZipFileUtilsTest { + + /** + * Check dictzip inputstream. + * @throws Exception when fails. + */ + @Test + public void testCheckDictZipInputStreamString() throws Exception { + String targetFile = this.getClass().getResource("/test.dict.dz").getFile(); + Assertions.assertTrue(DictZipFileUtils.checkDictZipInputStream(targetFile)); + } + + /** + * Check dictzip input streasm which is not exist. + */ + @Test + public void testCheckDictZipInputStreamStringNoExist() { + String targetFile = "false.dict.dz"; + boolean result; + try { + DictZipFileUtils.checkDictZipInputStream(targetFile); + result = false; + } catch (IOException e) { + // expected. + result = true; + } + assertTrue(result); + } + + /** + * Check dictzip input stream. + * @throws Exception when fails. + */ + @Test + public void testCheckDictZipInputStream() throws Exception { + String targetFile = this.getClass().getResource("/test.dict.dz").getFile(); + try (DictZipInputStream dzin = new DictZipInputStream(new + RandomAccessInputStream(targetFile, "r"))) { + Assertions.assertTrue(DictZipFileUtils.checkDictZipInputStream(dzin)); + } + } +} diff --git a/gradle.properties b/gradle.properties index a1b5201..7b35825 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,5 +7,5 @@ projectDesc=DictZip compression library and CLI projectOwner=miurahr developerEmail=miurahr@linux.com developerName='Hiroshi Miura' -projectYears=2016-2021 +projectYears=2016-2022 projectReleaseBody="Fix release for critical bug on seek." diff --git a/dictzip-lib/src/test/java/tokyo/northside/io/FileUtils2.java b/northside-io/src/main/java/tokyo/northside/io/FileUtils2.java similarity index 100% rename from dictzip-lib/src/test/java/tokyo/northside/io/FileUtils2.java rename to northside-io/src/main/java/tokyo/northside/io/FileUtils2.java diff --git a/dictzip-lib/src/test/java/tokyo/northside/io/IOUtils2.java b/northside-io/src/main/java/tokyo/northside/io/IOUtils2.java similarity index 100% rename from dictzip-lib/src/test/java/tokyo/northside/io/IOUtils2.java rename to northside-io/src/main/java/tokyo/northside/io/IOUtils2.java diff --git a/dictzip-lib/src/test/java/org/dict/zip/FileUtils2Test.java b/northside-io/src/test/java/tokyo/northside/FileUtils2Test.java similarity index 77% rename from dictzip-lib/src/test/java/org/dict/zip/FileUtils2Test.java rename to northside-io/src/test/java/tokyo/northside/FileUtils2Test.java index eef7d40..d0d973e 100644 --- a/dictzip-lib/src/test/java/org/dict/zip/FileUtils2Test.java +++ b/northside-io/src/test/java/tokyo/northside/FileUtils2Test.java @@ -34,13 +34,11 @@ * obligated to do so. If you do not wish to do so, delete this * exception statement from your version. */ -package org.dict.zip; - +package tokyo.northside; import org.junit.jupiter.api.Test; import java.io.File; -import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Objects; @@ -114,44 +112,4 @@ public class FileUtils2Test { File secondFile = new File(this.getClass().getResource("/test_util2.txt").getFile()); assertFalse(contentEquals(firstFile, secondFile, 0, 64)); } - - /** - * Check dictzip inputstream. - * @throws Exception when fails. - */ - @Test - public void testCheckDictZipInputStreamString() throws Exception { - String targetFile = this.getClass().getResource("/test.dict.dz").getFile(); - assertTrue(DictZipFileUtils.checkDictZipInputStream(targetFile)); - } - - /** - * Check dictzip input streasm which is not exist. - */ - @Test - public void testCheckDictZipInputStreamStringNoExist() { - String targetFile = "false.dict.dz"; - boolean result; - try { - DictZipFileUtils.checkDictZipInputStream(targetFile); - result = false; - } catch (IOException e) { - // expected. - result = true; - } - assertTrue(result); - } - - /** - * Check dictzip input stream. - * @throws Exception when fails. - */ - @Test - public void testCheckDictZipInputStream() throws Exception { - String targetFile = this.getClass().getResource("/test.dict.dz").getFile(); - try (DictZipInputStream dzin = new DictZipInputStream(new - RandomAccessInputStream(targetFile, "r"))) { - assertTrue(DictZipFileUtils.checkDictZipInputStream(dzin)); - } - } } diff --git a/dictzip-lib/src/test/resources/test_util.txt b/northside-io/src/test/resources/test_util.txt similarity index 100% rename from dictzip-lib/src/test/resources/test_util.txt rename to northside-io/src/test/resources/test_util.txt diff --git a/dictzip-lib/src/test/resources/test_util1.txt b/northside-io/src/test/resources/test_util1.txt similarity index 100% rename from dictzip-lib/src/test/resources/test_util1.txt rename to northside-io/src/test/resources/test_util1.txt diff --git a/dictzip-lib/src/test/resources/test_util2.txt b/northside-io/src/test/resources/test_util2.txt similarity index 100% rename from dictzip-lib/src/test/resources/test_util2.txt rename to northside-io/src/test/resources/test_util2.txt diff --git a/settings.gradle b/settings.gradle index 28d2b09..4aec9c3 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,2 @@ rootProject.name = 'dictzip' -include 'dictzip-lib', 'dictzip-cli' +include 'northside-io', 'dictzip-lib', 'dictzip-cli' -- 2.11.0