...
 
Commits (2)
......@@ -49,6 +49,7 @@ import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.util.InvalidXMLException;
import org.apache.uima.util.XMLInputSource;
import org.apache.uima.util.XMLParser;
import org.springframework.core.annotation.AnnotationUtils;
import javax.inject.Inject;
import java.io.File;
......@@ -59,15 +60,18 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.stream.Collectors;
import static de.schrieveslaach.nlpf.maven.plugin.MojoUtil.swallowDkproLogging;
import static org.apache.uima.fit.util.JCasUtil.select;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
public abstract class AbstractTestMojo extends AbstractMojo {
......@@ -114,7 +118,10 @@ public abstract class AbstractTestMojo extends AbstractMojo {
try {
@Cleanup
XMLInputSource s = new XMLInputSource(descriptorFile);
descriptions.add(xmlParser.parseAnalysisEngineDescription(s));
AnalysisEngineDescription engineDescription = xmlParser.parseAnalysisEngineDescription(s);
if (filterEngineDescriptionWithoutTypeCapability(engineDescription)) {
descriptions.add(engineDescription);
}
} catch (IOException | InvalidXMLException e) {
throw new MojoExecutionException("Could not read engine description from " + descriptorFile, e);
}
......@@ -123,6 +130,11 @@ public abstract class AbstractTestMojo extends AbstractMojo {
return descriptions;
}
private boolean filterEngineDescriptionWithoutTypeCapability(AnalysisEngineDescription engineDescription){
Class<?> implementationClass = analysisEngineService.loadAnalysisEngineImplementation(engineDescription);
return findAnnotation(implementationClass, TypeCapability.class) != null;
}
@Override
public void execute() throws MojoExecutionException {
swallowDkproLogging();
......@@ -236,7 +248,8 @@ public abstract class AbstractTestMojo extends AbstractMojo {
this.engineDescription = engineDescription;
Class<?> cls = analysisEngineService.loadAnalysisEngineImplementation(engineDescription);
TypeCapability typeCapability = cls.getAnnotation(TypeCapability.class);
TypeCapability typeCapability = findAnnotation(cls, TypeCapability.class);
this.outputs = Sets.newHashSet(typeCapability.outputs());
Object parameterVariant = engineDescription.getAnalysisEngineMetaData().getConfigurationParameterSettings().getParameterValue(ComponentParameters.PARAM_VARIANT);
......
......@@ -38,6 +38,7 @@ import org.apache.uima.collection.CollectionReaderDescription;
import org.apache.uima.fit.descriptor.ResourceMetaData;
import org.apache.uima.fit.descriptor.TypeCapability;
import org.apache.uima.jcas.JCas;
import org.springframework.core.annotation.AnnotationUtils;
import java.io.File;
import java.io.FileOutputStream;
......@@ -48,6 +49,7 @@ import java.util.Objects;
import static de.schrieveslaach.nlpf.maven.plugin.EngineDescriptionUtil.hash;
import static de.schrieveslaach.nlpf.maven.plugin.JCasDataUtil.loadJCas;
import static org.apache.uima.fit.pipeline.SimplePipeline.runPipeline;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
@Mojo(name = "tools-test", requiresDependencyResolution = ResolutionScope.COMPILE)
public class IsolatedToolsTestMojo extends AbstractTestMojo {
......@@ -88,7 +90,7 @@ public class IsolatedToolsTestMojo extends AbstractTestMojo {
Class<?> implClass = analysisEngineService.loadAnalysisEngineImplementation(engineDescription);
String name;
ResourceMetaData metaData = implClass.getAnnotation(ResourceMetaData.class);
ResourceMetaData metaData = findAnnotation(implClass, ResourceMetaData.class);
if (metaData != null) {
name = metaData.name();
} else {
......@@ -106,8 +108,8 @@ public class IsolatedToolsTestMojo extends AbstractTestMojo {
private String[] getOutputTypesOfDescription(AnalysisEngineDescription engineDescription) {
Class<?> implementationClass = analysisEngineService.loadAnalysisEngineImplementation(engineDescription);
TypeCapability typeCapability = implementationClass.getAnnotation(TypeCapability.class);
return typeCapability.outputs();
TypeCapability typeCapability = findAnnotation(implementationClass, TypeCapability.class);
return typeCapability != null ? typeCapability.outputs() : new String[]{};
}
@AllArgsConstructor
......
......@@ -49,6 +49,7 @@ import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.CasCopier;
import org.springframework.core.annotation.AnnotationUtils;
import java.io.File;
import java.io.FileOutputStream;
......@@ -77,6 +78,7 @@ import static de.schrieveslaach.nlpf.maven.plugin.JCasDataUtil.loadJCas;
import static de.schrieveslaach.nlpf.plumbing.BestPerformingPipelineFactory.sortByTopologicalDependencyOrder;
import static org.apache.uima.fit.pipeline.SimplePipeline.runPipeline;
import static org.apache.uima.fit.util.JCasUtil.select;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
@Mojo(name = "pipelines-test", requiresDependencyResolution = ResolutionScope.COMPILE)
public class PipelinesTestMojo extends AbstractTestMojo {
......@@ -156,7 +158,7 @@ public class PipelinesTestMojo extends AbstractTestMojo {
Set<String> outputTypes = pipelines.stream()
.flatMap(Collection::stream)
.map(analysisEngineService::loadAnalysisEngineImplementation)
.map(cls -> cls.getAnnotation(TypeCapability.class))
.map(cls -> findAnnotation(cls, TypeCapability.class))
.filter(Objects::nonNull)
.map(TypeCapability::outputs)
.flatMap(Arrays::stream)
......
......@@ -10,18 +10,19 @@ package de.schrieveslaach.nlpf.maven.plugin;
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* =========================LICENSE_END==================================
*/
import de.schrieveslaach.nlpf.maven.plugin.annotators.EmptyAnalysisComponent;
import de.schrieveslaach.nlpf.maven.plugin.reader.ClearTypesCombinationReader;
import de.schrieveslaach.nlpf.maven.plugin.service.AnalysisEngineDescriptionService;
import de.schrieveslaach.nlpf.maven.plugin.service.ClassLoaderService;
......@@ -38,6 +39,7 @@ import org.apache.uima.collection.CollectionReaderDescription;
import org.apache.uima.jcas.JCas;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mock;
import org.xml.sax.SAXException;
......@@ -45,6 +47,7 @@ import org.xml.sax.SAXException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import static de.schrieveslaach.nlpf.maven.plugin.EngineDescriptionUtil.hash;
import static de.schrieveslaach.nlpf.testing.JCasTestFactory.createOpenNlpExample;
......@@ -53,6 +56,9 @@ import static de.schrieveslaach.nlpf.testing.JCasTestFactory.createTorwaldsExamp
import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngineDescription;
import static org.apache.uima.fit.factory.CollectionReaderFactory.createReaderDescription;
import static org.apache.uima.fit.pipeline.SimplePipeline.runPipeline;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
......@@ -112,6 +118,19 @@ public abstract class AbstractTestMojoTest {
});
}
@Test
public void shouldFilterAnalysisEngineDescriptionWithoutTypeCapability() throws Exception {
mockEngineDescriptionsInDescriptorsDirectory(createEngineDescription(
EmptyAnalysisComponent.class
));
provideTestJCas();
List<AnalysisEngineDescription> analysisEngineDescriptions = getMojoUnderTest().loadEngineDescriptions();
assertThat(analysisEngineDescriptions, is(empty()));
}
@SneakyThrows({IOException.class, UIMAException.class})
public void mockEngineDescriptionsInDescriptorsDirectory(Class<? extends AnalysisComponent>... engineDescriptions) {
File descriptorsDirectory = folder.newFolder("target", "descriptors");
......@@ -199,4 +218,6 @@ public abstract class AbstractTestMojoTest {
});
}
protected abstract AbstractTestMojo getMojoUnderTest();
}
......@@ -156,6 +156,11 @@ public class IsolatedToolsTestMojoTest extends AbstractTestMojoTest {
assertThat(json, jsonEquals(jsonOpenNlpPersonTestResult()).withTolerance(0.001));
}
@Override
protected AbstractTestMojo getMojoUnderTest() {
return mojo;
}
private File assertThatIsolatedTestDataFileExists(String hash) {
File testResult = new File(folder.getRoot(), "target/isolated-tools-test-data/" + hash + ".json");
assertThat(testResult, is(anExistingFile()));
......
......@@ -401,4 +401,9 @@ public class PipelinesTestMojoPipelinesTest extends AbstractTestMojoTest {
)
));
}
@Override
protected AbstractTestMojo getMojoUnderTest() {
return mojo;
}
}
package de.schrieveslaach.nlpf.maven.plugin.annotators;
/*-
* ========================LICENSE_START=================================
* nlp-maven-plugin
* %%
* Copyright (C) 2017 - 2018 Schrieveslaach
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* =========================LICENSE_END==================================
*/
import org.apache.uima.analysis_component.AnalysisComponent_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.AbstractCas;
public class EmptyAnalysisComponent extends AnalysisComponent_ImplBase {
@Override
public void process(AbstractCas aCAS) throws AnalysisEngineProcessException {
}
@Override
public boolean hasNext() throws AnalysisEngineProcessException {
return false;
}
@Override
public AbstractCas next() throws AnalysisEngineProcessException {
return null;
}
@Override
public Class<? extends AbstractCas> getRequiredCasInterface() {
return null;
}
@Override
public int getCasInstancesRequired() {
return 0;
}
}
......@@ -44,6 +44,8 @@ import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
public class DKProCoreReflection {
private static final Logger LOGGER = LoggerFactory.getLogger(DKProCoreReflection.class);
......@@ -84,8 +86,8 @@ public class DKProCoreReflection {
return convertToFlatStreamOfMimeTypes(
classes,
cls,
c -> c.getAnnotation(MimeTypeCapability.class) != null,
c -> Arrays.asList(c.getAnnotation(MimeTypeCapability.class).value()).stream()
c -> findAnnotation(c, MimeTypeCapability.class) != null,
c -> Arrays.stream(findAnnotation(c, MimeTypeCapability.class).value())
.map(a -> new ClassMimeTypeMapping(c, a))
);
}
......@@ -117,7 +119,7 @@ public class DKProCoreReflection {
static boolean hasWriterClassFileNameExtensionParameter(ClassMimeTypeMappingPair pair, String extension) {
for (Field field : pair.getSecond().getImplementationClass().getDeclaredFields()) {
ConfigurationParameter annotation = field.getAnnotation(ConfigurationParameter.class);
ConfigurationParameter annotation = findAnnotation(field, ConfigurationParameter.class);
if (annotation != null && annotation.name().equals(ComponentParameters.PARAM_FILENAME_EXTENSION)) {
for (String suffix : annotation.defaultValue()) {
if (suffix.equals(extension)) {
......
......@@ -35,6 +35,8 @@ import org.apache.uima.jcas.tcas.Annotation;
import java.util.HashSet;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
@EqualsAndHashCode(exclude = {"typeCapability", "implClass", "outputType", "graph"})
public class AnalysisEngineDescriptionNode {
......@@ -57,11 +59,14 @@ public class AnalysisEngineDescriptionNode {
this.graph = graph;
this.implClass = classLoader.loadClass(description.getAnnotatorImplementationName());
this.typeCapability = implClass.getAnnotation(TypeCapability.class);
this.typeCapability = findAnnotation(implClass, TypeCapability.class);
ImmutableList.Builder<Class<? extends Annotation>> builder = ImmutableList.builder();
for (String outputType : typeCapability.outputs()) {
builder.add((Class<? extends Annotation>) classLoader.loadClass(outputType));
if (this.typeCapability != null) {
for (String outputType : typeCapability.outputs()) {
builder.add((Class<? extends Annotation>) classLoader.loadClass(outputType));
}
}
this.outputTypes = builder.build();
}
......@@ -93,7 +98,7 @@ public class AnalysisEngineDescriptionNode {
@Override
public String toString() {
String name;
ResourceMetaData metaData = implClass.getAnnotation(ResourceMetaData.class);
ResourceMetaData metaData = findAnnotation(implClass, ResourceMetaData.class);
if (metaData != null) {
name = metaData.name();
} else {
......