1 package org.drools.semantics.java;
2
3 import org.drools.rule.Declaration;
4
5 import junit.framework.TestCase;
6
7 public class ExprAnalyzerTest extends TestCase
8 {
9 private Declaration a;
10 private Declaration b;
11 private Declaration c;
12 private Declaration d;
13
14 private Declaration[] allDecls;
15
16 private ExprAnalyzer analyzer;
17
18 public ExprAnalyzerTest(String name)
19 {
20 super( name );
21 }
22
23 public void setUp()
24 {
25 this.allDecls = new Declaration[4];
26
27 this.a = new Declaration( new ClassObjectType( java.lang.String.class ),
28 "a" );
29
30 this.allDecls[0] = this.a;
31
32 this.b = new Declaration( new ClassObjectType( java.lang.String.class ),
33 "b" );
34
35 this.allDecls[1] = this.b;
36
37 this.c = new Declaration( new ClassObjectType( java.lang.String.class ),
38 "c" );
39
40 this.allDecls[2] = this.c;
41
42 this.d = new Declaration( new ClassObjectType( java.lang.String.class ),
43 "d" );
44
45 this.allDecls[3] = this.d;
46
47 this.analyzer = new ExprAnalyzer();
48 }
49
50 public void tearDown()
51 {
52 this.a = null;
53 this.b = null;
54 this.c = null;
55 this.d = null;
56
57 this.analyzer = null;
58 }
59
60 public void testAnalyze_Exact() throws Exception
61 {
62 Declaration[] decls = this.analyzer.analyze( "a + b + c + d",
63 this.allDecls );
64
65 assertEquals( 4,
66 decls.length );
67
68 assertContains( decls,
69 this.a );
70
71 assertContains( decls,
72 this.b );
73
74 assertContains( decls,
75 this.c );
76
77 assertContains( decls,
78 this.d );
79 }
80
81 public void testAnalyze_Subset() throws Exception
82 {
83 Declaration[] decls = this.analyzer.analyze( "a + b + d",
84 this.allDecls );
85
86 assertEquals( 3,
87 decls.length );
88
89 assertContains( decls,
90 this.a );
91
92 assertContains( decls,
93 this.b );
94
95 assertContains( decls,
96 this.d );
97 }
98
99 public void testAnalyze_Superset() throws Exception
100 {
101 try
102 {
103 this.analyzer.analyze( "a + b + c + d + e",
104 this.allDecls );
105
106 // fail( "Should have thrown MissingDeclarationException" );
107 }
108 catch (MissingDeclarationException e)
109 {
110 // expected and correct
111
112 assertEquals( "e",
113 e.getIdentifier() );
114 }
115 }
116
117 protected void assertContains(Declaration[] decls,
118 Declaration decl)
119 {
120 for ( int i = 0 ; i < decls.length ; ++i )
121 {
122 if ( decls[i].equals( decl ) )
123 {
124 return;
125 }
126 }
127
128 fail( "Declarations does not contain " + decl );
129 }
130 }
This page was automatically generated by Maven