You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2002/11/23 02:07:37 UTC

cvs commit: jakarta-commons-sandbox/util/src/test/org/apache/commons/util/identifier TestIdentifierUtils.java TestIdentifierFactories.java IdentifierTestSuite.java

scolebourne    2002/11/22 17:07:37

  Added:       util/src/test/org/apache/commons/util/identifier
                        TestIdentifierUtils.java
                        TestIdentifierFactories.java
                        IdentifierTestSuite.java
  Log:
  Added tests for new identifier package
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/util/src/test/org/apache/commons/util/identifier/TestIdentifierUtils.java
  
  Index: TestIdentifierUtils.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.util.identifier;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.lang.functor.Factory;
  /**
   * Tests the org.apache.commons.util.identifier.IdentifierUtils class.
   *
   * @author Stephen Colebourne
   * @version $Id: TestIdentifierUtils.java,v 1.1 2002/11/23 01:07:36 scolebourne Exp $
   */
  public class TestIdentifierUtils extends junit.framework.TestCase {
  
      /**
       * Construct
       */
      public TestIdentifierUtils(String name) {
          super(name);
      }
  
      /**
       * Return class aa a test suite.
       */
      public static Test suite() {
          return new TestSuite(TestIdentifierUtils.class);
      }
  
      /**
       * Set up instance variables required by this test case.
       */
      public void setUp() {
      }
  
      /**
       * Tear down instance variables required by this test case.
       */
      public void tearDown() {
      }
  
      public void testGetSetDefault() {
          Factory f = IdentifierUtils.getDefaultIdentifierFactory();
          assertEquals(LongIncrementingIdentifierFactory.class, f.getClass());
          try {
              IdentifierUtils.setDefaultIdentifierFactory(StringAlphanumeric10IdentifierFactory.INSTANCE);
              assertSame(StringAlphanumeric10IdentifierFactory.INSTANCE, IdentifierUtils.getDefaultIdentifierFactory());
              
          } finally {
              IdentifierUtils.setDefaultIdentifierFactory(f);
          }
      }
  
      public void testSetDefaultEx() {
          try {
              IdentifierUtils.setDefaultIdentifierFactory(null);
              
          } catch (IllegalArgumentException ex) {
              return;
          }
          fail();
      }
  
      public void testNextDefault() {
          Factory f = IdentifierUtils.getDefaultIdentifierFactory();
          try {
              IdentifierUtils.setDefaultIdentifierFactory(new LongIncrementingIdentifierFactory());
              assertEquals(new Long(0), IdentifierUtils.nextIdentifier());
              assertEquals(new Long(1), IdentifierUtils.nextIdentifier());
              assertEquals(new Long(2), IdentifierUtils.nextIdentifier());
              
          } finally {
              IdentifierUtils.setDefaultIdentifierFactory(f);
          }
      }
  
      public void testNextInstance() {
          Factory f = new LongIncrementingIdentifierFactory();
          assertEquals(new Long(0), IdentifierUtils.nextIdentifier(f));
          assertEquals(new Long(1), IdentifierUtils.nextIdentifier(f));
          assertEquals(new Long(2), IdentifierUtils.nextIdentifier(f));
      }
      
      public void testNextInstanceEx() {
          try {
              IdentifierUtils.nextIdentifier(null);
              
          } catch (IllegalArgumentException ex) {
              return;
          }
          fail();
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/util/src/test/org/apache/commons/util/identifier/TestIdentifierFactories.java
  
  Index: TestIdentifierFactories.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.util.identifier;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.lang.functor.Factory;
  /**
   * Tests the identifier factory classes.
   *
   * @author Stephen Colebourne
   * @version $Id: TestIdentifierFactories.java,v 1.1 2002/11/23 01:07:36 scolebourne Exp $
   */
  public class TestIdentifierFactories extends junit.framework.TestCase {
  
      /**
       * Construct
       */
      public TestIdentifierFactories(String name) {
          super(name);
      }
  
      /**
       * Return class aa a test suite.
       */
      public static Test suite() {
          return new TestSuite(TestIdentifierFactories.class);
      }
  
      /**
       * Set up instance variables required by this test case.
       */
      public void setUp() {
      }
  
      /**
       * Tear down instance variables required by this test case.
       */
      public void tearDown() {
      }
  
      public void testLongIncrementing() {
          Factory f = LongIncrementingIdentifierFactory.INSTANCE;
          assertEquals(new Long(0), f.create());
          assertEquals(new Long(1), f.create());
          assertEquals(new Long(2), f.create());
          assertEquals(new Long(3), f.create());
          assertEquals(new Long(4), f.create());
      }
  
      public void testLongIncrementingInit() {
          Factory f = new LongIncrementingIdentifierFactory(true, 100);
          assertEquals(new Long(100), f.create());
          assertEquals(new Long(101), f.create());
      }
  
      public void testLongIncrementingWrap() {
          Factory f = new LongIncrementingIdentifierFactory(true, Long.MAX_VALUE);
          assertEquals(new Long(Long.MAX_VALUE), f.create());
          assertEquals(new Long(Long.MIN_VALUE), f.create());
      }
  
      public void testLongIncrementingNoWrap() {
          Factory f = new LongIncrementingIdentifierFactory(false, Long.MAX_VALUE);
          try {
              f.create();
          } catch (IdentifierFactoryException ex) {
              return;
          }
          fail();
      }
  
      public void testLongRandom() {
          Factory f = LongRandomIdentifierFactory.INSTANCE;
          Long a = (Long) f.create();
          Long b = (Long) f.create();
          assertTrue(a.longValue() != 0);
          assertTrue(b.longValue() != 0);
          // could fail, but unlikely
          assertTrue(a.longValue() != b.longValue());
          assertTrue(a.longValue() != (b.longValue() + 1));
          assertTrue(a.longValue() != (b.longValue() - 1));
      }
  
      public void testStringLong() {
          Factory f = new StringIncrementingLongIdentifierFactory();
          assertEquals("0", f.create());
          assertEquals("1", f.create());
      }
  
      public void testStringLongInit() {
          Factory f = new StringIncrementingLongIdentifierFactory(true, 100L);
          assertEquals("100", f.create());
          assertEquals("101", f.create());
      }
  
      public void testStringLong12() {
          Factory f = new StringIncrementingLongIdentifierFactory(true, 0, 12);
          assertEquals("000000000000", f.create());
          assertEquals("000000000001", f.create());
      }
  
      public void testStringVLong() {
          Factory f = new StringIncrementingVLongIdentifierFactory();
          assertEquals("00", f.create());
          assertEquals("01", f.create());
      }
  
      public void testStringVLong12() {
          Factory f = new StringIncrementingVLongIdentifierFactory(true, 12);
          assertEquals("000000000000", f.create());
          assertEquals("000000000001", f.create());
      }
  
      public void testStringAN10() {
          Factory f = new StringAlphanumeric10IdentifierFactory();
          String a = (String) f.create();
          String b = (String) f.create();
          assertTrue(a.length() >= 10);
          assertTrue(b.length() >= 10);
          // could fail, but unlikely
          assertTrue(a.substring(6, 9) != b.substring(6, 9));
          assertEquals("0", a.substring(9));
          assertEquals("1", b.substring(9));
      }
  
      public void testStringAN15() {
          Factory f = new StringAlphanumeric15IdentifierFactory();
          String a = (String) f.create();
          String b = (String) f.create();
          assertEquals(15, a.length());
          assertEquals(15, b.length());
          // could fail, but unlikely
          assertTrue(a.substring(5, 8) != b.substring(5, 8));
          assertEquals("0", a.substring(14));
          assertEquals("1", b.substring(14));
      }
  
      public void testStringRMI() {
          Factory f = new StringRMIIdentifierFactory();
          String a = (String) f.create();
          String b = (String) f.create();
          assertTrue(a.length() > 10);
          assertTrue(b.length() > 10);
          assertTrue(a.equals(b) == false);
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/util/src/test/org/apache/commons/util/identifier/IdentifierTestSuite.java
  
  Index: IdentifierTestSuite.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.util.identifier;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  /**
   * Test suite for the Identifier pattern package.
   *
   * @author Stephen Colebourne
   * @version $Id: IdentifierTestSuite.java,v 1.1 2002/11/23 01:07:36 scolebourne Exp $
   */
  public class IdentifierTestSuite extends TestCase {
      
      /**
       * Construct a new instance.
       */
      public IdentifierTestSuite(String name) {
          super(name);
      }
  
      /**
       * Command-line interface.
       */
      public static void main(String[] args) {
          TestRunner.run(suite());
      }
  
      /**
       * Get the suite of tests
       */
      public static Test suite() {
          TestSuite suite = new TestSuite();
          suite.addTest(TestIdentifierUtils.suite());
          suite.addTest(TestIdentifierFactories.suite());
          return suite;
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>