001: package org.drools.brms.server.builder;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.StringReader;
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.jar.JarInputStream;
023:
024: import junit.framework.TestCase;
025:
026: import org.drools.compiler.PackageBuilderConfiguration;
027: import org.drools.lang.descr.PackageDescr;
028: import org.drools.lang.dsl.DSLMappingFile;
029: import org.drools.rule.Package;
030: import org.drools.rule.builder.dialect.java.JavaDialectConfiguration;
031:
032: public class BRMSPackageBuilderTest extends TestCase {
033:
034: // Added this empty test so this class doesn't fail
035: public void testEmpty() {
036:
037: }
038:
039: public void setUp() {
040: System.getProperties().remove("drools.dialect.java.compiler");
041: }
042:
043: public void tearDown() {
044: System.getProperties().remove("drools.dialect.java.compiler");
045: }
046:
047: // @FIXME rule "abc" is null and the Packge has no namespace
048: public void testPartialPackage() throws Exception {
049:
050: JarInputStream jis = new JarInputStream(this .getClass()
051: .getResourceAsStream("/billasurf.jar"));
052: List<JarInputStream> l = new ArrayList<JarInputStream>();
053: l.add(jis);
054: BRMSPackageBuilder builder = BRMSPackageBuilder.getInstance(l);
055:
056: PackageDescr pc = new PackageDescr("foo.bar");
057: builder.addPackage(pc);
058:
059: String header = "import com.billasurf.Person\n import com.billasurf.Board";
060: builder.addPackageFromDrl(new StringReader(header));
061: assertFalse(builder.hasErrors());
062:
063: JavaDialectConfiguration javaConf = (JavaDialectConfiguration) builder
064: .getPackageBuilderConfiguration()
065: .getDialectConfiguration("java");
066: assertEquals(JavaDialectConfiguration.JANINO, javaConf
067: .getCompiler());
068:
069: String ruleAtom = "rule foo \n when \n Person() \n then \n System.out.println(42); end";
070: builder.addPackageFromDrl(new StringReader(ruleAtom));
071: if (builder.hasErrors()) {
072: System.err.println(builder.getErrors().getErrors()[0]
073: .getMessage());
074: }
075: assertFalse(builder.hasErrors());
076:
077: ruleAtom = "rule foo2 \n when \n Person() \n then \n System.out.println(42); end";
078: builder.addPackageFromDrl(new StringReader(ruleAtom));
079: if (builder.hasErrors()) {
080: System.err.println(builder.getErrors().getErrors()[0]
081: .getMessage());
082: }
083: assertFalse(builder.hasErrors());
084:
085: assertEquals("foo.bar", builder.getPackage().getName());
086:
087: String functionAtom = "function int fooBar(String x) { return 42; }";
088: builder.addPackageFromDrl(new StringReader(functionAtom));
089: if (builder.hasErrors()) {
090: System.err.println(builder.getErrors().getErrors()[0]
091: .getMessage());
092: }
093: assertFalse(builder.hasErrors());
094:
095: Package p = builder.getPackage();
096: assertEquals(2, p.getRules().length);
097: assertEquals(1, p.getFunctions().size());
098: assertNotNull(p.getRule("foo2"));
099:
100: functionAtom = "xxx";
101: builder.addPackageFromDrl(new StringReader(functionAtom));
102: assertTrue(builder.hasErrors());
103: builder.clearErrors();
104: assertFalse(builder.hasErrors());
105:
106: }
107:
108: public void testHasDSL() {
109: BRMSPackageBuilder builder = new BRMSPackageBuilder(null);
110: assertFalse(builder.hasDSL());
111: }
112:
113: public void testGetExpander() {
114: BRMSPackageBuilder builder = new BRMSPackageBuilder(null);
115: List<DSLMappingFile> files = new ArrayList<DSLMappingFile>();
116: files.add(new DSLMappingFile());
117: builder.setDSLFiles(files);
118: assertTrue(builder.hasDSL());
119: assertNotNull(builder.getDSLExpander());
120: }
121:
122: // public void testDefaultCompiler() {
123: // assertEquals(JavaDialectConfiguration.JANINO, BRMSPackageBuilder.COMPILER);
124: // assertEquals(PackageBuilderConfiguration.JANINO, BRMSPackageBuilder.getPreferredBRMSCompiler());
125: // System.setProperty( "drools.compiler", "ECLIPSE" );
126: // assertEquals(PackageBuilderConfiguration.ECLIPSE, BRMSPackageBuilder.getPreferredBRMSCompiler());
127: // System.setProperty( "drools.compiler", "" );
128: // assertEquals(PackageBuilderConfiguration.JANINO, BRMSPackageBuilder.getPreferredBRMSCompiler());
129: // }
130:
131: // @FIXME rule "abc" is null and the Packge has no namespace
132: public void testDefaultCompiler() throws Exception {
133:
134: JarInputStream jis = new JarInputStream(this .getClass()
135: .getResourceAsStream("/billasurf.jar"));
136: List<JarInputStream> l = new ArrayList<JarInputStream>();
137: l.add(jis);
138: BRMSPackageBuilder builder = BRMSPackageBuilder.getInstance(l);
139:
140: PackageDescr pc = new PackageDescr("foo.bar");
141: builder.addPackage(pc);
142:
143: String header = "import com.billasurf.Person\n import com.billasurf.Board";
144: builder.addPackageFromDrl(new StringReader(header));
145: assertFalse(builder.hasErrors());
146:
147: JavaDialectConfiguration javaConf = (JavaDialectConfiguration) builder
148: .getPackageBuilderConfiguration()
149: .getDialectConfiguration("java");
150: assertEquals(JavaDialectConfiguration.JANINO, javaConf
151: .getCompiler());
152: }
153:
154: public void testEclipseCompiler() throws Exception {
155:
156: System.setProperty("drools.dialect.java.compiler", "ECLIPSE");
157: JarInputStream jis = new JarInputStream(this .getClass()
158: .getResourceAsStream("/billasurf.jar"));
159: List<JarInputStream> l = new ArrayList<JarInputStream>();
160: l.add(jis);
161: BRMSPackageBuilder builder = BRMSPackageBuilder.getInstance(l);
162:
163: PackageDescr pc = new PackageDescr("foo.bar");
164: builder.addPackage(pc);
165:
166: String header = "import com.billasurf.Person\n import com.billasurf.Board";
167: builder.addPackageFromDrl(new StringReader(header));
168: assertFalse(builder.hasErrors());
169:
170: JavaDialectConfiguration javaConf = (JavaDialectConfiguration) builder
171: .getPackageBuilderConfiguration()
172: .getDialectConfiguration("java");
173: assertEquals(JavaDialectConfiguration.ECLIPSE, javaConf
174: .getCompiler());
175: }
176:
177: }
|