[portal-commits] JBoss Portal SVN: r8876 - in modules/common/trunk/common/src: test/java/org/jboss/portal/test/common/util and 1 other directory.

portal-commits at lists.jboss.org portal-commits at lists.jboss.org
Mon Nov 12 07:05:59 EST 2007


Author: julien at jboss.com
Date: 2007-11-12 07:05:59 -0500 (Mon, 12 Nov 2007)
New Revision: 8876

Added:
   modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/VersionTestCase.java
Modified:
   modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Version.java
Log:
test case for Version class

Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Version.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Version.java	2007-11-12 10:54:49 UTC (rev 8875)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Version.java	2007-11-12 12:05:59 UTC (rev 8876)
@@ -32,12 +32,25 @@
 public class Version
 {
 
+   /** . */
    private final String codeName;
+
+   /** . */
    private final String toString;
+
+   /** . */
    private final String name;
+
+   /** . */
    private final int major;
+
+   /** . */
    private final int minor;
+
+   /** . */
    private final int patch;
+
+   /** . */
    private final Qualifier qualifier;
 
    public Version(String name, int major, int minor, int patch, Qualifier qualifier, String codeName)
@@ -50,18 +63,10 @@
       {
          throw new IllegalArgumentException("Major cannot be negative");
       }
-      if (major > 9)
-      {
-         throw new IllegalArgumentException("Major cannot be greater than 9");
-      }
       if (minor < 0)
       {
          throw new IllegalArgumentException("Minor cannot be negative");
       }
-      if (minor > 9)
-      {
-         throw new IllegalArgumentException("Minor cannot be greater than 9");
-      }
       if (patch < 0)
       {
          throw new IllegalArgumentException("Patch cannot be negative");
@@ -80,7 +85,7 @@
       this.patch = patch;
       this.qualifier = qualifier;
       this.codeName = codeName;
-      this.toString = buildToString();
+      this.toString = Format.JBOSS_PRODUCT_CONVENTION.toString(this);
    }
 
    public String getCodeName()
@@ -123,33 +128,36 @@
       return toString;
    }
 
-   private String buildToString()
+   public String toString(Format format) throws IllegalArgumentException
    {
-      StringBuffer buffer = new StringBuffer(getName());
-      buffer.append(" ")
-         .append(getMajor()).append('.')
-         .append(getMinor()).append('.')
-         .append(getPatch()).append('-')
-         .append(getQualifier());
-      return buffer.toString();
+      if (format == null)
+      {
+         throw new IllegalArgumentException();
+      }
+      return format.toString(this);
    }
 
    /** Type safe enum for intermediate major. */
    public static class Qualifier
    {
 
-      public static class Prefix
+      public enum Prefix
       {
 
-         public static final Prefix SNAPSHOT = new Prefix("SNAPSHOT", false, "Snapshot");
-         public static final Prefix ALPHA = new Prefix("ALPHA", true, "Alpha");
-         public static final Prefix BETA = new Prefix("BETA", true, "Beta");
-         public static final Prefix CR = new Prefix("CR", true, "Candidate for release");
-         public static final Prefix GA = new Prefix("GA", false, "General Availability");
-         public static final Prefix SP = new Prefix("SP", true, "Service pack");
+         SNAPSHOT("SNAPSHOT", false, "Snapshot"),
+         ALPHA("ALPHA", true, "Alpha"),
+         BETA("BETA", true, "Beta"),
+         CR("CR", true, "Candidate for release"),
+         GA("GA", false, "General Availability"),
+         SP("SP", true, "Service pack");
 
+         /** . */
          private final String name;
+
+         /** . */
          private final String description;
+
+         /** . */
          private final boolean suffixable;
 
          private Prefix(String name, boolean suffixable, String description)
@@ -180,16 +188,16 @@
          }
       }
 
-      public static class Suffix
+      public enum Suffix
       {
 
-         public static final Suffix EMPTY = new Suffix("");
-         public static final Suffix SUFFIX_1 = new Suffix("1");
-         public static final Suffix SUFFIX_2 = new Suffix("2");
-         public static final Suffix SUFFIX_3 = new Suffix("3");
-         public static final Suffix SUFFIX_4 = new Suffix("4");
-         public static final Suffix SUFFIX_5 = new Suffix("5");
-         public static final Suffix SUFFIX_6 = new Suffix("6");
+         EMPTY(""),
+         SUFFIX_1("1"),
+         SUFFIX_2("2"),
+         SUFFIX_3("3"),
+         SUFFIX_4("4"),
+         SUFFIX_5("5"),
+         SUFFIX_6("6");
 
          /** . */
          private final String value;
@@ -227,7 +235,7 @@
          }
          if (suffix == null)
          {
-            suffix = new Suffix("");
+            suffix = Suffix.EMPTY;
          }
          if (prefix.isSuffixable() == false && suffix.value.length() > 0)
          {
@@ -253,4 +261,27 @@
          return toString;
       }
    }
+
+   public interface Format
+   {
+
+      /**
+       * Implement formatting as defined <a href="http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossProductVersioning">here</a>
+       */
+      Format JBOSS_PRODUCT_CONVENTION = new Format()
+      {
+         public String toString(Version version)
+         {
+            StringBuffer buffer = new StringBuffer(version.getName());
+            buffer.append(" ")
+               .append(version.getMajor()).append('.')
+               .append(version.getMinor()).append('.')
+               .append(version.getPatch()).append('-')
+               .append(version.getQualifier());
+            return buffer.toString();
+         }
+      };
+
+      String toString(Version version);
+   }
 }

Added: modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/VersionTestCase.java
===================================================================
--- modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/VersionTestCase.java	                        (rev 0)
+++ modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/VersionTestCase.java	2007-11-12 12:05:59 UTC (rev 8876)
@@ -0,0 +1,150 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat                                               *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual                    *
+ * contributors as indicated by the @authors tag. See the                     *
+ * copyright.txt in the distribution for a full listing of                    *
+ * individual contributors.                                                   *
+ *                                                                            *
+ * This is free software; you can redistribute it and/or modify it            *
+ * under the terms of the GNU Lesser General Public License as                *
+ * published by the Free Software Foundation; either version 2.1 of           *
+ * the License, or (at your option) any later version.                        *
+ *                                                                            *
+ * This software is distributed in the hope that it will be useful,           *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU           *
+ * Lesser General Public License for more details.                            *
+ *                                                                            *
+ * You should have received a copy of the GNU Lesser General Public           *
+ * License along with this software; if not, write to the Free                *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA         *
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.                   *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.util.Version;
+
+/**
+ * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class VersionTestCase extends TestCase
+{
+
+   /** . */
+   private final Version.Qualifier GAQualifer = new Version.Qualifier(Version.Qualifier.Prefix.GA);
+
+   public void testIllegalNameThrowsIAE()
+   {
+      try
+      {
+         new Version(null, 0, 0, 0, GAQualifer, "code");
+         fail();
+      }
+      catch (IllegalArgumentException ignore)
+      {
+      }
+   }
+
+   public void testIllegalMajorThrowsIAE()
+   {
+      try
+      {
+         new Version("name", -1, 0, 0, GAQualifer, "code");
+         fail();
+      }
+      catch (IllegalArgumentException ignore)
+      {
+      }
+   }
+
+   public void testIllegalMinorThrowsIAE()
+   {
+      try
+      {
+         new Version("name", 0, -1, 0, GAQualifer, "code");
+         fail();
+      }
+      catch (IllegalArgumentException ignore)
+      {
+      }
+   }
+
+   public void testIllegalQualifierThrowsIAE()
+   {
+      try
+      {
+         new Version("name", 0, 0, 0, null, "code");
+         fail();
+      }
+      catch (IllegalArgumentException ignore)
+      {
+      }
+
+      //
+      try
+      {
+         new Version.Qualifier(null);
+         fail();
+      }
+      catch (IllegalArgumentException ignore)
+      {
+      }
+
+      //
+      try
+      {
+         new Version.Qualifier(null, Version.Qualifier.Suffix.EMPTY);
+         fail();
+      }
+      catch (IllegalArgumentException ignore)
+      {
+      }
+
+      //
+      try
+      {
+         new Version.Qualifier(Version.Qualifier.Prefix.GA, Version.Qualifier.Suffix.SUFFIX_1);
+         fail();
+      }
+      catch (IllegalArgumentException ignore)
+      {
+      }
+   }
+
+   public void testIllegalCodeNameThrowsIAE()
+   {
+      try
+      {
+         new Version("name", 0, 0, 0, GAQualifer, null);
+         fail();
+      }
+      catch (IllegalArgumentException ignore)
+      {
+      }
+   }
+
+   public void testState()
+   {
+      Version version = new Version("foo", 1, 2, 3, new Version.Qualifier(Version.Qualifier.Prefix.CR, Version.Qualifier.Suffix.SUFFIX_3), "bar");
+      assertEquals("foo", version.getName());
+      assertEquals(1, version.getMajor());
+      assertEquals(2, version.getMinor());
+      assertEquals(3, version.getPatch());
+      assertEquals(Version.Qualifier.Prefix.CR, version.getQualifier().getPrefix());
+      assertEquals(Version.Qualifier.Suffix.SUFFIX_3, version.getQualifier().getSuffix());
+      assertEquals("bar", version.getCodeName());
+   }
+
+   public void testQualifier()
+   {
+      
+   }
+
+   public void testFormat()
+   {
+      Version version = new Version("foo", 1, 2, 3, new Version.Qualifier(Version.Qualifier.Prefix.CR, Version.Qualifier.Suffix.SUFFIX_3), "bar");
+      assertEquals("foo 1.2.3-CR3", version.toString());
+   }
+}




More information about the portal-commits mailing list