001: /*
002: * Copyright 2006 JBoss Inc
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.drools.rule.builder.dialect.java;
018:
019: import java.io.InputStreamReader;
020: import java.lang.reflect.Field;
021: import java.util.ArrayList;
022: import java.util.Calendar;
023: import java.util.HashSet;
024: import java.util.List;
025:
026: import junit.framework.Assert;
027: import junit.framework.TestCase;
028:
029: import org.drools.base.ClassTypeResolver;
030: import org.drools.base.TypeResolver;
031: import org.drools.compiler.Dialect;
032: import org.drools.compiler.DialectRegistry;
033: import org.drools.compiler.DrlParser;
034: import org.drools.compiler.PackageBuilder;
035: import org.drools.compiler.PackageBuilderConfiguration;
036: import org.drools.lang.descr.AttributeDescr;
037: import org.drools.lang.descr.PackageDescr;
038: import org.drools.lang.descr.RuleDescr;
039: import org.drools.rule.GroupElement;
040: import org.drools.rule.Package;
041: import org.drools.rule.Rule;
042: import org.drools.rule.builder.RuleBuildContext;
043: import org.drools.rule.builder.RuleBuilder;
044:
045: /**
046: * @author etirelli
047: *
048: */
049: public class RuleBuilderTest extends TestCase {
050:
051: /* (non-Javadoc)
052: * @see junit.framework.TestCase#setUp()
053: */
054: protected void setUp() throws Exception {
055: super .setUp();
056: }
057:
058: /* (non-Javadoc)
059: * @see junit.framework.TestCase#tearDown()
060: */
061: protected void tearDown() throws Exception {
062: super .tearDown();
063: }
064:
065: /**
066: * Test method for {@link org.drools.rule.builder.RuleBuilder#build(org.drools.rule.Package, org.drools.lang.descr.RuleDescr)}.
067: */
068: public void testBuild() throws Exception {
069: final DrlParser parser = new DrlParser();
070: final PackageDescr pkgDescr = parser
071: .parse(new InputStreamReader(getClass()
072: .getResourceAsStream(
073: "nestedConditionalElements.drl")));
074:
075: // just checking there is no parsing errors
076: Assert.assertFalse(parser.getErrors().toString(), parser
077: .hasErrors());
078:
079: final Package pkg = new Package("org.drools");
080:
081: final RuleDescr ruleDescr = (RuleDescr) pkgDescr.getRules()
082: .get(0);
083: final String ruleClassName = "RuleClassName.java";
084: ruleDescr.setClassName(ruleClassName);
085: ruleDescr.addAttribute(new AttributeDescr("dialect", "java"));
086:
087: final TypeResolver typeResolver = new ClassTypeResolver(
088: new HashSet(), this .getClass().getClassLoader());
089: // make an automatic import for the current package
090: typeResolver.addImport(pkgDescr.getName() + ".*");
091: typeResolver.addImport("java.lang.*");
092:
093: final RuleBuilder builder = new RuleBuilder();
094:
095: final PackageBuilder pkgBulider = new PackageBuilder(pkg);
096: final PackageBuilderConfiguration conf = pkgBulider
097: .getPackageBuilderConfiguration();
098: Dialect dialect = pkgBulider.getPackageBuilderConfiguration()
099: .getDialectRegistry().getDialectConfiguration("java")
100: .getDialect();
101:
102: RuleBuildContext context = new RuleBuildContext(conf, pkg,
103: ruleDescr, conf.getDialectRegistry(), dialect);
104:
105: builder.build(context);
106:
107: Assert.assertTrue(context.getErrors().toString(), context
108: .getErrors().isEmpty());
109:
110: final Rule rule = context.getRule();
111:
112: assertEquals("There should be 2 rule level declarations", 2,
113: rule.getDeclarations().length);
114:
115: // second GE should be a not
116: final GroupElement not = (GroupElement) rule.getLhs()
117: .getChildren().get(1);
118: assertTrue(not.isNot());
119: // not has no outer declarations
120: assertTrue(not.getOuterDeclarations().isEmpty());
121: assertEquals(1, not.getInnerDeclarations().size());
122: assertTrue(not.getInnerDeclarations().keySet().contains(
123: "$state"));
124:
125: // second not
126: final GroupElement not2 = (GroupElement) ((GroupElement) not
127: .getChildren().get(0)).getChildren().get(1);
128: assertTrue(not2.isNot());
129: // not has no outer declarations
130: assertTrue(not2.getOuterDeclarations().isEmpty());
131: assertEquals(1, not2.getInnerDeclarations().size());
132: assertTrue(not2.getInnerDeclarations().keySet().contains(
133: "$likes"));
134: }
135:
136: public void testBuildAttributes() throws Exception {
137: Rule rule = new Rule("my rule");
138:
139: List attributes = new ArrayList();
140:
141: attributes.add(new AttributeDescr("dialect", "java"));
142: attributes.add(new AttributeDescr("no-loop", "true"));
143: attributes.add(new AttributeDescr("enabled", "false"));
144: attributes.add(new AttributeDescr("ruleflow-group", "mygroup"));
145:
146: RuleBuildContext.setAttributes(rule, null, attributes);
147:
148: assertTrue(rule.isNoLoop());
149: assertFalse(rule.isEffective());
150: assertEquals("mygroup", rule.getRuleFlowGroup());
151:
152: attributes = new ArrayList();
153: attributes.add(new AttributeDescr("date-effective",
154: "10-Jul-1974"));
155: attributes
156: .add(new AttributeDescr("date-expires", "10-Jul-2040"));
157:
158: rule = new Rule("myrule");
159:
160: RuleBuildContext.setAttributes(rule, null, attributes);
161:
162: final Field eff = rule.getClass().getDeclaredField(
163: "dateEffective");
164: eff.setAccessible(true);
165: final Calendar effectiveDate = (Calendar) eff.get(rule);
166: assertNotNull(effectiveDate);
167:
168: assertEquals(1974, effectiveDate.get(Calendar.YEAR));
169:
170: final Field exp = rule.getClass().getDeclaredField(
171: "dateExpires");
172: exp.setAccessible(true);
173: final Calendar expiryDate = (Calendar) exp.get(rule);
174:
175: assertEquals(2040, expiryDate.get(Calendar.YEAR));
176:
177: assertNotNull(expiryDate);
178:
179: }
180:
181: }
|