Skip to content

Commit

Permalink
SHRINKRES-104 Honoring programmatic offline flag, new local repositor…
Browse files Browse the repository at this point in the history
…y tests
  • Loading branch information
kpiwko committed Jan 13, 2013
1 parent c99cd0f commit 2ceb3c3
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public class MavenWorkingSessionImpl implements MavenWorkingSession {

private boolean useMavenCentralRepository = true;

// make sure that programmatic call to offline method is always preserved
private Boolean programmaticOffline;

public MavenWorkingSessionImpl() {
this.system = new MavenRepositorySystem();
this.settings = new MavenSettingsBuilder().buildDefaultSettings();
Expand Down Expand Up @@ -197,8 +200,13 @@ public MavenWorkingSession configureSettingsFromFile(File globalSettings, File u

MavenSettingsBuilder builder = new MavenSettingsBuilder();
this.settings = builder.buildSettings(request);
// propagate offline settings from system properties
return goOffline(settings.isOffline());

// ensure we keep offline(boolean) if previously set
if (programmaticOffline != null) {
this.settings.setOffline(programmaticOffline.booleanValue());
}

return regenerateSession();
}

@Override
Expand Down Expand Up @@ -249,19 +257,6 @@ public Collection<MavenResolvedArtifact> resolveDependencies(MavenResolutionStra
return PreAndPostResolutionFilterApplicator.postFilter(resolvedArtifacts);
}

// @Override
public MavenWorkingSession goOffline(boolean value) {
String goOffline = SecurityActions.getProperty(MavenSettingsBuilder.ALT_MAVEN_OFFLINE);
if (goOffline != null) {
this.settings.setOffline(Boolean.valueOf(goOffline));
log.log(Level.FINE, "Offline settings is set via a system property. The new offline flag value is: {0}",
settings.isOffline());
} else {
settings.setOffline(value);
}
return this;
}

@Override
public ParsedPomFile getParsedPomFile() {
return new ParsedPomFileImpl(model, session.getArtifactTypeRegistry());
Expand All @@ -276,9 +271,12 @@ public MavenWorkingSession regenerateSession() {
@Override
public void setOffline(final boolean offline) {
if (log.isLoggable(Level.FINER)) {
log.finer("Set offline mode to: " + offline);
log.finer("Set offline mode programatically to: " + offline);
}

this.programmaticOffline = new Boolean(offline);
this.settings.setOffline(offline);
regenerateSession();
}

/**
Expand Down Expand Up @@ -312,7 +310,7 @@ public void disableMavenCentral() {

private List<RemoteRepository> getRemoteRepositories() throws IllegalStateException {
// disable repositories if working offline
if (settings.isOffline()) {
if (isOffline()) {
log.log(Level.FINE, "No remote repositories will be available, working in offline mode");
return Collections.emptyList();
}
Expand Down Expand Up @@ -427,6 +425,13 @@ public List<String> getActiveProfileIds() {
return new ArrayList<RemoteRepository>(mirroredRepos);
}

private boolean isOffline() {
if (programmaticOffline != null) {
return programmaticOffline.booleanValue();
}
return settings.isOffline();
}

private List<Profile> getSettingsDefinedProfiles() {
return MavenConverter.asProfiles(settings.getProfiles());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.shrinkwrap.resolver.impl.maven.integration;

import java.io.File;

import junit.framework.Assert;

import org.jboss.shrinkwrap.resolver.api.NoResolvedResultException;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.jboss.shrinkwrap.resolver.impl.maven.bootstrap.MavenSettingsBuilder;
import org.jboss.shrinkwrap.resolver.impl.maven.util.TestFileUtil;
import org.jboss.shrinkwrap.resolver.impl.maven.util.ValidationUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* Ensures that artifact is correctly resolved from local repository.
* This test will fail outside the presence of an internet connection and access to repo1.maven.org
*
* @author <a href="mailto:kpiwko@redhat.com">Karel Piwko</a>
*/
public class LocalRepositoryTestCase {

private static final String LOCAL_REPOSITORY = "target/local-only-repository";
private static final String REMOTE_ENABLED_SETTINGS = "target/settings/profiles/settings.xml";
private static final String CENTRAL_ONLY_SETTINGS = "target/settings/profiles/settings-central.xml";

@BeforeClass
public static void setRemoteRepository() {
System.setProperty(MavenSettingsBuilder.ALT_LOCAL_REPOSITORY_LOCATION, LOCAL_REPOSITORY);
}

/**
* Cleanup, remove the repositories from previous tests
*/
@Before
@After
// For debugging you might want to temporarily remove the @After lifecycle call just to sanity-check for yourself
// the repo
public void cleanup() throws Exception {
TestFileUtil.removeDirectory(new File(LOCAL_REPOSITORY));
}

/**
* Ensures that we can contact Maven Central (as a control test)
*/
@Test
public void resolveFromLocalRepository() throws Exception {

// fixture
prepareLocalRepository();

// now, we need to remove Maven repository checks
// https://cwiki.apache.org/confluence/display/MAVEN/Maven+3.x+Compatibility+Notes#Maven3.xCompatibilityNotes-ResolutionfromLocalRepository
// this behavior can be switched using non-tracking LocalRepositoryManager
// the other way is to ensure
TestFileUtil.removeFilesRecursively(new File(LOCAL_REPOSITORY), "_maven.repositories");

// now, we disable remote repository and rely only on what's available in local repository
File[] filesLocal = Maven.configureResolver().fromFile(CENTRAL_ONLY_SETTINGS)
.resolve("org.jboss.shrinkwrap.test:test-deps-c:1.0.0")
.withTransitivity().as(File.class);

ValidationUtil.fromDependencyTree(new File("src/test/resources/dependency-trees/test-deps-c.tree")).validate(
filesLocal);
}

/**
* Ensures that we can contact Maven Central (as a control test)
*/
@Test
public void resolveFromLocalRepositoryOffline() throws Exception {

// fixture
prepareLocalRepository();

// now, we disable remote repository and rely only on what's available in local repository
// test will work in offline mode, so it won't touch remote repositories
File[] filesLocal = Maven.configureResolver().fromFile(CENTRAL_ONLY_SETTINGS).offline()
.resolve("org.jboss.shrinkwrap.test:test-deps-c:1.0.0")
.withTransitivity().as(File.class);

ValidationUtil.fromDependencyTree(new File("src/test/resources/dependency-trees/test-deps-c.tree")).validate(
filesLocal);
}

/**
* Ensures that we can contact Maven Central (as a control test)
*/
@Test(expected = NoResolvedResultException.class)
public void resolveFromLocalRepositoryTrackingRemotes() throws Exception {

// fixture
prepareLocalRepository();

// now, following will fail because remote repository is no longer available and tracking remotes will reject locally
// cached dependencies
Maven.configureResolver().fromFile(CENTRAL_ONLY_SETTINGS)
.resolve("org.jboss.shrinkwrap.test:test-deps-c:1.0.0")
.withTransitivity().as(File.class);

Assert.fail("Maven 3 is tracking artifact remote repositories origin by default, which should cause test to fail.");
}

private void prepareLocalRepository() {

// ensure we get an artifact from remote repository
File[] filesRemote = Maven.configureResolver().fromFile(REMOTE_ENABLED_SETTINGS)
.resolve("org.jboss.shrinkwrap.test:test-deps-c:1.0.0")
.withTransitivity().as(File.class);

ValidationUtil.fromDependencyTree(new File("src/test/resources/dependency-trees/test-deps-c.tree")).validate(
filesRemote);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,25 @@ public class OfflineRepositoryTestCase {

private static final int HTTP_TEST_PORT = 12345;

private static final String JETTY_REPOSITORY = "target/jetty-repository";

private static final String OFFLINE_REPOSITORY = "target/offline-repository";

@Rule
public ExpectedException exception = ExpectedException.none();

@BeforeClass
public static void initialize(){
System.clearProperty("maven.repo.local"); // May conflict with release settings
public static void initialize() {
System.clearProperty(MavenSettingsBuilder.ALT_LOCAL_REPOSITORY_LOCATION); // May conflict with release settings
}

/**
* Cleanup, remove the repositories from previous tests
*/
@Before
public void cleanup() throws IOException {
TestFileUtil.removeDirectory(new File("target/jetty-repository"));
TestFileUtil.removeDirectory(new File("target/offline-repository"));
TestFileUtil.removeDirectory(new File(JETTY_REPOSITORY));
TestFileUtil.removeDirectory(new File(OFFLINE_REPOSITORY));
}

/**
Expand All @@ -82,7 +86,7 @@ public void cleanup() throws IOException {
@Test(expected = NoResolvedResultException.class)
public void searchJunitOnOffineSettingsTest() {
Maven.configureResolver().fromFile("target/settings/profiles/settings-offline.xml")
.resolve("junit:junit:3.8.2").withTransitivity().as(File.class);
.resolve("junit:junit:3.8.2").withTransitivity().as(File.class);
}

/**
Expand All @@ -96,7 +100,7 @@ public void offlineProgramatically() throws IOException {

// Precondition; we can resolve when connected
final File file = Maven.configureResolver().fromFile(settingsFile).resolve(artifactWhichShouldNotResolve)
.withTransitivity().asSingle(File.class);
.withTransitivity().asSingle(File.class);
new ValidationUtil("junit-3.8.2.jar").validate(file);

// Manually cleanup; we're gonna run a test again
Expand All @@ -105,27 +109,35 @@ public void offlineProgramatically() throws IOException {
// Now try in offline mode and ensure we cannot resolve
exception.expect(NoResolvedResultException.class);
Maven.configureResolver().fromFile(settingsFile).offline().resolve(artifactWhichShouldNotResolve)
.withTransitivity().asSingle(File.class);
.withTransitivity().asSingle(File.class);
}

/**
* Goes offline with .pom based resolver
*/
@Test
public void offlineProgramaticallyPomBased() throws IOException {
final String pomFile = "poms/test-parent.xml";
// set local repository to point to offline repository
System.setProperty(MavenSettingsBuilder.ALT_LOCAL_REPOSITORY_LOCATION, OFFLINE_REPOSITORY);
try {
final String pomFile = "poms/test-parent.xml";

// Precondition; we can resolve when connected
final File[] files = Maven.resolver().loadPomFromClassLoaderResource(pomFile).importRuntimeDependencies().as(File.class);
ValidationUtil.fromDependencyTree(new File("src/test/resources/dependency-trees/test-parent.tree"),
ScopeType.COMPILE, ScopeType.RUNTIME).validate(files);
// Precondition; we can resolve when connected
final File[] files = Maven.resolver().loadPomFromClassLoaderResource(pomFile).importRuntimeDependencies()
.as(File.class);
ValidationUtil.fromDependencyTree(new File("src/test/resources/dependency-trees/test-parent.tree"),
ScopeType.COMPILE, ScopeType.RUNTIME).validate(files);

// Manually cleanup; we're gonna run a test again
this.cleanup();
// Manually cleanup; we're gonna run a test again
this.cleanup();

// Now try in offline mode and ensure we cannot resolve
exception.expect(NoResolvedResultException.class);
Maven.resolver().offline().loadPomFromClassLoaderResource(pomFile).importRuntimeDependencies().as(File.class);
// Now try in offline mode and ensure we cannot resolve because we cannot hit repository defined in pom.xml (working
// offline) and local repository was cleaned
exception.expect(NoResolvedResultException.class);
Maven.resolver().offline().loadPomFromClassLoaderResource(pomFile).importRuntimeDependencies().as(File.class);
} finally {
System.clearProperty(MavenSettingsBuilder.ALT_LOCAL_REPOSITORY_LOCATION);
}
}

/**
Expand All @@ -136,7 +148,7 @@ public void offlineBySysProp() {
System.setProperty(MavenSettingsBuilder.ALT_MAVEN_OFFLINE, "true");
try {
Maven.configureResolver().fromFile("target/settings/profiles/settings-jetty.xml")
.resolve("junit:junit:3.8.2").withTransitivity().as(File.class);
.resolve("junit:junit:3.8.2").withTransitivity().as(File.class);
Assert.fail("Artifact junit:junit:3.8.2 should not be present in local repository");
} finally {
System.clearProperty(MavenSettingsBuilder.ALT_MAVEN_OFFLINE);
Expand All @@ -150,7 +162,7 @@ public void searchWithRemoteOffAndOn() {
System.setProperty(MavenSettingsBuilder.ALT_MAVEN_OFFLINE, "true");

Maven.configureResolver().fromFile("target/settings/profiles/settings-jetty.xml")
.resolve("org.jboss.shrinkwrap.test:test-deps-i:1.0.0").withTransitivity().asSingle(File.class);
.resolve("org.jboss.shrinkwrap.test:test-deps-i:1.0.0").withTransitivity().asSingle(File.class);

Assert.fail("Artifact org.jboss.shrinkwrap.test:test-deps-i:1.0.0 is not present in local repository");

Expand All @@ -164,14 +176,14 @@ public void searchWithRemoteOffAndOn() {
System.clearProperty(MavenSettingsBuilder.ALT_MAVEN_OFFLINE);

Maven.configureResolver().fromFile("target/settings/profiles/settings-jetty.xml")
.resolve("org.jboss.shrinkwrap.test:test-deps-i:1.0.0").withTransitivity().asSingle(File.class);
.resolve("org.jboss.shrinkwrap.test:test-deps-i:1.0.0").withTransitivity().asSingle(File.class);
shutdownHttpServer(server);

// offline with artifact in local repository
System.setProperty(MavenSettingsBuilder.ALT_MAVEN_OFFLINE, "true");

Maven.configureResolver().fromFile("target/settings/profiles/settings-jetty.xml")
.resolve("org.jboss.shrinkwrap.test:test-deps-i:1.0.0").withTransitivity().asSingle(File.class);
.resolve("org.jboss.shrinkwrap.test:test-deps-i:1.0.0").withTransitivity().asSingle(File.class);

System.clearProperty(MavenSettingsBuilder.ALT_MAVEN_OFFLINE);
}
Expand Down Expand Up @@ -214,7 +226,7 @@ private static class StaticFileHandler extends AbstractHandler implements Handle
*/
@Override
public void handle(final String target, final HttpServletRequest request, final HttpServletResponse response,
final int dispatch) throws IOException, ServletException {
final int dispatch) throws IOException, ServletException {
// Set content type and status before we write anything to the stream
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
Expand Down
Loading

0 comments on commit 2ceb3c3

Please sign in to comment.