1 package org.drools.io;
2
3 import org.drools.rule.Declaration;
4 import org.drools.rule.Extraction;
5 import org.drools.rule.Rule;
6 import org.drools.rule.RuleSet;
7
8 import org.drools.semantics.java.ClassObjectType;
9 import org.drools.semantics.java.ExprExtractor;
10 import org.drools.semantics.java.ExprCondition;
11 import org.drools.semantics.java.BlockConsequence;
12
13 import junit.framework.TestCase;
14
15 import java.io.IOException;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.HashSet;
19 import java.util.Iterator;
20
21 public class RuleSetLoaderTest extends TestCase
22 {
23 private RuleSetLoader loader;
24
25 public RuleSetLoaderTest(String name)
26 {
27 super( name );
28 }
29
30 public void setUp()
31 {
32 this.loader = new RuleSetLoader();
33 }
34
35 public void tearDown()
36 {
37 this.loader = new RuleSetLoader();
38 }
39
40 public void testLoad_Invalid() throws Exception
41 {
42 try
43 {
44 this.loader.load( "file:///i/am/on/the/road/to/nowhere.drl" );
45 fail( "Should have thrown IOException" );
46 }
47 catch (IOException e)
48 {
49 // expected and correct
50 }
51 }
52
53 public void testLoad_ValidEmpty() throws Exception
54 {
55 List ruleSets = this.loader.load( getClass().getResource( "RuleSetLoaderTest-1.drl" ) );
56
57 assertNotNull( ruleSets );
58
59 assertTrue( ruleSets.isEmpty() );
60 }
61
62 public void testLoad_ValidNonEmpty() throws Exception
63 {
64 List ruleSets = this.loader.load( getClass().getResource( "RuleSetLoaderTest-2.drl" ) );
65
66 assertNotNull( ruleSets );
67
68 assertEquals( 3,
69 ruleSets.size() );
70
71 RuleSet ruleSet = null;
72 List rules = null;
73 Set paramDecls = null;
74 Rule rule = null;
75
76 Declaration decl = null;
77 ClassObjectType type = null;
78
79 Extraction extraction = null;
80 ExprExtractor extractor = null;
81
82 ExprCondition condition = null;
83
84 BlockConsequence consequence = null;
85
86 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
87 // ruleset.1
88 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
89
90 ruleSet = (RuleSet) ruleSets.get( 0 );
91
92 assertEquals( "ruleset.1",
93 ruleSet.getName() );
94
95 rules = ruleSet.getRules();
96
97 assertNotNull( rules );
98
99 assertEquals( 1,
100 rules.size() );
101
102 rule = (Rule) ruleSet.getRules().get( 0 );
103
104 assertNotNull( rule );
105
106 assertEquals( "rule.1.1",
107 rule.getName() );
108
109 decl = rule.getDeclaration( "str" );
110
111 assertNotNull( decl );
112
113 assertSame( decl,
114 rule.getParameterDeclaration( "str" ) );
115
116 type = (ClassObjectType) decl.getObjectType();
117
118 assertNotNull( type );
119
120 assertTrue( type.getType() == String.class );
121
122 decl = rule.getDeclaration( "maxLen" );
123
124 assertNotNull( decl );
125
126 assertSame( decl,
127 rule.getParameterDeclaration( "maxLen" ) );
128
129 type = (ClassObjectType) decl.getObjectType();
130
131 assertNotNull( type );
132
133 assertTrue( type.getType() == Integer.class );
134
135 decl = rule.getDeclaration( "strLen" );
136
137 assertNotNull( decl );
138
139 assertNull( rule.getParameterDeclaration( "strLen" ) );
140
141 type = (ClassObjectType) decl.getObjectType();
142
143 assertNotNull( type );
144
145 assertTrue( type.getType() == Integer.class );
146
147 assertEquals( 1,
148 rule.getExtractions().size() );
149
150 extraction = (Extraction) rule.getExtractions().iterator().next();
151
152 assertNotNull( extraction );
153
154 assertEquals( rule.getDeclaration( "strLen" ),
155 extraction.getTargetDeclaration() );
156
157 extractor = (ExprExtractor) extraction.getExtractor();
158
159 assertNotNull( extractor );
160
161 assertEquals( "str.length()",
162 extractor.getExpression() );
163
164 Set conditionExprs = new HashSet();
165 conditionExprs.add( "strLen > 5" );
166 conditionExprs.add( "strLen < maxLen" );
167
168 assertEquals( 2,
169 rule.getConditions().size() );
170
171 Iterator conditionIter = rule.getConditions().iterator();
172
173 condition = (ExprCondition) conditionIter.next();
174
175 assertTrue( conditionExprs.remove( condition.getExpression() ) );
176
177 condition = (ExprCondition) conditionIter.next();
178
179 assertTrue( conditionExprs.remove( condition.getExpression() ) );
180
181 assertTrue( conditionExprs.isEmpty() );
182
183 consequence = (BlockConsequence) rule.getConsequence();
184
185 assertNotNull( consequence );
186
187 assertEquals( "System.err.println( \"str: \" + str );\n"
188 + " System.err.println( \"strLen: \" + strLen );\n"
189 + " System.err.println( \"maxLen: \" + maxLen );",
190 consequence.getText() );
191
192
193 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
194 // ruleset.2
195 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
196
197 ruleSet = (RuleSet) ruleSets.get( 1 );
198
199 assertEquals( "ruleset.2",
200 ruleSet.getName() );
201
202 assertTrue( ruleSet.getRules().isEmpty() );
203 }
204 }
This page was automatically generated by Maven