Search in sources :

Example 1 with Resource

use of io.github.classgraph.Resource in project sda-dropwizard-commons by SDA-SE.

the class DuplicateClassesTest method checkForDuplicateClasses.

/**
 * This test finds and logs duplicate classes in the classpath. Such duplicates appear for example
 * when some libraries repackage standard functionality or APIs like javax.* or jakarta.* or when
 * providers change their Maven GAV without changing the internal package structure. In both cases
 * the dependency management can't identify the duplication.
 *
 * <p>When this test is not ignored any more by the assumption, the assumption can be turned into
 * an assertion.
 *
 * <p>This approach of finding duplicates is inspired by <a
 * href="https://stackoverflow.com/a/52639079">Stackoverflow</a>
 */
@Test
public void checkForDuplicateClasses() {
    int numberOfDuplicates = 0;
    ResourceList allResourcesInClasspath = new ClassGraph().scan().getAllResources();
    ResourceList classFilesInClasspath = allResourcesInClasspath.filter(resource -> !resource.getURL().toString().contains("/.gradle/wrapper/")).classFilesOnly();
    for (Map.Entry<String, ResourceList> duplicate : classFilesInClasspath.findDuplicatePaths()) {
        if ("module-info.class".equals(duplicate.getKey())) {
            continue;
        }
        // Classfile path
        LOG.warn("Class files path: {}", duplicate.getKey());
        numberOfDuplicates++;
        for (Resource res : duplicate.getValue()) {
            // Resource URL, showing classpath element
            LOG.warn(" -> {}", res.getURL());
        }
    }
    LOG.warn("Found {} duplicates.", numberOfDuplicates);
    assertThat(numberOfDuplicates).describedAs("already saw only %s duplicate classes but now there are %s", LAST_SEEN_NUMBER_OF_DUPLICATES, numberOfDuplicates).isLessThanOrEqualTo(LAST_SEEN_NUMBER_OF_DUPLICATES);
    assumeThat(numberOfDuplicates).describedAs("expecting no duplicate classes but found %s", numberOfDuplicates).isZero();
}
Also used : ResourceList(io.github.classgraph.ResourceList) ClassGraph(io.github.classgraph.ClassGraph) Resource(io.github.classgraph.Resource) Logger(org.slf4j.Logger) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Map(java.util.Map) LoggerFactory(org.slf4j.LoggerFactory) Test(org.junit.Test) ResourceList(io.github.classgraph.ResourceList) ClassGraph(io.github.classgraph.ClassGraph) Resource(io.github.classgraph.Resource) Map(java.util.Map) Test(org.junit.Test)

Example 2 with Resource

use of io.github.classgraph.Resource in project tsunami-security-scanner-plugins by google.

the class ResourceFingerprintLoader method loadFingerprints.

@Override
public ImmutableMap<SoftwareIdentity, FingerprintData> loadFingerprints() throws IOException {
    Stopwatch loadTimeStopwatch = Stopwatch.createStarted();
    ResourceList fingerprintsResources = scanResult.getResourcesMatchingPattern(FINGERPRINTS_RESOURCE_PATTERN);
    ImmutableMap.Builder<SoftwareIdentity, FingerprintData> fingerprintsBuilder = ImmutableMap.builder();
    for (Resource resource : fingerprintsResources) {
        logger.atInfo().log("Loading fingerprints from resource %s.", resource.getPath());
        Fingerprints fingerprints = Fingerprints.parseFrom(resource.load());
        fingerprintsBuilder.put(fingerprints.getSoftwareIdentity(), FingerprintData.fromProto(fingerprints));
    }
    ImmutableMap<SoftwareIdentity, FingerprintData> fingerprints = fingerprintsBuilder.build();
    logger.atInfo().log("Finished loading %s web fingerprints data in %s.", fingerprints.size(), loadTimeStopwatch.stop());
    return fingerprints;
}
Also used : ResourceList(io.github.classgraph.ResourceList) Fingerprints(com.google.tsunami.plugins.fingerprinters.web.proto.Fingerprints) Stopwatch(com.google.common.base.Stopwatch) SoftwareIdentity(com.google.tsunami.plugins.fingerprinters.web.proto.SoftwareIdentity) Resource(io.github.classgraph.Resource) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with Resource

use of io.github.classgraph.Resource in project camel-kamelets by apache.

the class KameletsCatalog method initCatalog.

private static Map<String, Kamelet> initCatalog() {
    Map<String, Kamelet> kameletModels = new HashMap<>();
    try (ScanResult scanResult = new ClassGraph().acceptPaths("/" + KAMELETS_DIR + "/").scan()) {
        for (Resource resource : scanResult.getAllResources()) {
            try (InputStream is = resource.open()) {
                String name = sanitizeFileName(resource.getPath());
                Kamelet kamelet = MAPPER.readValue(is, Kamelet.class);
                LOG.debug("Loading kamelet from: {}, path: {}, name: {}", resource.getClasspathElementFile(), resource.getPath(), name);
                kameletModels.put(name, kamelet);
            } catch (IOException e) {
                LOG.warn("Cannot init Kamelet Catalog with content of " + resource.getPath(), e);
            }
        }
    }
    return Collections.unmodifiableMap(kameletModels);
}
Also used : ScanResult(io.github.classgraph.ScanResult) Kamelet(io.fabric8.camelk.v1alpha1.Kamelet) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ClassGraph(io.github.classgraph.ClassGraph) Resource(io.github.classgraph.Resource) IOException(java.io.IOException)

Example 4 with Resource

use of io.github.classgraph.Resource in project classgraph by classgraph.

the class Issue83Test method jarAccept.

/**
 * Jar accept.
 */
@Test
public void jarAccept() {
    assertThat(jarPathURL).isNotNull();
    final List<String> paths = new ArrayList<>();
    try (ScanResult scanResult = new ClassGraph().overrideClasspath(jarPathURL).acceptJars("nested-jars-level1.zip").scan()) {
        final ResourceList resources = scanResult.getAllResources();
        for (final Resource res : resources) {
            paths.add(res.getPath());
        }
        assertThat(paths).contains("level2.jar");
    }
}
Also used : ScanResult(io.github.classgraph.ScanResult) ResourceList(io.github.classgraph.ResourceList) ClassGraph(io.github.classgraph.ClassGraph) ArrayList(java.util.ArrayList) Resource(io.github.classgraph.Resource) Test(org.junit.jupiter.api.Test)

Example 5 with Resource

use of io.github.classgraph.Resource in project classgraph by classgraph.

the class Issue83Test method jarReject.

/**
 * Jar reject.
 */
@Test
public void jarReject() {
    assertThat(jarPathURL).isNotNull();
    final ArrayList<String> paths = new ArrayList<>();
    try (ScanResult scanResult = new ClassGraph().overrideClasspath(jarPathURL).rejectJars("nested-jars-level1.zip").scan()) {
        final ResourceList resources = scanResult.getAllResources();
        for (final Resource res : resources) {
            paths.add(res.getPath());
        }
        assertThat(paths).doesNotContain("level2.jar");
    }
}
Also used : ScanResult(io.github.classgraph.ScanResult) ResourceList(io.github.classgraph.ResourceList) ClassGraph(io.github.classgraph.ClassGraph) ArrayList(java.util.ArrayList) Resource(io.github.classgraph.Resource) Test(org.junit.jupiter.api.Test)

Aggregations

Resource (io.github.classgraph.Resource)7 ClassGraph (io.github.classgraph.ClassGraph)6 ScanResult (io.github.classgraph.ScanResult)5 ResourceList (io.github.classgraph.ResourceList)4 Test (org.junit.jupiter.api.Test)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Stopwatch (com.google.common.base.Stopwatch)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Fingerprints (com.google.tsunami.plugins.fingerprinters.web.proto.Fingerprints)1 SoftwareIdentity (com.google.tsunami.plugins.fingerprinters.web.proto.SoftwareIdentity)1 Kamelet (io.fabric8.camelk.v1alpha1.Kamelet)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 FastPathResolver (nonapi.io.github.classgraph.utils.FastPathResolver)1 VersionFinder (nonapi.io.github.classgraph.utils.VersionFinder)1