1   /*
2    * Copyright 2004-2005 Germinus XXI
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package kortsoft.kmx.bootstrap;
17  
18  import java.io.File;
19  import java.util.Properties;
20  
21  import kortsoft.kmx.AbstractTestCase;
22  import kortsoft.kmx.test.FileUtil;
23  
24  /***
25   * Test KmxLauncher main methods with both usages.
26   * <a href="KmxLauncherTest.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author  Alvaro
29   * @version $Revision: 1.1 $
30   *
31   */
32  public class KmxLauncherTest extends AbstractTestCase {
33  	//Needed only to remove them in tearDown method
34  	File testProperties;
35  	File defaultProperties;
36  	File testJarLib;
37  	
38  	/***
39  	 * @param testName
40  	 */
41  	public KmxLauncherTest() {
42  		super("KmxLauncherTest");
43  	}
44  	
45  	public void testMainWithoutParams(){
46  		try {
47  			KmxLauncher.main(new String[0]);
48  		} catch (Exception e) {
49  			fail("Not Expected Exception: "+e);
50  		}
51  		assertKmxMainLifeCycle();
52  	}
53  	
54  	public void testMainWithParam(){
55  		try {
56  			KmxLauncher.main(new String[]{"target/test-classes/kmxlauncher_test.properties"});
57  		} catch (Exception e) {
58  			fail("Not Expected Exception: "+e);
59  		}
60  		assertKmxMainLifeCycle();
61  	}
62  
63  	/***
64  	 * 
65  	 */
66  	private void assertKmxMainLifeCycle() {
67  		assertTrue("KmxMain not started",MockKmxMain.startup);
68  		try {
69  			KmxLauncher.finalizador.run();
70  			assertTrue("KmxMain not closed",MockKmxMain.closed);
71  		} catch (RuntimeException e1) {
72  			fail("Error executing finalizer: "+e1);
73  		}
74  	}
75  	
76  	
77  	/* (non-Javadoc)
78  	 * @see junit.framework.TestCase#setUp()
79  	 */
80  	protected void setUp() throws Exception {
81  		Properties prop=new Properties();
82  		prop.put(KmxLauncher.LIB_DIR_PARAM,"target/test-classes/lib");
83  		prop.put(KmxLauncher.RESOURCES_DIR_PARAM,"target/test-classes/resources");
84  		prop.put(KmxLauncher.MAIN_CLASS_PARAM,MockKmxMain.class.getName());
85  		testProperties=new File("target/test-classes/kmxlauncher_test.properties");
86  		defaultProperties=new File("target/classes"+KmxLauncher.PROP_FILE_NAME);
87  		FileUtil.write(defaultProperties,prop);
88  		FileUtil.write(testProperties,prop);
89  		FileUtil.mkdirs("target/test-classes/resources");
90  		FileUtil.mkdirs("target/test-classes/lib");
91  		testJarLib=new File("target/test-classes/lib/testJarLib.jar");
92  		Properties testProperties=new Properties();
93  		testProperties.put(MockKmxMain.TEST_PROPERTY,MockKmxMain.TEST_PROPERTY_VALUE);		
94  		FileUtil.writeAsJAR(testJarLib,MockKmxMain.TEST_PROPERTY_FILE,testProperties);
95  	}
96  	
97  	
98  	/* (non-Javadoc)
99  	 * @see junit.framework.TestCase#tearDown()
100 	 */
101 	protected void tearDown() throws Exception {
102 		testProperties.delete();
103 		defaultProperties.delete();
104 		testJarLib.delete();
105 		MockKmxMain.startup=false;
106 		MockKmxMain.closed=false;
107 	}
108 }