Skip to content

Commit

Permalink
FORGE-2493: Fixes tests accepting invalid maven coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Oct 2, 2015
1 parent a6a8027 commit 8f91efc
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Map;

import org.jboss.forge.addon.dependencies.Coordinate;
import org.jboss.forge.addon.dependencies.Dependency;
import org.jboss.forge.furnace.util.Strings;

public class CoordinateBuilder implements Coordinate
{
Expand Down Expand Up @@ -240,37 +240,42 @@ else if (!version.equals(other.version))
}

/**
* Convenience method which should be used to convert a {@link Dependency} object into its id representation, for
* Convenience method which should be used to convert a {@link Coordinate} object into its id representation, for
* example: "groupId:artifactId:::version", "groupId:artifactId:packaging::version" or
* "groupId:artifactId:packaging:classifier:version"
*
* @see {@link Dependency#toCoordinates()}
*/
private String toId()
@Override
public String toString()
{
StringBuilder gav = new StringBuilder(getGroupId()).append(":").append(getArtifactId());
gav.append(":");
if (getPackaging() != null)
{
gav.append(getPackaging());
}
gav.append(":");
if (getClassifier() != null)
if (Strings.isNullOrEmpty(getClassifier())
&& (Strings.isNullOrEmpty(getPackaging()) || "jar".equalsIgnoreCase(getPackaging())))
{
gav.append(getClassifier());
gav.append(":");
if (getVersion() != null)
{
gav.append(getVersion());
}
}
gav.append(":");
if (getVersion() != null)
else
{
gav.append(getVersion());
gav.append(":");
if (!Strings.isNullOrEmpty(getPackaging()))
{
gav.append(getPackaging());
}
if (!Strings.isNullOrEmpty(getClassifier()))
{
gav.append(":");
gav.append(getClassifier());
}
gav.append(":");
if (getVersion() != null)
{
gav.append(getVersion());
}
}
return gav.toString();
}

@Override
public String toString()
{
return toId();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ public static DependencyBuilder create(final Dependency dep)
}

/**
* @param identifier of the form "groupId:artifactId", "groupId:artifactId:version",
* "groupId:artifactId:scope, "groupId
* :artifactId:version:scope", "groupId:artifactId:version:scope:packaging"
* @param identifier of the form "groupId:artifactId", "groupId:artifactId:version", "groupId:artifactId:scope, "
* groupId :artifactId:version:scope", "groupId:artifactId:version:scope:packaging"
*
* For classifier specification, see {@link #setClassifier(String)}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.dependencies.builder;

import org.junit.Assert;
import org.junit.Test;

/**
* Ensures that {@link CoordinateBuilder} creates coordinates in the following schema:
*
* {@code <groupId>:<artifactId>[:<packaging>[:<classifier>]]:<version>}
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
public class CoordinateBuilderTest
{
@Test
public void testCreateCoordinateFromString()
{
String coordinates = "org.jboss.forge.addon:projects-api:2.0.0.Final";
CoordinateBuilder coord = CoordinateBuilder.create(coordinates);
Assert.assertEquals("org.jboss.forge.addon", coord.getGroupId());
Assert.assertEquals("projects-api", coord.getArtifactId());
Assert.assertNull(coord.getPackaging());
Assert.assertNull(coord.getClassifier());
Assert.assertEquals("2.0.0.Final", coord.getVersion());
Assert.assertEquals(coordinates, coord.toString());
}

@Test
public void testCreateVersionlessCoordinateFromString()
{
String coordinates = "org.jboss.forge.addon:projects-api:";
CoordinateBuilder coord = CoordinateBuilder.create(coordinates);
Assert.assertEquals("org.jboss.forge.addon", coord.getGroupId());
Assert.assertEquals("projects-api", coord.getArtifactId());
Assert.assertNull(coord.getPackaging());
Assert.assertNull(coord.getClassifier());
Assert.assertNull(coord.getVersion());
Assert.assertEquals(coordinates, coord.toString());
}

}
Loading

0 comments on commit 8f91efc

Please sign in to comment.