1 package org.drools.semantics.java;
2
3 import org.drools.smf.ConfigurationException;
4
5 import org.drools.rule.Declaration;
6
7 import junit.framework.TestCase;
8
9 public class ExprTest extends TestCase
10 {
11 private Declaration a;
12 private Declaration b;
13 private Declaration c;
14 private Declaration d;
15
16 private Declaration[] allDecls;
17
18 private Expr expr;
19
20 public ExprTest(String name)
21 {
22 super( name );
23 }
24
25 public void setUp()
26 {
27 this.allDecls = new Declaration[4];
28
29 this.a = new Declaration( new ClassObjectType( java.lang.String.class ),
30 "a" );
31
32 this.allDecls[0] = this.a;
33
34 this.b = new Declaration( new ClassObjectType( java.lang.String.class ),
35 "b" );
36
37 this.allDecls[1] = this.b;
38
39 this.c = new Declaration( new ClassObjectType( java.lang.String.class ),
40 "c" );
41
42 this.allDecls[2] = this.c;
43
44 this.d = new Declaration( new ClassObjectType( java.lang.String.class ),
45 "d" );
46
47 this.allDecls[3] = this.d;
48
49 this.expr = new Expr();
50 }
51
52 public void tearDown()
53 {
54 this.a = null;
55 this.b = null;
56 this.c = null;
57 this.d = null;
58
59 this.expr = null;
60 }
61
62 public void testConstruct_Fully() throws Exception
63 {
64 Expr expr = new Expr( "a + b + d",
65 this.allDecls );
66
67 Declaration[] decls = expr.getRequiredTupleMembers();
68
69 assertEquals( 3,
70 decls.length );
71
72 assertContains( decls,
73 this.a );
74
75 assertContains( decls,
76 this.b );
77
78 assertContains( decls,
79 this.d );
80
81 }
82
83 public void testConfigure_None() throws Exception
84 {
85 this.expr.configure( "10 + 20",
86 this.allDecls );
87
88 assertEquals( 0,
89 this.expr.getRequiredTupleMembers().length );
90 }
91
92 public void testConfigure_Exact() throws Exception
93 {
94 this.expr.configure( "a + b + c + d",
95 this.allDecls );
96
97 Declaration[] decls = this.expr.getRequiredTupleMembers();
98
99 assertEquals( 4,
100 decls.length );
101
102 assertContains( decls,
103 this.a );
104
105 assertContains( decls,
106 this.b );
107
108 assertContains( decls,
109 this.c );
110
111 assertContains( decls,
112 this.d );
113 }
114
115 public void testConfigure_Subset() throws Exception
116 {
117 this.expr.configure( "a + b + d",
118 this.allDecls );
119
120 Declaration[] decls = this.expr.getRequiredTupleMembers();
121
122 assertEquals( 3,
123 decls.length );
124
125 assertContains( decls,
126 this.a );
127
128 assertContains( decls,
129 this.b );
130
131 assertContains( decls,
132 this.d );
133 }
134
135 public void testConfigure_Superset() throws Exception
136 {
137 try
138 {
139 this.expr.configure( "a + b + c + d + e",
140 this.allDecls );
141
142 // fail( "Should have thrown MissingDeclarationException" );
143 }
144 catch (ConfigurationException e)
145 {
146 // expected and correct
147
148 MissingDeclarationException mde = (MissingDeclarationException) e.getRootCause();
149
150 assertEquals( "e",
151 mde.getIdentifier() );
152 }
153 }
154
155 protected void assertContains(Declaration[] decls,
156 Declaration decl)
157 {
158 for ( int i = 0 ; i < decls.length ; ++i )
159 {
160 if ( decls[i].equals( decl ) )
161 {
162 return;
163 }
164 }
165
166 fail( "Declarations does not contain " + decl );
167 }
168 }
This page was automatically generated by Maven