From 3a60a241f4836598dfd66cfb1682cc954e8be7c8 Mon Sep 17 00:00:00 2001 From: koji Date: Mon, 17 Apr 2017 18:26:26 +0900 Subject: [PATCH] OPENNLP-1018: Add more tests for ObjectStreams --- .../tools/ml/model/FileEventStream.java | 15 ++-- .../tools/ml/model/FileEventStreamTest.java | 69 +++++++++++++++++++ .../model/RealValueFileEventStreamTest.java | 69 +++++++++++++++++++ .../tools/util/ParagraphStreamTest.java | 26 ++++--- .../tools/util/PlainTextByLineStreamTest.java | 38 +++++++--- 5 files changed, 192 insertions(+), 25 deletions(-) create mode 100644 opennlp-tools/src/test/java/opennlp/tools/ml/model/FileEventStreamTest.java create mode 100644 opennlp-tools/src/test/java/opennlp/tools/ml/model/RealValueFileEventStreamTest.java diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/FileEventStream.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/FileEventStream.java index 7adc36bf4..6f9e31698 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/FileEventStream.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/FileEventStream.java @@ -23,6 +23,7 @@ import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.Reader; import java.util.StringTokenizer; import opennlp.tools.util.ObjectStream; @@ -33,7 +34,7 @@ */ public class FileEventStream implements ObjectStream { - protected BufferedReader reader; + protected final BufferedReader reader; /** * Creates a new file event stream from the specified file name. @@ -41,18 +42,18 @@ public class FileEventStream implements ObjectStream { * @throws IOException When the specified file can not be read. */ public FileEventStream(String fileName, String encoding) throws IOException { - if (encoding == null) { - reader = new BufferedReader(new FileReader(fileName)); - } - else { - reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),encoding)); - } + this(encoding == null ? + new FileReader(fileName) : new InputStreamReader(new FileInputStream(fileName), encoding)); } public FileEventStream(String fileName) throws IOException { this(fileName,null); } + public FileEventStream(Reader reader) throws IOException { + this.reader = new BufferedReader(reader); + } + /** * Creates a new file event stream from the specified file. * @param file the file containing the events. diff --git a/opennlp-tools/src/test/java/opennlp/tools/ml/model/FileEventStreamTest.java b/opennlp-tools/src/test/java/opennlp/tools/ml/model/FileEventStreamTest.java new file mode 100644 index 000000000..3837a25eb --- /dev/null +++ b/opennlp-tools/src/test/java/opennlp/tools/ml/model/FileEventStreamTest.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 opennlp.tools.ml.model; + +import java.io.IOException; +import java.io.StringReader; + +import org.junit.Assert; +import org.junit.Test; + +public class FileEventStreamTest { + + private static final String EVENTS = + "other wc=ic w&c=he,ic n1wc=lc n1w&c=belongs,lc n2wc=lc\n" + + "other wc=lc w&c=belongs,lc p1wc=ic p1w&c=he,ic n1wc=lc\n" + + "other wc=lc w&c=to,lc p1wc=lc p1w&c=belongs,lc p2wc=ic\n" + + "org-start wc=ic w&c=apache,ic p1wc=lc p1w&c=to,lc\n" + + "org-cont wc=ic w&c=software,ic p1wc=ic p1w&c=apache,ic\n" + + "org-cont wc=ic w&c=foundation,ic p1wc=ic p1w&c=software,ic\n" + + "other wc=other w&c=.,other p1wc=ic\n"; + + @Test + public void testSimpleReading() throws IOException { + FileEventStream feStream = new FileEventStream(new StringReader(EVENTS)); + + Assert.assertEquals("other [wc=ic w&c=he,ic n1wc=lc n1w&c=belongs,lc n2wc=lc]", + feStream.read().toString()); + Assert.assertEquals("other [wc=lc w&c=belongs,lc p1wc=ic p1w&c=he,ic n1wc=lc]", + feStream.read().toString()); + Assert.assertEquals("other [wc=lc w&c=to,lc p1wc=lc p1w&c=belongs,lc p2wc=ic]", + feStream.read().toString()); + Assert.assertEquals("org-start [wc=ic w&c=apache,ic p1wc=lc p1w&c=to,lc]", + feStream.read().toString()); + Assert.assertEquals("org-cont [wc=ic w&c=software,ic p1wc=ic p1w&c=apache,ic]", + feStream.read().toString()); + Assert.assertEquals("org-cont [wc=ic w&c=foundation,ic p1wc=ic p1w&c=software,ic]", + feStream.read().toString()); + Assert.assertEquals("other [wc=other w&c=.,other p1wc=ic]", + feStream.read().toString()); + Assert.assertNull(feStream.read()); + } + + @Test + public void testReset() throws IOException { + FileEventStream feStream = new FileEventStream(new StringReader(EVENTS)); + + try { + feStream.reset(); + Assert.fail("UnsupportedOperationException should be thrown"); + } + catch (UnsupportedOperationException expected) { + } + } +} diff --git a/opennlp-tools/src/test/java/opennlp/tools/ml/model/RealValueFileEventStreamTest.java b/opennlp-tools/src/test/java/opennlp/tools/ml/model/RealValueFileEventStreamTest.java new file mode 100644 index 000000000..9474dc6a0 --- /dev/null +++ b/opennlp-tools/src/test/java/opennlp/tools/ml/model/RealValueFileEventStreamTest.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 opennlp.tools.ml.model; + +import java.io.IOException; +import java.io.StringReader; + +import org.junit.Assert; +import org.junit.Test; + +public class RealValueFileEventStreamTest { + + private static final String EVENTS = + "other wc=ic=1 w&c=he,ic=2 n1wc=lc=3 n1w&c=belongs,lc=4 n2wc=lc=5\n" + + "other wc=lc=1 w&c=belongs,lc=2 p1wc=ic=3 p1w&c=he,ic=4 n1wc=lc=5\n" + + "other wc=lc=1 w&c=to,lc=2 p1wc=lc=3 p1w&c=belongs,lc=4 p2wc=ic=5\n" + + "org-start wc=ic=1 w&c=apache,ic=2 p1wc=lc=3 p1w&c=to,lc=4\n" + + "org-cont wc=ic=1 w&c=software,ic=2 p1wc=ic=3 p1w&c=apache,ic=4\n" + + "org-cont wc=ic=1 w&c=foundation,ic=2 p1wc=ic=3 p1w&c=software,ic=4\n" + + "other wc=other=1 w&c=.,other=2 p1wc=ic=3\n"; + + @Test + public void testSimpleReading() throws IOException { + FileEventStream feStream = new FileEventStream(new StringReader(EVENTS)); + + Assert.assertEquals("other [wc=ic=1 w&c=he,ic=2 n1wc=lc=3 n1w&c=belongs,lc=4 n2wc=lc=5]", + feStream.read().toString()); + Assert.assertEquals("other [wc=lc=1 w&c=belongs,lc=2 p1wc=ic=3 p1w&c=he,ic=4 n1wc=lc=5]", + feStream.read().toString()); + Assert.assertEquals("other [wc=lc=1 w&c=to,lc=2 p1wc=lc=3 p1w&c=belongs,lc=4 p2wc=ic=5]", + feStream.read().toString()); + Assert.assertEquals("org-start [wc=ic=1 w&c=apache,ic=2 p1wc=lc=3 p1w&c=to,lc=4]", + feStream.read().toString()); + Assert.assertEquals("org-cont [wc=ic=1 w&c=software,ic=2 p1wc=ic=3 p1w&c=apache,ic=4]", + feStream.read().toString()); + Assert.assertEquals("org-cont [wc=ic=1 w&c=foundation,ic=2 p1wc=ic=3 p1w&c=software,ic=4]", + feStream.read().toString()); + Assert.assertEquals("other [wc=other=1 w&c=.,other=2 p1wc=ic=3]", + feStream.read().toString()); + Assert.assertNull(feStream.read()); + } + + @Test + public void testReset() throws IOException { + FileEventStream feStream = new FileEventStream(new StringReader(EVENTS)); + + try { + feStream.reset(); + Assert.fail("UnsupportedOperationException should be thrown"); + } + catch (UnsupportedOperationException expected) { + } + } +} diff --git a/opennlp-tools/src/test/java/opennlp/tools/util/ParagraphStreamTest.java b/opennlp-tools/src/test/java/opennlp/tools/util/ParagraphStreamTest.java index 82ba01d1d..3d0c11737 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/util/ParagraphStreamTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/util/ParagraphStreamTest.java @@ -26,23 +26,31 @@ public class ParagraphStreamTest { @Test public void testSimpleReading() throws IOException { - String line1 = "1"; - String line2 = "2"; - String line3 = ""; - String line4 = "4"; - String line5 = "5"; - String line6 = ""; - ParagraphStream paraStream = new ParagraphStream( - ObjectStreamUtils.createObjectStream(line1, line2, line3, line4, line5)); + ObjectStreamUtils.createObjectStream("1", "2", "", "", "4", "5")); Assert.assertEquals("1\n2\n", paraStream.read()); Assert.assertEquals("4\n5\n", paraStream.read()); + Assert.assertNull(paraStream.read()); paraStream = new ParagraphStream( - ObjectStreamUtils.createObjectStream(line1, line2, line3, line4, line5, line6)); + ObjectStreamUtils.createObjectStream("1", "2", "", "", "4", "5", "")); + + Assert.assertEquals("1\n2\n", paraStream.read()); + Assert.assertEquals("4\n5\n", paraStream.read()); + Assert.assertNull(paraStream.read()); + } + + @Test + public void testReset() throws IOException { + ParagraphStream paraStream = new ParagraphStream( + ObjectStreamUtils.createObjectStream("1", "2", "", "", "4", "5", "")); + + Assert.assertEquals("1\n2\n", paraStream.read()); + paraStream.reset(); Assert.assertEquals("1\n2\n", paraStream.read()); Assert.assertEquals("4\n5\n", paraStream.read()); + Assert.assertNull(paraStream.read()); } } diff --git a/opennlp-tools/src/test/java/opennlp/tools/util/PlainTextByLineStreamTest.java b/opennlp-tools/src/test/java/opennlp/tools/util/PlainTextByLineStreamTest.java index 5307f6fb4..c551b3a46 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/util/PlainTextByLineStreamTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/util/PlainTextByLineStreamTest.java @@ -28,24 +28,44 @@ */ public class PlainTextByLineStreamTest { + static final String testString = "line1" + + '\n' + + "line2" + + '\n' + + "line3" + + "\r\n" + + "line4" + + '\n'; + @Test public void testLineSegmentation() throws IOException { - String testString = "line1" + - '\n' + - "line2" + - '\n' + - "line3" + - "\r\n" + - "line4" + - '\n'; + ObjectStream stream = + new PlainTextByLineStream(new MockInputStreamFactory(testString), StandardCharsets.UTF_8); + + Assert.assertEquals("line1", stream.read()); + Assert.assertEquals("line2", stream.read()); + Assert.assertEquals("line3", stream.read()); + Assert.assertEquals("line4", stream.read()); + Assert.assertNull(stream.read()); + stream.close(); + } + + @Test + public void testReset() throws IOException { ObjectStream stream = - new PlainTextByLineStream(new MockInputStreamFactory(testString), StandardCharsets.UTF_8); + new PlainTextByLineStream(new MockInputStreamFactory(testString), StandardCharsets.UTF_8); + + Assert.assertEquals("line1", stream.read()); + Assert.assertEquals("line2", stream.read()); + Assert.assertEquals("line3", stream.read()); + stream.reset(); Assert.assertEquals("line1", stream.read()); Assert.assertEquals("line2", stream.read()); Assert.assertEquals("line3", stream.read()); Assert.assertEquals("line4", stream.read()); + Assert.assertNull(stream.read()); stream.close(); }