Skip to content

Commit

Permalink
Check Android repository too (#3605)
Browse files Browse the repository at this point in the history
* rename test class to match model class
* demock tests and add resolve test
* testResolveArtifact
* testResolveArtifact_android
* hack around bug
* match group ID to repo
  • Loading branch information
elharo authored Feb 14, 2020
1 parent a6ee29a commit 2d2aee1
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 57 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.tools.eclipse.appengine.libraries.repository;

import static org.junit.Assert.assertTrue;

import com.google.cloud.tools.eclipse.appengine.libraries.model.MavenCoordinates;
import org.apache.maven.artifact.Artifact;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.junit.Assert;
import org.junit.Test;

public class MavenHelperTest {

private static final String EXPECTED_DOWNLOAD_FOLDER =
".metadata/.plugins/com.google.cloud.tools.eclipse.appengine.libraries/downloads/groupId/artifactId/1.0.0";

@Test
public void testBundleStateBasedMavenFolder_withSpecificVersion() {
MavenCoordinates coordinates = new MavenCoordinates.Builder()
.setGroupId("groupId")
.setArtifactId("artifactId")
.setVersion("1.0.0")
.build();
IPath folder = MavenHelper.bundleStateBasedMavenFolder(coordinates);
assertTrue(folder.toString().endsWith(EXPECTED_DOWNLOAD_FOLDER));
}

@Test
public void testResolveArtifact() throws CoreException {
MavenCoordinates coordinates = new MavenCoordinates.Builder()
.setGroupId("com.google.cloud")
.setArtifactId("google-cloud-datastore")
.setVersion("1.102.1")
.build();
Artifact artifact = MavenHelper.resolveArtifact(coordinates, new NullProgressMonitor());
Assert.assertEquals("google-cloud-datastore", artifact.getArtifactId());
}

@Test
public void testResolveArtifact_googleApiClient() throws CoreException {
MavenCoordinates coordinates = new MavenCoordinates.Builder()
.setGroupId("com.google.api-client")
.setArtifactId("google-api-client")
.setVersion("1.30.8")
.build();
Artifact artifact = MavenHelper.resolveArtifact(coordinates, new NullProgressMonitor());
Assert.assertEquals("google-api-client", artifact.getArtifactId());
}

@Test
public void testResolveArtifact_android() throws CoreException {
MavenCoordinates coordinates = new MavenCoordinates.Builder()
.setGroupId("androidx.annotation")
.setArtifactId("annotation")
.setVersion("1.1.0")
.setRepository("https://maven.google.com")
.build();
Artifact artifact = MavenHelper.resolveArtifact(coordinates, new NullProgressMonitor());
Assert.assertEquals("annotation", artifact.getArtifactId());
}

@Test
public void testResolveArtifact_android_noRepository() throws CoreException {
MavenCoordinates coordinates = new MavenCoordinates.Builder()
.setGroupId("androidx.annotation")
.setArtifactId("annotation")
.setVersion("1.1.0")
.build();
Artifact artifact = MavenHelper.resolveArtifact(coordinates, new NullProgressMonitor());
Assert.assertEquals("annotation", artifact.getArtifactId());
}

@Test
public void testBundleStateBasedMavenFolder_withLatestVersion() {
MavenCoordinates coordinates = new MavenCoordinates.Builder()
.setGroupId("com.google.cloud")
.setArtifactId("datastore")
.setVersion("LATEST")
.build();
try {
MavenHelper.bundleStateBasedMavenFolder(coordinates);
Assert.fail();
} catch (IllegalArgumentException expected) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ public Builder setGroupId(String groupId) {
Preconditions.checkNotNull(groupId, "groupId null");
Preconditions.checkArgument(!groupId.isEmpty(), "groupId is empty");
this.groupId = groupId;
if (groupId.startsWith("androidx")) {
setRepository("https://maven.google.com");
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public static Artifact resolveArtifact(
private static List<ArtifactRepository> getRepository(MavenCoordinates mavenCoordinates)
throws CoreException {
if (MavenCoordinates.MAVEN_CENTRAL_REPO.equals(mavenCoordinates.getRepository())) {
// M2Eclipse will use the Maven Central repo in case null is used
// M2Eclipse uses the Maven Central repo if the repsoitory list is null
return null;
} else {
return Collections.singletonList(getCustomRepository(mavenCoordinates.getRepository()));
return Collections.singletonList(makeRepository(mavenCoordinates.getRepository()));
}
}

private static ArtifactRepository getCustomRepository(String repository) throws CoreException {
private static ArtifactRepository makeRepository(String repository) throws CoreException {
try {
URI repoUri = new URI(repository);
if (!repoUri.isAbsolute()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.embedder.IMavenExecutionContext;
import org.eclipse.m2e.core.internal.MavenPluginActivator;
import org.w3c.dom.DOMException;
Expand Down Expand Up @@ -93,9 +94,9 @@ public static Artifact resolveArtifact(
return runOperation(
monitor,
(context, system, progress) -> {
Artifact artifact =
MavenPlugin.getMaven()
.resolve(groupId, artifactId, version, type, classifier, repositories, progress);
IMaven maven = MavenPlugin.getMaven();
Artifact artifact = maven.resolve(
groupId, artifactId, version, type, classifier, repositories, progress);
return artifact;
});
}
Expand Down

0 comments on commit 2d2aee1

Please sign in to comment.