You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by re...@apache.org on 2013/01/18 01:26:27 UTC
svn commit: r1434987 [3/7] - in /uima/sandbox/uimafit/trunk: ./
uimafit-examples/ uimafit-legacy-support/ uimafit-legacy-support/.settings/
uimafit-legacy-support/src/ uimafit-legacy-support/src/main/
uimafit-legacy-support/src/main/java/ uimafit-legac...
Added: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnalysisEngineFactoryExternalResourceTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnalysisEngineFactoryExternalResourceTest.java?rev=1434987&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnalysisEngineFactoryExternalResourceTest.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnalysisEngineFactoryExternalResourceTest.java Fri Jan 18 00:26:25 2013
@@ -0,0 +1,406 @@
+/*
+ * 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 org.apache.uima.fit.factory;
+
+import static org.apache.uima.fit.factory.AnalysisEngineFactory.createAggregate;
+import static org.apache.uima.fit.factory.AnalysisEngineFactory.createAggregateDescription;
+import static org.apache.uima.fit.factory.AnalysisEngineFactory.createPrimitive;
+import static org.apache.uima.fit.factory.AnalysisEngineFactory.createPrimitiveDescription;
+import static org.apache.uima.fit.factory.ExternalResourceFactory.bindExternalResource;
+import static org.apache.uima.fit.factory.ExternalResourceFactory.createExternalResourceDescription;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import org.apache.commons.io.output.ByteArrayOutputStream;
+import org.apache.uima.UIMAFramework;
+import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.analysis_engine.AnalysisEngineDescription;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.fit.component.CasAnnotator_ImplBase;
+import org.apache.uima.fit.component.ExternalResourceAware;
+import org.uimafit.descriptor.ConfigurationParameter;
+import org.uimafit.descriptor.ExternalResource;
+import org.apache.uima.fit.factory.testRes.TestExternalResource;
+import org.apache.uima.fit.factory.testRes.TestSharedResourceObject;
+import org.apache.uima.resource.CustomResourceSpecifier;
+import org.apache.uima.resource.ExternalResourceDescription;
+import org.apache.uima.resource.ResourceCreationSpecifier;
+import org.apache.uima.resource.metadata.ExternalResourceBinding;
+import org.apache.uima.util.InvalidXMLException;
+import org.apache.uima.util.XMLInputSource;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestName;
+import org.xml.sax.SAXException;
+
+/**
+ */
+public class AnalysisEngineFactoryExternalResourceTest {
+ /**
+ * Test simple injection.
+ */
+ @Test
+ public void resource_testInjection() throws Exception {
+ AnalysisEngineDescription aeDesc = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithResource.class,
+ TestAnalysisEngineWithResource.PARAM_RESOURCE,
+ createExternalResourceDescription(TestExternalResource.class,
+ TestExternalResource.PARAM_VALUE, TestExternalResource.EXPECTED_VALUE)));
+
+ AnalysisEngine ae = createPrimitive(aeDesc);
+ ae.process(ae.newCAS());
+ }
+
+ /**
+ * Test shared simple injection.
+ */
+ @Test
+ public void resource_testSharedInjection() throws Exception {
+ ExternalResourceDescription resDesc = createExternalResourceDescription(
+ TestExternalResource.class, TestExternalResource.PARAM_VALUE,
+ TestExternalResource.EXPECTED_VALUE);
+
+ AnalysisEngineDescription aeDesc1 = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithResource.class, TestAnalysisEngineWithResource.PARAM_RESOURCE,
+ resDesc));
+
+ AnalysisEngineDescription aeDesc2 = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithResource.class, TestAnalysisEngineWithResource.PARAM_RESOURCE,
+ resDesc));
+
+ dumpResourceConfiguration(aeDesc1);
+ dumpResourceConfiguration(aeDesc2);
+
+ AnalysisEngine ae1 = createPrimitive(aeDesc1);
+ AnalysisEngine ae2 = createPrimitive(aeDesc2);
+ ae1.process(ae1.newCAS());
+ ae2.process(ae2.newCAS());
+
+ AnalysisEngine ae3 = createAggregate(createAggregateDescription(aeDesc1, aeDesc2));
+ ae3.process(ae3.newCAS());
+ }
+
+ /**
+ * Test simple nesting.
+ */
+ @Test
+ public void resource_testSimpleNesting() throws Exception {
+ AnalysisEngineDescription aeDesc = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithResource.class,
+ TestAnalysisEngineWithResource.PARAM_RESOURCE,
+ createExternalResourceDescription(
+ TestExternalResource2.class,
+ TestExternalResource.PARAM_VALUE,
+ TestExternalResource.EXPECTED_VALUE,
+ TestExternalResource2.PARAM_RESOURCE,
+ createExternalResourceDescription(TestExternalResource.class,
+ TestExternalResource.PARAM_VALUE, TestExternalResource.EXPECTED_VALUE))));
+
+ AnalysisEngine ae = createPrimitive(aeDesc);
+ ae.process(ae.newCAS());
+ }
+
+ /**
+ * Test simple nesting.
+ */
+ @Test
+ public void resource_testSharedSimpleNesting() throws Exception {
+ ExternalResourceDescription resDesc = createExternalResourceDescription(
+ TestExternalResource2.class,
+ TestExternalResource.PARAM_VALUE,
+ TestExternalResource.EXPECTED_VALUE,
+ TestExternalResource2.PARAM_RESOURCE,
+ createExternalResourceDescription(TestExternalResource.class,
+ TestExternalResource.PARAM_VALUE, TestExternalResource.EXPECTED_VALUE));
+
+ AnalysisEngineDescription aeDesc1 = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithResource.class, TestAnalysisEngineWithResource.PARAM_RESOURCE,
+ resDesc));
+
+ dumpResourceConfiguration(aeDesc1);
+
+ AnalysisEngineDescription aeDesc2 = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithResource.class, TestAnalysisEngineWithResource.PARAM_RESOURCE,
+ resDesc));
+
+ dumpResourceConfiguration(aeDesc1);
+ dumpResourceConfiguration(aeDesc2);
+
+ AnalysisEngine ae1 = createPrimitive(aeDesc1);
+ AnalysisEngine ae2 = createPrimitive(aeDesc2);
+ ae1.process(ae1.newCAS());
+ ae2.process(ae2.newCAS());
+
+ AnalysisEngine ae3 = createAggregate(createAggregateDescription(aeDesc1, aeDesc2));
+ ae3.process(ae3.newCAS());
+ }
+
+ /**
+ * Test deeper nesting level.
+ */
+ @Test
+ public void resource_testDeeperNesting() throws Exception {
+ ExternalResourceDescription resDesc2 = createExternalResourceDescription(
+ TestExternalResource.class, TestExternalResource.PARAM_VALUE,
+ TestExternalResource.EXPECTED_VALUE);
+
+ ExternalResourceDescription resDesc = createExternalResourceDescription(
+ TestExternalResource2.class, TestExternalResource2.PARAM_RESOURCE, resDesc2,
+ TestExternalResource.PARAM_VALUE, TestExternalResource.EXPECTED_VALUE);
+
+ AnalysisEngineDescription aeDesc = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithResource.class,
+ TestAnalysisEngineWithResource.PARAM_RESOURCE,
+ createExternalResourceDescription(TestExternalResource2.class,
+ TestExternalResource.PARAM_VALUE, TestExternalResource.EXPECTED_VALUE,
+ TestExternalResource2.PARAM_RESOURCE, resDesc)));
+
+ dumpResourceConfiguration(aeDesc);
+
+ AnalysisEngine ae = createPrimitive(aeDesc);
+ ae.process(ae.newCAS());
+ }
+
+ /**
+ * Test self-injection
+ */
+ @Test
+ public void resource_testSelfInjection() throws Exception {
+ ExternalResourceDescription resDesc = createExternalResourceDescription(
+ TestExternalResource2.class, TestExternalResource.PARAM_VALUE,
+ TestExternalResource.EXPECTED_VALUE);
+ bindExternalResource(resDesc, TestExternalResource2.PARAM_RESOURCE, resDesc);
+
+ AnalysisEngineDescription aeDesc = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithResource.class, TestAnalysisEngineWithResource.PARAM_RESOURCE,
+ resDesc));
+
+ dumpResourceConfiguration(aeDesc);
+
+ AnalysisEngine ae = createPrimitive(aeDesc);
+ ae.process(ae.newCAS());
+ }
+
+ /**
+ * Test self-injection
+ */
+ @Test
+ public void resource_testDoubleSelfInjection() throws Exception {
+ ExternalResourceDescription resDesc = createExternalResourceDescription(
+ TestExternalResource2.class, TestExternalResource.PARAM_VALUE,
+ TestExternalResource.EXPECTED_VALUE);
+ bindExternalResource(resDesc, TestExternalResource2.PARAM_RESOURCE, resDesc);
+
+ AnalysisEngineDescription aeDesc1 = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithResource.class, TestAnalysisEngineWithResource.PARAM_RESOURCE,
+ resDesc));
+
+ AnalysisEngineDescription aeDesc2 = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithResource.class, TestAnalysisEngineWithResource.PARAM_RESOURCE,
+ resDesc));
+
+ dumpResourceConfiguration(aeDesc1);
+ dumpResourceConfiguration(aeDesc2);
+
+ AnalysisEngine ae1 = createPrimitive(aeDesc1);
+ AnalysisEngine ae2 = createPrimitive(aeDesc2);
+ ae1.process(ae1.newCAS());
+ ae2.process(ae2.newCAS());
+
+ AnalysisEngine ae3 = createAggregate(createAggregateDescription(aeDesc1, aeDesc2));
+ ae3.process(ae3.newCAS());
+ }
+
+ /**
+ * Test simple injection.
+ */
+ @Test
+ public void sharedObject_testInjection() throws Exception {
+ AnalysisEngineDescription aeDesc = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithSharedResourceObject.class,
+ TestAnalysisEngineWithSharedResourceObject.PARAM_RESOURCE,
+ createExternalResourceDescription(TestSharedResourceObject.class, "http://dumm.my",
+ TestSharedResourceObject.PARAM_VALUE, TestSharedResourceObject.EXPECTED_VALUE)));
+
+ AnalysisEngine ae = createPrimitive(aeDesc);
+ ae.process(ae.newCAS());
+ }
+
+ /**
+ * Test shared simple injection.
+ */
+ @Test
+ public void sharedObject_testSharedInjection() throws Exception {
+ ExternalResourceDescription resDesc = createExternalResourceDescription(
+ TestSharedResourceObject.class, "http://dumm.my", TestSharedResourceObject.PARAM_VALUE,
+ TestSharedResourceObject.EXPECTED_VALUE);
+
+ AnalysisEngineDescription aeDesc1 = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithSharedResourceObject.class,
+ TestAnalysisEngineWithSharedResourceObject.PARAM_RESOURCE, resDesc));
+
+ AnalysisEngineDescription aeDesc2 = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithSharedResourceObject.class,
+ TestAnalysisEngineWithSharedResourceObject.PARAM_RESOURCE, resDesc));
+
+ dumpResourceConfiguration(aeDesc1);
+ dumpResourceConfiguration(aeDesc2);
+
+ AnalysisEngine ae1 = createPrimitive(aeDesc1);
+ AnalysisEngine ae2 = createPrimitive(aeDesc2);
+ ae1.process(ae1.newCAS());
+ ae2.process(ae2.newCAS());
+
+ AnalysisEngine ae3 = createAggregate(createAggregateDescription(aeDesc1, aeDesc2));
+ ae3.process(ae3.newCAS());
+ }
+
+ /**
+ * Test self-injection
+ */
+ @Test
+ public void sharedObject_testSelfInjection() throws Exception {
+ ExternalResourceDescription resDesc = createExternalResourceDescription(
+ TestSharedResourceObject2.class, "http://dumm.my",
+ TestSharedResourceObject.PARAM_VALUE, TestSharedResourceObject.EXPECTED_VALUE);
+ bindExternalResource(resDesc, TestSharedResourceObject2.PARAM_RESOURCE, resDesc);
+
+ AnalysisEngineDescription aeDesc = saveLoad(createPrimitiveDescription(
+ TestAnalysisEngineWithSharedResourceObject.class,
+ TestAnalysisEngineWithSharedResourceObject.PARAM_RESOURCE, resDesc));
+
+ dumpResourceConfiguration(aeDesc);
+
+ AnalysisEngine ae = createPrimitive(aeDesc);
+ ae.process(ae.newCAS());
+ }
+
+ public static class TestExternalResource2 extends TestExternalResource {
+ public final static String PARAM_RESOURCE = "resource2";
+
+ @ExternalResource(key = PARAM_RESOURCE)
+ private TestExternalResource resource;
+
+ @Override
+ public void afterResourcesInitialized() {
+ System.out.println(getClass().getSimpleName() + ".afterResourcesInitialized()");
+ // Ensure the External Resource is bound
+ assertNotNull(resource);
+ if (this != resource) {
+ resource.assertConfiguredOk();
+ }
+ assertConfiguredOk();
+ }
+ }
+
+ public static class TestSharedResourceObject2 extends TestSharedResourceObject implements
+ ExternalResourceAware {
+ public final static String PARAM_RESOURCE = "resource2";
+
+ @ExternalResource(key = PARAM_RESOURCE)
+ private TestSharedResourceObject resource;
+
+ @ConfigurationParameter(name = ExternalResourceFactory.PARAM_RESOURCE_NAME)
+ private String resourceName;
+
+ public void afterResourcesInitialized() {
+ System.out.println(getClass().getSimpleName() + ".afterResourcesInitialized()");
+ // Ensure the External Resource is bound
+ assertNotNull(resource);
+ if (this != resource) {
+ resource.assertConfiguredOk();
+ }
+ assertConfiguredOk();
+ }
+
+ public String getResourceName() {
+ return resourceName;
+ }
+ }
+
+ public static class TestAnalysisEngineWithResource extends CasAnnotator_ImplBase {
+
+ public final static String PARAM_RESOURCE = "resource";
+
+ @ExternalResource(key = PARAM_RESOURCE)
+ private TestExternalResource resource;
+
+ @Override
+ public void process(CAS aCAS) throws AnalysisEngineProcessException {
+ System.out.println(getClass().getSimpleName() + ".process()");
+ assertNotNull(resource);
+ resource.assertConfiguredOk();
+ }
+ }
+
+ public static class TestAnalysisEngineWithSharedResourceObject extends CasAnnotator_ImplBase {
+
+ public final static String PARAM_RESOURCE = "resource";
+
+ @ExternalResource(key = PARAM_RESOURCE)
+ private TestSharedResourceObject resource;
+
+ @Override
+ public void process(CAS aCAS) throws AnalysisEngineProcessException {
+ System.out.println(getClass().getSimpleName() + ".process()");
+ assertNotNull(resource);
+ resource.assertConfiguredOk();
+ }
+ }
+
+ AnalysisEngineDescription saveLoad(AnalysisEngineDescription aDesc) throws InvalidXMLException,
+ SAXException, IOException {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ aDesc.toXML(bos);
+ return UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
+ new XMLInputSource(new ByteArrayInputStream(bos.toByteArray()), null));
+ }
+
+ private void dumpResourceConfiguration(ResourceCreationSpecifier aSpec) {
+ System.out.println("-- begin resource configuration");
+ for (ExternalResourceBinding b : aSpec.getResourceManagerConfiguration()
+ .getExternalResourceBindings()) {
+ System.out.printf("Binding : %s -> %s %n", b.getKey(), b.getResourceName());
+ }
+
+ for (ExternalResourceDescription r : aSpec.getResourceManagerConfiguration()
+ .getExternalResources()) {
+ if (r.getImplementationName() != null) {
+ System.out.printf("Resource: %s -> %s %n", r.getName(), r.getImplementationName());
+ } else {
+ System.out.printf("Resource: %s -> %s %n", r.getName(),
+ ((CustomResourceSpecifier) r.getResourceSpecifier()).getResourceClassName());
+ }
+ }
+ System.out.println("-- end resource configuration");
+ }
+
+ @Rule
+ public TestName name = new TestName();
+
+ @Before
+ public void printSeparator() {
+ System.out.println("\n=== " + name.getMethodName() + " =====================");
+ }
+}
Propchange: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnalysisEngineFactoryExternalResourceTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnalysisEngineFactoryTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnalysisEngineFactoryTest.java?rev=1434987&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnalysisEngineFactoryTest.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnalysisEngineFactoryTest.java Fri Jan 18 00:26:25 2013
@@ -0,0 +1,480 @@
+/*
+ * 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 org.apache.uima.fit.factory;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.uima.UIMAException;
+import org.apache.uima.UIMAFramework;
+import org.apache.uima.analysis_component.AnalysisComponent;
+import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.analysis_engine.AnalysisEngineDescription;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.analysis_engine.metadata.SofaMapping;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.fit.ComponentTestBase;
+import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
+import org.apache.uima.fit.component.NoOpAnnotator;
+import org.uimafit.descriptor.OperationalProperties;
+import org.apache.uima.fit.factory.testAes.Annotator1;
+import org.apache.uima.fit.factory.testAes.Annotator2;
+import org.apache.uima.fit.factory.testAes.Annotator3;
+import org.apache.uima.fit.factory.testAes.Annotator4;
+import org.apache.uima.fit.factory.testAes.ParameterizedAE;
+import org.apache.uima.fit.factory.testAes.ViewNames;
+import org.apache.uima.fit.pipeline.SimplePipeline;
+import org.apache.uima.fit.type.Sentence;
+import org.apache.uima.fit.type.Token;
+import org.apache.uima.fit.util.JCasUtil;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.jcas.tcas.Annotation;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.metadata.Capability;
+import org.apache.uima.resource.metadata.ConfigurationParameter;
+import org.apache.uima.resource.metadata.ConfigurationParameterDeclarations;
+import org.apache.uima.resource.metadata.ConfigurationParameterSettings;
+import org.apache.uima.resource.metadata.TypePriorities;
+import org.apache.uima.resource.metadata.TypePriorityList;
+import org.junit.Test;
+
+/**
+ */
+
+public class AnalysisEngineFactoryTest extends ComponentTestBase {
+
+ @Test
+ public void testViewAE() throws Exception {
+ AnalysisEngineDescription aed = AnalysisEngineFactory.createPrimitiveDescription(
+ Annotator4.class, typeSystemDescription);
+ AnalysisEngine ae = AnalysisEngineFactory.createAnalysisEngine(aed, "A");
+
+ JCas aView = jCas.createView("A");
+ tokenBuilder.buildTokens(aView, "'Verb' is a noun!?");
+ ae.process(jCas);
+ assertEquals("'Verb' is a noun!?", jCas.getView("A").getDocumentText());
+ assertEquals("NN", JCasUtil.selectByIndex(aView, Token.class, 0).getPos());
+ }
+
+ @Test
+ public void testCreateAnalysisEngineFromPath() throws UIMAException, IOException {
+ AnalysisEngine engine = AnalysisEngineFactory
+ .createAnalysisEngineFromPath("src/test/resources/org/apache/uima/fit/component/NoOpAnnotator.xml");
+ assertNotNull(engine);
+ }
+
+ @Test
+ public void testProcess1() throws UIMAException, IOException {
+ jCas = AnalysisEngineFactory.process(NoOpAnnotator.class.getName(), "There is no excuse!");
+
+ assertEquals("There is no excuse!", jCas.getDocumentText());
+ }
+
+ @Test
+ public void testProcess2() throws UIMAException, IOException {
+ jCas = AnalysisEngineFactory.process(NoOpAnnotator.class.getName(),
+ "src/test/resources/data/docs/A.txt");
+
+ assertEquals("Aaa Bbbb Cc Dddd eeee ff .", jCas.getDocumentText());
+ }
+
+ @Test
+ public void testCreateAnalysisEngineWithPrioritizedTypes() throws UIMAException {
+ String[] prioritizedTypeNames = new String[] { "org.apache.uima.fit.type.Token",
+ "org.apache.uima.fit.type.Sentence" };
+ AnalysisEngine engine = AnalysisEngineFactory.createPrimitive(
+ org.apache.uima.fit.component.NoOpAnnotator.class, typeSystemDescription,
+ prioritizedTypeNames, (Object[]) null);
+
+ typePriorities = engine.getAnalysisEngineMetaData().getTypePriorities();
+ assertEquals(1, typePriorities.getPriorityLists().length);
+ TypePriorityList typePriorityList = typePriorities.getPriorityLists()[0];
+ assertEquals(2, typePriorityList.getTypes().length);
+ assertEquals("org.apache.uima.fit.type.Token", typePriorityList.getTypes()[0]);
+ assertEquals("org.apache.uima.fit.type.Sentence", typePriorityList.getTypes()[1]);
+
+ jCas = engine.newJCas();
+ tokenBuilder.buildTokens(jCas, "word");
+ FSIterator<Annotation> tokensInSentence = jCas.getAnnotationIndex().subiterator(
+ JCasUtil.selectByIndex(jCas, Sentence.class, 0));
+ assertFalse(tokensInSentence.hasNext());
+
+ prioritizedTypeNames = new String[] { "org.apache.uima.fit.type.Sentence",
+ "org.apache.uima.fit.type.Token" };
+ engine = AnalysisEngineFactory.createPrimitive(
+ org.apache.uima.fit.component.NoOpAnnotator.class, typeSystemDescription,
+ prioritizedTypeNames, (Object[]) null);
+ jCas = engine.newJCas();
+ tokenBuilder.buildTokens(jCas, "word");
+ tokensInSentence = jCas.getAnnotationIndex().subiterator(
+ JCasUtil.selectByIndex(jCas, Sentence.class, 0));
+ assertTrue(tokensInSentence.hasNext());
+
+ }
+
+ @Test
+ public void testAggregate() throws UIMAException {
+ tokenBuilder.buildTokens(jCas, "Anyone up for a game of Foosball?");
+
+ SofaMapping[] sofaMappings = new SofaMapping[] {
+ SofaMappingFactory.createSofaMapping(Annotator1.class, ViewNames.PARENTHESES_VIEW, "A"),
+ SofaMappingFactory.createSofaMapping(Annotator2.class, ViewNames.SORTED_VIEW, "B"),
+ SofaMappingFactory.createSofaMapping(Annotator2.class, ViewNames.SORTED_PARENTHESES_VIEW,
+ "C"),
+ SofaMappingFactory.createSofaMapping(Annotator2.class, ViewNames.PARENTHESES_VIEW, "A"),
+ SofaMappingFactory.createSofaMapping(Annotator3.class, ViewNames.INITIAL_VIEW, "B") };
+
+ List<Class<? extends AnalysisComponent>> primitiveAEClasses = new ArrayList<Class<? extends AnalysisComponent>>();
+ primitiveAEClasses.add(Annotator1.class);
+ primitiveAEClasses.add(Annotator2.class);
+ primitiveAEClasses.add(Annotator3.class);
+
+ AnalysisEngine aggregateEngine = AnalysisEngineFactory.createAggregate(primitiveAEClasses,
+ typeSystemDescription, null, sofaMappings);
+
+ aggregateEngine.process(jCas);
+
+ assertEquals("Anyone up for a game of Foosball?", jCas.getDocumentText());
+ assertEquals("Any(o)n(e) (u)p f(o)r (a) g(a)m(e) (o)f F(oo)sb(a)ll?", jCas.getView("A")
+ .getDocumentText());
+ assertEquals("?AFaaabeeffgllmnnoooooprsuy", jCas.getView("B").getDocumentText());
+ assertEquals("(((((((((())))))))))?AFaaabeeffgllmnnoooooprsuy", jCas.getView("C")
+ .getDocumentText());
+ assertEquals("yusrpooooonnmllgffeebaaaFA?", jCas.getView(ViewNames.REVERSE_VIEW)
+ .getDocumentText());
+
+ }
+
+ @Test
+ public void testAggregate2() throws UIMAException, IOException {
+ tokenBuilder.buildTokens(jCas, "Anyone up for a game of Foosball?");
+
+ SofaMapping[] sofaMappings = new SofaMapping[] {
+ SofaMappingFactory.createSofaMapping("ann1", ViewNames.PARENTHESES_VIEW, "A"),
+ SofaMappingFactory.createSofaMapping("ann2", ViewNames.SORTED_VIEW, "B"),
+ SofaMappingFactory.createSofaMapping("ann2", ViewNames.SORTED_PARENTHESES_VIEW, "C"),
+ SofaMappingFactory.createSofaMapping("ann2", ViewNames.PARENTHESES_VIEW, "A"),
+ SofaMappingFactory.createSofaMapping("ann3", ViewNames.INITIAL_VIEW, "B") };
+
+ List<AnalysisEngineDescription> primitiveDescriptors = new ArrayList<AnalysisEngineDescription>();
+ primitiveDescriptors.add(AnalysisEngineFactory.createPrimitiveDescription(Annotator1.class,
+ typeSystemDescription, (TypePriorities) null));
+ primitiveDescriptors.add(AnalysisEngineFactory.createPrimitiveDescription(Annotator2.class,
+ typeSystemDescription, (TypePriorities) null));
+ primitiveDescriptors.add(AnalysisEngineFactory.createPrimitiveDescription(Annotator3.class,
+ typeSystemDescription, (TypePriorities) null));
+
+ List<String> componentNames = Arrays.asList("ann1", "ann2", "ann3");
+
+ AnalysisEngine aggregateEngine = AnalysisEngineFactory.createAggregate(primitiveDescriptors,
+ componentNames, typeSystemDescription, null, sofaMappings);
+
+ aggregateEngine.process(jCas);
+
+ assertEquals("Anyone up for a game of Foosball?", jCas.getDocumentText());
+ assertEquals("Any(o)n(e) (u)p f(o)r (a) g(a)m(e) (o)f F(oo)sb(a)ll?", jCas.getView("A")
+ .getDocumentText());
+ assertEquals("?AFaaabeeffgllmnnoooooprsuy", jCas.getView("B").getDocumentText());
+ assertEquals("(((((((((())))))))))?AFaaabeeffgllmnnoooooprsuy", jCas.getView("C")
+ .getDocumentText());
+ assertEquals("yusrpooooonnmllgffeebaaaFA?", jCas.getView(ViewNames.REVERSE_VIEW)
+ .getDocumentText());
+
+ JCasFactory.loadJCas(jCas, "src/test/resources/data/docs/test.xmi");
+ AnalysisEngine ae1 = AnalysisEngineFactory.createPrimitive(NoOpAnnotator.class,
+ typeSystemDescription);
+
+ SimplePipeline.runPipeline(jCas, ae1, aggregateEngine);
+
+ }
+
+ @Test
+ public void testReflectPrimitiveDescription() throws ResourceInitializationException {
+ AnalysisEngineDescription aed = AnalysisEngineFactory.createPrimitiveDescription(
+ Annotator2.class, typeSystemDescription, typePriorities);
+ Capability[] capabilities = aed.getAnalysisEngineMetaData().getCapabilities();
+ assertEquals(1, capabilities.length);
+ String[] inputSofas = capabilities[0].getInputSofas();
+ assertArrayEquals(new String[] { CAS.NAME_DEFAULT_SOFA, ViewNames.PARENTHESES_VIEW },
+ inputSofas);
+ String[] outputSofas = capabilities[0].getOutputSofas();
+ assertArrayEquals(new String[] { ViewNames.SORTED_VIEW, ViewNames.SORTED_PARENTHESES_VIEW },
+ outputSofas);
+
+ aed = AnalysisEngineFactory.createPrimitiveDescription(ParameterizedAE.class,
+ typeSystemDescription, typePriorities);
+ capabilities = aed.getAnalysisEngineMetaData().getCapabilities();
+ assertEquals(1, capabilities.length);
+ inputSofas = capabilities[0].getInputSofas();
+ assertArrayEquals(new String[] { CAS.NAME_DEFAULT_SOFA }, inputSofas);
+ outputSofas = capabilities[0].getOutputSofas();
+ assertArrayEquals(new String[] {}, outputSofas);
+
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_STRING_1,
+ ConfigurationParameter.TYPE_STRING, true, false, "pineapple");
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_STRING_2,
+ ConfigurationParameter.TYPE_STRING, false, true, new String[] { "coconut", "mango" });
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_STRING_3,
+ ConfigurationParameter.TYPE_STRING, false, false, null);
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_STRING_4,
+ ConfigurationParameter.TYPE_STRING, true, true, new String[] { "apple" });
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_STRING_5,
+ ConfigurationParameter.TYPE_STRING, false, true, new String[] { "" });
+
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_BOOLEAN_1,
+ ConfigurationParameter.TYPE_BOOLEAN, true, false, Boolean.FALSE);
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_BOOLEAN_2,
+ ConfigurationParameter.TYPE_BOOLEAN, false, false, null);
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_BOOLEAN_3,
+ ConfigurationParameter.TYPE_BOOLEAN, true, true, new Boolean[] { true, true, false });
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_BOOLEAN_4,
+ ConfigurationParameter.TYPE_BOOLEAN, true, true, new Boolean[] { true, false, true });
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_BOOLEAN_5,
+ ConfigurationParameter.TYPE_BOOLEAN, true, true, new Boolean[] { false });
+
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_INT_1,
+ ConfigurationParameter.TYPE_INTEGER, true, false, 0);
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_INT_2,
+ ConfigurationParameter.TYPE_INTEGER, false, false, 42);
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_INT_3,
+ ConfigurationParameter.TYPE_INTEGER, false, true, new Integer[] { 42, 111 });
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_INT_4,
+ ConfigurationParameter.TYPE_INTEGER, true, true, new Integer[] { 2 });
+
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_FLOAT_1,
+ ConfigurationParameter.TYPE_FLOAT, true, false, 0.0f);
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_FLOAT_2,
+ ConfigurationParameter.TYPE_FLOAT, false, false, 3.1415f);
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_FLOAT_3,
+ ConfigurationParameter.TYPE_FLOAT, true, false, null);
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_FLOAT_4,
+ ConfigurationParameter.TYPE_FLOAT, false, true, null);
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_FLOAT_5,
+ ConfigurationParameter.TYPE_FLOAT, false, true,
+ new Float[] { 0.0f, 3.1415f, 2.7182818f });
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_FLOAT_6,
+ ConfigurationParameter.TYPE_FLOAT, true, true, null);
+ testConfigurationParameter(aed, ParameterizedAE.PARAM_FLOAT_7,
+ ConfigurationParameter.TYPE_FLOAT, true, true, new Float[] { 1.1111f, 2.2222f, 3.333f });
+
+ AnalysisEngine ae = AnalysisEngineFactory
+ .createPrimitive(aed, ParameterizedAE.PARAM_FLOAT_3, 3.1415f,
+ ParameterizedAE.PARAM_FLOAT_6, new Float[] { 2.71828183f }, "file2", "foo/bar");
+ Object paramValue = ae.getAnalysisEngineMetaData().getConfigurationParameterSettings()
+ .getParameterValue(ParameterizedAE.PARAM_FLOAT_3);
+ assertEquals(paramValue, 3.1415f);
+ paramValue = ae.getAnalysisEngineMetaData().getConfigurationParameterSettings()
+ .getParameterValue(ParameterizedAE.PARAM_FLOAT_6);
+ assertEquals(((Float[]) paramValue)[0].floatValue(), 2.71828183f, 0.00001f);
+
+ }
+
+ private void testConfigurationParameter(AnalysisEngineDescription aed, String parameterName,
+ String parameterType, boolean mandatory, boolean multiValued, Object parameterValue) {
+ ConfigurationParameterDeclarations cpd = aed.getMetaData()
+ .getConfigurationParameterDeclarations();
+ ConfigurationParameter cp = cpd.getConfigurationParameter(null, parameterName);
+ assertNotNull("Parameter [" + parameterName + "] does not exist!", cp);
+ assertEquals("Parameter [" + parameterName + "] has wrong name", parameterName, cp.getName());
+ assertEquals("Parameter [" + parameterName + "] has wrong type", parameterType, cp.getType());
+ assertEquals("Parameter [" + parameterName + "] has wrong mandatory flag", mandatory,
+ cp.isMandatory());
+ assertEquals("Parameter [" + parameterName + "] has wrong multi-value flag", multiValued,
+ cp.isMultiValued());
+ ConfigurationParameterSettings cps = aed.getMetaData().getConfigurationParameterSettings();
+ Object actualValue = cps.getParameterValue(parameterName);
+ if (parameterValue == null) {
+ assertNull(actualValue);
+ } else if (!multiValued) {
+ if (parameterType.equals(ConfigurationParameter.TYPE_FLOAT)) {
+ assertEquals(((Float) parameterValue).floatValue(), ((Float) actualValue).floatValue(),
+ .001f);
+ } else {
+ assertEquals(parameterValue, actualValue);
+ }
+ } else {
+ assertEquals(Array.getLength(parameterValue), Array.getLength(actualValue));
+ for (int i = 0; i < Array.getLength(parameterValue); ++i) {
+ assertEquals(Array.get(parameterValue, i), Array.get(actualValue, i));
+ }
+ }
+
+ }
+
+ @Test
+ public void testPrimitiveDescription() throws ResourceInitializationException {
+
+ AnalysisEngineDescription aed = AnalysisEngineFactory.createPrimitiveDescription(
+ NoOpAnnotator.class, typeSystemDescription);
+ assertNotNull(aed);
+ // assertEquals("org.apache.uima.fit.type.TypeSystem",
+ // aed.getAnalysisEngineMetaData().getTypeSystem().getImports()[0].getName());
+ }
+
+ /**
+ * Test that a {@link OperationalProperties} annotation on an ancestor of a analysis engine class
+ * is found and taken into account.
+ */
+ @Test
+ public void testComponentAnnotationOnAncestor() throws Exception {
+ AnalysisEngineDescription desc1 = AnalysisEngineFactory.createPrimitiveDescription(
+ PristineAnnotatorClass.class, (Object[]) null);
+ assertTrue(
+ "Multiple deployment should be allowed on " + desc1.getAnnotatorImplementationName(),
+ desc1.getAnalysisEngineMetaData().getOperationalProperties()
+ .isMultipleDeploymentAllowed());
+
+ AnalysisEngineDescription desc2 = AnalysisEngineFactory.createPrimitiveDescription(
+ UnannotatedAnnotatorClass.class, (Object[]) null);
+ assertFalse(
+ "Multiple deployment should be prohibited on " + desc2.getAnnotatorImplementationName(),
+ desc2.getAnalysisEngineMetaData().getOperationalProperties()
+ .isMultipleDeploymentAllowed());
+
+ AnalysisEngineDescription desc3 = AnalysisEngineFactory.createPrimitiveDescription(
+ AnnotatedAnnotatorClass.class, (Object[]) null);
+ assertTrue(
+ "Multiple deployment should be allowed on " + desc3.getAnnotatorImplementationName(),
+ desc3.getAnalysisEngineMetaData().getOperationalProperties()
+ .isMultipleDeploymentAllowed());
+ }
+
+ /*
+ * This test case illustrates that UIMA throws an exception unless the multipleDeploymentAllowed
+ * flag is properly set to false when mixing multi-deployment and non-multi-deployment AEs.
+ */
+ @Test(expected = ResourceInitializationException.class)
+ public void testAAEMultipleDeploymentPolicyProblem() throws Exception {
+ {
+ AnalysisEngineDescription desc1 = AnalysisEngineFactory.createPrimitiveDescription(
+ PristineAnnotatorClass.class, (Object[]) null);
+ assertTrue(
+ "Multiple deployment should be allowed on " + desc1.getAnnotatorImplementationName(),
+ desc1.getAnalysisEngineMetaData().getOperationalProperties()
+ .isMultipleDeploymentAllowed());
+
+ AnalysisEngineDescription desc2 = AnalysisEngineFactory.createPrimitiveDescription(
+ UnannotatedAnnotatorClass.class, (Object[]) null);
+ assertFalse(
+ "Multiple deployment should be prohibited on "
+ + desc2.getAnnotatorImplementationName(), desc2.getAnalysisEngineMetaData()
+ .getOperationalProperties().isMultipleDeploymentAllowed());
+
+ AnalysisEngineDescription aae = AnalysisEngineFactory
+ .createAggregateDescription(desc1, desc2);
+ aae.getAnalysisEngineMetaData().getOperationalProperties().setMultipleDeploymentAllowed(true);
+ UIMAFramework.produceAnalysisEngine(aae);
+ }
+ }
+
+ @Test
+ public void testAAEMultipleDeploymentPolicy() throws Exception {
+ {
+ AnalysisEngineDescription desc1 = AnalysisEngineFactory.createPrimitiveDescription(
+ PristineAnnotatorClass.class, (Object[]) null);
+ assertTrue(
+ "Multiple deployment should be allowed on " + desc1.getAnnotatorImplementationName(),
+ desc1.getAnalysisEngineMetaData().getOperationalProperties()
+ .isMultipleDeploymentAllowed());
+
+ AnalysisEngineDescription desc2 = AnalysisEngineFactory.createPrimitiveDescription(
+ UnannotatedAnnotatorClass.class, (Object[]) null);
+ assertFalse(
+ "Multiple deployment should be prohibited on "
+ + desc2.getAnnotatorImplementationName(), desc2.getAnalysisEngineMetaData()
+ .getOperationalProperties().isMultipleDeploymentAllowed());
+
+ AnalysisEngineDescription aae = AnalysisEngineFactory
+ .createAggregateDescription(desc1, desc2);
+ UIMAFramework.produceAnalysisEngine(aae);
+
+ assertFalse("Multiple deployment should be prohibited on AAE", aae
+ .getAnalysisEngineMetaData().getOperationalProperties().isMultipleDeploymentAllowed());
+ }
+
+ {
+ AnalysisEngineDescription desc1 = AnalysisEngineFactory.createPrimitiveDescription(
+ PristineAnnotatorClass.class, (Object[]) null);
+ assertTrue(
+ "Multiple deployment should be allowed on " + desc1.getAnnotatorImplementationName(),
+ desc1.getAnalysisEngineMetaData().getOperationalProperties()
+ .isMultipleDeploymentAllowed());
+
+ AnalysisEngineDescription desc3 = AnalysisEngineFactory.createPrimitiveDescription(
+ AnnotatedAnnotatorClass.class, (Object[]) null);
+ assertTrue(
+ "Multiple deployment should be allowed on " + desc3.getAnnotatorImplementationName(),
+ desc3.getAnalysisEngineMetaData().getOperationalProperties()
+ .isMultipleDeploymentAllowed());
+
+ AnalysisEngineDescription aae = AnalysisEngineFactory
+ .createAggregateDescription(desc1, desc3);
+ UIMAFramework.produceAnalysisEngine(aae);
+
+ assertTrue("Multiple deployment should be prohibited on AAE", aae.getAnalysisEngineMetaData()
+ .getOperationalProperties().isMultipleDeploymentAllowed());
+ }
+ }
+
+ public static class PristineAnnotatorClass extends JCasAnnotator_ImplBase {
+ @Override
+ public void process(JCas aJCas) throws AnalysisEngineProcessException {
+ // Dummy
+ }
+ }
+
+ @org.apache.uima.fit.descriptor.OperationalProperties(multipleDeploymentAllowed = false)
+ public static class AncestorClass extends JCasAnnotator_ImplBase {
+ @Override
+ public void process(JCas aJCas) throws AnalysisEngineProcessException {
+ // Dummy
+ }
+ }
+
+ public static class UnannotatedAnnotatorClass extends AncestorClass {
+ // Dummy
+ }
+
+ @org.apache.uima.fit.descriptor.OperationalProperties(multipleDeploymentAllowed = true)
+ public static class AnnotatedAnnotatorClass extends UnannotatedAnnotatorClass {
+ // Vessel for the annotation
+ }
+
+ @Test
+ public void testIssue5a() throws ResourceInitializationException {
+ AnalysisEngineFactory.createPrimitiveDescription(ParameterizedAE.class, typeSystemDescription);
+ }
+
+ @Test(expected = ResourceInitializationException.class)
+ public void testIssue5b() throws ResourceInitializationException {
+ AnalysisEngineFactory.createPrimitive(ParameterizedAE.class, typeSystemDescription);
+ }
+
+}
Propchange: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnalysisEngineFactoryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnnotationFactoryTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnnotationFactoryTest.java?rev=1434987&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnnotationFactoryTest.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnnotationFactoryTest.java Fri Jan 18 00:26:25 2013
@@ -0,0 +1,54 @@
+/*
+ * 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 org.apache.uima.fit.factory;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import org.apache.uima.UIMAException;
+import org.apache.uima.fit.ComponentTestBase;
+import org.apache.uima.fit.type.Sentence;
+import org.apache.uima.fit.type.Token;
+import org.junit.Test;
+
+/**
+ */
+
+public class AnnotationFactoryTest extends ComponentTestBase {
+
+ @Test
+ public void testCreateAnnotation() throws UIMAException {
+ Token token = AnnotationFactory.createAnnotation(jCas, 0, 10, Token.class);
+ assertEquals(0, token.getBegin());
+ assertEquals(10, token.getEnd());
+
+ Sentence sentence = AnnotationFactory.createAnnotation(jCas, 0, 10, Sentence.class);
+ assertEquals(0, sentence.getBegin());
+ assertEquals(10, sentence.getEnd());
+
+ UIMAException ue = null;
+ try {
+ AnnotationFactory.createAnnotation(null, 0, 10, Sentence.class);
+ } catch (UIMAException e) {
+ ue = e;
+ }
+ assertNotNull(ue);
+
+ }
+}
Propchange: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/AnnotationFactoryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/CollectionReaderFactoryExternalResourceTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/CollectionReaderFactoryExternalResourceTest.java?rev=1434987&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/CollectionReaderFactoryExternalResourceTest.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/CollectionReaderFactoryExternalResourceTest.java Fri Jan 18 00:26:25 2013
@@ -0,0 +1,91 @@
+/*
+ * 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.
+ */
+
+/*
+ * 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 org.apache.uima.fit.factory;
+
+import static org.apache.uima.fit.factory.CollectionReaderFactory.createCollectionReader;
+import static org.apache.uima.fit.factory.ExternalResourceFactory.createExternalResourceDescription;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+
+import org.apache.uima.UIMAException;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.collection.CollectionException;
+import org.apache.uima.collection.CollectionReader;
+import org.apache.uima.fit.component.CasCollectionReader_ImplBase;
+import org.uimafit.descriptor.ExternalResource;
+import org.apache.uima.fit.factory.testRes.TestExternalResource;
+import org.apache.uima.util.Progress;
+import org.junit.Test;
+
+/**
+ */
+public class CollectionReaderFactoryExternalResourceTest {
+ @Test
+ public void testAutoExternalResourceBinding() throws UIMAException, IOException {
+ CollectionReader reader = createCollectionReader(
+ TestReader.class,
+ TestReader.PARAM_RESOURCE,
+ createExternalResourceDescription(TestExternalResource.class,
+ TestExternalResource.PARAM_VALUE, TestExternalResource.EXPECTED_VALUE));
+
+ reader.hasNext();
+ }
+
+ public static class TestReader extends CasCollectionReader_ImplBase {
+ public final static String PARAM_RESOURCE = "resource";
+
+ @ExternalResource(key = PARAM_RESOURCE)
+ private TestExternalResource resource;
+
+ public boolean hasNext() throws IOException, CollectionException {
+ assertNotNull(resource);
+ resource.assertConfiguredOk();
+ return false;
+ }
+
+ public void getNext(CAS aCAS) throws IOException, CollectionException {
+ // This is never called
+ }
+
+ public Progress[] getProgress() {
+ return new Progress[0];
+ }
+ }
+}
Propchange: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/CollectionReaderFactoryExternalResourceTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/CollectionReaderFactoryTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/CollectionReaderFactoryTest.java?rev=1434987&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/CollectionReaderFactoryTest.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/CollectionReaderFactoryTest.java Fri Jan 18 00:26:25 2013
@@ -0,0 +1,127 @@
+/*
+ * 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 org.apache.uima.fit.factory;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+
+import org.apache.uima.UIMAException;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.collection.CollectionException;
+import org.apache.uima.collection.CollectionReader;
+import org.apache.uima.collection.CollectionReader_ImplBase;
+import org.apache.uima.fit.ComponentTestBase;
+import org.apache.uima.fit.factory.testCrs.SingleFileXReader;
+import org.apache.uima.fit.pipeline.JCasIterable;
+import org.apache.uima.fit.type.Token;
+import org.apache.uima.fit.util.JCasUtil;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.util.Progress;
+import org.junit.Test;
+
+/**
+ */
+
+public class CollectionReaderFactoryTest extends ComponentTestBase {
+
+ @Test
+ public void testCreateCollectionReader() throws UIMAException, IOException {
+
+ CollectionReader reader = CollectionReaderFactory.createCollectionReader(
+ SingleFileXReader.class, typeSystemDescription, SingleFileXReader.PARAM_FILE_NAME,
+ "src/test/resources/data/docs/test.xmi", SingleFileXReader.PARAM_XML_SCHEME,
+ SingleFileXReader.XMI);
+
+ JCasIterable jCasIterable = new JCasIterable(reader, typeSystemDescription);
+ jCas = jCasIterable.next();
+ assertNotNull(jCas);
+ assertEquals("Me and all my friends are non-conformists.", jCas.getDocumentText());
+ Token token = JCasUtil.selectByIndex(jCas, Token.class, 2);
+ assertEquals("all", token.getCoveredText());
+ assertEquals("A", token.getPos());
+ assertEquals("all", token.getStem());
+
+ reader = CollectionReaderFactory.createCollectionReader(
+ "org.apache.uima.fit.factory.testCrs.SingleFileXReader",
+ SingleFileXReader.PARAM_FILE_NAME, "src/test/resources/data/docs/test.xmi",
+ SingleFileXReader.PARAM_XML_SCHEME, SingleFileXReader.XMI);
+
+ jCasIterable = new JCasIterable(reader, typeSystemDescription);
+ jCas = jCasIterable.next();
+ assertNotNull(jCas);
+ assertEquals("Me and all my friends are non-conformists.", jCas.getDocumentText());
+ token = JCasUtil.selectByIndex(jCas, Token.class, 9);
+ assertEquals(".", token.getCoveredText());
+ assertEquals(".", token.getPos());
+ assertEquals(".", token.getStem());
+
+ reader = CollectionReaderFactory.createCollectionReaderFromPath(
+ "src/test/resources/org/apache/uima/fit/factory/testCrs/SingleFileXReader.xml",
+ SingleFileXReader.PARAM_FILE_NAME, "src/test/resources/data/docs/test.xmi",
+ SingleFileXReader.PARAM_XML_SCHEME, SingleFileXReader.XMI);
+
+ jCasIterable = new JCasIterable(reader, typeSystemDescription);
+ jCas = jCasIterable.next();
+ assertNotNull(jCas);
+ assertEquals("Me and all my friends are non-conformists.", jCas.getDocumentText());
+ token = JCasUtil.selectByIndex(jCas, Token.class, 4);
+ assertEquals("friends", token.getCoveredText());
+ assertEquals("F", token.getPos());
+ assertEquals("friend", token.getStem());
+
+ }
+
+ @Test
+ public void testExceptions() {
+ ResourceInitializationException rie = null;
+ try {
+ CollectionReaderFactory.createCollectionReader(TestCR.class, (Object[]) null);
+ } catch (ResourceInitializationException e) {
+ rie = e;
+ }
+ assertNotNull(rie);
+ }
+
+ private class TestCR extends CollectionReader_ImplBase {
+
+ private TestCR() {
+ // do not instantiate
+ }
+
+ public void getNext(CAS acas) throws IOException, CollectionException {
+ // TODO Auto-generated method stub
+ }
+
+ public void close() throws IOException {
+ // TODO Auto-generated method stub
+ }
+
+ public Progress[] getProgress() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public boolean hasNext() throws IOException, CollectionException {
+ // TODO Auto-generated method stub
+ return false;
+ }
+ }
+}
Propchange: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/CollectionReaderFactoryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ConfigurationParameterFactoryTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ConfigurationParameterFactoryTest.java?rev=1434987&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ConfigurationParameterFactoryTest.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ConfigurationParameterFactoryTest.java Fri Jan 18 00:26:25 2013
@@ -0,0 +1,221 @@
+/*
+ * 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 org.apache.uima.fit.factory;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.lang.reflect.Field;
+import java.util.List;
+import java.util.Set;
+
+import org.uimafit.descriptor.ConfigurationParameter;
+import org.junit.Test;
+
+/**
+ */
+
+public class ConfigurationParameterFactoryTest {
+
+ public static final String PARAM_DOUBLE_1 = "org.uimafit.factory.ConfigurationParameterFactoryTest.PARAM_STRING_1";
+
+ @ConfigurationParameter(name = PARAM_DOUBLE_1, mandatory = true, defaultValue = "3.1415")
+ private Double double1;
+
+ public static final String PARAM_DOUBLE_2 = "org.uimafit.factory.ConfigurationParameterFactoryTest.PARAM_DOUBLE_2";
+
+ @ConfigurationParameter(name = PARAM_DOUBLE_2, mandatory = true, defaultValue = "3.3333")
+ private Double[] double2;
+
+ private Double[] double3;
+
+ public Double[] getDouble2() {
+ return double2;
+ }
+
+ public void setDouble2(Double[] double2) {
+ this.double2 = double2;
+ }
+
+ public Double[] getDouble3() {
+ return double3;
+ }
+
+ public void setDouble3(Double[] double3) {
+ this.double3 = double3;
+ }
+
+ public Double getDouble1() {
+ return double1;
+ }
+
+ public void setDouble1(Double double1) {
+ this.double1 = double1;
+ }
+
+ @Test
+ public void test1() throws SecurityException, NoSuchFieldException {
+ Float value = (Float) ConfigurationParameterFactory
+ .getDefaultValue(ConfigurationParameterFactoryTest.class.getDeclaredField("double1"));
+ assertEquals(3.1415, value, 1e-4);
+
+ Float[] values = (Float[]) ConfigurationParameterFactory
+ .getDefaultValue(ConfigurationParameterFactoryTest.class.getDeclaredField("double2"));
+ assertEquals(1, values.length);
+ assertEquals(3.3333, values[0], 1e-4);
+
+ IllegalArgumentException iae = null;
+ try {
+ ConfigurationParameterFactory.getDefaultValue(ConfigurationParameterFactoryTest.class
+ .getDeclaredField("double3"));
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNotNull(iae);
+
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void test2() throws Exception {
+ ConfigurationParameterFactory.createPrimitiveParameter(ConfigurationParameterFactoryTest.class
+ .getDeclaredField("double3"));
+ }
+
+ @ConfigurationParameter
+ public String param1;
+
+ @Test
+ public void testParam1() throws Exception, NoSuchFieldException {
+ Field field1 = ConfigurationParameterFactoryTest.class.getDeclaredField("param1");
+ org.apache.uima.resource.metadata.ConfigurationParameter cp = ConfigurationParameterFactory
+ .createPrimitiveParameter(field1);
+ assertEquals("org.apache.uima.fit.factory.ConfigurationParameterFactoryTest.param1",
+ cp.getName());
+ assertEquals(org.apache.uima.resource.metadata.ConfigurationParameter.TYPE_STRING, cp.getType());
+ assertEquals("", cp.getDescription());
+ assertFalse(cp.isMandatory());
+ assertFalse(cp.isMultiValued());
+ assertNull(ConfigurationParameterFactory.getDefaultValue(field1));
+ }
+
+ @SuppressWarnings("unused")
+ @ConfigurationParameter(name = "my-boolean-param", mandatory = true, description = "my description", defaultValue = {
+ "false", "false", "true" })
+ private boolean[] param2;
+
+ @Test
+ public void testParam2() throws Exception, NoSuchFieldException {
+ Field field2 = ConfigurationParameterFactoryTest.class.getDeclaredField("param2");
+ org.apache.uima.resource.metadata.ConfigurationParameter cp = ConfigurationParameterFactory
+ .createPrimitiveParameter(field2);
+ assertEquals("my-boolean-param", cp.getName());
+ assertEquals(org.apache.uima.resource.metadata.ConfigurationParameter.TYPE_BOOLEAN,
+ cp.getType());
+ assertEquals("my description", cp.getDescription());
+ assertTrue(cp.isMandatory());
+ assertTrue(cp.isMultiValued());
+ Boolean[] defaultValue = (Boolean[]) ConfigurationParameterFactory.getDefaultValue(field2);
+ assertFalse(defaultValue[0]);
+ assertFalse(defaultValue[1]);
+ assertTrue(defaultValue[2]);
+ }
+
+ @SuppressWarnings("unused")
+ @ConfigurationParameter
+ private Integer param3;
+
+ @Test
+ public void testParam3() throws Exception, NoSuchFieldException {
+ Field field3 = ConfigurationParameterFactoryTest.class.getDeclaredField("param3");
+ org.apache.uima.resource.metadata.ConfigurationParameter cp = ConfigurationParameterFactory
+ .createPrimitiveParameter(field3);
+ assertEquals("org.apache.uima.fit.factory.ConfigurationParameterFactoryTest.param3",
+ cp.getName());
+ assertEquals(org.apache.uima.resource.metadata.ConfigurationParameter.TYPE_INTEGER,
+ cp.getType());
+ assertEquals("", cp.getDescription());
+ assertFalse(cp.isMandatory());
+ assertFalse(cp.isMultiValued());
+ assertNull(ConfigurationParameterFactory.getDefaultValue(field3));
+ }
+
+ private static class CPFT {
+ @SuppressWarnings("unused")
+ @ConfigurationParameter(defaultValue = { "a", "b", "c" })
+ private String[] param4;
+ }
+
+ @Test
+ public void testParam4() throws Exception, NoSuchFieldException {
+ Field field4 = CPFT.class.getDeclaredField("param4");
+ org.apache.uima.resource.metadata.ConfigurationParameter cp = ConfigurationParameterFactory
+ .createPrimitiveParameter(field4);
+ assertEquals("org.apache.uima.fit.factory.ConfigurationParameterFactoryTest$CPFT.param4",
+ cp.getName());
+ assertEquals(org.apache.uima.resource.metadata.ConfigurationParameter.TYPE_STRING, cp.getType());
+ assertEquals("", cp.getDescription());
+ assertFalse(cp.isMandatory());
+ assertTrue(cp.isMultiValued());
+ assertArrayEquals(new String[] { "a", "b", "c" },
+ (String[]) ConfigurationParameterFactory.getDefaultValue(field4));
+ }
+
+ @SuppressWarnings("unused")
+ @ConfigurationParameter(defaultValue = { "data/foo", "bar" })
+ private List<File> fileList;
+
+ @Test
+ public void testFileList() throws Exception {
+ Field field = this.getClass().getDeclaredField("fileList");
+ org.apache.uima.resource.metadata.ConfigurationParameter param;
+ param = ConfigurationParameterFactory.createPrimitiveParameter(field);
+ assertEquals(this.getClass().getName() + ".fileList", param.getName());
+ assertEquals(org.apache.uima.resource.metadata.ConfigurationParameter.TYPE_STRING,
+ param.getType());
+ assertEquals("", param.getDescription());
+ assertFalse(param.isMandatory());
+ String[] expected = new String[] { "data/foo", "bar" };
+ String[] actual = (String[]) ConfigurationParameterFactory.getDefaultValue(field);
+ assertArrayEquals(expected, actual);
+ }
+
+ @SuppressWarnings("unused")
+ @ConfigurationParameter(defaultValue = { "5", "5", "4", "3" })
+ private Set<String> stringSet;
+
+ @Test
+ public void testStringSet() throws Exception {
+ Field field = this.getClass().getDeclaredField("stringSet");
+ org.apache.uima.resource.metadata.ConfigurationParameter param;
+ param = ConfigurationParameterFactory.createPrimitiveParameter(field);
+ assertEquals(this.getClass().getName() + ".stringSet", param.getName());
+ assertEquals(org.apache.uima.resource.metadata.ConfigurationParameter.TYPE_STRING,
+ param.getType());
+ assertFalse(param.isMandatory());
+ String[] expected = new String[] { "5", "5", "4", "3" };
+ String[] actual = (String[]) ConfigurationParameterFactory.getDefaultValue(field);
+ assertArrayEquals(expected, actual);
+ }
+
+}
Propchange: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ConfigurationParameterFactoryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ExternalResourceConfiguratorTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ExternalResourceConfiguratorTest.java?rev=1434987&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ExternalResourceConfiguratorTest.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ExternalResourceConfiguratorTest.java Fri Jan 18 00:26:25 2013
@@ -0,0 +1,83 @@
+/*
+ * 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 org.apache.uima.fit.factory;
+
+import static org.apache.uima.fit.component.initialize.ExternalResourceInitializer.getResourceDeclarations;
+import static org.apache.uima.fit.factory.AnalysisEngineFactory.createPrimitiveDescription;
+import static org.junit.Assert.assertEquals;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.uima.analysis_engine.AnalysisEngineDescription;
+import org.apache.uima.fit.ComponentTestBase;
+import org.apache.uima.fit.component.initialize.ExternalResourceInitializer;
+import org.apache.uima.fit.factory.testAes.ParameterizedAE2;
+import org.apache.uima.resource.ExternalResourceDependency;
+import org.junit.Test;
+
+/**
+ * Test the {@link ExternalResourceInitializer}.
+ *
+ */
+public class ExternalResourceConfiguratorTest extends ComponentTestBase {
+ @Test
+ public void testAnalyze() throws Exception {
+ Map<String, ExternalResourceDependency> deps = getResourceDeclarations(ParameterizedAE2.class);
+
+ verify(deps);
+ }
+
+ @Test
+ public void testDescriptor() throws Exception {
+ AnalysisEngineDescription desc = createPrimitiveDescription(ParameterizedAE2.class,
+ typeSystemDescription);
+
+ Map<String, ExternalResourceDependency> deps = new HashMap<String, ExternalResourceDependency>();
+ for (ExternalResourceDependency dep : desc.getExternalResourceDependencies()) {
+ deps.put(dep.getKey(), dep);
+ }
+
+ verify(deps);
+ }
+
+ private void verify(Map<String, ExternalResourceDependency> deps) {
+ assertEquals(3, deps.size());
+
+ String key = ParameterizedAE2.DummyResource.class.getName();
+ String api = ParameterizedAE2.DummyResource.class.getName();
+ ExternalResourceDependency d = deps.get(key);
+ assertEquals(key, d.getKey());
+ assertEquals(api, d.getInterfaceName());
+ assertEquals(false, d.isOptional());
+
+ key = ParameterizedAE2.RES_OTHER;
+ d = deps.get(key);
+ assertEquals(key, d.getKey());
+ assertEquals(api, d.getInterfaceName());
+ assertEquals(false, d.isOptional());
+
+ key = ParameterizedAE2.RES_OPTIONAL;
+ d = deps.get(key);
+ assertEquals(key, d.getKey());
+ assertEquals(api, d.getInterfaceName());
+ assertEquals(true, d.isOptional());
+ }
+}
Propchange: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ExternalResourceConfiguratorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ExternalResourceFactoryTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ExternalResourceFactoryTest.java?rev=1434987&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ExternalResourceFactoryTest.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ExternalResourceFactoryTest.java Fri Jan 18 00:26:25 2013
@@ -0,0 +1,412 @@
+/*
+ * 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 org.apache.uima.fit.factory;
+
+import static org.apache.uima.fit.factory.AnalysisEngineFactory.createAggregate;
+import static org.apache.uima.fit.factory.AnalysisEngineFactory.createAggregateDescription;
+import static org.apache.uima.fit.factory.AnalysisEngineFactory.createPrimitiveDescription;
+import static org.apache.uima.fit.factory.ExternalResourceFactory.bindExternalResource;
+import static org.apache.uima.fit.factory.ExternalResourceFactory.bindResource;
+import static org.apache.uima.fit.factory.ExternalResourceFactory.createDependencyAndBind;
+import static org.apache.uima.fit.factory.ExternalResourceFactory.createExternalResourceDescription;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.uima.UIMAFramework;
+import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.analysis_engine.AnalysisEngineDescription;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.fit.ComponentTestBase;
+import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
+import org.apache.uima.fit.component.Resource_ImplBase;
+import org.apache.uima.fit.component.initialize.ConfigurationParameterInitializer;
+import org.uimafit.descriptor.ConfigurationParameter;
+import org.uimafit.descriptor.ExternalResource;
+import org.apache.uima.fit.factory.locator.JndiResourceLocator;
+import org.apache.uima.fit.pipeline.SimplePipeline;
+import org.apache.uima.fit.util.SimpleNamedResourceManager;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.resource.DataResource;
+import org.apache.uima.resource.ExternalResourceDescription;
+import org.apache.uima.resource.ParameterizedDataResource;
+import org.apache.uima.resource.ResourceAccessException;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.SharedResourceObject;
+import org.apache.uima.util.CasCreationUtils;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.springframework.mock.jndi.SimpleNamingContextBuilder;
+
+/**
+ * Test case for {@link ExternalResource} annotations.
+ *
+ */
+public class ExternalResourceFactoryTest extends ComponentTestBase {
+ private static final String EX_URI = "http://dum.my";
+
+ private static final String EX_FILE_1 = "src/test/resources/data/html/1.html";
+
+ private static final String EX_FILE_3 = "src/test/resources/data/html/3.html";
+
+ @BeforeClass
+ public static void initJNDI() throws Exception {
+ // Set up JNDI context to test the JndiResourceLocator
+ final SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
+ Properties deDict = new Properties();
+ deDict.setProperty("Hans", "proper noun");
+ builder.bind("dictionaries/german", deDict);
+ builder.activate();
+ }
+
+ @Test
+ public void testScanBind() throws Exception {
+ // Create analysis enginge description
+ AnalysisEngineDescription desc = createPrimitiveDescription(DummyAE.class);
+
+ // Bind external resources
+ bindResources(desc);
+
+ // Test with the default resource manager implementation
+ AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc);
+ assertNotNull(ae);
+ }
+
+ @Test
+ public void testDirectInjection() throws Exception {
+ // Create analysis enginge description
+ AnalysisEngineDescription desc = createPrimitiveDescription(DummyAE2.class);
+
+ // Bind external resources for DummyAE
+ bindResources(desc);
+
+ // Bind external resources for DummyAE2 - necessary because autowiring is disabled
+ bindExternalResource(desc, DummyAE2.RES_INJECTED_POJO1, "pojoName1");
+ bindExternalResource(desc, DummyAE2.RES_INJECTED_POJO2, "pojoName2");
+
+ // Create a custom resource manager that allows to inject any Java object as an external
+ // dependency
+ final Map<String, Object> externalContext = new HashMap<String, Object>();
+ externalContext.put("pojoName1", "Just an injected POJO");
+ externalContext.put("pojoName2", new AtomicInteger(5));
+
+ SimpleNamedResourceManager resMgr = new SimpleNamedResourceManager();
+ resMgr.setExternalContext(externalContext);
+ assertFalse(resMgr.isAutoWireEnabled());
+
+ AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc, resMgr, null);
+ assertNotNull(ae);
+
+ ae.process(ae.newJCas());
+ }
+
+ @Test
+ public void testDirectInjectionAutowire() throws Exception {
+ // Create analysis enginge description
+ AnalysisEngineDescription desc = createPrimitiveDescription(DummyAE2.class);
+
+ // Bind external resources for DummyAE
+ bindResources(desc);
+
+ // Create a custom resource manager that allows to inject any Java object as an external
+ // dependency
+ final Map<String, Object> externalContext = new HashMap<String, Object>();
+ externalContext.put(DummyAE2.RES_INJECTED_POJO1, "Just an injected POJO");
+ externalContext.put(DummyAE2.RES_INJECTED_POJO2, new AtomicInteger(5));
+
+ SimpleNamedResourceManager resMgr = new SimpleNamedResourceManager();
+ resMgr.setExternalContext(externalContext);
+ resMgr.setAutoWireEnabled(true);
+ assertTrue(resMgr.isAutoWireEnabled());
+
+ AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc, resMgr, null);
+ assertNotNull(ae);
+
+ ae.process(ae.newJCas());
+ }
+
+ @Test
+ public void testMultiBinding() throws Exception {
+ ExternalResourceDescription extDesc = createExternalResourceDescription(DummyResource.class);
+
+ // Binding external resource to each Annotator individually
+ AnalysisEngineDescription aed1 = createPrimitiveDescription(MultiBindAE.class,
+ MultiBindAE.RES_KEY, extDesc);
+ AnalysisEngineDescription aed2 = createPrimitiveDescription(MultiBindAE.class,
+ MultiBindAE.RES_KEY, extDesc);
+
+ // Check the external resource was injected
+ AnalysisEngineDescription aaed = createAggregateDescription(aed1, aed2);
+ AnalysisEngine ae = createAggregate(aaed);
+ ae.process(ae.newJCas());
+
+ MultiBindAE.reset();
+
+ // Check the external resource was injected
+ SimplePipeline.runPipeline(CasCreationUtils.createCas(aaed.getAnalysisEngineMetaData()), aaed);
+ }
+
+ private static void bindResources(AnalysisEngineDescription desc) throws Exception {
+ bindResource(desc, DummyResource.class);
+ bindResource(desc, DummyAE.RES_KEY_1, ConfigurableResource.class,
+ ConfigurableResource.PARAM_VALUE, "1");
+ bindResource(desc, DummyAE.RES_KEY_2, ConfigurableResource.class,
+ ConfigurableResource.PARAM_VALUE, "2");
+ bindResource(desc, DummyAE.RES_KEY_3, ParametrizedResource.class,
+ ParametrizedResource.PARAM_EXTENSION, ".lala");
+ bindResource(desc, DummySharedResourceObject.class, EX_URI,
+ DummySharedResourceObject.PARAM_VALUE, "3");
+ // An undefined URL may be used if the specified file/remote URL does not exist or if
+ // the network is down.
+ bindResource(desc, DummyAE.RES_SOME_URL, new File(EX_FILE_1).toURI().toURL());
+ bindResource(desc, DummyAE.RES_SOME_OTHER_URL, new File(EX_FILE_3).toURI().toURL());
+ bindResource(desc, DummyAE.RES_SOME_FILE, new File(EX_FILE_1));
+ bindResource(desc, DummyAE.RES_JNDI_OBJECT, JndiResourceLocator.class,
+ JndiResourceLocator.PARAM_NAME, "dictionaries/german");
+ createDependencyAndBind(desc, "legacyResource", DummySharedResourceObject.class, EX_URI,
+ DummySharedResourceObject.PARAM_VALUE, "3");
+ }
+
+ public static class DummyAE extends JCasAnnotator_ImplBase {
+ @ExternalResource
+ DummyResource r;
+
+ static final String RES_KEY_1 = "Key1";
+
+ @ExternalResource(key = RES_KEY_1)
+ ConfigurableResource configRes1;
+
+ static final String RES_KEY_2 = "Key2";
+
+ @ExternalResource(key = RES_KEY_2)
+ ConfigurableResource configRes2;
+
+ static final String RES_KEY_3 = "Key3";
+
+ @ExternalResource
+ DummySharedResourceObject sharedObject;
+
+ static final String RES_SOME_URL = "SomeUrl";
+
+ @ExternalResource(key = RES_SOME_URL)
+ DataResource someUrl;
+
+ static final String RES_SOME_OTHER_URL = "SomeOtherUrl";
+
+ @ExternalResource(key = RES_SOME_OTHER_URL)
+ DataResource someOtherUrl;
+
+ static final String RES_SOME_FILE = "SomeFile";
+
+ @ExternalResource(key = RES_SOME_FILE)
+ DataResource someFile;
+
+ static final String RES_JNDI_OBJECT = "JndiObject";
+
+ @ExternalResource(key = RES_JNDI_OBJECT)
+ Properties jndiPropertes;
+
+ @Override
+ public void process(JCas aJCas) throws AnalysisEngineProcessException {
+ assertNotNull(r);
+
+ assertNotNull(configRes1);
+ assertEquals("1", configRes1.getValue());
+
+ assertNotNull(configRes2);
+ assertEquals("2", configRes2.getValue());
+
+ try {
+ DataResource configuredResource = (DataResource) getContext().getResourceObject(RES_KEY_3,
+ new String[] { ConfigurableDataResource.PARAM_URI, "http://dum.my/conf" });
+ assertNotNull(configuredResource);
+ assertEquals("http://dum.my/conf.lala", configuredResource.getUri().toString());
+ } catch (ResourceAccessException e) {
+ throw new AnalysisEngineProcessException(e);
+ }
+
+ assertNotNull(sharedObject);
+ assertEquals("3", sharedObject.getValue());
+
+ assertNotNull(sharedObject);
+ assertEquals(EX_URI, sharedObject.getUrl().toString());
+
+ assertNotNull(jndiPropertes);
+ assertEquals("proper noun", jndiPropertes.get("Hans"));
+
+ assertNotNull(someUrl);
+ assertEquals(new File(EX_FILE_1).toURI().toString(), someUrl.getUri().toString());
+
+ assertNotNull(someOtherUrl);
+ assertEquals(new File(EX_FILE_3).toURI().toString(), someOtherUrl.getUri().toString());
+
+ assertTrue(someFile.getUrl().toString().startsWith("file:"));
+ assertTrue("URL [" + someFile.getUrl() + "] should end in [" + EX_FILE_1 + "]", someFile
+ .getUrl().toString().endsWith(EX_FILE_1));
+
+ try {
+ assertNotNull(getContext().getResourceObject("legacyResource"));
+ } catch (ResourceAccessException e) {
+ throw new AnalysisEngineProcessException(e);
+ }
+ }
+ }
+
+ public static final class DummyAE2 extends DummyAE {
+ static final String RES_INJECTED_POJO1 = "InjectedPojo1";
+
+ @ExternalResource(key = RES_INJECTED_POJO1)
+ String injectedString;
+
+ static final String RES_INJECTED_POJO2 = "InjectedPojo2";
+
+ @ExternalResource(key = RES_INJECTED_POJO2)
+ Number injectedAtomicInt;
+
+ @Override
+ public void process(JCas aJCas) throws AnalysisEngineProcessException {
+ super.process(aJCas);
+ assertEquals("Just an injected POJO", injectedString);
+ assertEquals(5, injectedAtomicInt.intValue());
+ }
+ }
+
+ /**
+ * Example annotator that uses the share model object. In the process() we only test if the model
+ * was properly initialized by uimaFIT
+ */
+ public static class MultiBindAE extends org.apache.uima.fit.component.JCasAnnotator_ImplBase {
+ static int prevHashCode = -1;
+
+ static final String RES_KEY = "Res";
+
+ @ExternalResource(key = RES_KEY)
+ DummyResource res;
+
+ @Override
+ public void process(JCas aJCas) throws AnalysisEngineProcessException {
+ if (prevHashCode == -1) {
+ prevHashCode = res.hashCode();
+ } else {
+ assertEquals(prevHashCode, res.hashCode());
+ }
+
+ System.out.println(getClass().getSimpleName() + ": " + res);
+ }
+
+ public static void reset() {
+ prevHashCode = -1;
+ }
+ }
+
+ public static final class DummyResource extends Resource_ImplBase {
+ // Nothing
+ }
+
+ public static final class ConfigurableResource extends Resource_ImplBase {
+ public static final String PARAM_VALUE = "Value";
+
+ @ConfigurationParameter(name = PARAM_VALUE, mandatory = true)
+ private String value;
+
+ public String getValue() {
+ return value;
+ }
+ }
+
+ public static final class ConfigurableDataResource extends Resource_ImplBase implements
+ DataResource {
+ public static final String PARAM_URI = "Uri";
+
+ @ConfigurationParameter(name = PARAM_URI, mandatory = true)
+ private String uri;
+
+ public static final String PARAM_EXTENSION = "Extension";
+
+ @ConfigurationParameter(name = PARAM_EXTENSION, mandatory = true)
+ private String extension;
+
+ public InputStream getInputStream() throws IOException {
+ return null;
+ }
+
+ public URI getUri() {
+ return URI.create(uri + extension);
+ }
+
+ public URL getUrl() {
+ return null;
+ }
+ }
+
+ public static final class ParametrizedResource extends Resource_ImplBase implements
+ ParameterizedDataResource {
+ public static final String PARAM_EXTENSION = "Extension";
+
+ @ConfigurationParameter(name = PARAM_EXTENSION, mandatory = true)
+ private String extension;
+
+ public DataResource getDataResource(String[] aParams) throws ResourceInitializationException {
+ List<String> params = new ArrayList<String>(Arrays.asList(aParams));
+ params.add(ConfigurableDataResource.PARAM_EXTENSION);
+ params.add(extension);
+ ExternalResourceDescription desc = ExternalResourceFactory.createExternalResourceDescription(
+ null, ConfigurableDataResource.class, params.toArray(new String[params.size()]));
+ return (DataResource) UIMAFramework.produceResource(desc.getResourceSpecifier(), null);
+ }
+ }
+
+ public static final class DummySharedResourceObject implements SharedResourceObject {
+ public static final String PARAM_VALUE = "Value";
+
+ @ConfigurationParameter(name = PARAM_VALUE, mandatory = true)
+ private String value;
+
+ private URI uri;
+
+ public void load(DataResource aData) throws ResourceInitializationException {
+ ConfigurationParameterInitializer.initialize(this, aData);
+ assertEquals(EX_URI, aData.getUri().toString());
+ uri = aData.getUri();
+ }
+
+ public URI getUrl() {
+ return uri;
+ }
+
+ public String getValue() {
+ return value;
+ }
+ }
+}
Propchange: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/test/java/org/apache/uima/fit/factory/ExternalResourceFactoryTest.java
------------------------------------------------------------------------------
svn:eol-style = native