View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.supplier;
20  
21  import java.io.File;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.eclipse.aether.RepositorySystem;
26  import org.eclipse.aether.RepositorySystemSession.CloseableSession;
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.artifact.DefaultArtifact;
29  import org.eclipse.aether.impl.Deployer;
30  import org.eclipse.aether.repository.RemoteRepository;
31  import org.eclipse.aether.resolution.VersionRangeRequest;
32  import org.eclipse.aether.resolution.VersionRangeResult;
33  import org.eclipse.aether.spi.io.PathProcessor;
34  import org.eclipse.aether.version.Version;
35  import org.junit.jupiter.api.Test;
36  
37  import static org.junit.jupiter.api.Assertions.*;
38  
39  public class RepositorySystemSupplierTest {
40      @Test
41      void smoke() throws Exception {
42          try (RepositorySystem system = new RepositorySystemSupplier().get();
43                  CloseableSession session = new SessionBuilderSupplier(system)
44                          .get()
45                          .withLocalRepositoryBaseDirectories(new File("target/local-repo").toPath())
46                          .build()) {
47              Artifact artifact = new DefaultArtifact("org.apache.maven.resolver:maven-resolver-util:[0,)");
48              VersionRangeRequest rangeRequest = new VersionRangeRequest();
49              rangeRequest.setArtifact(artifact);
50              rangeRequest.setRepositories(Collections.singletonList(
51                      new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/")
52                              .build()));
53              VersionRangeResult rangeResult = system.resolveVersionRange(session, rangeRequest);
54              List<Version> versions = rangeResult.getVersions();
55  
56              // As of 2023-11-14, Maven Central has 36 versions of this artifact (and it will just grow)
57              assertTrue(versions.size() >= 36);
58              System.out.println("Available " + versions.size() + " versions: " + versions);
59          }
60      }
61  
62      @Test
63      void closed() {
64          RepositorySystemSupplier systemSupplier = new RepositorySystemSupplier();
65          systemSupplier.get().close(); // get an instance and immediately shut it down, this closes supplier as well
66          assertThrows(IllegalStateException.class, systemSupplier::get);
67          assertThrows(IllegalStateException.class, systemSupplier::getRepositorySystem);
68          assertThrows(IllegalStateException.class, systemSupplier::getChecksumProcessor);
69      }
70  
71      @Test
72      void memorizing() {
73          RepositorySystemSupplier systemSupplier = new RepositorySystemSupplier();
74          PathProcessor pathProcessor = systemSupplier.getPathProcessor();
75          Deployer deployer = systemSupplier.getDeployer();
76          try (RepositorySystem repositorySystem = systemSupplier.get()) {
77              assertSame(systemSupplier.get(), repositorySystem);
78              assertSame(systemSupplier.getPathProcessor(), pathProcessor);
79              assertSame(systemSupplier.getDeployer(), deployer);
80          }
81          assertThrows(IllegalStateException.class, systemSupplier::get);
82          assertThrows(IllegalStateException.class, systemSupplier::getRepositorySystem);
83          assertThrows(IllegalStateException.class, systemSupplier::getDeployer);
84          assertThrows(IllegalStateException.class, systemSupplier::getPathProcessor);
85      }
86  }