Search in sources :

Example 31 with DefaultDependencyDescriptor

use of org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor in project ant-ivy by apache.

the class URLResolverTest method testTimeoutConstraint.

/**
 * Tests that the timeout constraint set on the URL resolver is used correctly by the resolver
 *
 * @throws Exception if something goes wrong
 */
@Test
public void testTimeoutConstraint() throws Exception {
    final NamedTimeoutConstraint highTimeout = new NamedTimeoutConstraint("test-high-timeout");
    highTimeout.setConnectionTimeout(60000);
    settings.addConfigured(highTimeout);
    final NamedTimeoutConstraint extremelyLowTimeout = new NamedTimeoutConstraint("test-extremely-low-timeout");
    extremelyLowTimeout.setConnectionTimeout(10);
    extremelyLowTimeout.setReadTimeout(20);
    settings.addConfigured(extremelyLowTimeout);
    // setup a HTTP backed repo
    // TODO: Right now the port is hard coded, but we need to find a "available" port to which can be bind to.
    // Else this can lead to occasional bind failures
    final InetSocketAddress fastServerBindAddr = new InetSocketAddress("localhost", 12345);
    final String contextRoot = "/testTimeouts";
    final Path repoRoot = new File("test/repositories/1").toPath();
    assertTrue(repoRoot + " is not a directory", Files.isDirectory(repoRoot));
    final DependencyDescriptor dependency = new DefaultDependencyDescriptor(ModuleRevisionId.newInstance("org1", "mod1.1", "2.0"), false);
    try (final AutoCloseable httpServer = TestHelper.createHttpServerBackedRepository(fastServerBindAddr, contextRoot, repoRoot)) {
        final String ivyPattern = "http://" + fastServerBindAddr.getHostName() + ":" + fastServerBindAddr.getPort() + "/testTimeouts/[organisation]/[module]/ivys/ivy-[revision].xml";
        final String artifactPattern = "http://" + fastServerBindAddr.getHostName() + ":" + fastServerBindAddr.getPort() + "/testTimeouts/[organisation]/[module]/[type]s/[artifact]-[revision].[type]";
        // first use a resolver with a high timeout to make sure
        // it can actually fetch the resources
        final URLResolver highTimeoutResolver = new URLResolver();
        highTimeoutResolver.setName("high-timeout-resolver");
        highTimeoutResolver.setAllownomd(false);
        highTimeoutResolver.setTimeoutConstraint("test-high-timeout");
        highTimeoutResolver.setSettings(settings);
        highTimeoutResolver.setIvyPatterns(Collections.singletonList(ivyPattern));
        highTimeoutResolver.setArtifactPatterns(Collections.singletonList(artifactPattern));
        highTimeoutResolver.validate();
        final TimeoutConstraint resolverTimeoutConstraint = highTimeoutResolver.getTimeoutConstraint();
        assertNotNull("Timeout constraint is missing on resolver " + highTimeoutResolver.getName(), resolverTimeoutConstraint);
        assertEquals("Unexpected connection timeout on resolver", 60000, resolverTimeoutConstraint.getConnectionTimeout());
        assertEquals("Unexpected read timeout on resolver", -1, resolverTimeoutConstraint.getReadTimeout());
        // do the fetch (expected to work fine)
        final ResolvedModuleRevision resolvedModule = highTimeoutResolver.getDependency(dependency, data);
        assertNotNull("Dependency wasn't resolved by resolver " + highTimeoutResolver.getName(), resolvedModule);
        assertEquals("Unexpected dependency resolved by resolver " + highTimeoutResolver.getName(), dependency.getDependencyRevisionId(), resolvedModule.getId());
    }
    // now test this whole fetch using a resolver with a very low connection timeout and
    // by starting the repo server with a delay so that the connection request can timeout
    // clean the cache before testing to ensure the resource isn't fetched from cache
    settings.getDefaultRepositoryCacheManager().clean();
    settings.getResolutionCacheManager().clean();
    final InetSocketAddress slowServerAddr = new InetSocketAddress("localhost", 23456);
    final String ivyPattern = "http://" + slowServerAddr.getHostName() + ":" + slowServerAddr.getPort() + "/testTimeouts/[organisation]/[module]/ivys/ivy-[revision].xml";
    final String artifactPattern = "http://" + slowServerAddr.getHostName() + ":" + slowServerAddr.getPort() + "/testTimeouts/[organisation]/[module]/[type]s/[artifact]-[revision].[type]";
    final URLResolver lowTimeoutResolver = new URLResolver();
    lowTimeoutResolver.setAllownomd(false);
    lowTimeoutResolver.setName("low-timeout-resolver");
    lowTimeoutResolver.setTimeoutConstraint("test-extremely-low-timeout");
    lowTimeoutResolver.setSettings(settings);
    lowTimeoutResolver.setIvyPatterns(Collections.singletonList(ivyPattern));
    lowTimeoutResolver.setArtifactPatterns(Collections.singletonList(artifactPattern));
    lowTimeoutResolver.validate();
    final TimeoutConstraint lowTimeoutConstraint = lowTimeoutResolver.getTimeoutConstraint();
    assertNotNull("Timeout constraint is missing on resolver " + lowTimeoutResolver.getName(), lowTimeoutConstraint);
    assertEquals("Unexpected connection timeout on resolver", 10, lowTimeoutConstraint.getConnectionTimeout());
    assertEquals("Unexpected read timeout on resolver", 20, lowTimeoutConstraint.getReadTimeout());
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    final long serverStartupDelayInMillis = 500;
    final Future<AutoCloseable> httpServer = executor.submit(new ServerManager(slowServerAddr, contextRoot, repoRoot, serverStartupDelayInMillis));
    try {
        // do the fetch (resolution *isn't* expected to return resolved module)
        final ResolvedModuleRevision resolvedModuleFromLowTimeouts = lowTimeoutResolver.getDependency(dependency, data);
        assertNull("Dependency wasn't expected to be resolved by resolver " + lowTimeoutResolver.getName(), resolvedModuleFromLowTimeouts);
    } finally {
        try {
            // stop the server
            httpServer.get().close();
        } catch (Exception e) {
        // ignore
        // TODO: Better log it too. But I don't see usage of loggers in test cases currently. So need to get to this later
        }
        try {
            executor.shutdownNow();
        } catch (Exception e) {
        // ignore
        // TODO: Better log it too. But I don't see usage of loggers in test cases currently. So need to get to this later
        }
    }
}
Also used : Path(java.nio.file.Path) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) InetSocketAddress(java.net.InetSocketAddress) NamedTimeoutConstraint(org.apache.ivy.core.settings.NamedTimeoutConstraint) TimeoutConstraint(org.apache.ivy.core.settings.TimeoutConstraint) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) NamedTimeoutConstraint(org.apache.ivy.core.settings.NamedTimeoutConstraint) ExecutorService(java.util.concurrent.ExecutorService) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) File(java.io.File) Test(org.junit.Test)

Example 32 with DefaultDependencyDescriptor

use of org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor in project ant-ivy by apache.

the class URLResolverTest method testFile.

@Test
public void testFile() throws Exception {
    URLResolver resolver = new URLResolver();
    resolver.setSettings(settings);
    String rootpath = new File("test/repositories/1").toURI().toURL().toExternalForm();
    resolver.addIvyPattern(rootpath + "/[organisation]/[module]/ivys/ivy-[revision].xml");
    resolver.addArtifactPattern(rootpath + "/[organisation]/[module]/[type]s/[artifact]-[revision].[type]");
    resolver.setName("test");
    assertEquals("test", resolver.getName());
    ModuleRevisionId mrid = ModuleRevisionId.newInstance("org1", "mod1.1", "1.0");
    ResolvedModuleRevision rmr = resolver.getDependency(new DefaultDependencyDescriptor(mrid, false), data);
    assertNotNull(rmr);
    assertEquals(mrid, rmr.getId());
    Date pubdate = new GregorianCalendar(2004, 10, 1, 11, 0, 0).getTime();
    assertEquals(pubdate, rmr.getPublicationDate());
    // test to ask to download
    DefaultArtifact artifact = new DefaultArtifact(mrid, pubdate, "mod1.1", "jar", "jar");
    DownloadReport report = resolver.download(new Artifact[] { artifact }, downloadOptions());
    assertNotNull(report);
    assertEquals(1, report.getArtifactsReports().length);
    ArtifactDownloadReport ar = report.getArtifactReport(artifact);
    assertNotNull(ar);
    assertEquals(artifact, ar.getArtifact());
    assertEquals(DownloadStatus.SUCCESSFUL, ar.getDownloadStatus());
    // test to ask to download again, should use cache
    report = resolver.download(new Artifact[] { artifact }, downloadOptions());
    assertNotNull(report);
    assertEquals(1, report.getArtifactsReports().length);
    ar = report.getArtifactReport(artifact);
    assertNotNull(ar);
    assertEquals(artifact, ar.getArtifact());
    assertEquals(DownloadStatus.NO, ar.getDownloadStatus());
}
Also used : DownloadReport(org.apache.ivy.core.report.DownloadReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) GregorianCalendar(java.util.GregorianCalendar) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) File(java.io.File) Date(java.util.Date) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) Test(org.junit.Test)

Example 33 with DefaultDependencyDescriptor

use of org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor in project ant-ivy by apache.

the class ResolveReport method toFixedModuleDescriptor.

public ModuleDescriptor toFixedModuleDescriptor(IvySettings settings, List<ModuleId> midToKeep) {
    DefaultModuleDescriptor fixedmd = new DefaultModuleDescriptor(md.getModuleRevisionId(), md.getStatus(), new Date());
    // copy namespaces
    for (Map.Entry<String, String> ns : md.getExtraAttributesNamespaces().entrySet()) {
        fixedmd.addExtraAttributeNamespace(ns.getKey(), ns.getValue());
    }
    // copy info
    fixedmd.setDescription(md.getDescription());
    fixedmd.setHomePage(md.getHomePage());
    fixedmd.getExtraInfos().addAll(md.getExtraInfos());
    // copy configurations
    List<String> resolvedConfs = Arrays.asList(getConfigurations());
    for (String conf : resolvedConfs) {
        fixedmd.addConfiguration(new Configuration(conf));
    }
    // copy artifacts
    for (String conf : resolvedConfs) {
        for (Artifact a : md.getArtifacts(conf)) {
            fixedmd.addArtifact(conf, a);
        }
    }
    // add resolved dependencies
    for (IvyNode dep : dependencies) {
        ModuleRevisionId depMrid;
        boolean force;
        if (midToKeep != null && midToKeep.contains(dep.getModuleId())) {
            depMrid = dep.getId();
            force = false;
        } else {
            depMrid = dep.getResolvedId();
            force = true;
        }
        DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(fixedmd, depMrid, force, false, false);
        boolean evicted = true;
        for (String rootConf : dep.getRootModuleConfigurations()) {
            if (dep.isEvicted(rootConf)) {
                continue;
            }
            evicted = false;
            for (String targetConf : dep.getConfigurations(rootConf)) {
                dd.addDependencyConfiguration(rootConf, targetConf);
            }
        }
        if (!evicted) {
            fixedmd.addDependency(dd);
        }
    }
    return fixedmd;
}
Also used : Configuration(org.apache.ivy.core.module.descriptor.Configuration) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Date(java.util.Date) Artifact(org.apache.ivy.core.module.descriptor.Artifact) IvyNode(org.apache.ivy.core.resolve.IvyNode)

Example 34 with DefaultDependencyDescriptor

use of org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor in project ant-ivy by apache.

the class RepositoryManagementEngine method loadModuleRevision.

private void loadModuleRevision(ModuleRevisionId mrid) throws Exception {
    ResolvedModuleRevision module = settings.getResolver(mrid).getDependency(new DefaultDependencyDescriptor(mrid, false), newResolveData());
    if (module == null) {
        Message.warn("module not found while listed: " + mrid);
    } else {
        revisions.put(module.getId(), module.getDescriptor());
        getAllRevisions(module.getId()).add(module.getDescriptor());
    }
    Message.progress();
}
Also used : ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)

Example 35 with DefaultDependencyDescriptor

use of org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor in project ant-ivy by apache.

the class ResolveEngine method findModule.

public ResolvedModuleRevision findModule(ModuleRevisionId id, ResolveOptions options) {
    DependencyResolver r = settings.getResolver(id);
    if (r == null) {
        throw new IllegalStateException("no resolver found for " + id.getModuleId());
    }
    DefaultModuleDescriptor md = DefaultModuleDescriptor.newCallerInstance(id, new String[] { "*" }, false, false);
    if (options.getResolveId() == null) {
        options.setResolveId(ResolveOptions.getDefaultResolveId(md));
    }
    try {
        return r.getDependency(new DefaultDependencyDescriptor(id, true), new ResolveData(this, options, new ConfigurationResolveReport(this, md, "default", null, options)));
    } catch (ParseException e) {
        throw new RuntimeException("problem while parsing repository module descriptor for " + id + ": " + e, e);
    }
}
Also used : DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) ParseException(java.text.ParseException) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver)

Aggregations

DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)80 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)61 Test (org.junit.Test)60 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)47 GregorianCalendar (java.util.GregorianCalendar)25 Date (java.util.Date)23 Artifact (org.apache.ivy.core.module.descriptor.Artifact)23 File (java.io.File)22 ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)19 DownloadReport (org.apache.ivy.core.report.DownloadReport)18 DefaultArtifact (org.apache.ivy.core.module.descriptor.DefaultArtifact)16 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)12 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)9 DownloadOptions (org.apache.ivy.core.resolve.DownloadOptions)9 LatestRevisionStrategy (org.apache.ivy.plugins.latest.LatestRevisionStrategy)7 Configuration (org.apache.ivy.core.module.descriptor.Configuration)6 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)5 ModuleId (org.apache.ivy.core.module.id.ModuleId)5 BufferedReader (java.io.BufferedReader)3 Locale (java.util.Locale)3