Skip to content

Commit

Permalink
feat: improve test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
miurahr committed May 18, 2024
1 parent 110c9b8 commit 61ecf6f
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 129 deletions.
129 changes: 0 additions & 129 deletions src/test/java/org/dts/spell/tests/JMySpellTest.java

This file was deleted.

117 changes: 117 additions & 0 deletions src/test/java/org/dts/spell/tests/OpenOfficeSpellDictonaryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package org.dts.spell.tests;


import org.dts.spell.SpellChecker;
import org.dts.spell.dictionary.OpenOfficeSpellDictionary;
import org.dts.spell.dictionary.SpellDictionary;
import org.dts.spell.dictionary.SpellDictionaryException;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.util.List;
import java.util.Objects;
import java.util.zip.ZipFile;

import static org.assertj.core.api.Assertions.assertThat;

public class OpenOfficeSpellDictonaryTest extends TestBase {

private static final String DICTIONARY_ZIP = "en_US.zip";

@Override
protected String getDictionary() {
return DICTIONARY_ZIP;
}

@Test
public void testCheckOpenOfficeDictionary() throws IOException {
try (InputStream affFile = Files.newInputStream(tempDir.resolve("en_US.aff"));
InputStream dicFile = Files.newInputStream(tempDir.resolve("en_US.dic"))) {
SpellDictionary dict = new OpenOfficeSpellDictionary(affFile, dicFile);
SpellChecker checker = new SpellChecker(dict);
checker.setCaseSensitive(false);

String word = "Hello world!";
assertThat(checker.isCorrect(word)).isTrue();
List<String> suggestions = dict.getSuggestions(word);
assertThat(suggestions.size()).isEqualTo(10);
assertThat(suggestions).contains("Worldliness's", "Worldlinesses", "Worldliness", "Worldliest", "Afterworlds",
"Afterworld's", "Afterworld", "Worldwide", "Dreamworlds");
}
}

@Test
public void testOpenOfficeDictionaryZipDictionaryStream() throws IOException {
try (InputStream is = getClass().getResourceAsStream(DICTIONARY_ZIP)) {
SpellDictionary dict = new OpenOfficeSpellDictionary(is, personalDictionary, false);
SpellChecker checker = new SpellChecker(dict);
checker.setCaseSensitive(false);

String word = "Hello world!";
assertThat(checker.isCorrect(word)).isTrue();
}
}

@Test
public void testOpenOfficeDictionaryUnknownWord() throws IOException {
try (InputStream is = getClass().getResourceAsStream(DICTIONARY_ZIP)) {
SpellDictionary dict = new OpenOfficeSpellDictionary(is, personalDictionary, false);
SpellChecker checker = new SpellChecker(dict);
checker.setCaseSensitive(false);
String word = "Cha0 world!";
assertThat(checker.isCorrect(word)).isFalse();
List<String> suggestions = dict.getSuggestions(word);
assertThat(suggestions.size()).isEqualTo(9);
}
}

@Test
public void testOpenOfficeDictionaryZipFile() throws IOException {
URL zipURL = getClass().getResource(DICTIONARY_ZIP);
ZipFile zipFile = new ZipFile(Objects.requireNonNull(zipURL).getFile());
SpellDictionary dict = new OpenOfficeSpellDictionary(zipFile);
SpellChecker checker = new SpellChecker(dict);
checker.setCaseSensitive(false);
String word = "Hello world!";
assertThat(checker.isCorrect(word)).isTrue();
}

@Test
public void testOpenOfficeDictionaryStreamBackground() throws IOException {
try (InputStream is = getClass().getResourceAsStream(DICTIONARY_ZIP)) {
SpellDictionary dict = new OpenOfficeSpellDictionary(is, personalDictionary, true);
SpellChecker checker = new SpellChecker(dict);
checker.setCaseSensitive(false);
String word = "Hello world!";
assertThat(checker.isCorrect(word)).isTrue();
}
}

@Test
public void testOpenOfficeDictionaryCharSequence() throws IOException {
try (InputStream is = getClass().getResourceAsStream(DICTIONARY_ZIP)) {
SpellDictionary dict = new OpenOfficeSpellDictionary(is, personalDictionary, true);
SpellChecker checker = new SpellChecker(dict);
checker.setCaseSensitive(false);
String word = "Hello world!";
assertThat(checker.checkSpell(word)).isNullOrEmpty();
}
}

@Test
public void testOpenOfficeDictionaryAddWord() throws IOException, SpellDictionaryException {
try (InputStream is = getClass().getResourceAsStream(DICTIONARY_ZIP)) {
SpellDictionary dict = new OpenOfficeSpellDictionary(is, personalDictionary, false);
dict.addWord("Chao");
SpellChecker checker = new SpellChecker(dict);
checker.setCaseSensitive(false);

String word = "Chao world!";
assertThat(checker.isCorrect(word)).isTrue();
}
}

}
54 changes: 54 additions & 0 deletions src/test/java/org/dts/spell/tests/SpanishTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.dts.spell.tests;


import org.dts.spell.SpellChecker;
import org.dts.spell.dictionary.OpenOfficeSpellDictionary;
import org.dts.spell.dictionary.SpellDictionary;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class SpanishTest extends TestBase {

private static final String DICTIONARY_ZIP = "es.zip";

@Override
protected String getDictionary() {
return DICTIONARY_ZIP;
}

@Test
public void testSpell() throws IOException {
try (InputStream affFile = Files.newInputStream(tempDir.resolve("es/index.aff"));
InputStream dicFile = Files.newInputStream(tempDir.resolve("es/index.dic"))) {
SpellDictionary dict = new OpenOfficeSpellDictionary(affFile, dicFile);
SpellChecker checker = new SpellChecker(dict);
checker.setCaseSensitive(false);

String word = "Hola mundo!";
assertThat(checker.isCorrect(word)).isTrue();
List<String> suggestions = dict.getSuggestions(word);
assertThat(suggestions.size()).isEqualTo(10);
assertThat(suggestions).contains("Inframundo", "Juzgamundos", "Inframundos", "Vagamundos",
"Vagamundo", "Trotamundos", "Trasmundos", "Trasmundo", "Inmundos", "Submundos");
}
}

@Test
public void testOpenOfficeDictionaryZipDictionary() throws IOException {
try (InputStream is = getClass().getResourceAsStream(DICTIONARY_ZIP)) {
SpellDictionary dict = new OpenOfficeSpellDictionary(is, personalDictionary, false);
SpellChecker checker = new SpellChecker(dict);
checker.setCaseSensitive(false);

String word = "Hola mundo!";
assertThat(checker.isCorrect(word)).isTrue();
}
}

}
61 changes: 61 additions & 0 deletions src/test/java/org/dts/spell/tests/TestBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.dts.spell.tests;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;

public abstract class TestBase {
protected Path tempDir;
protected File personalDictionary;

@Before
public void setUp() throws Exception {
tempDir = Files.createTempDirectory("jmyspell");
extractZip(getClass().getResourceAsStream(getDictionary()), tempDir);
personalDictionary = Files.createTempFile(tempDir, "personal", ".dic").toFile();
}

protected abstract String getDictionary();

@After
public void tearDown() throws Exception {
FileUtils.deleteDirectory(tempDir.toFile());
}


protected void extractZip(InputStream inputStream, Path extractDirectory) throws IOException, ArchiveException {
ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory();
ArchiveInputStream archiveInputStream = archiveStreamFactory
.createArchiveInputStream(ArchiveStreamFactory.ZIP, inputStream);
ArchiveEntry archiveEntry;
while ((archiveEntry = archiveInputStream.getNextEntry()) != null) {
Path path = extractDirectory.resolve(archiveEntry.getName());
File file = path.toFile();
if (archiveEntry.isDirectory()) {
if (!file.isDirectory()) {
var ignore = file.mkdirs();
}
} else {
File parent = file.getParentFile();
if (!parent.isDirectory()) {
var ignore = parent.mkdirs();
}
try (OutputStream outputStream = Files.newOutputStream(path)) {
IOUtils.copy(archiveInputStream, outputStream);
}
}
}
}
}
Binary file added src/test/resources/org/dts/spell/tests/es.zip
Binary file not shown.

0 comments on commit 61ecf6f

Please sign in to comment.