Search in sources :

Example 1 with ServiceLoaderDescriptor

use of com.buschmais.jqassistant.plugin.java.api.model.ServiceLoaderDescriptor in project jqa-java-plugin by buschmais.

the class ServiceLoaderIT method invalidDescriptor.

/**
     * Verifies that any files not representing service descriptors are ignored.
     *
     * @throws java.io.IOException
     *             If the test fails.
     */
@Test
public void invalidDescriptor() throws IOException {
    File file = getClassesDirectory(ServiceLoaderIT.class);
    final File propsFile = new File(file, "META-INF/test.properties");
    final String path = "META-INF/services/test.properties";
    store.beginTransaction();
    JavaClassesDirectoryDescriptor artifactDescriptor = getArtifactDescriptor("a1");
    execute(artifactDescriptor, new ScanClassPathOperation() {

        @Override
        public List<FileDescriptor> scan(JavaArtifactFileDescriptor artifact, Scanner scanner) {
            return singletonList(scanner.<File, FileDescriptor>scan(propsFile, path, JavaScope.CLASSPATH));
        }
    }, getScanner());
    List<ServiceLoaderDescriptor> s = query("MATCH (s:ServiceLoader:Properties:File) RETURN s").getColumn("s");
    assertThat(s.size(), equalTo(1));
    ServiceLoaderDescriptor serviceLoaderDescriptor = s.get(0);
    assertThat(serviceLoaderDescriptor.getFileName(), equalTo(path));
    store.commitTransaction();
}
Also used : Scanner(com.buschmais.jqassistant.core.scanner.api.Scanner) JavaClassesDirectoryDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaClassesDirectoryDescriptor) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor) ServiceLoaderDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ServiceLoaderDescriptor) File(java.io.File) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor) Test(org.junit.Test)

Example 2 with ServiceLoaderDescriptor

use of com.buschmais.jqassistant.plugin.java.api.model.ServiceLoaderDescriptor in project jqa-java-plugin by buschmais.

the class ServiceLoaderIT method verifyServiceLoaderDescriptor.

/**
     * Verifies the expected service loader descriptor and its content.
     */
private void verifyServiceLoaderDescriptor() {
    store.beginTransaction();
    List<ServiceLoaderDescriptor> serviceLoaderDescriptors = query("MATCH (sd:ServiceLoader:File) RETURN sd").getColumn("sd");
    assertThat(serviceLoaderDescriptors.size(), equalTo(1));
    ServiceLoaderDescriptor serviceLoaderDescriptor = serviceLoaderDescriptors.get(0);
    assertThat(serviceLoaderDescriptor.getFileName(), endsWith("/META-INF/services/" + Service.class.getName()));
    assertThat(serviceLoaderDescriptor.getType(), typeDescriptor(Service.class));
    List<TypeDescriptor> serviceTypes = serviceLoaderDescriptor.getContains();
    assertThat(serviceTypes.size(), equalTo(2));
    assertThat(serviceTypes, hasItem(typeDescriptor(ServiceImpl.class)));
    assertThat(serviceTypes, hasItem(typeDescriptor(OuterClass.InnerClassServiceImpl.class)));
    store.commitTransaction();
}
Also used : TypeDescriptor(com.buschmais.jqassistant.plugin.java.api.model.TypeDescriptor) OuterClass(com.buschmais.jqassistant.plugin.java.test.set.scanner.serviceloader.OuterClass) Service(com.buschmais.jqassistant.plugin.java.test.set.scanner.serviceloader.Service) ServiceLoaderDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ServiceLoaderDescriptor)

Example 3 with ServiceLoaderDescriptor

use of com.buschmais.jqassistant.plugin.java.api.model.ServiceLoaderDescriptor in project jqa-java-plugin by buschmais.

the class ServiceLoaderFileScannerPlugin method scan.

@Override
public ServiceLoaderDescriptor scan(FileResource item, String path, Scope scope, Scanner scanner) throws IOException {
    Matcher matcher = PATTERN.matcher(path);
    if (!matcher.matches()) {
        throw new IOException("Cannot match path name: " + path);
    }
    String serviceInterface = matcher.group(2);
    ScannerContext context = scanner.getContext();
    FileDescriptor fileDescriptor = context.getCurrentDescriptor();
    ServiceLoaderDescriptor serviceLoaderDescriptor = context.getStore().addDescriptorType(fileDescriptor, ServiceLoaderDescriptor.class);
    TypeDescriptor interfaceTypeDescriptor = getTypeDescriptor(serviceInterface, context);
    serviceLoaderDescriptor.setType(interfaceTypeDescriptor);
    try (InputStream stream = item.createStream()) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        String serviceImplementation;
        while ((serviceImplementation = reader.readLine()) != null) {
            if (!isEmptyOrComment(serviceImplementation)) {
                TypeDescriptor implementationTypeDescriptor = getTypeDescriptor(serviceImplementation, context);
                serviceLoaderDescriptor.getContains().add(implementationTypeDescriptor);
            }
        }
    }
    return serviceLoaderDescriptor;
}
Also used : TypeDescriptor(com.buschmais.jqassistant.plugin.java.api.model.TypeDescriptor) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ServiceLoaderDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ServiceLoaderDescriptor) ScannerContext(com.buschmais.jqassistant.core.scanner.api.ScannerContext) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor)

Aggregations

ServiceLoaderDescriptor (com.buschmais.jqassistant.plugin.java.api.model.ServiceLoaderDescriptor)3 FileDescriptor (com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor)2 TypeDescriptor (com.buschmais.jqassistant.plugin.java.api.model.TypeDescriptor)2 Scanner (com.buschmais.jqassistant.core.scanner.api.Scanner)1 ScannerContext (com.buschmais.jqassistant.core.scanner.api.ScannerContext)1 JavaArtifactFileDescriptor (com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor)1 JavaClassesDirectoryDescriptor (com.buschmais.jqassistant.plugin.java.api.model.JavaClassesDirectoryDescriptor)1 OuterClass (com.buschmais.jqassistant.plugin.java.test.set.scanner.serviceloader.OuterClass)1 Service (com.buschmais.jqassistant.plugin.java.test.set.scanner.serviceloader.Service)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 Test (org.junit.Test)1