Skip to content

Commit 2d2aee1

Browse files
authored
Check Android repository too (#3605)
* rename test class to match model class * demock tests and add resolve test * testResolveArtifact * testResolveArtifact_android * hack around bug * match group ID to repo
1 parent a6ee29a commit 2d2aee1

File tree

5 files changed

+113
-57
lines changed

5 files changed

+113
-57
lines changed

plugins/com.google.cloud.tools.eclipse.appengine.libraries.test/src/com/google/cloud/tools/eclipse/appengine/libraries/repository/MavenHelpTest.java

Lines changed: 0 additions & 51 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.tools.eclipse.appengine.libraries.repository;
18+
19+
import static org.junit.Assert.assertTrue;
20+
21+
import com.google.cloud.tools.eclipse.appengine.libraries.model.MavenCoordinates;
22+
import org.apache.maven.artifact.Artifact;
23+
import org.eclipse.core.runtime.CoreException;
24+
import org.eclipse.core.runtime.IPath;
25+
import org.eclipse.core.runtime.NullProgressMonitor;
26+
import org.junit.Assert;
27+
import org.junit.Test;
28+
29+
public class MavenHelperTest {
30+
31+
private static final String EXPECTED_DOWNLOAD_FOLDER =
32+
".metadata/.plugins/com.google.cloud.tools.eclipse.appengine.libraries/downloads/groupId/artifactId/1.0.0";
33+
34+
@Test
35+
public void testBundleStateBasedMavenFolder_withSpecificVersion() {
36+
MavenCoordinates coordinates = new MavenCoordinates.Builder()
37+
.setGroupId("groupId")
38+
.setArtifactId("artifactId")
39+
.setVersion("1.0.0")
40+
.build();
41+
IPath folder = MavenHelper.bundleStateBasedMavenFolder(coordinates);
42+
assertTrue(folder.toString().endsWith(EXPECTED_DOWNLOAD_FOLDER));
43+
}
44+
45+
@Test
46+
public void testResolveArtifact() throws CoreException {
47+
MavenCoordinates coordinates = new MavenCoordinates.Builder()
48+
.setGroupId("com.google.cloud")
49+
.setArtifactId("google-cloud-datastore")
50+
.setVersion("1.102.1")
51+
.build();
52+
Artifact artifact = MavenHelper.resolveArtifact(coordinates, new NullProgressMonitor());
53+
Assert.assertEquals("google-cloud-datastore", artifact.getArtifactId());
54+
}
55+
56+
@Test
57+
public void testResolveArtifact_googleApiClient() throws CoreException {
58+
MavenCoordinates coordinates = new MavenCoordinates.Builder()
59+
.setGroupId("com.google.api-client")
60+
.setArtifactId("google-api-client")
61+
.setVersion("1.30.8")
62+
.build();
63+
Artifact artifact = MavenHelper.resolveArtifact(coordinates, new NullProgressMonitor());
64+
Assert.assertEquals("google-api-client", artifact.getArtifactId());
65+
}
66+
67+
@Test
68+
public void testResolveArtifact_android() throws CoreException {
69+
MavenCoordinates coordinates = new MavenCoordinates.Builder()
70+
.setGroupId("androidx.annotation")
71+
.setArtifactId("annotation")
72+
.setVersion("1.1.0")
73+
.setRepository("https://maven.google.com")
74+
.build();
75+
Artifact artifact = MavenHelper.resolveArtifact(coordinates, new NullProgressMonitor());
76+
Assert.assertEquals("annotation", artifact.getArtifactId());
77+
}
78+
79+
@Test
80+
public void testResolveArtifact_android_noRepository() throws CoreException {
81+
MavenCoordinates coordinates = new MavenCoordinates.Builder()
82+
.setGroupId("androidx.annotation")
83+
.setArtifactId("annotation")
84+
.setVersion("1.1.0")
85+
.build();
86+
Artifact artifact = MavenHelper.resolveArtifact(coordinates, new NullProgressMonitor());
87+
Assert.assertEquals("annotation", artifact.getArtifactId());
88+
}
89+
90+
@Test
91+
public void testBundleStateBasedMavenFolder_withLatestVersion() {
92+
MavenCoordinates coordinates = new MavenCoordinates.Builder()
93+
.setGroupId("com.google.cloud")
94+
.setArtifactId("datastore")
95+
.setVersion("LATEST")
96+
.build();
97+
try {
98+
MavenHelper.bundleStateBasedMavenFolder(coordinates);
99+
Assert.fail();
100+
} catch (IllegalArgumentException expected) {
101+
}
102+
}
103+
}

plugins/com.google.cloud.tools.eclipse.appengine.libraries/src/com/google/cloud/tools/eclipse/appengine/libraries/model/MavenCoordinates.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ public Builder setGroupId(String groupId) {
138138
Preconditions.checkNotNull(groupId, "groupId null");
139139
Preconditions.checkArgument(!groupId.isEmpty(), "groupId is empty");
140140
this.groupId = groupId;
141+
if (groupId.startsWith("androidx")) {
142+
setRepository("https://maven.google.com");
143+
}
141144
return this;
142145
}
143146

plugins/com.google.cloud.tools.eclipse.appengine.libraries/src/com/google/cloud/tools/eclipse/appengine/libraries/repository/MavenHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ public static Artifact resolveArtifact(
5454
private static List<ArtifactRepository> getRepository(MavenCoordinates mavenCoordinates)
5555
throws CoreException {
5656
if (MavenCoordinates.MAVEN_CENTRAL_REPO.equals(mavenCoordinates.getRepository())) {
57-
// M2Eclipse will use the Maven Central repo in case null is used
57+
// M2Eclipse uses the Maven Central repo if the repsoitory list is null
5858
return null;
5959
} else {
60-
return Collections.singletonList(getCustomRepository(mavenCoordinates.getRepository()));
60+
return Collections.singletonList(makeRepository(mavenCoordinates.getRepository()));
6161
}
6262
}
6363

64-
private static ArtifactRepository getCustomRepository(String repository) throws CoreException {
64+
private static ArtifactRepository makeRepository(String repository) throws CoreException {
6565
try {
6666
URI repoUri = new URI(repository);
6767
if (!repoUri.isAbsolute()) {

plugins/com.google.cloud.tools.eclipse.util/src/com/google/cloud/tools/eclipse/util/MavenUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.eclipse.core.runtime.jobs.ISchedulingRule;
4040
import org.eclipse.core.runtime.jobs.Job;
4141
import org.eclipse.m2e.core.MavenPlugin;
42+
import org.eclipse.m2e.core.embedder.IMaven;
4243
import org.eclipse.m2e.core.embedder.IMavenExecutionContext;
4344
import org.eclipse.m2e.core.internal.MavenPluginActivator;
4445
import org.w3c.dom.DOMException;
@@ -93,9 +94,9 @@ public static Artifact resolveArtifact(
9394
return runOperation(
9495
monitor,
9596
(context, system, progress) -> {
96-
Artifact artifact =
97-
MavenPlugin.getMaven()
98-
.resolve(groupId, artifactId, version, type, classifier, repositories, progress);
97+
IMaven maven = MavenPlugin.getMaven();
98+
Artifact artifact = maven.resolve(
99+
groupId, artifactId, version, type, classifier, repositories, progress);
99100
return artifact;
100101
});
101102
}

0 commit comments

Comments
 (0)