View Javadoc
1   package org.codehaus.plexus.classworlds.launcher;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  
22  import org.codehaus.plexus.classworlds.AbstractClassWorldsTestCase;
23  import org.codehaus.plexus.classworlds.TestUtil;
24  
25  public class LauncherTest
26      extends AbstractClassWorldsTestCase
27  {
28      private Launcher launcher;
29  
30      public LauncherTest( String name )
31      {
32          super( name );
33      }
34  
35      public void setUp()
36      {
37          System.setProperty( "java.protocol.handler.pkgs", "org.codehaus.classworlds.protocol" );
38  
39          this.launcher = new Launcher();
40  
41          this.launcher.setSystemClassLoader( Thread.currentThread().getContextClassLoader() );
42      }
43  
44      public void tearDown()
45      {
46          this.launcher = null;
47      }
48  
49      public void testConfigure_Valid()
50          throws Exception
51      {
52          launcher.configure( getConfigPath( "valid-launch.conf" ) );
53  
54          Class<?> mainClass = launcher.getMainClass();
55  
56          assertNotNull( mainClass );
57  
58          assertEquals( "a.A", mainClass.getName() );
59  
60          assertEquals( "app", launcher.getMainRealm().getId() );
61      }
62  
63      public void testLaunch_ValidStandard()
64          throws Exception
65      {
66          launcher.configure( getConfigPath( "valid-launch.conf" ) );
67  
68          launcher.launch( new String[]{} );
69      }
70  
71      public void testLaunch_ValidStandardExitCode()
72          throws Exception
73      {
74          launcher.configure( getConfigPath( "valid-launch-exitCode.conf" ) );
75  
76          launcher.launch( new String[]{} );
77  
78          assertEquals( "check exit code", 15, launcher.getExitCode() );
79      }
80  
81      public void testLaunch_ValidEnhanced()
82          throws Exception
83      {
84          launcher.configure( getConfigPath( "valid-enh-launch.conf" ) );
85  
86          launcher.launch( new String[]{} );
87      }
88  
89      public void testLaunch_ValidEnhancedExitCode()
90          throws Exception
91      {
92          launcher.configure( getConfigPath( "valid-enh-launch-exitCode.conf" ) );
93  
94          launcher.launch( new String[]{} );
95  
96          assertEquals( "check exit code", 45, launcher.getExitCode() );
97      }
98  
99      public void testLaunch_NoSuchMethod()
100         throws Exception
101     {
102         launcher.configure( getConfigPath( "launch-nomethod.conf" ) );
103 
104         try
105         {
106             launcher.launch( new String[]{} );
107             fail( "should have thrown NoSuchMethodException" );
108         }
109         catch ( NoSuchMethodException e )
110         {
111             // expected and correct
112         }
113     }
114 
115     public void testLaunch_ClassNotFound()
116         throws Exception
117     {
118         launcher.configure( getConfigPath( "launch-noclass.conf" ) );
119 
120         try
121         {
122             launcher.launch( new String[]{} );
123             fail( "throw ClassNotFoundException" );
124         }
125         catch ( ClassNotFoundException e )
126         {
127             // expected and correct
128         }
129     }
130 
131     private FileInputStream getConfigPath( String name )
132         throws Exception
133     {
134         String basedir = TestUtil.getBasedir();
135 
136         return new FileInputStream( new File( new File( basedir, "src/test/test-data" ), name ) );
137     }
138 }