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.Test;
29 import org.junit.runner.RunWith;
30 
31 import javax.script.ScriptEngine;
32 import javax.script.ScriptEngineManager;
33 
34 import static bsh.TestUtil.eval;
35 import static org.junit.Assert.assertEquals;
36 import static org.junit.Assert.assertNotNull;
37 
38 @RunWith(FilteredTestRunner.class)
39 public class GoogleReports {
40 
41 	@Test
42 	@SuppressWarnings({"ConstantIfStatement"})
while_loop()43     public void while_loop() throws Exception {
44 		int loopCount = 0;
45 		do {
46 			loopCount++;
47             if (loopCount > 100) {
48                 break;
49             }
50             if (true) {
51                 continue;
52             }
53 		} while (false);
54 		assertEquals(1, loopCount);
55         loopCount = (Integer) eval("int loopCount = 0;", "do{", "	loopCount++;", "	if (loopCount > 100) break;", "	if (true) continue;", "} while (false);", "return loopCount");
56 		assertEquals(1, loopCount);
57         loopCount = (Integer) eval("int loopCount = 0;", "while (loopCount < 1) {", "	loopCount++;", "	if (loopCount > 100) return loopCount;", "	if (true) continue;", "}", "return loopCount");
58 		assertEquals(1, loopCount);
59 		assertEquals(Boolean.TRUE, eval("while(true) { break; return false; } return true;"));
60 		assertEquals(Boolean.TRUE, eval("do { break; return false; } while(true); return true;"));
61         loopCount = (Integer) eval("int loopCount = 0;", "while (++loopCount < 2);", "return loopCount");
62 		assertEquals(2, loopCount);
63         loopCount = (Integer) eval("int loopCount = 0;", "do { } while (++loopCount < 2);", "return loopCount");
64 		assertEquals(2, loopCount);
65 	}
66 
67 
68 	/**
69      * <a href="http://code.google.com/p/beanshell2/issues/detail?id=6">issue#60</a>
70      */
71     @Test
accessibility_issue_a()72     public void accessibility_issue_a() throws Exception {
73         final Interpreter interpreter = new Interpreter();
74         interpreter.set("x", this);
75         Capabilities.setAccessibility(true);
76         assertEquals("private-Integer", interpreter.eval("return x.issue6(new Integer(9));"));
77         Capabilities.setAccessibility(false);
78         assertEquals("public-Number", interpreter.eval("return x.issue6(new Integer(9));"));
79     }
80 
81 
82     /**
83      * <a href="http://code.google.com/p/beanshell2/issues/detail?id=6">issue#60</a>
84      */
85     @Test
accessibility_issue_b()86     public void accessibility_issue_b() throws Exception {
87         final Interpreter interpreter = new Interpreter();
88         interpreter.set("x", this);
89         assertEquals("public-Number", interpreter.eval("return x.issue6(new Integer(9));"));
90         Capabilities.setAccessibility(true);
91         assertEquals("private-Integer", interpreter.eval("return x.issue6(new Integer(9));"));
92     }
93 
94 
95     /**
96      * <a href="http://code.google.com/p/beanshell2/issues/detail?id=10">issue#10</a>
97      */
98     @Test(expected = ParseException.class)
parse_error()99     public void parse_error() throws Exception {
100         eval("\1;");
101     }
102 
103 
104     /**
105      * <a href="http://code.google.com/p/beanshell2/issues/detail?id=11">issue#11</a>
106      */
107     @Test
return_in_try_block_does_not_return()108     public void return_in_try_block_does_not_return() throws Exception {
109         assertEquals(
110                 "in try block",
111                 eval(
112                     /*1*/ "try {",
113                     /*2*/ "   return \"in try block\";",
114                     /*3*/ "} finally {}" +
115                     /*4*/ "return \"after try block\";"));
116     }
117 
118 
119     /**
120      * <a href="http://code.google.com/p/beanshell2/issues/detail?id=12">issue#12</a>
121 	 */
122 	@Test
override_method()123     public void override_method() throws Exception {
124         assertEquals(
125                 "changed",
126                 eval(
127                     /*1*/ "foo() { return \"original\";}",
128                     /*2*/ "foo() { return \"changed\";}",
129                     /*3*/ "return foo();"));
130 
131     }
132 
133 
134     /**
135      * <a href="http://code.google.com/p/beanshell2/issues/detail?id=32">issue#32</a>
136      */
137     @Test
binary_operator_or()138     public void binary_operator_or() throws Exception {
139         assertEquals(true, eval("return true | true"));
140         assertEquals(true, eval("return true | false"));
141         assertEquals(true, eval("return false | true"));
142         assertEquals(false, eval("return false | false"));
143     }
144 
145 
146     /**
147      * <a href="http://code.google.com/p/beanshell2/issues/detail?id=32">issue#32</a>
148      */
149     @Test
binary_operator_and()150     public void binary_operator_and() throws Exception {
151         assertEquals(true, eval("return true & true"));
152         assertEquals(false, eval("return true & false"));
153         assertEquals(false, eval("return false & true"));
154         assertEquals(false, eval("return false & false"));
155     }
156 
157 
158 
159     /**
160      * <a href="http://code.google.com/p/beanshell2/issues/detail?id=32">issue#32</a>
161      */
162     @Test
binary_operator_xor()163     public void binary_operator_xor() throws Exception {
164         assertEquals(false, eval("return true ^ true"));
165         assertEquals(true, eval("return true ^ false"));
166         assertEquals(true, eval("return false ^ true"));
167         assertEquals(false, eval("return false ^ false"));
168     }
169 
170 
171     /*
172     * helpers
173     */
issue6(Integer ignored)174     private static String issue6(Integer ignored) {
175         return "private-Integer";
176     }
177 
178 
issue6(Number ignored)179     public static String issue6(Number ignored) {
180         return "public-Number";
181 	}
182 
183 }
184