Selaa lähdekoodia

ZOOKEEPER-913. Version parser fails to parse "3.3.2-dev" from build.xml

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1043627 13f79535-47bb-0310-9956-ffa450edef68
Benjamin Reed 14 vuotta sitten
vanhempi
commit
0e604b1006

+ 2 - 0
CHANGES.txt

@@ -159,6 +159,8 @@ BUGFIXES:
 
   ZOOKEEPER-905. enhance zkServer.sh for easier zookeeper automation-izing (Nicholas Harteau via mahadev)
 
+  ZOOKEEPER-913. Version parser fails to parse "3.3.2-dev" from build.xml (Anthony Urso and phunt via breed)
+
 IMPROVEMENTS:
   ZOOKEEPER-724. Improve junit test integration - log harness information 
   (phunt via mahadev)

+ 3 - 2
src/java/main/org/apache/zookeeper/Version.java

@@ -18,7 +18,7 @@
 
 package org.apache.zookeeper;
 
-public class Version implements org.apache.zookeeper.version.Info{
+public class Version implements org.apache.zookeeper.version.Info {
 
     public static int getRevision() {
         return REVISION;
@@ -29,7 +29,8 @@ public class Version implements org.apache.zookeeper.version.Info{
     }
 
     public static String getVersion() {
-        return MAJOR + "." + MINOR + "." + MICRO;
+        return MAJOR + "." + MINOR + "." + MICRO
+            + (QUALIFIER == null ? "" : "-" + QUALIFIER);
     }
 
     public static String getVersionRevision() {

+ 49 - 18
src/java/main/org/apache/zookeeper/version/util/VerGen.java

@@ -21,6 +21,8 @@ package org.apache.zookeeper.version.util;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 public class VerGen {
     private static final String PACKAGE_NAME = "org.apache.zookeeper.version";
@@ -28,12 +30,12 @@ public class VerGen {
 
     static void printUsage() {
         System.out.print("Usage:\tjava  -cp <classpath> org.apache.zookeeper."
-                + "version.util.VerGen maj.min.micro rev buildDate");
+                + "version.util.VerGen maj.min.micro[-qualifier] rev buildDate");
         System.exit(1);
     }
 
-    static void generateFile(File outputDir, int maj, int min, int micro, int rev,
-            String buildDate) {
+    public static void generateFile(File outputDir, Version version, int rev, String buildDate)
+    {
         String path = PACKAGE_NAME.replaceAll("\\.", "/");
         File pkgdir = new File(outputDir, path);
         if (!pkgdir.exists()) {
@@ -74,9 +76,13 @@ public class VerGen {
             w.write("\n");
             w.write("package " + PACKAGE_NAME + ";\n\n");
             w.write("public interface " + TYPE_NAME + " {\n");
-            w.write("    public static final int MAJOR=" + maj + ";\n");
-            w.write("    public static final int MINOR=" + min + ";\n");
-            w.write("    public static final int MICRO=" + micro + ";\n");
+            w.write("    public static final int MAJOR=" + version.maj + ";\n");
+            w.write("    public static final int MINOR=" + version.min + ";\n");
+            w.write("    public static final int MICRO=" + version.micro + ";\n");
+            w.write("    public static final String QUALIFIER="
+                    + (version.qualifier == null ? null :
+                        "\"" + version.qualifier + "\"")
+                    + ";\n");
             if (rev < 0) {
                 System.out.println("Unknown REVISION number, using " + rev);
             }
@@ -100,18 +106,46 @@ public class VerGen {
         }
     }
 
+    public static class Version {
+        public int maj;
+        public int min;
+        public int micro;
+        public String qualifier;
+    }
+    
+    public static Version parseVersionString(String input) {
+        Version result = new Version();
+
+        Pattern p = Pattern.compile("^(\\d+).(\\d+).(\\d+)(-(.+))?$");
+        Matcher m = p.matcher(input);
+
+        if (!m.matches()) {
+            return null;
+        }
+        result.maj = Integer.parseInt(m.group(1));
+        result.min = Integer.parseInt(m.group(2));
+        result.micro = Integer.parseInt(m.group(3));
+        if (m.groupCount() == 5) {
+            result.qualifier = m.group(5);
+        } else {
+            result.qualifier = null;
+        }
+        return result;
+    }
+
     /**
      * Emits a org.apache.zookeeper.version.Info interface file with version and
      * revision information constants set to the values passed in as command
      * line parameters. The file is created in the current directory. <br>
-     * Usage: java org.apache.zookeeper.version.util.VerGen maj.min.micro rev
-     * buildDate
+     * Usage: java org.apache.zookeeper.version.util.VerGen maj.min.micro[-qualifier]
+     * rev buildDate
      *
      * @param args
      *            <ul>
      *            <li>maj - major version number
      *            <li>min - minor version number
      *            <li>micro - minor minor version number
+     *            <li>qualifier - optional qualifier (dash followed by qualifier text)
      *            <li>rev - current SVN revision number
      *            <li>buildDate - date the build
      *            </ul>
@@ -120,25 +154,22 @@ public class VerGen {
         if (args.length != 3)
             printUsage();
         try {
-            String[] v = args[0].split("\\.");
-            if (v.length != 3) {
-                System.err
-                        .println("Invalid version number format, must be \"x.y.z\"");
+            Version version = parseVersionString(args[0]);
+            if (version == null) {
+                System.err.println(
+                        "Invalid version number format, must be \"x.y.z(-.*)?\"");
                 System.exit(1);
             }
-            int maj = Integer.parseInt(v[0]);
-            int min = Integer.parseInt(v[1]);
-            int micro = Integer.parseInt(v[2]);
             int rev;
             try {
                 rev = Integer.parseInt(args[1]);
             } catch (NumberFormatException e) {
                 rev = -1;
             }
-            generateFile(new File("."), maj, min, micro, rev, args[2]);
+            generateFile(new File("."), version, rev, args[2]);
         } catch (NumberFormatException e) {
-            System.err
-                .println("All version-related parameters must be valid integers!");
+            System.err.println(
+                    "All version-related parameters must be valid integers!");
             throw e;
         }
     }

+ 75 - 0
src/java/test/org/apache/zookeeper/VerGenTest.java

@@ -0,0 +1,75 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.zookeeper;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.zookeeper.test.ClientBase;
+import org.apache.zookeeper.version.util.VerGen;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+
+/**
+ * Test VerGen, used during the build.
+ *
+ */
+@RunWith(Parameterized.class)
+public class VerGenTest extends ZKTestCase {
+    @Parameters
+    public static Collection<Object[]> data() {
+            return Arrays.asList(new Object[][] {
+                            {"1.2.3", new Object[] {1, 2, 3, null}},
+                            {"1.2.3-dev", new Object[] {1, 2, 3, "dev"}},
+                            {"1.2.3-SNAPSHOT", new Object[] {1, 2, 3, "SNAPSHOT"}},
+                            {"1.2.3-foo-bar+123", new Object[] {1, 2, 3, "foo-bar+123"}}
+            });
+    }
+
+    private String input;
+
+    private Object[] expected;
+
+    public VerGenTest(String input, Object[] expected) {
+        this.input = input;
+        this.expected = expected;
+    }
+
+    @Test
+    public void testParser() {
+        VerGen.Version v = VerGen.parseVersionString(input);
+        Assert.assertEquals(expected[0], v.maj);
+        Assert.assertEquals(expected[1], v.min);
+        Assert.assertEquals(expected[2], v.micro);
+        Assert.assertEquals(expected[3], v.qualifier);
+    }
+
+    @Test
+    public void testGenFile() throws Exception {
+        VerGen.Version v = VerGen.parseVersionString(input);
+        File outputDir = ClientBase.createTmpDir();
+        VerGen.generateFile(outputDir, v, 1, "Nov1");
+        ClientBase.recursiveDelete(outputDir);
+    }
+}