001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package de.softwareforge.testing.maven;
016
017import static org.assertj.core.api.Assertions.assertThat;
018
019import java.io.File;
020import java.io.IOException;
021import java.util.LinkedList;
022import java.util.List;
023import java.util.Optional;
024
025import org.eclipse.aether.repository.RemoteRepository;
026import org.junit.jupiter.api.BeforeEach;
027import org.junit.jupiter.api.Test;
028
029public final class TestMavenArtifactLoader {
030
031    static final String GROUP_ID = "de.softwareforge.testing";
032    static final String ARTIFACT_ID = "maven-loader";
033
034    MavenArtifactLoader loader;
035
036    @BeforeEach
037    public void setUp() {
038        // enforce repo list for reproducible tests
039        final List<RemoteRepository> knownRemoteRepos = List.of(
040                MavenArtifactLoader.CENTRAL_REPO,
041                new RemoteRepository.Builder("snapshots", "default", "https://oss.sonatype.org/content/repositories/snapshots/").build()
042        );
043
044        this.loader = new MavenArtifactLoader("jar", knownRemoteRepos);
045    }
046
047
048    @Test
049    void testFindVersion21X() throws IOException {
050        List<String> results = loader.builder(GROUP_ID, ARTIFACT_ID)
051                .includeSnapshots(false)
052                .partialMatch("2.1")
053                .findAll();
054
055        assertThat(results)
056                .isNotEmpty()
057                .contains("2.1.0", "2.1.1")
058                .doesNotContain("2.1.0-SNAPSHOT");
059    }
060
061    @Test
062    void testFindVersion210() throws IOException {
063        List<String> results = loader.builder(GROUP_ID, ARTIFACT_ID)
064                .includeSnapshots(false)
065                .exactMatch("2.1.0")
066                .findAll();
067
068        assertThat(results)
069                .isNotEmpty()
070                .containsExactly("2.1.0");
071    }
072
073    @Test
074    void testFindSnapshotVersion210() throws IOException {
075        List<String> results = loader.builder(GROUP_ID, ARTIFACT_ID)
076                .includeSnapshots(true)
077                .partialMatch("2.1")
078                .findAll();
079
080        assertThat(results)
081                .isNotEmpty()
082                .contains("2.1.0-SNAPSHOT");
083    }
084
085    @Test
086    void testSemVerMajor() throws IOException {
087        List<String> results = loader.builder(GROUP_ID, ARTIFACT_ID)
088                .semVerMajor(1)
089                .includeSnapshots(false)
090                .findAll();
091
092        assertThat(results)
093                .contains("1.0", "1.1")
094                .doesNotContain("2.0", "2.1.0");
095    }
096
097    @Test
098    void testSemVerMajorBest() throws IOException {
099        Optional<String> result = loader.builder(GROUP_ID, ARTIFACT_ID)
100                .semVerMajor(1)
101                .includeSnapshots(false)
102                .findBestMatch();
103
104        assertThat(result).isNotEmpty().contains("1.2");
105    }
106
107    @Test
108    void testSemVerMajor0OnlySnapshot() throws IOException {
109        Optional<String> result = loader.builder(GROUP_ID, ARTIFACT_ID)
110                .semVerMajor(0)
111                .findBestMatch();
112
113        assertThat(result).isNotEmpty()
114                .contains("0.1-SNAPSHOT");
115    }
116
117    @Test
118    void testSemVerMajorNotFound() throws IOException {
119        Optional<String> result = loader.builder(GROUP_ID, ARTIFACT_ID)
120                .semVerMajor(0)
121                .includeSnapshots(false)
122                .findBestMatch();
123
124        assertThat(result).isEmpty();
125    }
126
127    @Test
128    void testSemVerMinor() throws IOException {
129        List<String> results = loader.builder(GROUP_ID, ARTIFACT_ID)
130                .semVerMinor(2, 1)
131                .includeSnapshots(false)
132                .findAll();
133
134        assertThat(results)
135                .doesNotContain("1.0", "1.1", "2.0")
136                .contains("2.1.0", "2.1.1");
137    }
138
139
140    @Test
141    void testFindLatestVersion() throws IOException {
142        LinkedList<String> results = loader.builder(GROUP_ID, ARTIFACT_ID).findAll();
143
144        String latestVersion = loader.findLatestVersion(GROUP_ID, ARTIFACT_ID, "");
145
146        assertThat(results).isNotEmpty();
147        assertThat(latestVersion)
148                .isEqualTo(results.getLast());
149    }
150
151
152    @Test
153    void testLoadArtifact() throws IOException {
154        Optional<String> result = loader.builder(GROUP_ID, ARTIFACT_ID)
155                .includeSnapshots(false)
156                .partialMatch("2.1")
157                .findBestMatch();
158
159        assertThat(result).isPresent();
160
161        File artifactFile = loader.getArtifactFile(GROUP_ID, ARTIFACT_ID, result.get());
162
163        assertThat(artifactFile).exists();
164    }
165}