diff --git a/src/test/java/org/codelibs/fess/dict/kuromoji/KuromojiFileTest.java b/src/test/java/org/codelibs/fess/dict/kuromoji/KuromojiFileTest.java new file mode 100644 index 0000000000000000000000000000000000000000..33673c78a4053caeb7b5773eab512f12b4c8681e --- /dev/null +++ b/src/test/java/org/codelibs/fess/dict/kuromoji/KuromojiFileTest.java @@ -0,0 +1,182 @@ +/* + * Copyright 2012-2015 CodeLibs Project and the Others. + * + * 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 org.codelibs.fess.dict.kuromoji; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.codelibs.fess.dict.DictionaryFile.PagingList; +import org.codelibs.fess.unit.UnitFessTestCase; + +public class KuromojiFileTest extends UnitFessTestCase { + private KuromojiFile kuromojiFile; + + /* + // TODO + private File file1; + + @Override + protected void setUp() throws Exception { + file1 = File.createTempFile("kuromoji_", ".txt"); + FileUtil.write( + file1.getAbsolutePath(), + "token1,seg1,reading1,pos1\ntoken2,seg2,reading2,pos2\ntoken3,seg3,reading3,pos3" + .getBytes(Constants.UTF_8)); + } + + @Override + protected void tearDown() throws Exception { + file1.delete(); + } + */ + + @Override + public void setUp() throws Exception { + super.setUp(); + kuromojiFile = new KuromojiFile("1", "dummy", new Date()); + List<KuromojiItem> itemList = new ArrayList<>(); + itemList.add(new KuromojiItem(1, "token1", "seg1", "reading1", "pos1")); + itemList.add(new KuromojiItem(2, "token2", "seg2", "reading2", "pos2")); + itemList.add(new KuromojiItem(3, "token3", "seg3", "reading3", "pos3")); + kuromojiFile.kuromojiItemList = itemList; + } + + public void test_selectList() { + final PagingList<KuromojiItem> itemList1 = kuromojiFile.selectList(0, 20); + assertEquals(3, itemList1.size()); + assertEquals(3, itemList1.getAllRecordCount()); + assertEquals(1, itemList1.getCurrentPageNumber()); + assertEquals(20, itemList1.getPageSize()); + + final PagingList<KuromojiItem> itemList2 = kuromojiFile.selectList(2, 2); + assertEquals(1, itemList2.size()); + assertEquals(3, itemList2.getAllRecordCount()); + assertEquals(2, itemList2.getCurrentPageNumber()); + assertEquals(2, itemList2.getPageSize()); + + assertEquals(0, kuromojiFile.selectList(5, 5).size()); + assertEquals(0, kuromojiFile.selectList(-1, 5).size()); + } + + public void test_selectList2() { + final PagingList<KuromojiItem> itemList = kuromojiFile.selectList(0, 5); + for (int i = 0; i < itemList.size(); i++) { + final KuromojiItem kuromojiItem = itemList.get(i); + assertEquals("token" + (i + 1), kuromojiItem.getToken()); + assertEquals("seg" + (i + 1), kuromojiItem.getSegmentation()); + assertEquals("reading" + (i + 1), kuromojiItem.getReading()); + assertEquals("pos" + (i + 1), kuromojiItem.getPos()); + assertFalse(kuromojiItem.isUpdated()); + assertFalse(kuromojiItem.isUpdated()); + } + } + + /* + // TODO + public void test_insert() { + final KuromojiFile kuromojiFile = new KuromojiFile(file1); + final PagingList<KuromojiItem> itemList1 = kuromojiFile.selectList(0, + 20); + assertEquals(3, itemList1.size()); + + final KuromojiItem kuromojiItem1 = new KuromojiItem(0, "token4", + "seg4", "reading4", "pos4"); + kuromojiFile.insert(kuromojiItem1); + final PagingList<KuromojiItem> itemList2 = kuromojiFile.selectList(0, + 20); + assertEquals(4, itemList2.size()); + assertEquals("token4", itemList2.get(3).getToken()); + assertEquals("seg4", itemList2.get(3).getSegmentation()); + assertEquals("reading4", itemList2.get(3).getReading()); + assertEquals("pos4", itemList2.get(3).getPos()); + + final KuromojiItem kuromojiItem2 = new KuromojiItem(0, "token5", + "seg5", "reading5", "pos5"); + kuromojiFile.insert(kuromojiItem2); + final PagingList<KuromojiItem> itemList3 = kuromojiFile.selectList(0, + 20); + assertEquals(5, itemList3.size()); + assertEquals("token5", itemList3.get(4).getToken()); + assertEquals("seg5", itemList3.get(4).getSegmentation()); + assertEquals("reading5", itemList3.get(4).getReading()); + assertEquals("pos5", itemList3.get(4).getPos()); + } + + public void test_update() { + final KuromojiFile kuromojiFile = new KuromojiFile(file1); + final PagingList<KuromojiItem> itemList1 = kuromojiFile.selectList(0, + 20); + assertEquals(3, itemList1.size()); + + final KuromojiItem kuromojiItem1 = itemList1.get(0); + kuromojiItem1.setNewToken("TOKEN1"); + kuromojiItem1.setNewSegmentation("SEG1"); + kuromojiItem1.setNewReading("READING1"); + kuromojiItem1.setNewPos("POS1"); + kuromojiFile.update(kuromojiItem1); + final PagingList<KuromojiItem> itemList2 = kuromojiFile.selectList(0, + 20); + assertEquals(3, itemList2.size()); + final KuromojiItem kuromojiItem2 = itemList2.get(0); + assertEquals("TOKEN1", kuromojiItem2.getToken()); + assertEquals("SEG1", kuromojiItem2.getSegmentation()); + assertEquals("READING1", kuromojiItem2.getReading()); + assertEquals("POS1", kuromojiItem2.getPos()); + assertFalse(kuromojiItem2.isUpdated()); + + final KuromojiItem kuromojiItem3 = itemList2.get(2); + kuromojiItem3.setNewToken("TOKEN3"); + kuromojiItem3.setNewSegmentation("SEG3"); + kuromojiItem3.setNewReading("READING3"); + kuromojiItem3.setNewPos("POS3"); + kuromojiFile.update(kuromojiItem3); + final PagingList<KuromojiItem> itemList3 = kuromojiFile.selectList(0, + 20); + assertEquals(3, itemList3.size()); + final KuromojiItem kuromojiItem4 = itemList3.get(2); + assertEquals("TOKEN3", kuromojiItem4.getToken()); + assertEquals("SEG3", kuromojiItem4.getSegmentation()); + assertEquals("READING3", kuromojiItem4.getReading()); + assertEquals("POS3", kuromojiItem4.getPos()); + assertFalse(kuromojiItem4.isUpdated()); + } + + public void test_delete() throws Exception { + final KuromojiFile kuromojiFile = new KuromojiFile(file1); + final PagingList<KuromojiItem> itemList1 = kuromojiFile.selectList(0, + 20); + assertEquals(3, itemList1.size()); + + final KuromojiItem kuromojiItem1 = itemList1.get(0); + kuromojiFile.delete(kuromojiItem1); + final PagingList<KuromojiItem> itemList2 = kuromojiFile.selectList(0, + 20); + assertEquals(2, itemList2.size()); + + final KuromojiItem kuromojiItem2 = itemList2.get(1); + kuromojiFile.delete(kuromojiItem2); + final PagingList<KuromojiItem> itemList3 = kuromojiFile.selectList(0, + 20); + assertEquals(1, itemList3.size()); + + assertEquals("token2,seg2,reading2,pos2" + Constants.LINE_SEPARATOR, + new String(FileUtil.getBytes(file1), Constants.UTF_8)); + + } + */ + +} diff --git a/src/test/java/org/codelibs/fess/dict/kuromoji/KuromojiItemTest.java b/src/test/java/org/codelibs/fess/dict/kuromoji/KuromojiItemTest.java new file mode 100644 index 0000000000000000000000000000000000000000..3f1be41c28e8396f969c9bc4436908c667ac65c7 --- /dev/null +++ b/src/test/java/org/codelibs/fess/dict/kuromoji/KuromojiItemTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2012-2015 CodeLibs Project and the Others. + * + * 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 org.codelibs.fess.dict.kuromoji; + +import org.codelibs.fess.unit.UnitFessTestCase; + +public class KuromojiItemTest extends UnitFessTestCase { + public void test_new1() { + final KuromojiItem kuromojiItem = new KuromojiItem(1, "t1", "s1", "r1", "p1"); + assertEquals(1, kuromojiItem.getId()); + assertEquals("t1", kuromojiItem.getToken()); + assertEquals("s1", kuromojiItem.getSegmentation()); + assertEquals("r1", kuromojiItem.getReading()); + assertEquals("p1", kuromojiItem.getPos()); + assertNull(kuromojiItem.getNewToken()); + assertNull(kuromojiItem.getNewSegmentation()); + assertNull(kuromojiItem.getNewReading()); + assertNull(kuromojiItem.getNewPos()); + assertFalse(kuromojiItem.isUpdated()); + assertFalse(kuromojiItem.isDeleted()); + + kuromojiItem.setNewToken("T1"); + kuromojiItem.setNewSegmentation("S1"); + kuromojiItem.setNewReading("R1"); + kuromojiItem.setNewPos("P1"); + assertTrue(kuromojiItem.isUpdated()); + assertFalse(kuromojiItem.isDeleted()); + + kuromojiItem.setNewToken(""); + assertTrue(kuromojiItem.isUpdated()); + assertTrue(kuromojiItem.isDeleted()); + } + + public void test_equals1() { + final KuromojiItem kuromojiItem1 = new KuromojiItem(1, "t1", "s1", "r1", "p1"); + + assertTrue(kuromojiItem1.equals(kuromojiItem1)); + assertTrue(kuromojiItem1.equals(new KuromojiItem(1, "t1", "s1", "r1", "p1"))); + assertTrue(kuromojiItem1.equals(new KuromojiItem(2, "t1", "s1", "r1", "p1"))); + assertFalse(kuromojiItem1.equals(new KuromojiItem(1, "T1", "s1", "r1", "p1"))); + assertFalse(kuromojiItem1.equals(new KuromojiItem(1, "t1", "S1", "r1", "p1"))); + assertFalse(kuromojiItem1.equals(new KuromojiItem(1, "t1", "s1", "R1", "p1"))); + assertFalse(kuromojiItem1.equals(new KuromojiItem(1, "t1", "s1", "r1", "P1"))); + } + + public void test_toLineString() { + assertEquals("t1,s1,r1,p1", new KuromojiItem(1, "t1", "s1", "r1", "p1").toLineString()); + assertEquals("t\"\"1,s\"\"1,r\"\"1,p\"\"1", new KuromojiItem(1, "t\"1", "s\"1", "r\"1", "p\"1").toLineString()); + assertEquals("\"t,1\",\"s,1\",\"r,1\",\"p,1\"", new KuromojiItem(1, "t,1", "s,1", "r,1", "p,1").toLineString()); + assertEquals("\"t\"\",1\",\"s\"\",1\",\"r\"\",1\",\"p\"\",1\"", + new KuromojiItem(1, "t\",1", "s\",1", "r\",1", "p\",1").toLineString()); + } +} diff --git a/src/test/java/org/codelibs/fess/dict/synonym/SynonymFileTest.java b/src/test/java/org/codelibs/fess/dict/synonym/SynonymFileTest.java index 71e7b57f6a1b7f72ba47cc64a52247b2aa139c92..2d008fe20c5bfc77f36b9ac60430832052987044 100644 --- a/src/test/java/org/codelibs/fess/dict/synonym/SynonymFileTest.java +++ b/src/test/java/org/codelibs/fess/dict/synonym/SynonymFileTest.java @@ -15,33 +15,50 @@ */ package org.codelibs.fess.dict.synonym; -import java.io.File; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; -import org.codelibs.core.io.FileUtil; -import org.codelibs.fess.Constants; +import org.codelibs.fess.dict.DictionaryFile.PagingList; import org.codelibs.fess.unit.UnitFessTestCase; public class SynonymFileTest extends UnitFessTestCase { - private File file1; + private SynonymFile synonymFile; + // TODO + /* + private File file1; @Override public void setUp() throws Exception { super.setUp(); file1 = File.createTempFile("synonym", ".txt"); - FileUtil.writeBytes(file1.getAbsolutePath(), "a1=>A1\nb1,b2 => B1\nc1 => C1, C2\nx1,X1\ny1, Y1, y2".getBytes(Constants.UTF_8)); + FileUtil.writeBytes(file1.getAbsolutePath(), + "a1=>A1\nb1,b2 => B1\nc1 => C1, C2\nx1,X1\ny1, Y1, y2" + .getBytes(Constants.UTF_8)); + // TODO set up elasticsearch and dictionaryManager + synonymFile = new SynonymFile("1", file1.getAbsolutePath(), new Date()); } @Override public void tearDown() throws Exception { - super.tearDown(); file1.delete(); } + */ - public void test_create() { - // TODO - /* - final SynonymCreator synonymCreator = new SynonymCreator(); - final SynonymFile synonymFile = (SynonymFile) synonymCreator.create(file1.getPath(), new Date()); + @Override + public void setUp() throws Exception { + super.setUp(); + synonymFile = new SynonymFile("1", "dummy", new Date()); + List<SynonymItem> itemList = new ArrayList<>(); + itemList.add(new SynonymItem(1, new String[] { "a1" }, new String[] { "A1" })); + itemList.add(new SynonymItem(2, new String[] { "b1", "b2" }, new String[] { "B1" })); + itemList.add(new SynonymItem(3, new String[] { "c1" }, new String[] { "C1", "C2" })); + itemList.add(new SynonymItem(4, new String[] { "x1", "X1" }, new String[] { "x1", "X1" })); + itemList.add(new SynonymItem(5, new String[] { "y1", "Y1", "y2" }, new String[] { "y1", "Y1", "y2" })); + synonymFile.synonymItemList = itemList; + } + + public void test_selectList() { final PagingList<SynonymItem> itemList1 = synonymFile.selectList(0, 20); // error occurs assertEquals(5, itemList1.size()); assertEquals(5, itemList1.getAllRecordCount()); @@ -56,7 +73,129 @@ public class SynonymFileTest extends UnitFessTestCase { assertEquals(0, synonymFile.selectList(5, 5).size()); assertEquals(0, synonymFile.selectList(-1, 5).size()); - */ } + public void test_selectList2() { + final PagingList<SynonymItem> itemList = synonymFile.selectList(0, 5); + assertEquals(1, itemList.get(0).getInputs().length); + assertEquals(1, itemList.get(0).getOutputs().length); + assertEquals("a1", itemList.get(0).getInputs()[0]); + assertEquals("A1", itemList.get(0).getOutputs()[0]); + assertFalse(itemList.get(0).isUpdated()); + + assertEquals(2, itemList.get(1).getInputs().length); + assertEquals(1, itemList.get(1).getOutputs().length); + assertEquals("b1", itemList.get(1).getInputs()[0]); + assertEquals("b2", itemList.get(1).getInputs()[1]); + assertEquals("B1", itemList.get(1).getOutputs()[0]); + assertFalse(itemList.get(1).isUpdated()); + + assertEquals(1, itemList.get(2).getInputs().length); + assertEquals(2, itemList.get(2).getOutputs().length); + assertEquals("c1", itemList.get(2).getInputs()[0]); + assertEquals("C1", itemList.get(2).getOutputs()[0]); + assertEquals("C2", itemList.get(2).getOutputs()[1]); + assertFalse(itemList.get(2).isUpdated()); + + assertEquals(2, itemList.get(3).getInputs().length); + assertEquals(2, itemList.get(3).getOutputs().length); + assertEquals("X1", itemList.get(3).getInputs()[0]); + assertEquals("x1", itemList.get(3).getInputs()[1]); + assertEquals("X1", itemList.get(3).getOutputs()[0]); + assertEquals("x1", itemList.get(3).getOutputs()[1]); + assertFalse(itemList.get(3).isUpdated()); + + assertEquals(3, itemList.get(4).getInputs().length); + assertEquals(3, itemList.get(4).getOutputs().length); + assertEquals("Y1", itemList.get(4).getInputs()[0]); + assertEquals("y1", itemList.get(4).getInputs()[1]); + assertEquals("y2", itemList.get(4).getInputs()[2]); + assertEquals("Y1", itemList.get(4).getOutputs()[0]); + assertEquals("y1", itemList.get(4).getOutputs()[1]); + assertEquals("y2", itemList.get(4).getOutputs()[2]); + assertFalse(itemList.get(4).isUpdated()); + } + + /* + public void test_insert() { + final PagingList<SynonymItem> itemList1 = synonymFile.selectList(0, 20); + assertEquals(5, itemList1.size()); + + final SynonymItem synonymItem1 = new SynonymItem(0, new String[] { "z1", "z2" }, new String[] { "Z1", "Z2" }); + synonymFile.insert(synonymItem1); + final PagingList<SynonymItem> itemList2 = synonymFile.selectList(0, 20); + assertEquals(6, itemList2.size()); + assertEquals("z1", itemList2.get(5).getInputs()[0]); + assertEquals("z2", itemList2.get(5).getInputs()[1]); + assertEquals("Z1", itemList2.get(5).getOutputs()[0]); + assertEquals("Z2", itemList2.get(5).getOutputs()[1]); + + final SynonymItem synonymItem2 = new SynonymItem(0, new String[] {"z1", "z2" }, new String[] { "z1", "z2" }); + synonymFile.insert(synonymItem2); + final PagingList<SynonymItem> itemList3 = synonymFile.selectList(0, 20); + assertEquals(7, itemList3.size()); + assertEquals("z1", itemList3.get(6).getInputs()[0]); + assertEquals("z2", itemList3.get(6).getInputs()[1]); + assertEquals("z1", itemList3.get(6).getOutputs()[0]); + assertEquals("z2", itemList3.get(6).getOutputs()[1]); + } + + public void test_update() { + final SynonymFile synonymFile = new SynonymFile(file1); + final PagingList<SynonymItem> itemList1 = synonymFile.selectList(0, 20); + assertEquals(5, itemList1.size()); + + final SynonymItem synonymItem1 = itemList1.get(0); + synonymItem1.setNewInputs(new String[] { "a1", "a2" }); + synonymItem1.setNewOutputs(new String[] { "A1", "A2" }); + synonymFile.update(synonymItem1); + final PagingList<SynonymItem> itemList2 = synonymFile.selectList(0, 20); + assertEquals(5, itemList2.size()); + final SynonymItem synonymItem2 = itemList2.get(0); + assertEquals(2, synonymItem2.getInputs().length); + assertEquals(2, synonymItem2.getOutputs().length); + assertEquals("a1", synonymItem2.getInputs()[0]); + assertEquals("a2", synonymItem2.getInputs()[1]); + assertEquals("A1", synonymItem2.getOutputs()[0]); + assertEquals("A2", synonymItem2.getOutputs()[1]); + assertFalse(synonymItem2.isUpdated()); + + final SynonymItem synonymItem3 = itemList2.get(2); + synonymItem3.setNewInputs(new String[] { "c1", "c2" }); + synonymItem3.setNewOutputs(new String[] { "c1", "c2" }); + synonymFile.update(synonymItem3); + final PagingList<SynonymItem> itemList3 = synonymFile.selectList(0, 20); + assertEquals(5, itemList3.size()); + final SynonymItem synonymItem4 = itemList3.get(2); + assertEquals(2, synonymItem4.getInputs().length); + assertEquals(2, synonymItem4.getOutputs().length); + assertEquals("c1", synonymItem4.getInputs()[0]); + assertEquals("c2", synonymItem4.getInputs()[1]); + assertEquals("c1", synonymItem4.getOutputs()[0]); + assertEquals("c2", synonymItem4.getOutputs()[1]); + assertFalse(synonymItem2.isUpdated()); + } + + public void test_delete() throws Exception { + final SynonymFile synonymFile = new SynonymFile(file1); + final PagingList<SynonymItem> itemList1 = synonymFile.selectList(0, 20); + assertEquals(5, itemList1.size()); + + final SynonymItem synonymItem1 = itemList1.get(0); + synonymFile.delete(synonymItem1); + final PagingList<SynonymItem> itemList2 = synonymFile.selectList(0, 20); + assertEquals(4, itemList2.size()); + + final SynonymItem synonymItem2 = itemList2.get(3); + synonymFile.delete(synonymItem2); + final PagingList<SynonymItem> itemList3 = synonymFile.selectList(0, 20); + assertEquals(3, itemList3.size()); + + assertEquals( + "b1,b2=>B1" + Constants.LINE_SEPARATOR + "c1=>C1,C2" + + Constants.LINE_SEPARATOR + "X1,x1" + + Constants.LINE_SEPARATOR, + new String(FileUtil.getBytes(file1), Constants.UTF_8)); + } + */ }