From e3c0ab1ac5e157fb8c5ea41a1c29b6073fdb8fde Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Mon, 19 Oct 2015 12:50:52 -0200 Subject: [PATCH] FORGE-2501: Fixed CoordinateBuilder parsing --- .../builder/CoordinateBuilder.java | 55 +++++++++---------- .../builder/CoordinateBuilderTest.java | 13 +++++ 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/dependencies/api/src/main/java/org/jboss/forge/addon/dependencies/builder/CoordinateBuilder.java b/dependencies/api/src/main/java/org/jboss/forge/addon/dependencies/builder/CoordinateBuilder.java index 564269b62a..59c90ad3c4 100644 --- a/dependencies/api/src/main/java/org/jboss/forge/addon/dependencies/builder/CoordinateBuilder.java +++ b/dependencies/api/src/main/java/org/jboss/forge/addon/dependencies/builder/CoordinateBuilder.java @@ -36,38 +36,37 @@ public static CoordinateBuilder create(String coordinates) CoordinateBuilder builder = CoordinateBuilder.create(); // groupId:artifactId:packaging:classifier:version String[] split = coordinates.split("\\:"); - if (split.length == 0) - { - throw new IllegalArgumentException( - "Malformed coordinate. Should be groupId:artifactId:[packaging]:[classifier]:[version]"); - } - if (split.length > 0) + switch (split.length) { + case 1: + builder.setGroupId(split[0]); + break; + case 2: + builder.setGroupId(split[0]); + builder.setArtifactId(split[1]); + break; + case 3: builder.setGroupId(split[0]); - } - if (split.length > 1) - { builder.setArtifactId(split[1]); - } - if (split.length == 3) - { - // The last one is the version, otherwise, continue parsing builder.setVersion(split[2]); - } - else - { - if (split.length > 2) - { - builder.setPackaging(split[2]); - } - if (split.length > 3) - { - builder.setClassifier(split[3]); - } - if (split.length > 4) - { - builder.setVersion(split[4]); - } + break; + case 4: + builder.setGroupId(split[0]); + builder.setArtifactId(split[1]); + builder.setPackaging(split[2]); + builder.setVersion(split[3]); + break; + case 5: + builder.setGroupId(split[0]); + builder.setArtifactId(split[1]); + builder.setPackaging(split[2]); + builder.setClassifier(split[3]); + builder.setVersion(split[4]); + break; + default: + throw new IllegalArgumentException( + "Malformed coordinate (" + coordinates + + "). Should be groupId:artifactId:[packaging]:[classifier]:[version]"); } return builder; } diff --git a/dependencies/api/src/test/java/org/jboss/forge/addon/dependencies/builder/CoordinateBuilderTest.java b/dependencies/api/src/test/java/org/jboss/forge/addon/dependencies/builder/CoordinateBuilderTest.java index 2f5ca476d1..8a1d29fa21 100644 --- a/dependencies/api/src/test/java/org/jboss/forge/addon/dependencies/builder/CoordinateBuilderTest.java +++ b/dependencies/api/src/test/java/org/jboss/forge/addon/dependencies/builder/CoordinateBuilderTest.java @@ -45,4 +45,17 @@ public void testCreateVersionlessCoordinateFromString() Assert.assertEquals(coordinates, coord.toString()); } + @Test + public void testCreateCoordinateWithClassifierFromString() + { + String coordinates = "org.wildfly:wildfly-dist:zip:9.0.0.Final"; + CoordinateBuilder coord = CoordinateBuilder.create(coordinates); + Assert.assertEquals("org.wildfly", coord.getGroupId()); + Assert.assertEquals("wildfly-dist", coord.getArtifactId()); + Assert.assertEquals("zip", coord.getPackaging()); + Assert.assertNull(coord.getClassifier()); + Assert.assertEquals("9.0.0.Final", coord.getVersion()); + Assert.assertEquals(coordinates, coord.toString()); + } + }