View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.tests.services.debug;
18  
19  import static org.junit.Assert.assertEquals;
20  
21  import java.util.Properties;
22  
23  import org.junit.Test;
24  import org.springframework.context.ApplicationContext;
25  import org.springframework.context.support.FileSystemXmlApplicationContext;
26  
27  import ch.elca.el4j.services.debug.BshCommandLineLauncher;
28  import ch.elca.el4j.services.debug.ResultHolder;
29  import ch.elca.el4j.services.debug.ShellExecutor;
30  import ch.elca.el4j.services.debug.ShellExecutorImpl;
31  import ch.elca.el4j.util.codingsupport.annotations.FindBugsSuppressWarnings;
32  
33  /**
34   * @svnLink $Revision: 4010 $;$Date: 2009-12-01 10:59:54 +0100 (Di, 01. Dez 2009) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/bshlauncher/src/test/java/ch/elca/el4j/tests/services/debug/ShellExecutorTest.java $
35   */
36  public class ShellExecutorTest {
37  
38  	@Test
39  	public void testSimpleEval () {
40  		ShellExecutor se = new ShellExecutorImpl();
41  		
42  		se.eval("bsh.show=true;");
43  		Object result = se.eval("System.out.println(\"hello world\"); ii=10;");
44  		System.out.println("result:"+result);
45  	}
46  	
47  	@Test
48  	public void testBshCommandLauncher() {
49  		Properties bkup = System.getProperties();
50  		
51  		System.setProperty(BshCommandLineLauncher.BSH_LAUNCH_STRING, "uu=123;");
52  		ApplicationContext ac = new FileSystemXmlApplicationContext("classpath*:mandatory/debugStartBshLauncher.xml");
53  		
54  		BshCommandLineLauncher b = (BshCommandLineLauncher) ac.getBean("bshLauncher");
55  		ResultHolder result = b.getShellExecutor().eval("i=uu;"); // returns the value of "uu" (=123)
56  		assertEquals(123, ((Number) result.getReturnValue()).longValue());
57  		
58  		System.setProperties(bkup); // restore original system properties
59  	}
60  	
61  	@Test
62  	@FindBugsSuppressWarnings(value = "DLS_DEAD_LOCAL_STORE",
63  							justification = "Test method doesn't care for values that are never read")
64  	public void testBshCommandLauncher2() {
65  		Properties bkup = System.getProperties();
66  		
67  		System.setProperty(BshCommandLineLauncher.BSH_LAUNCH_STRING, "aa=2;");
68  		ApplicationContext ac = new FileSystemXmlApplicationContext ("classpath*:mandatory/debugStartBshLauncher.xml");
69  		
70  		BshCommandLineLauncher b = (BshCommandLineLauncher) ac.getBean("bshLauncher");
71  		b.addScriptletClasspath("/test_scriptlets");
72  		ResultHolder result = b.getShellExecutor().eval("debug(); el4j_test(); threadInfo();"); // increment aa
73  		result = b.getShellExecutor().eval("i=aa;"); // returns the value of "vv" (=3)
74  		assertEquals(3, ((Number)result.getReturnValue()).longValue());
75  		
76  		System.setProperties(bkup); // restore original system properties
77  	}
78  	
79  }