commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jke...@apache.org
Subject cvs commit: jakarta-commons/cli/src/test/org/apache/commons/cli2/validation ClassValidatorTest.java
Date Tue, 30 Mar 2004 23:50:07 GMT
jkeyes      2004/03/30 15:50:07

  Modified:    cli/src/test/org/apache/commons/cli2/validation Tag:
                        RESEARCH_CLI_2_ROXSPRING ClassValidatorTest.java
  Added:       cli/src/test/org/apache/commons/cli2/validation/protect Tag:
                        RESEARCH_CLI_2_ROXSPRING ProtectedClass.java
  Log:
  - small reorg of code
  - added tests for 100% code coverage
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.1.2.1   +21 -0     jakarta-commons/cli/src/test/org/apache/commons/cli2/validation/protect/Attic/ProtectedClass.java
  
  
  
  
  No                   revision
  No                   revision
  1.1.2.6   +136 -31   jakarta-commons/cli/src/test/org/apache/commons/cli2/validation/Attic/ClassValidatorTest.java
  
  Index: ClassValidatorTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli2/validation/Attic/ClassValidatorTest.java,v
  retrieving revision 1.1.2.5
  retrieving revision 1.1.2.6
  diff -u -r1.1.2.5 -r1.1.2.6
  --- ClassValidatorTest.java	8 Feb 2004 13:09:00 -0000	1.1.2.5
  +++ ClassValidatorTest.java	30 Mar 2004 23:50:07 -0000	1.1.2.6
  @@ -15,43 +15,46 @@
    */
   package org.apache.commons.cli2.validation;
   
  +import java.net.URL;
  +import java.net.URLClassLoader;
   import java.util.Arrays;
   import java.util.Iterator;
   import java.util.List;
   
  -import org.apache.commons.cli2.resources.ResourceHelper;
  -
   import junit.framework.TestCase;
   
  +import org.apache.commons.cli2.resources.ResourceHelper;
  +
   public class ClassValidatorTest extends TestCase {
   
       private final static ResourceHelper resources =
           ResourceHelper.getResourceHelper(ClassValidatorTest.class);
   
  -    public void testValidate_NameValid() throws InvalidArgumentException {
  +    private ClassValidator validator;
  +
  +    protected void setUp() {
  +        validator = new ClassValidator();
  +    }
  +
  +    public void testValidName() throws InvalidArgumentException {
           final Object[] array = new Object[] { "MyApp", "org.apache.ant.Main" };
           final List list = Arrays.asList(array);
  -        final Validator validator = new ClassValidator();
   
           validator.validate(list);
   
  -        final Iterator i = list.iterator();
  -        assertEquals("MyApp", i.next());
  -        assertEquals("org.apache.ant.Main", i.next());
  -        assertFalse(i.hasNext());
  +        assertEquals("Name is incorrect", "MyApp", list.get(0));
  +        assertEquals("Name is incorrect", "org.apache.ant.Main", list.get(1));
       }
   
  -    public void testValidate_NameBadStart() {
  +    public void testNameBadStart() {
           final String className = "1stClass";
           final Object[] array = new Object[] { className };
           final List list = Arrays.asList(array);
  -        final Validator validator = new ClassValidator();
   
           try {
               validator.validate(list);
  -            fail("BadStart!");
  -        }
  -        catch (InvalidArgumentException ive) {
  +            fail("Class name cannot start with a number.");
  +        } catch (InvalidArgumentException ive) {
               assertEquals(
                   resources.getMessage(
                       "ClassValidator.error.bad.classname",
  @@ -60,18 +63,16 @@
           }
       }
   
  -    public void testValidate_NameBadEnd() {
  +    public void testNameBadEnd() {
           final String className = "My.Class.";
   
           final Object[] array = new Object[] { className };
           final List list = Arrays.asList(array);
  -        final Validator validator = new ClassValidator();
   
           try {
               validator.validate(list);
  -            fail("BadEnd!");
  -        }
  -        catch (InvalidArgumentException ive) {
  +            fail("Trailing period not permitted.");
  +        } catch (InvalidArgumentException ive) {
               assertEquals(
                   resources.getMessage(
                       "ClassValidator.error.bad.classname",
  @@ -80,18 +81,34 @@
           }
       }
   
  -    public void testValidate_NameBadMiddle() {
  +    public void testNameBadMiddle() {
           final String className = "My..Class";
   
           final Object[] array = new Object[] { className };
           final List list = Arrays.asList(array);
  -        final Validator validator = new ClassValidator();
   
           try {
               validator.validate(list);
  -            fail("BadMiddle!");
  +            fail("Two consecutive periods is not permitted.");
  +        } catch (InvalidArgumentException ive) {
  +            assertEquals(
  +                resources.getMessage(
  +                    "ClassValidator.error.bad.classname",
  +                    className),
  +                ive.getMessage());
           }
  -        catch (InvalidArgumentException ive) {
  +    }
  +
  +    public void testIllegalNameChar() {
  +        final String className = "My?Class";
  +
  +        final Object[] array = new Object[] { className };
  +        final List list = Arrays.asList(array);
  +
  +        try {
  +            validator.validate(list);
  +            fail("Illegal character not allowed in Class name.");
  +        } catch (InvalidArgumentException ive) {
               assertEquals(
                   resources.getMessage(
                       "ClassValidator.error.bad.classname",
  @@ -100,43 +117,131 @@
           }
       }
   
  -    public void testValidate_LoadValid() throws InvalidArgumentException {
  +    public void testLoadable() throws InvalidArgumentException {
  +        assertFalse("Validator is loadable", validator.isLoadable());
  +        validator.setLoadable(true);
  +        assertTrue("Validator is NOT loadable", validator.isLoadable());
  +        validator.setLoadable(false);
  +        assertFalse("Validator is loadable", validator.isLoadable());
  +    }
  +
  +    public void testLoadValid() throws InvalidArgumentException {
           final Object[] array =
               new Object[] {
                   "org.apache.commons.cli2.Option",
                   "java.util.Vector" };
           final List list = Arrays.asList(array);
  -        final ClassValidator validator = new ClassValidator();
  -        validator.setLoadable(true);
   
  +        validator.setLoadable(true);
           validator.validate(list);
   
           final Iterator i = list.iterator();
           assertEquals(
               "org.apache.commons.cli2.Option",
  -            ((Class)i.next()).getName());
  -        assertEquals("java.util.Vector", ((Class)i.next()).getName());
  +            ((Class) i.next()).getName());
  +        assertEquals("java.util.Vector", ((Class) i.next()).getName());
           assertFalse(i.hasNext());
       }
   
  -    public void testValidate_LoadInvalid() {
  +    public void testLoadInvalid() {
           final String className = "org.apache.commons.cli2.NonOption";
   
           final Object[] array = new Object[] { className, "java.util.Vectors" };
           final List list = Arrays.asList(array);
  -        final ClassValidator validator = new ClassValidator();
  +
           validator.setLoadable(true);
   
           try {
               validator.validate(list);
               fail("Class Not Found");
  -        }
  -        catch (InvalidArgumentException ive) {
  +        } catch (InvalidArgumentException ive) {
               assertEquals(
                   resources.getMessage(
                       "ClassValidator.error.class.notfound",
                       className),
                   ive.getMessage());
           }
  +    }
  +
  +    public void testInstantiate() throws InvalidArgumentException {
  +        assertFalse("Validator creates instances", validator.isInstance());
  +        validator.setInstance(true);
  +        assertTrue(
  +            "Validator does NOT create instances",
  +            validator.isInstance());
  +        validator.setInstance(false);
  +        assertFalse("Validator creates instances", validator.isInstance());
  +    }
  +
  +    public void testCreateClassInstance() throws InvalidArgumentException {
  +        final Object[] array = new Object[] { "java.util.Vector" };
  +        final List list = Arrays.asList(array);
  +
  +        validator.setInstance(true);
  +
  +        validator.validate(list);
  +        assertTrue(
  +            "Vector instance NOT found",
  +            list.get(0) instanceof java.util.Vector);
  +    }
  +
  +    public void testCreateInterfaceInstance() throws InvalidArgumentException {
  +        final String className = "java.util.Map";
  +        final Object[] array = new Object[] { className };
  +        final List list = Arrays.asList(array);
  +
  +        validator.setInstance(true);
  +        
  +        try {
  +            validator.validate(list);
  +            fail("It's not possible to create a '" + className + "'");
  +        }
  +        catch (final InvalidArgumentException ive) {
  +            assertEquals(
  +                    resources.getMessage(
  +                            "ClassValidator.error.class.create",
  +                            className),
  +                            ive.getMessage());
  +        }
  +    }
  +
  +    public void testCreateProtectedInstance() throws InvalidArgumentException {
  +        final String className = "org.apache.commons.cli2.validation.protect.ProtectedClass";
  +        final Object[] array = new Object[] { className };
  +        final List list = Arrays.asList(array);
  +
  +        validator.setInstance(true);
  +        
  +        try {
  +            validator.validate(list);
  +            fail("It's not possible to create a '" + className + "'");
  +        }
  +        catch (final InvalidArgumentException ive) {
  +            assertEquals(
  +                    resources.getMessage(
  +                            "ClassValidator.error.class.access",
  +                            className,
  +                            "Class org.apache.commons.cli2.validation.ClassValidator "
+
  +                            "can not access a member of class " +
  +                            "org.apache.commons.cli2.validation.protect.ProtectedClass
" +
  +                            "with modifiers \"protected\""),
  +                            ive.getMessage());
  +        }
  +    }
  +    
  +    public void testClassloader() throws InvalidArgumentException {
  +        assertEquals(
  +            "Wrong classloader found",
  +            validator.getClass().getClassLoader(),
  +            validator.getClassLoader());
  +
  +        URLClassLoader classloader = new URLClassLoader(new URL[] {
  +        });
  +        validator.setClassLoader(classloader);
  +
  +        assertEquals(
  +            "Wrong classloader found",
  +            classloader,
  +            validator.getClassLoader());
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message