Author: schor Date: Wed Jul 18 18:28:58 2018 New Revision: 1836216 URL: http://svn.apache.org/viewvc?rev=1836216&view=rev Log: [UIMA-5837] test cases for select
Added: uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/resources/ExampleCas/testTypeSystem_token_sentence_no_features.xml Modified: uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java Modified: uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java URL: http://svn.apache.org/viewvc/uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java?rev=1836216&r1=1836215&r2=1836216&view=diff ============================================================================== --- uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java (original) +++ uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java Wed Jul 18 18:28:58 2018 @@ -24,11 +24,13 @@ import static org.junit.Assert.assertFal import static org.junit.Assert.assertTrue; import java.io.File; +import java.util.Arrays; import java.util.Iterator; import java.util.List; import org.apache.uima.UIMAFramework; import org.apache.uima.jcas.JCas; +import org.apache.uima.jcas.tcas.Annotation; import org.apache.uima.resource.metadata.TypeSystemDescription; import org.apache.uima.resource.metadata.impl.TypePriorities_impl; import org.apache.uima.test.junit_extension.JUnitExtension; @@ -37,6 +39,7 @@ import org.apache.uima.util.XMLInputSour import org.junit.BeforeClass; import org.junit.Test; +import x.y.z.Sentence; import x.y.z.Token; public class SelectFsTest { @@ -45,7 +48,7 @@ public class SelectFsTest { static private CASImpl cas; - static File typeSystemFile1 = JUnitExtension.getFile("ExampleCas/testTypeSystem_token_no_features.xml"); + static File typeSystemFile1 = JUnitExtension.getFile("ExampleCas/testTypeSystem_token_sentence_no_features.xml"); @BeforeClass public static void setUpClass() throws Exception { @@ -57,7 +60,7 @@ public class SelectFsTest { @Test public void testSelect_asList() { - + cas.reset(); JCas jcas = cas.getJCas(); Token p1 = new Token(jcas, 0, 1); @@ -85,18 +88,79 @@ public class SelectFsTest { Iterator<Token> it = jcas.select(Token.class).preceding(c).iterator(); assertTrue(it.hasNext()); Token x = it.next(); - assertTrue(x == p2); + assertTrue(x == p1); assertTrue(it.hasNext()); x = it.next(); - assertTrue(x == p1); + assertTrue(x == p2); assertFalse(it.hasNext()); + List<Token> preceedingTokens = jcas.select(Token.class).preceding(c).limit(2).asList(); assertEquals(2, preceedingTokens.size()); - assertTrue(preceedingTokens.get(0) == p2); - assertTrue(preceedingTokens.get(1) == p1); + assertTrue(preceedingTokens.get(0) == p1); + assertTrue(preceedingTokens.get(1) == p2); } + + @Test + public void testPrecedingAndShifted() { + cas.reset(); + JCas jCas = cas.getJCas(); + Annotation a = new Annotation(jCas, 0, 1); + Annotation b = new Annotation(jCas, 2, 3); + Annotation c = new Annotation(jCas, 4, 5); + + for (Annotation ann : Arrays.asList(a, b, c)) { + ann.addToIndexes(); + } + + // uimaFIT: Arrays.asList(a, b), selectPreceding(this.jCas, Annotation.class, c, 2)); + // Produces reverse order + assertEquals(Arrays.asList(a, b), jCas.select(Annotation.class).preceding(c).limit(2).asList()); + // Produces: java.lang.IllegalArgumentException: Strict requires BoundsUse.coveredBy + assertEquals(Arrays.asList(a, b), jCas.select(Annotation.class).startAt(c).shifted(-2).limit(2).asList()); + } + @Test + public void testBetween() { + cas.reset(); + JCas jCas = cas.getJCas(); + Token t1 = new Token(jCas, 45, 57); + t1.addToIndexes(); + Token t2 = new Token(jCas, 52, 52); + t2.addToIndexes(); + + new Sentence(jCas, 52, 52).addToIndexes(); + + // uimaFIT: selectBetween(jCas, Sentence.class, t1, t2); + List<Sentence> stem1 = jCas.select(Sentence.class).between(t1, t2).asList(); + assertTrue(stem1.isEmpty()); + + t1 = new Token(jCas, 45, 52); + stem1 = jCas.select(Sentence.class).between(t1, t2).asList(); + assertEquals(1, stem1.size()); + } + + @Test + public void testBackwards() { + cas.reset(); + JCas jcas = cas.getJCas(); + cas.setDocumentText("t1 t2 t3 t4"); + + Token p1 = new Token(jcas, 0, 2); + p1.addToIndexes(); + + Token p2 = new Token(jcas, 3, 5); + p2 .addToIndexes(); + + Token p3 = new Token(jcas, 6, 8); + p3 .addToIndexes(); + + Token p4 = new Token(jcas, 9, 11); + p4 .addToIndexes(); + + // uimaFIT: JCasUtil.selectByIndex(jCas, Token.class, -1).getCoveredText() + assertEquals("t4", jcas.select(Token.class).backwards().get(0).getCoveredText()); + } } Added: uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/resources/ExampleCas/testTypeSystem_token_sentence_no_features.xml URL: http://svn.apache.org/viewvc/uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/resources/ExampleCas/testTypeSystem_token_sentence_no_features.xml?rev=1836216&view=auto ============================================================================== --- uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/resources/ExampleCas/testTypeSystem_token_sentence_no_features.xml (added) +++ uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/resources/ExampleCas/testTypeSystem_token_sentence_no_features.xml Wed Jul 18 18:28:58 2018 @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * 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. + --> +<typeSystemDescription xmlns="http://uima.apache.org/resourceSpecifier"> + <types> + <typeDescription> + <name>x.y.z.Token</name> + <description/> + <supertypeName>uima.tcas.Annotation</supertypeName> + </typeDescription> + <typeDescription> + <name>x.y.z.Sentence</name> + <description/> + <supertypeName>uima.tcas.Annotation</supertypeName> + </typeDescription> + + </types> +</typeSystemDescription>