Permalink
Browse files

Added unit tests. Fixed bug with trailing zero characters at the end …

…of the hyphenated word.
  • Loading branch information...
1 parent e91e95e commit 1b349735806c1b092edf5ec79cca35137c8a331b @dedeibel committed May 15, 2012
Showing with 46 additions and 2 deletions.
  1. +3 −0 .classpath
  2. +14 −2 src/name/benjaminpeter/hyphen/Hyphen.java
  3. +29 −0 test/src/name/benjaminpeter/hyphen/HyphenTest.java
View
@@ -1,7 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
+ <classpathentry kind="src" path="test/src"/>
<classpathentry kind="lib" path="lib/jna.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
+ <classpathentry kind="lib" path="native-lib"/>
<classpathentry kind="output" path="build"/>
</classpath>
@@ -108,7 +108,6 @@ protected Hyphen(final String libDir) throws UnsatisfiedLinkError,
} catch (IOException e) {
throw new Error("Failed to create temporary file for " + libFile, e);
-
} finally {
try {
is.close();
@@ -301,7 +300,20 @@ public String hyphenate(final String word) {
@SuppressWarnings("unused")
int success = hsl.hnj_hyphen_hyphenate2(hunspellDict, asciiWord,
asciiWord.length, hyphens, hyphenated, rep, pos, cut);
- return new String(hyphenated, Charset.forName("ISO-8859-1"));
+ return new String(hyphenated, 0, strlen(hyphenated),
+ Charset.forName("ISO-8859-1"));
+ }
+
+ /*
+ * Determine the size of the string, byte is expected to be a zero
+ * terminated C-string.
+ */
+ private int strlen(final byte[] hyphenated) {
+ int i = 0;
+ while (hyphenated[i] != 0) {
+ i++;
+ }
+ return i;
}
/**
@@ -0,0 +1,29 @@
+package name.benjaminpeter.hyphen;
+
+import static org.junit.Assert.*;
+
+import java.io.FileNotFoundException;
+import java.io.UnsupportedEncodingException;
+import name.benjaminpeter.hyphen.Hyphen.Dictionary;
+
+import org.junit.Test;
+
+public class HyphenTest {
+
+ @Test
+ public void testInstance() {
+ Hyphen firstInstance = Hyphen.getInstance();
+ Hyphen secondInstance = Hyphen.getInstance();
+ assertEquals(firstInstance, secondInstance);
+ assertSame(firstInstance, secondInstance);
+ }
+
+ @Test
+ public void testGetDictionary() throws FileNotFoundException,
+ UnsupportedEncodingException {
+ Hyphen hyphen = Hyphen.getInstance();
+ Dictionary dic = hyphen.getDictionary("test/resources/hyph_mini_de.dic");
+ assertNotNull(dic);
+ assertEquals("dan=ke", dic.hyphenate("danke"));
+ }
+}

0 comments on commit 1b34973

Please sign in to comment.