View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.cli2.validation;
18  
19  import java.net.URL;
20  import java.net.URLClassLoader;
21  import java.util.Arrays;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.commons.cli2.resource.ResourceHelper;
28  
29  public class ClassValidatorTest extends TestCase {
30  
31      private final static ResourceHelper resources =
32          ResourceHelper.getResourceHelper();
33  
34      private ClassValidator validator;
35  
36      protected void setUp() {
37          validator = new ClassValidator();
38      }
39  
40      public void testValidName() throws InvalidArgumentException {
41          final Object[] array = new Object[] { "MyApp", "org.apache.ant.Main" };
42          final List list = Arrays.asList(array);
43  
44          validator.validate(list);
45  
46          assertEquals("Name is incorrect", "MyApp", list.get(0));
47          assertEquals("Name is incorrect", "org.apache.ant.Main", list.get(1));
48      }
49  
50      public void testNameBadStart() {
51          final String className = "1stClass";
52          final Object[] array = new Object[] { className };
53          final List list = Arrays.asList(array);
54  
55          try {
56              validator.validate(list);
57              fail("Class name cannot start with a number.");
58          } catch (InvalidArgumentException ive) {
59              assertEquals(
60                  resources.getMessage(
61                      "ClassValidator.bad.classname",
62                      className),
63                  ive.getMessage());
64          }
65      }
66  
67      public void testNameBadEnd() {
68          final String className = "My.Class.";
69  
70          final Object[] array = new Object[] { className };
71          final List list = Arrays.asList(array);
72  
73          try {
74              validator.validate(list);
75              fail("Trailing period not permitted.");
76          } catch (InvalidArgumentException ive) {
77              assertEquals(
78                  resources.getMessage(
79                      "ClassValidator.bad.classname",
80                      className),
81                  ive.getMessage());
82          }
83      }
84  
85      public void testNameBadMiddle() {
86          final String className = "My..Class";
87  
88          final Object[] array = new Object[] { className };
89          final List list = Arrays.asList(array);
90  
91          try {
92              validator.validate(list);
93              fail("Two consecutive periods is not permitted.");
94          } catch (InvalidArgumentException ive) {
95              assertEquals(
96                  resources.getMessage(
97                      "ClassValidator.bad.classname",
98                      className),
99                  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 }