OSDN Git Service

Reorganize code structures
authorHiroshi Miura <miurahr@linux.com>
Tue, 18 Jan 2022 06:36:01 +0000 (15:36 +0900)
committerHiroshi Miura <miurahr@linux.com>
Tue, 18 Jan 2022 06:36:01 +0000 (15:36 +0900)
- Add test utility project

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
15 files changed:
build.gradle
dictzip-cli/src/main/java/org/dict/zip/cli/DictZipUtils.java
dictzip-cli/src/test/java/org/dict/zip/cli/DictDataTest.java
dictzip-cli/src/test/java/tokyo/northside/io/FileUtils2.java [deleted file]
dictzip-cli/src/test/java/tokyo/northside/io/IOUtils2.java [deleted file]
dictzip-lib/src/test/java/org/dict/zip/DictZipFileTest.java
dictzip-lib/src/test/java/org/dict/zip/DictZipFileUtilsTest.java [new file with mode: 0644]
gradle.properties
northside-io/src/main/java/tokyo/northside/io/FileUtils2.java [moved from dictzip-lib/src/test/java/tokyo/northside/io/FileUtils2.java with 100% similarity]
northside-io/src/main/java/tokyo/northside/io/IOUtils2.java [moved from dictzip-lib/src/test/java/tokyo/northside/io/IOUtils2.java with 100% similarity]
northside-io/src/test/java/tokyo/northside/FileUtils2Test.java [moved from dictzip-lib/src/test/java/org/dict/zip/FileUtils2Test.java with 77% similarity]
northside-io/src/test/resources/test_util.txt [moved from dictzip-lib/src/test/resources/test_util.txt with 100% similarity]
northside-io/src/test/resources/test_util1.txt [moved from dictzip-lib/src/test/resources/test_util1.txt with 100% similarity]
northside-io/src/test/resources/test_util2.txt [moved from dictzip-lib/src/test/resources/test_util2.txt with 100% similarity]
settings.gradle

index 25d8979..39d47fb 100644 (file)
@@ -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"
index 11b1628..3914685 100644 (file)
@@ -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";
     }
 
index 9352911..974c5b8 100644 (file)
@@ -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 (file)
index 4ceda97..0000000
+++ /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.
- * <p>
- * This class provides static utility methods for input/output operations.
- * <ul>
- * <li>contentEquals - these methods compare the content of two files
- * </ul>
- * <p>
- * 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.
- * <p>
- * 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 (file)
index 2b9e563..0000000
+++ /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.
- * <p>
- * This class provides static utility methods for input/output operations.
- * <ul>
- * <li>contentEquals - these methods compare the content of two streams
- * </ul>
- * <p>
- * The methods in this class that read a stream are buffered internally.
- * This means that there is no cause to use a <code>BufferedInputStream</code>
- * or <code>BufferedReader</code>. The default buffer size of 4K has been shown
- * to be efficient in tests.
- * <p>
- * Wherever possible, the methods in this class do <em>not</em> 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.
- * <p>
- * 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() {
-    }
-}
index 5e13ac1..b9bb8e8 100644 (file)
@@ -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 (file)
index 0000000..95d68cc
--- /dev/null
@@ -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));
+        }
+    }
+}
index a1b5201..7b35825 100644 (file)
@@ -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."
  * 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));
-        }
-    }
 }
index 28d2b09..4aec9c3 100644 (file)
@@ -1,2 +1,2 @@
 rootProject.name = 'dictzip'
-include 'dictzip-lib', 'dictzip-cli'
+include 'northside-io', 'dictzip-lib', 'dictzip-cli'