Skip to content

Commit

Permalink
JENKINS-15367 Fix for downstream dependencies when we have a specifie…
Browse files Browse the repository at this point in the history
…d version (not a version range).

Originally-Committed-As: 4043905580df5bf22cde0eaf3dec103abdc88017
  • Loading branch information
alexkoon committed Nov 2, 2012
1 parent 56cf6e3 commit 1331c06
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/main/java/hudson/maven/ModuleDependency.java
Expand Up @@ -35,7 +35,6 @@

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

Expand Down Expand Up @@ -161,6 +160,15 @@ public int hashCode() {
return result;
}

/**
* Returns true if the version specification is a version range per maven version range syntax.
*
* @return true if version specification is a range.
*/
public boolean isVersionRange() {
return version.startsWith("[") || version.startsWith("(");
}

public VersionRange getVersionAsRange() throws InvalidVersionSpecificationException {
if (range==null)
range = VersionRange.createFromVersionSpec(version);
Expand Down Expand Up @@ -204,7 +212,8 @@ protected Object readResolve() {

/**
* Checks whether this ModuleDependency is satisfied by the dependency of the given ModuleDependency.
* This caters for versions where the version string defines a version range.
* If the version string is a defined version, then it does a comparison. If the version string
* is a version range if parses this and caters for this.
*
* @param other The dependency to check for.
* @return true if contained false otherwise.
Expand All @@ -214,7 +223,9 @@ public boolean contains(ModuleDependency other) {
return false;

try {
return getVersionAsRange().containsVersion(other.parseVersion());
return isVersionRange()
? getVersionAsRange().containsVersion(other.parseVersion())
: parseVersion().compareTo(other.parseVersion()) == 0;
} catch (InvalidVersionSpecificationException ivse) {
return false;
}
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/hudson/maven/MavenModuleTest.java
Expand Up @@ -235,6 +235,52 @@ public void testMultipleDependencies() {
Assert.assertSame(dependZ.getVersion(), ((MavenModule) upstreamB.get(0)).getVersion());
}

/**
* This tests a project that has a dependency on a specific version of X.
* The project X has moved on and so should not have any dependencies on ProjectA.
*/
@Test
public void testProjectWithSpecifiedVersionAndNoDependencies() {
MavenProject projectA = createMavenProject("ProjectA", "test", "projectA", "1.0-SNAPSHOT", "jar");
Dependency dependencyA = createDependency("test", "library", "1.0");
projectA.getDependencies().add(dependencyA);

MavenProject dependX = createMavenProject("DependX-1.1", "test", "library", "1.2-SNAPSHOT", "jar");

MavenModuleSet parent = mock(MavenModuleSet.class);
when(parent.isAggregatorStyleBuild()).thenReturn(Boolean.FALSE);

//Now create maven modules for all the projects
MavenModule mavenModuleA = mockMavenModule(projectA);
MavenModule mavenModuleX = mockMavenModule(dependX);

Collection<AbstractProject<?,?>> allModules = Lists.<AbstractProject<?,?>>newArrayList(mavenModuleA,
mavenModuleX);

for (AbstractProject<?, ?> module : allModules) {
MavenModule mm = (MavenModule) module;
enhanceMavenModuleMock(mm, parent, allModules);
}

DependencyGraph graph = MockHelper.mockDependencyGraph(allModules);
doCallRealMethod().when(graph).getDownstream(Matchers.any(AbstractProject.class));
doCallRealMethod().when(graph).getUpstream(Matchers.any(AbstractProject.class));
doCallRealMethod().when(graph).compare(Matchers.<AbstractProject>any(), Matchers.<AbstractProject>any());
graph.build();

List<AbstractProject> downstreamA = graph.getDownstream(mavenModuleA);
List<AbstractProject> upstreamA = graph.getUpstream(mavenModuleA);

Assert.assertEquals(0, downstreamA.size());
Assert.assertEquals(0, upstreamA.size());

List<AbstractProject> downstreamX = graph.getDownstream(mavenModuleX);
List<AbstractProject> upstreamX = graph.getUpstream(mavenModuleX);

Assert.assertEquals(0, downstreamX.size());
Assert.assertEquals(0, upstreamX.size());
}

private TestComponents createTestComponents(String libraryVersion) {
MavenProject appProject = createMavenProject("testapp", "test", "application", "1.0-SNAPSHOT", "jar");
Dependency dependency = createDependency("test", "library", libraryVersion);
Expand Down

0 comments on commit 1331c06

Please sign in to comment.