1 /*****************************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one                *
3  * or more contributor license agreements.  See the NOTICE file              *
4  * distributed with this work for additional information                     *
5  * regarding copyright ownership.  The ASF licenses this file                *
6  * to you under the Apache License, Version 2.0 (the                         *
7  * "License"); you may not use this file except in compliance                *
8  * with the License.  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,                *
13  * software distributed under the License is distributed on an               *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY                    *
15  * KIND, either express or implied.  See the License for the                 *
16  * specific language governing permissions and limitations                   *
17  * under the License.                                                        *
18  *                                                                           *
19  * This file is part of the BeanShell Java Scripting distribution.           *
20  * Documentation and updates may be found at http://www.beanshell.org/       *
21  * Patrick Niemeyer (pat@pat.net)                                            *
22  * Author of Learning Java, O'Reilly & Associates                            *
23  *                                                                           *
24  *****************************************************************************/
25 
26 package bsh;
27 
28 import org.junit.Assert;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 import java.net.URL;
33 import java.util.Collections;
34 import java.util.concurrent.TimeUnit;
35 import java.util.concurrent.atomic.AtomicInteger;
36 
37 import static org.junit.Assert.assertEquals;
38 
39 @RunWith(FilteredTestRunner.class)
40 public class PreparsedScriptTest {
41 
42 	private ClassLoader _classLoader = new ClassLoader() {
43 		@Override
44 		protected Class<?> findClass(String name) throws ClassNotFoundException {
45 			System.out.println("find class " + name);
46 			// Thread.dumpStack();
47 			return super.findClass(name);
48 		}
49 
50 
51 		@Override
52 		protected URL findResource(String name) {
53 			System.out.println("find resource " + name);
54 			return super.findResource(name);
55 		}
56 	};
57 
58 
59 	@Test
x()60 	public void x() throws Exception {
61 		final PreparsedScript preparsedScript = new PreparsedScript("", _classLoader);
62 		preparsedScript.invoke(Collections.<String, Object>emptyMap());
63 	}
64 
65 
66 	@Test
y()67 	public void y() throws Exception {
68 		final PreparsedScript f = new PreparsedScript("return x;", _classLoader);
69 		assertEquals("hurz", f.invoke(Collections.singletonMap("x", "hurz")));
70 		assertEquals("foo", f.invoke(Collections.singletonMap("x", "foo")));
71 	}
72 
73 
74 	@Test
z()75 	public void z() throws Exception {
76 		final PreparsedScript f = new PreparsedScript(
77 				"import javax.crypto.*;" +
78 				"import javax.crypto.interfaces.*;" +
79 				"import javax.crypto.spec.*;" +
80 				"if (foo != void) print (\"check\");" +
81 				"class Echo {\n" +
82 				"\n" +
83 				"   public Object echo() {\n" +
84 				"      return param;\n" +
85 				"   }\n" +
86 				"\n" +
87 				"}\n" +
88 				"\n" +
89 				"return new Echo().echo();",
90 				_classLoader
91 		);
92 		assertEquals("bla", f.invoke(Collections.singletonMap("param", "bla")));
93 		System.out.println("second call");
94 		assertEquals("blubb", f.invoke(Collections.singletonMap("param", "blubb")));
95 	}
96 
97 
98 	@Test
multi_threaded()99 	public void multi_threaded() throws Exception {
100 		final AtomicInteger counter = new AtomicInteger();
101 		final String script = "return v;";
102 		final PreparsedScript f = new PreparsedScript(script, _classLoader);
103 		Assert.assertEquals("x", f.invoke(Collections.singletonMap("v", "x")));
104 		final Runnable runnable = new Runnable() {
105 			public void run() {
106 				final int value = counter.incrementAndGet();
107 				try {
108 					Assert.assertEquals(value, f.invoke(Collections.singletonMap("v", value)));
109 				} catch (final EvalError evalError) {
110 					throw new RuntimeException(evalError);
111 				}
112 			}
113 		};
114 		final long time = TestUtil.measureConcurrentTime(runnable, 30, 30, 1000);
115 		System.out.println(TimeUnit.NANOSECONDS.toMillis(time));
116 	}
117 
118 
119 	@Test
param_with_name_result()120 	public void param_with_name_result() throws Exception {
121 		final AtomicInteger result = new AtomicInteger();
122 		final PreparsedScript f = new PreparsedScript(
123 				"result.set(result.get() + 42);",
124 				_classLoader);
125 		f.invoke(Collections.singletonMap("result", result));
126 		Assert.assertEquals(42, result.get());
127 		f.invoke(Collections.singletonMap("result", result));
128 		Assert.assertEquals(84, result.get());
129 	}
130 }
131