Search in sources :

Example 1 with Store

use of com.buschmais.jqassistant.core.store.api.Store in project jqa-java-plugin by buschmais.

the class PropertyFileScannerPlugin method scan.

@Override
public PropertyFileDescriptor scan(FileResource item, String path, Scope scope, Scanner scanner) throws IOException {
    ScannerContext context = scanner.getContext();
    Store store = context.getStore();
    FileDescriptor fileDescriptor = context.getCurrentDescriptor();
    PropertyFileDescriptor propertyFileDescriptor = store.addDescriptorType(fileDescriptor, PropertyFileDescriptor.class);
    Properties properties = new Properties();
    try (InputStream stream = item.createStream()) {
        properties.load(stream);
    } catch (IllegalArgumentException e) {
        LOGGER.warn("Cannot load properties from '" + path + "': " + e.getMessage());
    }
    for (String name : properties.stringPropertyNames()) {
        String value = properties.getProperty(name);
        PropertyDescriptor propertyDescriptor = store.create(PropertyDescriptor.class);
        propertyDescriptor.setName(name);
        propertyDescriptor.setValue(value);
        propertyFileDescriptor.getProperties().add(propertyDescriptor);
    }
    return propertyFileDescriptor;
}
Also used : PropertyFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.PropertyFileDescriptor) PropertyDescriptor(com.buschmais.jqassistant.plugin.common.api.model.PropertyDescriptor) InputStream(java.io.InputStream) Store(com.buschmais.jqassistant.core.store.api.Store) Properties(java.util.Properties) ScannerContext(com.buschmais.jqassistant.core.scanner.api.ScannerContext) PropertyFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.PropertyFileDescriptor) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor)

Example 2 with Store

use of com.buschmais.jqassistant.core.store.api.Store in project jqa-core-framework by buschmais.

the class ScannerTest method pluginPipeline.

@Test
public void pluginPipeline() {
    ScannerContext scannerContext = mock(ScannerContext.class);
    Store store = mock(Store.class);
    when(scannerContext.getStore()).thenReturn(store);
    when(store.create(Mockito.any(Class.class))).thenAnswer(new Answer<Descriptor>() {

        @Override
        public Descriptor answer(InvocationOnMock invocation) throws Throwable {
            Class<? extends Descriptor> descriptorType = (Class<? extends Descriptor>) invocation.getArguments()[0];
            return mock(descriptorType);
        }
    });
    when(store.addDescriptorType(Mockito.any(Descriptor.class), Mockito.any(Class.class))).thenAnswer(new Answer<Descriptor>() {

        @Override
        public Descriptor answer(InvocationOnMock invocation) throws Throwable {
            Class<? extends Descriptor> descriptorType = (Class<? extends Descriptor>) invocation.getArguments()[1];
            return mock(descriptorType);
        }
    });
    Map<String, ScannerPlugin<?, ?>> scannerPlugins = new HashMap<>();
    scannerPlugins.put("TestScanner1", new TestScannerPlugin1());
    scannerPlugins.put("TestScanner2", new TestScannerPlugin2());
    scannerPlugins.put("TestScanner2A", new TestScannerPlugin2A());
    Scanner scanner = new ScannerImpl(new ScannerConfiguration(), scannerContext, scannerPlugins, Collections.<String, Scope>emptyMap());
    Descriptor descriptor = scanner.scan(new TestItem(), "/", DefaultScope.NONE);
    assertThat(descriptor, instanceOf(TestDescriptor2.class));
    verify(store).create(Mockito.eq(TestDescriptor1.class));
    verify(store).addDescriptorType(Mockito.any(TestDescriptor1.class), Mockito.eq(TestDescriptor2A.class));
    verify(store).addDescriptorType(Mockito.any(TestDescriptor2A.class), Mockito.eq(TestDescriptor2.class));
    verify(scannerContext).push(Mockito.eq(TestDescriptor1.class), Mockito.any(TestDescriptor1.class));
    verify(scannerContext).peek(TestDescriptor1.class);
    verify(scannerContext).pop(TestDescriptor1.class);
}
Also used : HashMap(java.util.HashMap) Store(com.buschmais.jqassistant.core.store.api.Store) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Descriptor(com.buschmais.jqassistant.core.store.api.model.Descriptor) Test(org.junit.Test)

Example 3 with Store

use of com.buschmais.jqassistant.core.store.api.Store in project jqa-java-plugin by buschmais.

the class JavaClassesDirectoryScannerPlugin method getContainerDescriptor.

@Override
protected JavaClassesDirectoryDescriptor getContainerDescriptor(File classPathDirectory, ScannerContext scannerContext) {
    JavaArtifactFileDescriptor javaArtifactDescriptor = scannerContext.peekOrDefault(JavaArtifactFileDescriptor.class, null);
    Store store = scannerContext.getStore();
    if (javaArtifactDescriptor == null) {
        return store.create(JavaClassesDirectoryDescriptor.class);
    }
    if (JavaClassesDirectoryDescriptor.class.isAssignableFrom(javaArtifactDescriptor.getClass())) {
        return JavaClassesDirectoryDescriptor.class.cast(javaArtifactDescriptor);
    }
    throw new IllegalStateException("Expected an instance of " + JavaClassesDirectoryDescriptor.class.getName() + " but got " + Arrays.asList(javaArtifactDescriptor.getClass().getInterfaces()));
}
Also used : Store(com.buschmais.jqassistant.core.store.api.Store) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor)

Example 4 with Store

use of com.buschmais.jqassistant.core.store.api.Store in project jqa-java-plugin by buschmais.

the class ManifestFileScannerPlugin method scan.

@Override
public ManifestFileDescriptor scan(FileResource item, String path, Scope scope, Scanner scanner) throws IOException {
    try (InputStream stream = item.createStream()) {
        Manifest manifest = new Manifest(stream);
        ScannerContext context = scanner.getContext();
        Store store = context.getStore();
        FileDescriptor fileDescriptor = context.getCurrentDescriptor();
        ManifestFileDescriptor manifestFileDescriptor = store.addDescriptorType(fileDescriptor, ManifestFileDescriptor.class);
        ManifestSectionDescriptor mainSectionDescriptor = store.create(ManifestSectionDescriptor.class);
        mainSectionDescriptor.setName(SECTION_MAIN);
        manifestFileDescriptor.setMainSection(mainSectionDescriptor);
        readSection(manifest.getMainAttributes(), mainSectionDescriptor, store);
        for (Map.Entry<String, Attributes> sectionEntry : manifest.getEntries().entrySet()) {
            ManifestSectionDescriptor sectionDescriptor = store.create(ManifestSectionDescriptor.class);
            sectionDescriptor.setName(sectionEntry.getKey());
            readSection(sectionEntry.getValue(), sectionDescriptor, store);
            manifestFileDescriptor.getManifestSections().add(sectionDescriptor);
        }
        return manifestFileDescriptor;
    }
}
Also used : ManifestSectionDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ManifestSectionDescriptor) InputStream(java.io.InputStream) Attributes(java.util.jar.Attributes) Store(com.buschmais.jqassistant.core.store.api.Store) Manifest(java.util.jar.Manifest) ScannerContext(com.buschmais.jqassistant.core.scanner.api.ScannerContext) ManifestFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ManifestFileDescriptor) Map(java.util.Map) ManifestFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ManifestFileDescriptor) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor)

Aggregations

Store (com.buschmais.jqassistant.core.store.api.Store)4 ScannerContext (com.buschmais.jqassistant.core.scanner.api.ScannerContext)2 FileDescriptor (com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor)2 InputStream (java.io.InputStream)2 Descriptor (com.buschmais.jqassistant.core.store.api.model.Descriptor)1 PropertyDescriptor (com.buschmais.jqassistant.plugin.common.api.model.PropertyDescriptor)1 JavaArtifactFileDescriptor (com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor)1 ManifestFileDescriptor (com.buschmais.jqassistant.plugin.java.api.model.ManifestFileDescriptor)1 ManifestSectionDescriptor (com.buschmais.jqassistant.plugin.java.api.model.ManifestSectionDescriptor)1 PropertyFileDescriptor (com.buschmais.jqassistant.plugin.java.api.model.PropertyFileDescriptor)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Attributes (java.util.jar.Attributes)1 Manifest (java.util.jar.Manifest)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1