OSDN Git Service

CLI: fix test and release v0.6.1
authorHiroshi Miura <miurahr@linux.com>
Tue, 12 Apr 2016 00:23:27 +0000 (09:23 +0900)
committerHiroshi Miura <miurahr@linux.com>
Tue, 12 Apr 2016 00:33:40 +0000 (09:33 +0900)
-CLI: DictDataTest depends on isFileBinaryEquals method but
 library's DictZipFileUtils now private. Adding it in a test class.
-Hot fix release v0.6.1.

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
CHANGELOG.md
README.md
dictzip-cli/src/main/resources/org/dict/zip/Version.properties
dictzip-cli/src/test/java/org/dict/zip/cli/DictDataTest.java

index baff96a..310b17b 100644 (file)
@@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.
 
 ## [Unreleased]
 
+## [0.6.1] - 2016-4-12
+### Fixed
+- CLI: test compile error because DictZipFileUtils is package private,
+  but cli test depends on it. We add a utility method on test class.
+
 ## [0.6.0] - 2016-4-10
 ### Add
 - gradle.properties.template
index e3dfd69..d0940f5 100644 (file)
--- a/README.md
+++ b/README.md
@@ -52,10 +52,10 @@ DictZip CLI utility depends on some libraries.
 DictZip library for Java and command line utility.
 
 Copyright (C) 2001-2004 Ho Ngoc Duc
+
 Copyright (C) 2016 Hiroshi Miura
 
-Some part of this program code are come from a part of abandoned jdictd on java
-by JDictd project.
+Some part of this program are come from a part of jdictd 1.5 on java.
 
 DictZip command line utility is distributed under the terms of GNU General
 Public License Version 3 or (at your option) any later version.
index 183ff43..c37bdb4 100644 (file)
@@ -22,11 +22,14 @@ package org.dict.zip.cli;
 
 import static org.testng.Assert.assertTrue;
 
+import java.io.BufferedInputStream;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.net.URL;
+import java.util.Arrays;
 
 import org.dict.zip.DictZipHeader;
-import org.dict.zip.DictZipFileUtils;
 
 import org.testng.annotations.Test;
 
@@ -62,7 +65,7 @@ public class DictDataTest {
         instance.doZip(DictZipHeader.CompressionLevel.DEFAULT_COMPRESSION);
         File resultFile = new File(testFile + ".dz");
         File expectFile = new File(this.getClass().getResource("/test_dozip.dict.dz.expected").getFile());
-        assertTrue(DictZipFileUtils.isFileBinaryEquals(resultFile, expectFile, 10, 512));
+        assertTrue(isFileBinaryEquals(resultFile, expectFile, 10, 512));
         resultFile.deleteOnExit();
     }
 
@@ -80,7 +83,7 @@ public class DictDataTest {
         File resultFile = new File(zippedFile);
         File expectFile = new File(this.getClass().getResource("/test_dozip.dict.dz.expected.best")
                  .getFile());
-        assertTrue(DictZipFileUtils.isFileBinaryEquals(resultFile, expectFile, 10, 512));
+        assertTrue(isFileBinaryEquals(resultFile, expectFile, 10, 512));
         resultFile.deleteOnExit();
     }
 
@@ -98,7 +101,7 @@ public class DictDataTest {
         File resultFile = new File(zippedFile);
         File expectFile = new File(this.getClass().getResource("/test_dozip.dict.dz.expected.fast")
                  .getFile());
-        assertTrue(DictZipFileUtils.isFileBinaryEquals(resultFile, expectFile, 10, 512));
+        assertTrue(isFileBinaryEquals(resultFile, expectFile, 10, 512));
         resultFile.deleteOnExit();
     }
 
@@ -119,7 +122,110 @@ public class DictDataTest {
         URL resultUrl = this.getClass().getResource("/test.dict");
         File resultFile = new File(resultUrl.getFile());
         URL expectedUrl = this.getClass().getResource("/test.dict.expected");
-        assertTrue(DictZipFileUtils.isFileBinaryEquals(resultFile, new File(expectedUrl.getFile())));
+        assertTrue(isFileBinaryEquals(resultFile, new File(expectedUrl.getFile())));
         resultFile.deleteOnExit();
     }
+
+    /**
+     * Compare binary files. Both files must be files (not directories) and exist.
+     *
+     * @param first  - first file
+     * @param second - second file
+     * @return boolean - true if files are binery equal
+     * @throws IOException - error in function
+     */
+    static boolean isFileBinaryEquals(File first, File second) throws IOException {
+        return isFileBinaryEquals(first, second, 0, first.length());
+    }
+
+    /**
+     * Compare binary files (for test). 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 binery equal
+     * @throws IOException - error in function
+     */
+    static boolean isFileBinaryEquals(File first, File second, final long off, final long len) throws IOException {
+        boolean result = false;
+        final int BUFFER_SIZE = 65536;
+        final int COMP_SIZE = 512;
+
+        if (len <= 1) {
+            throw new IllegalArgumentException();
+        }
+
+        if ((first.exists()) && (second.exists())
+                && (first.isFile()) && (second.isFile())) {
+            if (first.getCanonicalPath().equals(second.getCanonicalPath())) {
+                result = true;
+            } else {
+                FileInputStream firstInput;
+                FileInputStream secondInput;
+                BufferedInputStream bufFirstInput = null;
+                BufferedInputStream bufSecondInput = null;
+
+                try {
+                    firstInput = new FileInputStream(first);
+                    secondInput = new FileInputStream(second);
+                    bufFirstInput = new BufferedInputStream(firstInput, BUFFER_SIZE);
+                    bufSecondInput = new BufferedInputStream(secondInput, BUFFER_SIZE);
+
+                    byte[] firstBytes = new byte[COMP_SIZE];
+                    byte[] secondBytes = new byte[COMP_SIZE];
+
+                    bufFirstInput.skip(off);
+                    bufSecondInput.skip(off);
+
+                    long readLengthTotal = 0;
+                    result = true;
+                    while (readLengthTotal < len) {
+                        int readLength = COMP_SIZE;
+                        if (len - readLengthTotal < (long) COMP_SIZE) {
+                            readLength = (int) (len - readLengthTotal);
+                        }
+                        int lenFirst = bufFirstInput.read(firstBytes, 0, readLength);
+                        int lenSecond = bufSecondInput.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;
+                } finally {
+                    try {
+                        if (bufFirstInput != null) {
+                            bufFirstInput.close();
+                        }
+                    } finally {
+                        if (bufSecondInput != null) {
+                            bufSecondInput.close();
+                        }
+                    }
+                }
+            }
+        }
+
+        return result;
+    }
+
 }