Skip to content

Commit 7229abf

Browse files
committed
fix cli testing
1 parent e494039 commit 7229abf

File tree

5 files changed

+164
-51
lines changed

5 files changed

+164
-51
lines changed

src/main/java/org/tudo/sse/Main.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import org.apache.logging.log4j.LogManager;
44
import org.apache.logging.log4j.Logger;
5+
import org.tudo.sse.model.Artifact;
6+
import org.tudo.sse.resolution.PomResolutionException;
7+
import org.tudo.sse.utils.GAUtils;
58

69
import java.io.IOException;
710
import java.net.URISyntaxException;
11+
import java.util.List;
812

913
public class Main {
1014

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,114 @@
1-
package org.tudo.sse.utils;public class GAUtils {
1+
package org.tudo.sse.utils;
2+
3+
import org.apache.logging.log4j.LogManager;
4+
import org.apache.logging.log4j.Logger;
5+
import org.apache.maven.artifact.repository.metadata.Metadata;
6+
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
7+
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
8+
import org.tudo.sse.Main;
9+
import org.tudo.sse.model.Artifact;
10+
import org.tudo.sse.model.ArtifactIdent;
11+
import org.tudo.sse.resolution.FileNotFoundException;
12+
import org.tudo.sse.resolution.PomResolutionException;
13+
import org.tudo.sse.resolution.PomResolver;
14+
15+
import java.io.BufferedReader;
16+
import java.io.IOException;
17+
import java.io.InputStream;
18+
import java.io.InputStreamReader;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import java.util.Objects;
22+
23+
public class GAUtils {
24+
private static final MavenCentralRepository MavenRepo = MavenCentralRepository.getInstance();
25+
public static final Logger log = LogManager.getLogger(GAUtils.class);
26+
27+
public static List<Artifact> retrieveAllVersions(String groupId, String artifactId) {
28+
try {
29+
PomResolver pomResolver = new PomResolver(false);
30+
List<Artifact> toReturn = new ArrayList<>();
31+
Metadata meta = getVersions(groupId, artifactId);
32+
33+
if(meta.getVersioning() == null) {
34+
return toReturn;
35+
}
36+
37+
List<String> versions = meta.getVersioning().getVersions();
38+
39+
for(String version : versions) {
40+
try {
41+
toReturn.add(pomResolver.resolveArtifact(new ArtifactIdent(groupId, artifactId, version)));
42+
} catch (PomResolutionException | FileNotFoundException e) {
43+
log.error(e);
44+
}
45+
}
46+
return toReturn;
47+
} catch (FileNotFoundException | IOException | XmlPullParserException e) {
48+
throw new RuntimeException(e);
49+
}
50+
}
51+
52+
public static Artifact getLastModifiedVersion(String groupId, String artifactId) throws PomResolutionException {
53+
try {
54+
Metadata meta = getVersions(groupId, artifactId);
55+
56+
if(meta.getVersioning() == null) {
57+
return null;
58+
}
59+
60+
String latestVersion;
61+
if(meta.getVersioning().getRelease() != null) {
62+
latestVersion = meta.getVersioning().getRelease();
63+
} else {
64+
long latestLastModified = 0;
65+
List<String> versions = meta.getVersioning().getVersions();
66+
latestVersion = versions.get(0);
67+
for(String version : versions) {
68+
long currentLastModified = MavenRepo.getLastModified(new ArtifactIdent(groupId, artifactId, version));
69+
if(currentLastModified > latestLastModified) {
70+
latestLastModified = currentLastModified;
71+
latestVersion = version;
72+
}
73+
}
74+
}
75+
76+
PomResolver pomResolver = new PomResolver(false);
77+
Artifact toReturn = pomResolver.resolveArtifact(new ArtifactIdent(groupId, artifactId, latestVersion));
78+
return toReturn;
79+
} catch (FileNotFoundException | IOException | XmlPullParserException e) {
80+
throw new RuntimeException(e);
81+
}
82+
}
83+
84+
public static Artifact getHighestVersion(String groupId, String artifactId) throws PomResolutionException {
85+
try {
86+
Metadata meta = getVersions(groupId, artifactId);
87+
88+
if(meta.getVersioning() == null) {
89+
return null;
90+
}
91+
92+
String highestVersion;
93+
if(meta.getVersioning().getRelease() != null) {
94+
highestVersion = meta.getVersioning().getLatest();
95+
} else {
96+
List<String> versions = meta.getVersioning().getVersions();
97+
highestVersion = versions.get(versions.size() - 1);
98+
}
99+
100+
PomResolver pomResolver = new PomResolver(false);
101+
Artifact toReturn = pomResolver.resolveArtifact(new ArtifactIdent(groupId, artifactId, highestVersion));
102+
return toReturn;
103+
} catch (FileNotFoundException | IOException | XmlPullParserException e) {
104+
throw new RuntimeException(e);
105+
}
106+
}
107+
108+
private static Metadata getVersions(String groupId, String artifactId) throws FileNotFoundException, IOException, XmlPullParserException {
109+
InputStream versionings = MavenRepo.openXMLFileInputStream(new ArtifactIdent(groupId, artifactId, null));
110+
MetadataXpp3Reader reader = new MetadataXpp3Reader();
111+
return reader.read(new BufferedReader(new InputStreamReader(Objects.requireNonNull(versionings))));
112+
}
113+
2114
}

src/main/java/org/tudo/sse/utils/MavenCentralRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public InputStream openXMLFileInputStream(ArtifactIdent ident) throws IOExceptio
2828
return ResourceConnections.openInputStream(ident.getMavenCentralXMLUri());
2929
}
3030

31+
public long getLastModified(ArtifactIdent ident) throws FileNotFoundException, IOException {
32+
HttpURLConnection connection = ResourceConnections.openConnection(ident.getMavenCentralPomUri());
33+
return connection.getLastModified();
34+
}
35+
3136
public InputStream openPomFileInputStream(ArtifactIdent ident) throws FileNotFoundException, IOException {
3237
return ResourceConnections.openInputStream(ident.getMavenCentralPomUri());
3338
}

src/test/java/org/tudo/sse/MavenCentralAnalysisTest.java

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.*;
1515
import java.net.URI;
1616
import java.net.URISyntaxException;
17+
import java.nio.file.Files;
1718
import java.nio.file.Path;
1819
import java.util.*;
1920

@@ -47,49 +48,47 @@ void parseCmdLinePositive() {
4748
cliInputs.add(args);
4849
args = new String[]{"-ip", "src/test/resources/localPom.xml"};
4950
cliInputs.add(args);
50-
args = new String[]{"--output", "maven-resolution-files"};
51-
cliInputs.add(args);
5251

53-
List<List<String>> expected = (List<List<String>>) json.get("cliParsePos");
52+
try {
53+
Path tmpDir = Files.createTempDirectory("maven-resolution-files");
54+
args = new String[]{"--output", tmpDir.toString()};
55+
cliInputs.add(args);
56+
List<List<String>> expected = (List<List<String>>) json.get("cliParsePos");
5457

55-
int i = 0;
56-
for(String[] input : cliInputs) {
57-
MavenCentralAnalysis tester = new MavenCentralAnalysis() {
58-
@Override
59-
public void analyzeArtifact(Artifact current) {
58+
int i = 0;
59+
for(String[] input : cliInputs) {
60+
MavenCentralAnalysis tester = new MavenCentralAnalysis() {
61+
@Override
62+
public void analyzeArtifact(Artifact current) {
6063

64+
}
65+
};
66+
tester.parseCmdLine(input);
67+
CliInformation result = tester.getSetupInfo();
68+
List<String> currentExp = expected.get(i);
69+
//run asserts here
70+
assertEquals(Integer.parseInt(currentExp.get(0)), result.getSkip());
71+
assertEquals(Integer.parseInt(currentExp.get(1)), result.getTake());
72+
assertEquals(Integer.parseInt(currentExp.get(2)), result.getSince());
73+
assertEquals(Integer.parseInt(currentExp.get(3)), result.getUntil());
74+
75+
if(result.getToCoordinates() != null) {
76+
assertEquals(currentExp.get(4), result.getToCoordinates().toString());
77+
} else {
78+
assertEquals(currentExp.get(4), "null");
6179
}
62-
};
63-
tester.parseCmdLine(input);
64-
CliInformation result = tester.getSetupInfo();
65-
List<String> currentExp = expected.get(i);
66-
//run asserts here
67-
assertEquals(Integer.parseInt(currentExp.get(0)), result.getSkip());
68-
assertEquals(Integer.parseInt(currentExp.get(1)), result.getTake());
69-
assertEquals(Integer.parseInt(currentExp.get(2)), result.getSince());
70-
assertEquals(Integer.parseInt(currentExp.get(3)), result.getUntil());
71-
72-
if(result.getToCoordinates() != null) {
73-
assertEquals(currentExp.get(4), result.getToCoordinates().toString());
74-
} else {
75-
assertEquals(currentExp.get(4), "null");
76-
}
77-
78-
if(result.getToIndexPos() != null) {
79-
assertEquals(currentExp.get(5), result.getToIndexPos().toString());
80-
} else {
81-
assertEquals(currentExp.get(5), "null");
82-
}
8380

84-
assertEquals(Boolean.parseBoolean(currentExp.get(6)), result.isOutput());
81+
if(result.getToIndexPos() != null) {
82+
assertEquals(currentExp.get(5), result.getToIndexPos().toString());
83+
} else {
84+
assertEquals(currentExp.get(5), "null");
85+
}
8586

86-
if(result.getToOutputDirectory() != null) {
87-
assertEquals(currentExp.get(7), result.getToOutputDirectory().toString());
88-
} else {
89-
assertEquals(currentExp.get(7), "null");
87+
assertEquals(Boolean.parseBoolean(currentExp.get(6)), result.isOutput());
88+
i++;
9089
}
91-
92-
i++;
90+
} catch (IOException e) {
91+
throw new RuntimeException(e);
9392
}
9493

9594
}

src/test/resources/MavenAnalysis.json

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"-1",
88
"null",
99
"null",
10-
"false",
11-
"null"
10+
"false"
1211
],
1312
[
1413
"-1",
@@ -17,8 +16,7 @@
1716
"-1",
1817
"src/test/resources/localPom.xml",
1918
"null",
20-
"false",
21-
"null"
19+
"false"
2220
],
2321
[
2422
"-1",
@@ -27,8 +25,7 @@
2725
"-1",
2826
"src/test/resources/localPom.xml",
2927
"src/test/resources/localPom.xml",
30-
"false",
31-
"null"
28+
"false"
3229
],
3330
[
3431
"-1",
@@ -37,8 +34,7 @@
3734
"13243",
3835
"null",
3936
"null",
40-
"false",
41-
"null"
37+
"false"
4238
],
4339
[
4440
"-1",
@@ -47,8 +43,7 @@
4743
"-1",
4844
"src/test/resources/localPom.xml",
4945
"src/test/resources/localPom.xml",
50-
"false",
51-
"null"
46+
"false"
5247
],
5348
[
5449
"-1",
@@ -57,8 +52,7 @@
5752
"-1",
5853
"null",
5954
"src/test/resources/localPom.xml",
60-
"false",
61-
"null"
55+
"false"
6256
],
6357
[
6458
"-1",
@@ -67,8 +61,7 @@
6761
"-1",
6862
"null",
6963
"null",
70-
"true",
71-
"maven-resolution-files"
64+
"true"
7265
]
7366
],
7467
"readIdentsIn" : [

0 commit comments

Comments
 (0)