You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by ri...@apache.org on 2014/10/27 12:48:12 UTC
[6/7] git commit: Fix MavenArtifactTest
Fix MavenArtifactTest
- URLClassLoader does not follow redirects, so need to do that first
(for URLs pointing at search.maven.org), before seeing if they
can be resolved correctly
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/3287ab97
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/3287ab97
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/3287ab97
Branch: refs/heads/master
Commit: 3287ab974b69cadd04b9dabf16e458fd8136e9bc
Parents: ace92f6
Author: Aled Sage <al...@gmail.com>
Authored: Thu Oct 23 19:40:49 2014 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Fri Oct 24 13:28:00 2014 +0100
----------------------------------------------------------------------
.../brooklyn/util/maven/MavenArtifactTest.java | 67 ++++++++++++--------
1 file changed, 42 insertions(+), 25 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/3287ab97/utils/common/src/test/java/brooklyn/util/maven/MavenArtifactTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/brooklyn/util/maven/MavenArtifactTest.java b/utils/common/src/test/java/brooklyn/util/maven/MavenArtifactTest.java
index df8eef4..7087041 100644
--- a/utils/common/src/test/java/brooklyn/util/maven/MavenArtifactTest.java
+++ b/utils/common/src/test/java/brooklyn/util/maven/MavenArtifactTest.java
@@ -19,8 +19,8 @@
package brooklyn.util.maven;
import java.io.File;
-import java.io.IOException;
import java.io.InputStream;
+import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLClassLoader;
@@ -46,7 +46,7 @@ public class MavenArtifactTest {
final static String EXAMPLE_BZIP_COORDINATE = "com.example:example-artifact:tar.bz2:server-windows:2.0.1";
@Test
- public void testArtifact() {
+ public void testArtifact() throws Exception {
MavenArtifact m = MavenArtifact.fromCoordinate(MAVEN_JAR_PLUGIN_COORDINATE);
Assert.assertEquals(m.getGroupId(), "org.apache.maven.plugins");
@@ -62,7 +62,7 @@ public class MavenArtifactTest {
}
@Test
- public void testArtifactWithClassifier() {
+ public void testArtifactWithClassifier() throws Exception {
MavenArtifact m = MavenArtifact.fromCoordinate(RELEASED_SOURCES_COORDINATE);
Assert.assertEquals(m.getGroupId(), "io.brooklyn");
@@ -76,7 +76,7 @@ public class MavenArtifactTest {
}
@Test
- public void testRetrieval() {
+ public void testRetrieval() throws Exception {
MavenArtifact m = MavenArtifact.fromCoordinate(MAVEN_JAR_PLUGIN_COORDINATE);
String hostedUrl = new MavenRetriever().getHostedUrl(m);
@@ -89,7 +89,7 @@ public class MavenArtifactTest {
}
@Test
- public void testRetrievalWithClassifier() {
+ public void testRetrievalWithClassifier() throws Exception {
MavenArtifact m = MavenArtifact.fromCoordinate(RELEASED_SOURCES_COORDINATE);
String localPath = new MavenRetriever().getLocalPath(m);
@@ -99,7 +99,7 @@ public class MavenArtifactTest {
}
@Test
- public void testRetrievalWithUnusualClassifier() {
+ public void testRetrievalWithUnusualClassifier() throws Exception {
MavenArtifact m = MavenArtifact.fromCoordinate(EXAMPLE_BZIP_COORDINATE);
String localPath = new MavenRetriever().getLocalPath(m);
@@ -109,7 +109,7 @@ public class MavenArtifactTest {
}
@Test
- public void testSnapshotRetrieval() {
+ public void testSnapshotRetrieval() throws Exception {
MavenArtifact m = MavenArtifact.fromCoordinate(THIS_PROJECT_COORDINATE);
if (!m.isSnapshot()) {
@@ -126,7 +126,7 @@ public class MavenArtifactTest {
}
@Test(groups="Integration")
- public void testRetrievalLocalIntegration() throws IOException {
+ public void testRetrievalLocalIntegration() throws Exception {
MavenArtifact m = MavenArtifact.fromCoordinate(MAVEN_JAR_PLUGIN_COORDINATE);
String localPath = new MavenRetriever().getLocalPath(m);
@@ -134,17 +134,17 @@ public class MavenArtifactTest {
if (!f.exists())
Assert.fail("Could not load "+localPath+" when testing MavenRetriever: do a maven build with no integration tests first to ensure this is installed, then rerun");
- checkValidMavenJarUrl(MavenRetriever.localUrl(m));
+ checkValidMavenJarUrl(MavenRetriever.localUrl(m), "org/apache/maven/plugin/jar/JarMojo.class");
}
@Test(groups="Integration")
- public void testRetrievalHostedReleaseIntegration() {
+ public void testRetrievalHostedReleaseIntegration() throws Exception {
MavenArtifact m = MavenArtifact.fromCoordinate(MAVEN_JAR_PLUGIN_COORDINATE);
- checkValidMavenJarUrl(new MavenRetriever().getHostedUrl(m));
+ checkValidMavenJarUrl(new MavenRetriever().getHostedUrl(m), "org/apache/maven/plugin/jar/JarMojo.class");
}
- protected void checkAvailableUrl(String url) {
+ protected void checkAvailableUrl(String url) throws Exception {
try {
InputStream stream = new URL(url).openStream();
stream.read();
@@ -153,19 +153,18 @@ public class MavenArtifactTest {
throw Exceptions.propagate(e);
}
}
- protected void checkValidMavenJarUrl(String url) {
- try {
- URL innerU = new URLClassLoader(new URL[] { new URL(url) }).findResource(
- "org/apache/maven/plugin/jar/JarMojo.class");
- InputStream innerUin = innerU.openConnection().getInputStream();
- innerUin.close();
- } catch (Exception e) {
- throw Exceptions.propagate(e);
- }
+ protected void checkValidMavenJarUrl(String url, String resource) throws Exception {
+ // URLClassLoader doesn't follow redirects; find out the real URL
+ // Note URLClassLoader.close was only added in Java 7; do not call it until Java 6 support is not needed!
+ URL realUrl = followRedirects(new URL(url));
+ URLClassLoader classLoader = new URLClassLoader(new URL[] { realUrl });
+ URL innerU = classLoader.findResource(resource);
+ InputStream innerUin = innerU.openConnection().getInputStream();
+ innerUin.close();
}
@Test(groups="Integration")
- public void testRetrievalHostedSnapshotIntegration() {
+ public void testRetrievalHostedSnapshotIntegration() throws Exception {
MavenArtifact m = MavenArtifact.fromCoordinate(
"org.apache.brooklyn:brooklyn-utils-common:jar:0.7.0-SNAPSHOT"); // BROOKLYN_VERSION
@@ -175,7 +174,7 @@ public class MavenArtifactTest {
Assert.fail("Could not load "+localPath+" when testing MavenRetriever: do a maven build with no integration tests first to ensure this is installed, then rerun");
String l = new MavenRetriever().getLocalUrl(m);
- Assert.assertEquals(l, "file://"+localPath);
+ Assert.assertEquals(new URL(l), new URL("file://"+localPath));
checkAvailableUrl(l);
@@ -183,7 +182,7 @@ public class MavenArtifactTest {
if (!m.isSnapshot()) {
log.info("Skipping SNAPSHOT testing as this is not a snapshot build");
} else {
- Assert.assertTrue(h.contains("sonatype.org"));
+ Assert.assertTrue(h.contains("repository.apache.org"), "h="+h);
}
try {
@@ -194,5 +193,23 @@ public class MavenArtifactTest {
}
}
-
+ private URL followRedirects(URL url) throws Exception {
+ if ("file".equalsIgnoreCase(url.getProtocol())) return url;
+
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setReadTimeout(5000);
+
+ // normally, 3xx is redirect
+ int status = conn.getResponseCode();
+ boolean redirect = (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER);
+
+ if (redirect) {
+ // get redirect url from "location" header field
+ String newUrl = conn.getHeaderField("Location");
+ log.debug("Following redirect for "+url+", to "+newUrl);
+ return followRedirects(new URL(newUrl));
+ } else {
+ return url;
+ }
+ }
}