KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > JvmOptionsTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * $Id: JvmOptionsTest.java,v 1.3 2005/12/25 03:43:13 tcfujii Exp $
26  */

27
28 package com.sun.enterprise.admin.mbeans;
29
30 import java.util.*;
31
32 //junit imports
33
import junit.framework.*;
34 import junit.textui.TestRunner;
35
36 /**
37  * Depends on
38  * junit.jar, admin/mbeans.jar, admin-core/util.jar, appserv-commons.jar
39  */

40 public class JvmOptionsTest extends TestCase
41 {
42     public void testCreateNull() throws InvalidJvmOptionException
43     {
44         try
45         {
46             JvmOptions options = new JvmOptions(null);
47             Assert.assertTrue(false);
48         }
49         catch (IllegalArgumentException JavaDoc iae)
50         {
51             //ok
52
}
53     }
54
55     public void testZeroLength() throws InvalidJvmOptionException
56     {
57         JvmOptions options = new JvmOptions(new String JavaDoc[0]);
58         Assert.assertTrue(Arrays.equals(new String JavaDoc[0], options.getJvmOptions()));
59     }
60
61     public void testCreateJvmOptions() throws InvalidJvmOptionException
62     {
63         String JavaDoc[] sa = new String JavaDoc[] {"-x=y", "-a=b"};
64         JvmOptions options = new JvmOptions(sa);
65         Assert.assertTrue(Arrays.equals(sa, options.getJvmOptions()));
66
67         sa = new String JavaDoc[] {"-x=y -a=b", "-a=b"};
68         options = new JvmOptions(sa);
69         Assert.assertTrue(Arrays.equals(sa, options.getJvmOptions()));
70
71         sa = new String JavaDoc[] {"-x=y -x=y"};
72         options = new JvmOptions(sa);
73         Assert.assertTrue(Arrays.equals(
74             new String JavaDoc[] {"-x=y"}, options.getJvmOptions()));
75     }
76
77     public void testCreateOptionNull() throws InvalidJvmOptionException
78     {
79         String JavaDoc[] sa = new String JavaDoc[] {"-x=y", null};
80         try
81         {
82             JvmOptions options = new JvmOptions(sa);
83             Assert.assertTrue(false);
84         }
85         catch (IllegalArgumentException JavaDoc iae)
86         {
87             //ok
88
}
89     }
90
91     public void testAddJvmOptionsNull() throws InvalidJvmOptionException
92     {
93         String JavaDoc[] sa = new String JavaDoc[] {"-x=y", "-a=b"};
94         JvmOptions options = new JvmOptions(sa);
95         try
96         {
97             options.addJvmOptions(null);
98             Assert.assertTrue(false);
99         }
100         catch (IllegalArgumentException JavaDoc iae)
101         {
102             Assert.assertTrue(Arrays.equals(sa, options.getJvmOptions()));
103         }
104     }
105
106     public void testAddNullJvmOption() throws InvalidJvmOptionException
107     {
108         String JavaDoc[] sa = new String JavaDoc[] {"-x=y", "-a=b"};
109         JvmOptions options = new JvmOptions(sa);
110         try
111         {
112             options.addJvmOptions(new String JavaDoc[] {null});
113             Assert.assertTrue(false);
114         }
115         catch (IllegalArgumentException JavaDoc iae)
116         {
117             Assert.assertTrue(Arrays.equals(sa, options.getJvmOptions()));
118         }
119     }
120
121     public void testAddJvmOptionsZeroLength() throws InvalidJvmOptionException
122     {
123         String JavaDoc[] sa = new String JavaDoc[] {"-x=y", "-a=b"};
124         JvmOptions options = new JvmOptions(sa);
125         options.addJvmOptions(new String JavaDoc[0]);
126         Assert.assertTrue(Arrays.equals(sa, options.getJvmOptions()));
127     }
128
129     public void testAddJvmOptions() throws InvalidJvmOptionException
130     {
131         String JavaDoc[] sa = new String JavaDoc[] {"-x=y", "-a=b"};
132         JvmOptions options = new JvmOptions(sa);
133         String JavaDoc[] invalid = options.addJvmOptions(new String JavaDoc[] {"-c=d"});
134         Assert.assertTrue(invalid.length == 0);
135         Assert.assertTrue(Arrays.equals(new String JavaDoc[] {"-x=y", "-a=b", "-c=d"},
136             options.getJvmOptions()));
137     }
138     
139     public void testAddDuplicateJvmOptions0() throws InvalidJvmOptionException
140     {
141         String JavaDoc[] sa = new String JavaDoc[] {"-x=y"};
142         JvmOptions options = new JvmOptions(sa);
143         String JavaDoc[] invalid = options.addJvmOptions(new String JavaDoc[] {"-x=y"});
144         Assert.assertTrue(Arrays.equals(new String JavaDoc[] {"-x=y"}, invalid));
145         Assert.assertTrue(Arrays.equals(new String JavaDoc[] {"-x=y"},
146             options.getJvmOptions()));
147     }
148
149     public void testAddDuplicateJvmOptions1() throws InvalidJvmOptionException
150     {
151         String JavaDoc[] sa = new String JavaDoc[] {"-x=y", "-a=b"};
152         JvmOptions options = new JvmOptions(sa);
153         String JavaDoc[] invalid = options.addJvmOptions(new String JavaDoc[] {"-a=b"});
154         Assert.assertTrue(Arrays.equals(new String JavaDoc[] {"-a=b"}, invalid));
155         Assert.assertTrue(Arrays.equals(new String JavaDoc[] {"-x=y", "-a=b"},
156             options.getJvmOptions()));
157     }
158
159     public void testAddDuplicateJvmOptions2() throws InvalidJvmOptionException
160     {
161         String JavaDoc[] sa = new String JavaDoc[] {"-x=y -a=b"};
162         JvmOptions options = new JvmOptions(sa);
163         String JavaDoc[] invalid = options.addJvmOptions(new String JavaDoc[] {"-a=b"});
164         Assert.assertTrue(Arrays.equals(new String JavaDoc[] {"-a=b"}, invalid));
165         Assert.assertTrue(Arrays.equals(new String JavaDoc[] {"-x=y -a=b"},
166             options.getJvmOptions()));
167     }
168
169     public void testAddDuplicateJvmOptions3() throws InvalidJvmOptionException
170     {
171         String JavaDoc[] sa = new String JavaDoc[] {"-x=y -a=b"};
172         JvmOptions options = new JvmOptions(sa);
173         String JavaDoc[] invalid = options.addJvmOptions(new String JavaDoc[] {"-a=b", "-c=d"});
174         Assert.assertTrue(Arrays.equals(new String JavaDoc[] {"-a=b"}, invalid));
175         Assert.assertTrue(Arrays.equals(new String JavaDoc[] {"-x=y -a=b", "-c=d"},
176             options.getJvmOptions()));
177     }
178
179     public void testAddDuplicateJvmOptions4() throws InvalidJvmOptionException
180     {
181         String JavaDoc[] sa = new String JavaDoc[] {"-x=y -a=b"};
182         JvmOptions options = new JvmOptions(sa);
183         String JavaDoc[] invalid = options.addJvmOptions(new String JavaDoc[] {"-a=b -c=d"});
184         Assert.assertTrue(invalid.length == 0);
185         Assert.assertTrue(Arrays.equals(new String JavaDoc[] {"-x=y -a=b", "-a=b -c=d"},
186             options.getJvmOptions()));
187     }
188
189     public void testDeleteJvmOptionsNull() throws InvalidJvmOptionException
190     {
191         String JavaDoc[] sa = new String JavaDoc[] {"-x=y"};
192         JvmOptions options = new JvmOptions(sa);
193         try
194         {
195             options.deleteJvmOptions(null);
196         }
197         catch (IllegalArgumentException JavaDoc iae)
198         {
199             Assert.assertTrue(Arrays.equals(sa, options.getJvmOptions()));
200         }
201     }
202
203     public void testDeleteNullJvmOption() throws InvalidJvmOptionException
204     {
205         String JavaDoc[] sa = new String JavaDoc[] {"-x=y"};
206         JvmOptions options = new JvmOptions(sa);
207         try
208         {
209             options.deleteJvmOptions(new String JavaDoc[]{null});
210         }
211         catch (IllegalArgumentException JavaDoc iae)
212         {
213             Assert.assertTrue(Arrays.equals(sa, options.getJvmOptions()));
214         }
215     }
216
217     public void testDeleteJvmOptionsZeroLength() throws InvalidJvmOptionException
218     {
219         String JavaDoc[] sa = new String JavaDoc[] {"-x=y", "-a=b"};
220         JvmOptions options = new JvmOptions(sa);
221         options.deleteJvmOptions(new String JavaDoc[0]);
222         Assert.assertTrue(Arrays.equals(sa, options.getJvmOptions()));
223     }
224
225     public void testDeleteJvmOptions() throws InvalidJvmOptionException
226     {
227         String JavaDoc[] sa = new String JavaDoc[] {"-x=y"};
228         JvmOptions options = new JvmOptions(sa);
229         String JavaDoc[] invalid = options.deleteJvmOptions(new String JavaDoc[]{"-x=y"});
230         Assert.assertTrue(invalid.length == 0);
231         Assert.assertTrue(Arrays.equals(
232             new String JavaDoc[0], options.getJvmOptions()));
233     }
234
235     public void testDeleteJvmOptions1() throws InvalidJvmOptionException
236     {
237         String JavaDoc[] sa = new String JavaDoc[] {"-x=y", "-a=b"};
238         JvmOptions options = new JvmOptions(sa);
239         String JavaDoc[] invalid = options.deleteJvmOptions(new String JavaDoc[]{"-x=y"});
240         Assert.assertTrue(invalid.length == 0);
241         Assert.assertTrue(Arrays.equals(
242             new String JavaDoc[]{"-a=b"}, options.getJvmOptions()));
243     }
244
245     public void testDeleteJvmOptions2() throws InvalidJvmOptionException
246     {
247         String JavaDoc[] sa = new String JavaDoc[] {"-x=y -a=b"};
248         JvmOptions options = new JvmOptions(sa);
249         String JavaDoc[] invalid = options.deleteJvmOptions(new String JavaDoc[]{"-x=y"});
250         Assert.assertTrue(invalid.length == 0);
251         Assert.assertTrue(Arrays.equals(
252             new String JavaDoc[]{"-a=b"}, options.getJvmOptions()));
253     }
254
255     public void testDeleteJvmOptions3() throws InvalidJvmOptionException
256     {
257         String JavaDoc[] sa = new String JavaDoc[] {"-x=y -a=b -c=d"};
258         JvmOptions options = new JvmOptions(sa);
259         String JavaDoc[] invalid = options.deleteJvmOptions(new String JavaDoc[]{"-x=y", "-c=d"});
260         Assert.assertTrue(invalid.length == 0);
261         Assert.assertTrue(Arrays.equals(
262             new String JavaDoc[]{"-a=b"}, options.getJvmOptions()));
263     }
264
265     public void testDeleteJvmOptions4() throws InvalidJvmOptionException
266     {
267         String JavaDoc[] sa = new String JavaDoc[] {"-x=y -a=b", "-a=b -c=d"};
268         JvmOptions options = new JvmOptions(sa);
269         String JavaDoc[] invalid = options.deleteJvmOptions(new String JavaDoc[]{"-x=y", "-c=d"});
270         Assert.assertTrue(invalid.length == 0);
271         Assert.assertTrue(Arrays.equals(
272             new String JavaDoc[]{"-a=b"}, options.getJvmOptions()));
273     }
274
275     public void testDeleteJvmOptions5() throws InvalidJvmOptionException
276     {
277         String JavaDoc[] sa = new String JavaDoc[] {"-x=y"};
278         JvmOptions options = new JvmOptions(sa);
279         String JavaDoc[] invalid = options.deleteJvmOptions(new String JavaDoc[]{"-c=d"});
280         Assert.assertTrue(Arrays.equals(invalid, new String JavaDoc[]{"-c=d"}));
281         Assert.assertTrue(Arrays.equals(
282             new String JavaDoc[]{"-x=y"}, options.getJvmOptions()));
283     }
284
285     public void testDeleteJvmOptions6() throws InvalidJvmOptionException
286     {
287         String JavaDoc[] sa = new String JavaDoc[] {"-x=y", "-a=b"};
288         JvmOptions options = new JvmOptions(sa);
289         String JavaDoc[] invalid = options.deleteJvmOptions(new String JavaDoc[]{"-a=b", "-c=d"});
290         Assert.assertTrue(Arrays.equals(invalid, new String JavaDoc[]{"-c=d"}));
291         Assert.assertTrue(Arrays.equals(
292             new String JavaDoc[]{"-x=y"}, options.getJvmOptions()));
293     }
294
295     public void testDeleteJvmOptions7() throws InvalidJvmOptionException
296     {
297         String JavaDoc[] sa = new String JavaDoc[] {"-x=y "};
298         JvmOptions options = new JvmOptions(sa);
299         String JavaDoc[] invalid = options.deleteJvmOptions(new String JavaDoc[]{"-a=b"});
300         Assert.assertTrue(Arrays.equals(invalid, new String JavaDoc[]{"-a=b"}));
301         Assert.assertTrue(Arrays.equals(
302             new String JavaDoc[]{"-x=y"}, options.getJvmOptions()));
303     }
304
305     public void testJvmOptions() throws InvalidJvmOptionException
306     {
307         String JavaDoc[] sa = new String JavaDoc[] {"-x=y"};
308         JvmOptions options = new JvmOptions(sa);
309
310         options.addJvmOptions(new String JavaDoc[]{"-a=b", "-c=d"});
311         Assert.assertTrue(Arrays.equals(
312             new String JavaDoc[]{"-x=y", "-a=b", "-c=d"}, options.getJvmOptions()));
313
314         options.addJvmOptions(new String JavaDoc[]{"-a=b -c=d"});
315         Assert.assertTrue(Arrays.equals(
316             new String JavaDoc[]{"-x=y", "-a=b", "-c=d", "-a=b -c=d"}, options.getJvmOptions()));
317         String JavaDoc[] invalid = options.deleteJvmOptions(new String JavaDoc[]{"-a=b"});
318         Assert.assertTrue(invalid.length == 0);
319     }
320
321     public void testJvmOptionsElement() throws InvalidJvmOptionException
322     {
323         JvmOptionsElement e = new JvmOptionsElement("-x=y");
324         Assert.assertFalse(e.hasNext());
325         JvmOptionsElement next = new JvmOptionsElement("-a=b");
326         e.setNext(next);
327         Assert.assertTrue(e.hasNext());
328         Assert.assertEquals(next, e.next());
329         JvmOptionsElement next1 = new JvmOptionsElement("-c=d");
330         next.setNext(next1);
331         Assert.assertEquals(next1, e.next().next());
332         JvmOptionsElement next2 = new JvmOptionsElement("-a=b -c=d");
333         next1.setNext(next2);
334
335         boolean b = e.deleteJvmOption("-a=b");
336         Assert.assertTrue(b);
337     }
338
339     public void testCreateInValidJvmOptions()
340     {
341         try
342         {
343             JvmOptions options = new JvmOptions(new String JavaDoc[] {"option_with_no_dash"});
344         }
345         catch (InvalidJvmOptionException e)
346         {
347             //ok
348
}
349     }
350
351     public void testAddInValidJvmOptions() throws InvalidJvmOptionException
352     {
353         JvmOptions options = new JvmOptions(new String JavaDoc[] {"-option_with_dash"});
354         try
355         {
356             options.addJvmOptions(new String JavaDoc[] {"option_with_no_dash"});
357         }
358         catch (InvalidJvmOptionException e)
359         {
360             //ok
361
}
362     }
363
364     public void testDeleteInValidJvmOptions() throws InvalidJvmOptionException
365     {
366         JvmOptions options = new JvmOptions(new String JavaDoc[] {"-option_with_dash"});
367         options.deleteJvmOptions(new String JavaDoc[] {"option_with_no_dash"});
368         Assert.assertTrue(Arrays.equals(
369             new String JavaDoc[] {"-option_with_dash"}, options.getJvmOptions()));
370     }
371
372     public void testQuotedOptions() throws InvalidJvmOptionException
373     {
374         String JavaDoc[] options = new String JavaDoc[] {"-Dfile=\"a b\""};
375         JvmOptions jvmOptions = new JvmOptions(options);
376         Assert.assertTrue(Arrays.equals(options, jvmOptions.getJvmOptions()));
377         jvmOptions.deleteJvmOptions(options);
378
379         try {
380             new JvmOptions(new String JavaDoc[] {"-Dfile=\""});
381             Assert.assertTrue(false);
382         } catch (InvalidJvmOptionException e) {}
383
384         try {
385             new JvmOptions(new String JavaDoc[] {"-Dx=\"a\" -Dy=\"b"});
386             Assert.assertTrue(false);
387         } catch (InvalidJvmOptionException e) {}
388
389         try {
390             new JvmOptions(new String JavaDoc[] {"-Dx=\"a\" -Dy=\"b"});
391             Assert.assertTrue(false);
392         } catch (InvalidJvmOptionException e) {}
393     }
394
395     public void testAppServerJvmOptions()
396     {
397         String JavaDoc[] options = new String JavaDoc[] {
398             "-client",
399             "-Djava.endorsed.dirs=${com.sun.aas.installRoot}/lib/endorsed",
400             "-Djava.security.policy=${com.sun.aas.instanceRoot}/config/server.policy",
401             "-Djava.security.auth.login.config=${com.sun.aas.instanceRoot}/config/login.conf",
402             "-Dsun.rmi.dgc.server.gcInterval=3600000",
403             "-Dcom.sun.web.console.appbase=/${com.sun.aas.installRoot}/lib/install/applications/com_sun_web_ui",
404             "-Xmx512m",
405             "-Djavax.net.ssl.keyStore=${com.sun.aas.instanceRoot}/config/keystore.jks",
406             "-Djavax.net.ssl.trustStore=${com.sun.aas.instanceRoot}/config/cacerts.jks",
407             "-Djava.ext.dirs=${com.sun.aas.javaRoot}/jre/lib/ext${path.separator}${com.sun.aas.instanceRoot}/lib/ext",
408             "-Djdbc.drivers=com.pointbase.jdbc.jdbcUniversalDriver",
409             "-Djavax.xml.transform.TransformerFactory=org.apache.xalan.xsltc.trax.TransformerFactoryImpl"
410         };
411         try
412         {
413             JvmOptions jvmOptions = new JvmOptions(options);
414             Assert.assertTrue(Arrays.equals(options, jvmOptions.getJvmOptions()));
415         }
416         catch (InvalidJvmOptionException e)
417         {
418             Assert.assertTrue(false);
419         }
420     }
421
422     private void printOptions(JvmOptions o)
423     {
424         String JavaDoc[] options = o.getJvmOptions();
425         System.out.println("============================");
426         for (int i = 0; i < options.length; i++)
427         {
428             System.out.println("Option[" + options[i] + "]");
429         }
430         System.out.println("============================");
431     }
432
433     public JvmOptionsTest(String JavaDoc name) throws Exception JavaDoc
434     {
435         super(name);
436     }
437
438     protected void setUp()
439     {
440     }
441
442     protected void tearDown()
443     {
444     }
445
446     public static junit.framework.Test suite()
447     {
448         TestSuite suite = new TestSuite(JvmOptionsTest.class);
449         return suite;
450     }
451
452     public static void main(String JavaDoc args[]) throws Exception JavaDoc
453     {
454         final TestRunner runner= new TestRunner();
455         final TestResult result = runner.doRun(JvmOptionsTest.suite(), false);
456         System.exit(result.errorCount() + result.failureCount());
457     }
458 }
Popular Tags