01: /*
02: * Copyright 2007 JBoss Inc
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: * Created on Jul 1, 2007
17: */
18: package org.drools.rule.builder.dialect.java;
19:
20: import java.util.Iterator;
21: import java.util.List;
22: import java.util.Set;
23:
24: import junit.framework.TestCase;
25:
26: import org.antlr.runtime.RecognitionException;
27:
28: /**
29: * @author etirelli
30: *
31: */
32: public class JavaExprAnalyzerTest extends TestCase {
33:
34: /* (non-Javadoc)
35: * @see junit.framework.TestCase#setUp()
36: */
37: protected void setUp() throws Exception {
38: super .setUp();
39: }
40:
41: /* (non-Javadoc)
42: * @see junit.framework.TestCase#tearDown()
43: */
44: protected void tearDown() throws Exception {
45: super .tearDown();
46: }
47:
48: /**
49: * Test method for {@link org.drools.rule.builder.dialect.java.JavaExprAnalyzer#analyzeBlock(java.lang.String, java.util.Set[])}.
50: */
51: public void testAnalyzeBlock() {
52: JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
53: String codeBlock = "int x;\n"
54: + "Cheese cheese = new Cheese();\n"
55: + "for( Iterator it = list.iterator(); it.hasNext(); ) {\n"
56: + " int shouldNotBeIncluded = 1;\n" + "}\n" + "{\n"
57: + " String anotherNonTopLevelVar = \"test\";\n"
58: + "}\n" + "double thisIsAGoodVar = 0;\n"
59: + "method();\n";
60: try {
61: JavaAnalysisResult analysis = analyzer.analyzeBlock(
62: codeBlock, new Set[0]);
63: List vars = analysis.getLocalVariables();
64:
65: assertEquals(3, vars.size());
66: assertTrue(vars.contains("x"));
67: assertTrue(vars.contains("cheese"));
68: assertTrue(vars.contains("thisIsAGoodVar"));
69:
70: } catch (RecognitionException e) {
71: e.printStackTrace();
72: fail("Not supposed to raise exception: " + e.getMessage());
73: }
74: }
75:
76: }
|