001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.cli2.validation;
018
019import java.net.URL;
020import java.net.URLClassLoader;
021import java.util.Arrays;
022import java.util.Iterator;
023import java.util.List;
024
025import junit.framework.TestCase;
026
027import org.apache.commons.cli2.resource.ResourceHelper;
028
029public class ClassValidatorTest extends TestCase {
030
031    private final static ResourceHelper resources =
032        ResourceHelper.getResourceHelper();
033
034    private ClassValidator validator;
035
036    protected void setUp() {
037        validator = new ClassValidator();
038    }
039
040    public void testValidName() throws InvalidArgumentException {
041        final Object[] array = new Object[] { "MyApp", "org.apache.ant.Main" };
042        final List list = Arrays.asList(array);
043
044        validator.validate(list);
045
046        assertEquals("Name is incorrect", "MyApp", list.get(0));
047        assertEquals("Name is incorrect", "org.apache.ant.Main", list.get(1));
048    }
049
050    public void testNameBadStart() {
051        final String className = "1stClass";
052        final Object[] array = new Object[] { className };
053        final List list = Arrays.asList(array);
054
055        try {
056            validator.validate(list);
057            fail("Class name cannot start with a number.");
058        } catch (InvalidArgumentException ive) {
059            assertEquals(
060                resources.getMessage(
061                    "ClassValidator.bad.classname",
062                    className),
063                ive.getMessage());
064        }
065    }
066
067    public void testNameBadEnd() {
068        final String className = "My.Class.";
069
070        final Object[] array = new Object[] { className };
071        final List list = Arrays.asList(array);
072
073        try {
074            validator.validate(list);
075            fail("Trailing period not permitted.");
076        } catch (InvalidArgumentException ive) {
077            assertEquals(
078                resources.getMessage(
079                    "ClassValidator.bad.classname",
080                    className),
081                ive.getMessage());
082        }
083    }
084
085    public void testNameBadMiddle() {
086        final String className = "My..Class";
087
088        final Object[] array = new Object[] { className };
089        final List list = Arrays.asList(array);
090
091        try {
092            validator.validate(list);
093            fail("Two consecutive periods is not permitted.");
094        } catch (InvalidArgumentException ive) {
095            assertEquals(
096                resources.getMessage(
097                    "ClassValidator.bad.classname",
098                    className),
099                ive.getMessage());
100        }
101    }
102
103    public void testIllegalNameChar() {
104        final String className = "My?Class";
105
106        final Object[] array = new Object[] { className };
107        final List list = Arrays.asList(array);
108
109        try {
110            validator.validate(list);
111            fail("Illegal character not allowed in Class name.");
112        } catch (InvalidArgumentException ive) {
113            assertEquals(
114                resources.getMessage(
115                    "ClassValidator.bad.classname",
116                    className),
117                ive.getMessage());
118        }
119    }
120
121    public void testLoadable() {
122        assertFalse("Validator is loadable", validator.isLoadable());
123        validator.setLoadable(true);
124        assertTrue("Validator is NOT loadable", validator.isLoadable());
125        validator.setLoadable(false);
126        assertFalse("Validator is loadable", validator.isLoadable());
127    }
128
129    public void testLoadValid() throws InvalidArgumentException {
130        final Object[] array =
131            new Object[] {
132                "org.apache.commons.cli2.Option",
133                "java.util.Vector" };
134        final List list = Arrays.asList(array);
135
136        validator.setLoadable(true);
137        validator.validate(list);
138
139        final Iterator i = list.iterator();
140        assertEquals(
141            "org.apache.commons.cli2.Option",
142            ((Class) i.next()).getName());
143        assertEquals("java.util.Vector", ((Class) i.next()).getName());
144        assertFalse(i.hasNext());
145    }
146
147    public void testLoadInvalid() {
148        final String className = "org.apache.commons.cli2.NonOption";
149
150        final Object[] array = new Object[] { className, "java.util.Vectors" };
151        final List list = Arrays.asList(array);
152
153        validator.setLoadable(true);
154
155        try {
156            validator.validate(list);
157            fail("Class Not Found");
158        } catch (InvalidArgumentException ive) {
159            assertEquals(
160                resources.getMessage(
161                    "ClassValidator.class.notfound",
162                    className),
163                ive.getMessage());
164        }
165    }
166
167    public void testInstantiate() {
168        assertFalse("Validator creates instances", validator.isInstance());
169        validator.setInstance(true);
170        assertTrue(
171            "Validator does NOT create instances",
172            validator.isInstance());
173        validator.setInstance(false);
174        assertFalse("Validator creates instances", validator.isInstance());
175    }
176
177    public void testCreateClassInstance() throws InvalidArgumentException {
178        final Object[] array = new Object[] { "java.util.Vector" };
179        final List list = Arrays.asList(array);
180
181        validator.setInstance(true);
182
183        validator.validate(list);
184        assertTrue(
185            "Vector instance NOT found",
186            list.get(0) instanceof java.util.Vector);
187    }
188
189    public void testCreateInterfaceInstance() {
190        final String className = "java.util.Map";
191        final Object[] array = new Object[] { className };
192        final List list = Arrays.asList(array);
193
194        validator.setInstance(true);
195
196        try {
197            validator.validate(list);
198            fail("It's not possible to create a '" + className + "'");
199        }
200        catch (final InvalidArgumentException ive) {
201            assertEquals(
202                    resources.getMessage(
203                            "ClassValidator.class.create",
204                            className),
205                            ive.getMessage());
206        }
207    }
208
209    public void testCreateProtectedInstance() {
210        final String className = "org.apache.commons.cli2.validation.protect.ProtectedClass";
211        final Object[] array = new Object[] { className };
212        final List list = Arrays.asList(array);
213
214        validator.setInstance(true);
215
216        try {
217            validator.validate(list);
218            fail("It's not possible to create a '" + className + "'");
219        }
220        catch (final InvalidArgumentException ive) {
221            assertEquals(
222                    resources.getMessage(
223                            "ClassValidator.class.access",
224                            className,
225                            "Class org.apache.commons.cli2.validation.ClassValidator " +
226                            "can not access a member of class " +
227                            "org.apache.commons.cli2.validation.protect.ProtectedClass " +
228                            "with modifiers \"protected\""),
229                            ive.getMessage());
230        }
231    }
232
233    public void testClassloader() {
234        assertEquals(
235            "Wrong classloader found",
236            validator.getClass().getClassLoader(),
237            validator.getClassLoader());
238
239        URLClassLoader classloader = new URLClassLoader(new URL[] {
240        });
241        validator.setClassLoader(classloader);
242
243        assertEquals(
244            "Wrong classloader found",
245            classloader,
246            validator.getClassLoader());
247    }
248}