[jboss-svn-commits] JBL Code SVN: r8185 - in labs/jbossrules/trunk: . drools-ant drools-ant/lib drools-ant/src drools-ant/src/main drools-ant/src/main/java drools-ant/src/main/java/org drools-ant/src/main/java/org/drools drools-ant/src/main/java/org/drools/contrib drools-ant/src/test drools-ant/src/test/java drools-ant/src/test/java/org drools-ant/src/test/java/org/drools drools-ant/src/test/java/org/drools/contrib drools-ant/src/test/resources drools-ant/src/test/resources/rules

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Dec 10 15:15:03 EST 2006


Author: tirelli
Date: 2006-12-10 15:14:52 -0500 (Sun, 10 Dec 2006)
New Revision: 8185

Added:
   labs/jbossrules/trunk/drools-ant/
   labs/jbossrules/trunk/drools-ant/.classpath
   labs/jbossrules/trunk/drools-ant/.project
   labs/jbossrules/trunk/drools-ant/lib/
   labs/jbossrules/trunk/drools-ant/lib/cheese.jar
   labs/jbossrules/trunk/drools-ant/pom.xml
   labs/jbossrules/trunk/drools-ant/src/
   labs/jbossrules/trunk/drools-ant/src/main/
   labs/jbossrules/trunk/drools-ant/src/main/java/
   labs/jbossrules/trunk/drools-ant/src/main/java/org/
   labs/jbossrules/trunk/drools-ant/src/main/java/org/drools/
   labs/jbossrules/trunk/drools-ant/src/main/java/org/drools/contrib/
   labs/jbossrules/trunk/drools-ant/src/main/java/org/drools/contrib/DroolsAntTask.java
   labs/jbossrules/trunk/drools-ant/src/test/
   labs/jbossrules/trunk/drools-ant/src/test/java/
   labs/jbossrules/trunk/drools-ant/src/test/java/org/
   labs/jbossrules/trunk/drools-ant/src/test/java/org/drools/
   labs/jbossrules/trunk/drools-ant/src/test/java/org/drools/contrib/
   labs/jbossrules/trunk/drools-ant/src/test/java/org/drools/contrib/BuildFileTest.java
   labs/jbossrules/trunk/drools-ant/src/test/java/org/drools/contrib/DroolsAntTaskTest.java
   labs/jbossrules/trunk/drools-ant/src/test/resources/
   labs/jbossrules/trunk/drools-ant/src/test/resources/DroolsAntTask.xml
   labs/jbossrules/trunk/drools-ant/src/test/resources/rules/
   labs/jbossrules/trunk/drools-ant/src/test/resources/rules/cheese.drl
Log:
Adding drools-ant project


Property changes on: labs/jbossrules/trunk/drools-ant
___________________________________________________________________
Name: svn:ignore
   + target


Added: labs/jbossrules/trunk/drools-ant/.classpath
===================================================================
--- labs/jbossrules/trunk/drools-ant/.classpath	2006-12-10 19:57:12 UTC (rev 8184)
+++ labs/jbossrules/trunk/drools-ant/.classpath	2006-12-10 20:14:52 UTC (rev 8185)
@@ -0,0 +1,30 @@
+<classpath>
+  <classpathentry kind="src" path="src/main/java"/>
+  <classpathentry kind="src" path="src/test/java" output="target/test-classes"/>
+  <classpathentry kind="src" path="src/test/resources" output="target/test-classes"/>
+  <classpathentry kind="output" path="target/classes"/>
+  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+  <classpathentry kind="var" path="M2_REPO/org/apache/commons/commons-jci-eclipse/3.2.0.666/commons-jci-eclipse-3.2.0.666.jar"/>
+  <classpathentry kind="var" path="M2_REPO/concurrent/concurrent/1.3.4/concurrent-1.3.4.jar"/>
+  <classpathentry kind="var" path="M2_REPO/jung/jung/1.7.2/jung-1.7.2.jar"/>
+  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
+  <classpathentry kind="var" path="M2_REPO/xstream/xstream/1.1.3/xstream-1.1.3.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/apache/commons/commons-jci-janino/2.4.3/commons-jci-janino-2.4.3.jar"/>
+  <classpathentry kind="var" path="M2_REPO/ant/ant/1.6.5/ant-1.6.5.jar"/>
+  <classpathentry kind="var" path="M2_REPO/janino/janino/2.4.3/janino-2.4.3.jar"/>
+  <classpathentry kind="var" path="M2_REPO/antlr/stringtemplate/2.3b6/stringtemplate-2.3b6.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/drools/drools-core/3.0.2/drools-core-3.0.2.jar"/>
+  <classpathentry kind="var" path="M2_REPO/colt/colt/1.2.0/colt-1.2.0.jar"/>
+  <classpathentry kind="var" path="M2_REPO/commons-lang/commons-lang/2.1/commons-lang-2.1.jar" sourcepath="M2_REPO/commons-lang/commons-lang/2.1/commons-lang-2.1-sources.jar"/>
+  <classpathentry kind="var" path="M2_REPO/xpp3/xpp3/1.1.3.4.O/xpp3-1.1.3.4.O.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/eclipse/jdt/core/3.2.0.666/core-3.2.0.666.jar"/>
+  <classpathentry kind="var" path="M2_REPO/commons-collections/commons-collections/3.1/commons-collections-3.1.jar" sourcepath="M2_REPO/commons-collections/commons-collections/3.1/commons-collections-3.1-sources.jar"/>
+  <classpathentry kind="var" path="M2_REPO/ant/ant-nodeps/1.6.5/ant-nodeps-1.6.5.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/apache/commons/commons-jci-core/1.0-406301/commons-jci-core-1.0-406301.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/drools/drools-compiler/3.0.2/drools-compiler-3.0.2.jar"/>
+  <classpathentry kind="var" path="M2_REPO/xerces/xercesImpl/2.6.2/xercesImpl-2.6.2.jar"/>
+  <classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar"/>
+  <classpathentry kind="var" path="M2_REPO/antlr/antlr/2.7.7/antlr-2.7.7.jar"/>
+  <classpathentry kind="var" path="M2_REPO/commons-io/commons-io/1.1/commons-io-1.1.jar" sourcepath="M2_REPO/commons-io/commons-io/1.1/commons-io-1.1-sources.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/antlr/antlr/3.0b5/antlr-3.0b5.jar"/>
+</classpath>
\ No newline at end of file


Property changes on: labs/jbossrules/trunk/drools-ant/.classpath
___________________________________________________________________
Name: svn:executable
   + *

Added: labs/jbossrules/trunk/drools-ant/.project
===================================================================
--- labs/jbossrules/trunk/drools-ant/.project	2006-12-10 19:57:12 UTC (rev 8184)
+++ labs/jbossrules/trunk/drools-ant/.project	2006-12-10 20:14:52 UTC (rev 8185)
@@ -0,0 +1,14 @@
+<projectDescription>
+  <name>drools-ant</name>
+  <comment/>
+  <projects/>
+  <buildSpec>
+    <buildCommand>
+      <name>org.eclipse.jdt.core.javabuilder</name>
+      <arguments/>
+    </buildCommand>
+  </buildSpec>
+  <natures>
+    <nature>org.eclipse.jdt.core.javanature</nature>
+  </natures>
+</projectDescription>
\ No newline at end of file


Property changes on: labs/jbossrules/trunk/drools-ant/.project
___________________________________________________________________
Name: svn:executable
   + *

Added: labs/jbossrules/trunk/drools-ant/lib/cheese.jar
===================================================================
(Binary files differ)


Property changes on: labs/jbossrules/trunk/drools-ant/lib/cheese.jar
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:mime-type
   + application/octet-stream

Added: labs/jbossrules/trunk/drools-ant/pom.xml
===================================================================
--- labs/jbossrules/trunk/drools-ant/pom.xml	2006-12-10 19:57:12 UTC (rev 8184)
+++ labs/jbossrules/trunk/drools-ant/pom.xml	2006-12-10 20:14:52 UTC (rev 8185)
@@ -0,0 +1,82 @@
+<project>
+  <parent>
+    <artifactId>drools</artifactId>
+    <groupId>org.drools</groupId>
+    <version>3.0.2</version>
+  </parent>
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>drools-ant</artifactId>
+
+  <name>Drools :: Ant Task</name>
+  <version>3.0.2</version>
+
+  <repositories>
+    <repository>
+      <id>basedir</id>
+      <url>file://${basedir}/../repository</url>
+    </repository>
+  </repositories>  
+
+  <dependencies>   
+    <dependency>
+      <groupId>org.drools</groupId>
+      <artifactId>drools-core</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.drools</groupId>
+      <artifactId>drools-compiler</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>ant</groupId>
+      <artifactId>ant</artifactId>
+      <version>1.6.5</version>
+    </dependency>
+
+    <dependency>
+      <groupId>ant</groupId>
+      <artifactId>ant-nodeps</artifactId>
+      <version>1.6.5</version>
+    </dependency>
+
+           
+  </dependencies>
+
+  <!-- This is needed to copy the generated jars into the IDE/Plugs lib directory -->
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-antrun-plugin</artifactId>
+        <executions>
+            <execution>
+                <id>package</id>
+                <phase>package</phase>
+                <configuration>
+                    <tasks>
+                        <echo message="Copying file ${project.build.directory}/${project.artifactId}-${project.version}.${project.packaging} to ../drools-ide/lib/${project.artifactId}.${project.packaging}" />
+                        <copy file="${project.build.directory}/${project.artifactId}-${project.version}.${project.packaging}" tofile="../drools-ide/lib/${project.artifactId}.${project.packaging}" />
+                    </tasks>
+                </configuration>
+                <goals>
+                    <goal>run</goal>
+                </goals>
+            </execution>
+        </executions>
+        <dependencies>
+	        <dependency>
+                <groupId>ant</groupId>
+                <artifactId>ant-antlr</artifactId>
+                <version>1.6.5</version>
+	        </dependency>
+        </dependencies>
+      </plugin>      
+    </plugins>
+  </build>
+
+</project>


Property changes on: labs/jbossrules/trunk/drools-ant/pom.xml
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:eol-style
   + native

Added: labs/jbossrules/trunk/drools-ant/src/main/java/org/drools/contrib/DroolsAntTask.java
===================================================================
--- labs/jbossrules/trunk/drools-ant/src/main/java/org/drools/contrib/DroolsAntTask.java	2006-12-10 19:57:12 UTC (rev 8184)
+++ labs/jbossrules/trunk/drools-ant/src/main/java/org/drools/contrib/DroolsAntTask.java	2006-12-10 20:14:52 UTC (rev 8185)
@@ -0,0 +1,240 @@
+/*
+ * Copyright 2006 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.contrib;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.ObjectOutputStream;
+
+import org.apache.tools.ant.AntClassLoader;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.DirectoryScanner;
+import org.apache.tools.ant.taskdefs.MatchingTask;
+import org.apache.tools.ant.types.Path;
+import org.apache.tools.ant.types.Reference;
+import org.drools.RuleBase;
+import org.drools.RuleBaseFactory;
+import org.drools.compiler.DroolsParserException;
+import org.drools.compiler.PackageBuilder;
+import org.drools.compiler.PackageBuilderConfiguration;
+
+/**
+ * An ant task to allow rulebase compilation and serialization
+ * during a build.
+ * 
+ * @author etirelli
+ */
+public class DroolsAntTask extends MatchingTask {
+
+    private File srcdir;
+    private File toFile;
+    private Path classpath;
+
+    /**
+     * Source directory to read DRL files from
+     * 
+     * @param directory
+     */
+    public void setSrcDir(File directory) {
+        this.srcdir = directory;
+    }
+
+    /**
+     * File to serialize the rulebase to
+     * 
+     * @param toFile
+     */
+    public void setToFile(File toFile) {
+        this.toFile = toFile;
+    }
+
+    /**
+     * The classpath to use when compiling the rulebase
+     * 
+     * @param classpath
+     */
+    public void setClasspath(Path classpath) {
+        createClasspath().append( classpath );
+    }
+    
+    /**
+     * Classpath to use, by reference, when compiling the rulebase
+     *
+     * @param r a reference to an existing classpath
+     */
+    public void setClasspathref(Reference r) {
+        createClasspath().setRefid(r);
+    }
+    
+    /**
+     * Adds a path to the classpath.
+     *
+     * @return created classpath
+     */
+    public Path createClasspath() {
+        if( this.classpath == null) {
+            this.classpath = new Path(getProject()); 
+        }
+        return this.classpath.createPath();
+    }
+
+    /**
+     * Task's main method
+     */
+    public void execute() throws BuildException {
+        super.execute();
+
+        // checking parameters are set
+        if ( toFile == null ) {
+            throw new BuildException( "Destination rulebase file does not specified." );
+        }
+
+        // checking parameters are set
+        if ( srcdir == null || !srcdir.exists() ) {
+            throw new BuildException( "Source directory not specified or does not exists." );
+        }
+
+        try {
+            // create a specialized classloader
+            AntClassLoader loader = getClassLoader();
+
+            // create a package builder configured to use the given classloader
+            PackageBuilder builder = getPackageBuilder( loader );
+
+            // get the list of files to be added to the rulebase
+            String[] fileNames = getFileList();
+
+            for ( int i = 0; i < fileNames.length; i++ ) {
+                // compile rule file and add to the builder
+                compileAndAddFile( builder,
+                                   fileNames[i] );
+            }
+
+            // gets the package 
+            org.drools.rule.Package pkg = builder.getPackage();
+
+            // creates the rulebase
+            RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+
+            // adds the package
+            ruleBase.addPackage( pkg );
+
+            // serialize the rule base to the destination file
+            serializeRulebase( ruleBase );
+
+        } catch ( Exception e ) {
+            throw new BuildException( "RuleBaseTask failed: " + e.getMessage(),
+                                      e );
+        }
+    }
+
+    /**
+     * @param ruleBase
+     * @throws FileNotFoundException
+     * @throws IOException
+     */
+    private void serializeRulebase(RuleBase ruleBase) throws FileNotFoundException,
+                                                     IOException {
+        ObjectOutputStream outstream = null;
+        try {
+            FileOutputStream fout = new FileOutputStream( toFile );
+            outstream = new ObjectOutputStream( fout );
+            outstream.writeObject( ruleBase );
+        } finally {
+            if ( outstream != null ) {
+                outstream.close();
+            }
+        }
+
+    }
+
+    /**
+     * @param builder
+     * @param fileName
+     * @return
+     * @throws FileNotFoundException
+     * @throws DroolsParserException
+     * @throws IOException
+     */
+    private void compileAndAddFile(PackageBuilder builder,
+                                   String fileName) throws FileNotFoundException,
+                                                   DroolsParserException,
+                                                   IOException {
+        InputStreamReader instream = null;
+        try {
+            File file = new File( this.srcdir,
+                                  fileName );
+            instream = new InputStreamReader( new FileInputStream( file ) );
+            builder.addPackageFromDrl( instream );
+        } finally {
+            if ( instream != null ) {
+                instream.close();
+            }
+        }
+
+    }
+
+    /**
+     * @return
+     */
+    private AntClassLoader getClassLoader() {
+        // defining a new specialized classloader and setting it as the thread context classloader
+        AntClassLoader loader = null;
+        if ( classpath != null ) {
+            loader = new AntClassLoader( PackageBuilder.class.getClassLoader(),
+                                         getProject(),
+                                         classpath,
+                                         false );
+        } else {
+            loader = new AntClassLoader( PackageBuilder.class.getClassLoader(),
+                                         false );
+        }
+        loader.setThreadContextLoader();
+        return loader;
+    }
+
+    /**
+     * @param loader
+     * @return
+     */
+    private PackageBuilder getPackageBuilder(AntClassLoader loader) {
+        // creating package builder configured with the give classloader
+        PackageBuilderConfiguration conf = new PackageBuilderConfiguration();
+        conf.setClassLoader( loader );
+        PackageBuilder builder = new PackageBuilder( conf );
+        return builder;
+    }
+
+    /**
+     * Returns the list of files to be added into the rulebase
+     * @return
+     */
+    private String[] getFileList() {
+        // scan source directory for rule files
+        DirectoryScanner directoryScanner = getDirectoryScanner( srcdir );
+        String[] fileNames = directoryScanner.getIncludedFiles();
+
+        if ( fileNames == null || fileNames.length <= 0 ) {
+            throw new BuildException( "No rule files found in include directory." );
+        }
+        return fileNames;
+    }
+}


Property changes on: labs/jbossrules/trunk/drools-ant/src/main/java/org/drools/contrib/DroolsAntTask.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:keywords
   + id author date revision
Name: svn:eol-style
   + native

Added: labs/jbossrules/trunk/drools-ant/src/test/java/org/drools/contrib/BuildFileTest.java
===================================================================
--- labs/jbossrules/trunk/drools-ant/src/test/java/org/drools/contrib/BuildFileTest.java	2006-12-10 19:57:12 UTC (rev 8184)
+++ labs/jbossrules/trunk/drools-ant/src/test/java/org/drools/contrib/BuildFileTest.java	2006-12-10 20:14:52 UTC (rev 8185)
@@ -0,0 +1,484 @@
+/*
+ * Copyright  2001-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.drools.contrib;
+
+import java.io.File;
+import java.io.PrintStream;
+import java.net.URL;
+
+import junit.framework.TestCase;
+
+import org.apache.tools.ant.BuildEvent;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.BuildListener;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.ProjectHelper;
+
+/**
+ * A BuildFileTest is a TestCase which executes targets from an Ant buildfile
+ * for testing.
+ *
+ * This class provides a number of utility methods for particular build file
+ * tests which extend this class.
+ *
+ */
+public abstract class BuildFileTest extends TestCase {
+
+    protected Project project;
+
+    private StringBuffer logBuffer;
+    private StringBuffer fullLogBuffer;
+    private StringBuffer outBuffer;
+    private StringBuffer errBuffer;
+    private BuildException buildException;
+
+    /**
+     *  Constructor for the BuildFileTest object
+     *
+     *@param  name string to pass up to TestCase constructor
+     */
+    public BuildFileTest(String name) {
+        super(name);
+    }
+
+    /**
+     *  run a target, expect for any build exception
+     *
+     *@param  target target to run
+     *@param  cause  information string to reader of report
+     */
+    protected void expectBuildException(String target, String cause) {
+        expectSpecificBuildException(target, cause, null);
+    }
+
+    /**
+     * Assert that only the given message has been logged with a
+     * priority &gt;= INFO when running the given target.
+     */
+    protected void expectLog(String target, String log) {
+        executeTarget(target);
+        String realLog = getLog();
+        assertEquals(log, realLog);
+    }
+
+    /**
+     * Assert that the given substring is in the log messages
+     */
+
+    protected void assertLogContaining(String substring) {
+        String realLog = getLog();
+        assertTrue("expecting log to contain \"" + substring + "\" log was \""
+                   + realLog + "\"",
+                   realLog.indexOf(substring) >= 0);
+    }
+
+    /**
+     * Assert that the given message has been logged with a priority
+     * &gt;= INFO when running the given target.
+     */
+    protected void expectLogContaining(String target, String log) {
+        executeTarget(target);
+        assertLogContaining(log);
+    }
+
+    /**
+     *  Gets the log the BuildFileTest object.
+     *  only valid if configureProject() has
+     *  been called.
+     * @pre logBuffer!=null
+     * @return    The log value
+     */
+    protected String getLog() {
+        return logBuffer.toString();
+    }
+
+    /**
+     * Assert that the given message has been logged with a priority
+     * &gt;= DEBUG when running the given target.
+     */
+    protected void expectDebuglog(String target, String log) {
+        executeTarget(target);
+        String realLog = getFullLog();
+        assertEquals(log, realLog);
+    }
+
+    /**
+     *  Gets the log the BuildFileTest object.
+     *  only valid if configureProject() has
+     *  been called.
+     * @pre fullLogBuffer!=null
+     * @return    The log value
+     */
+    protected String getFullLog() {
+        return fullLogBuffer.toString();
+    }
+
+    /**
+     *  execute the target, verify output matches expectations
+     *
+     *@param  target  target to execute
+     *@param  output  output to look for
+     */
+
+    protected void expectOutput(String target, String output) {
+        executeTarget(target);
+        String realOutput = getOutput();
+        assertEquals(output, realOutput.trim());
+    }
+
+    /**
+     *  execute the target, verify output matches expectations
+     *  and that we got the named error at the end
+     *@param  target  target to execute
+     *@param  output  output to look for
+     *@param  error   Description of Parameter
+     */
+
+    protected void expectOutputAndError(String target, String output, String error) {
+        executeTarget(target);
+        String realOutput = getOutput();
+        assertEquals(output, realOutput);
+        String realError = getError();
+        assertEquals(error, realError);
+    }
+
+    protected String getOutput() {
+        return cleanBuffer(outBuffer);
+    }
+
+    protected String getError() {
+        return cleanBuffer(errBuffer);
+    }
+
+    protected BuildException getBuildException() {
+        return buildException;
+    }
+
+    private String cleanBuffer(StringBuffer buffer) {
+        StringBuffer cleanedBuffer = new StringBuffer();
+        boolean cr = false;
+        for (int i = 0; i < buffer.length(); i++) {
+            char ch = buffer.charAt(i);
+            if (ch == '\r') {
+                cr = true;
+                continue;
+            }
+
+            if (!cr) {
+                cleanedBuffer.append(ch);
+            } else {
+                cleanedBuffer.append(ch);
+            }
+        }
+        return cleanedBuffer.toString();
+    }
+
+    /**
+     *  set up to run the named project
+     *
+     * @param  filename name of project file to run
+     */
+    protected void configureProject(String filename) throws BuildException {
+        configureProject(filename, Project.MSG_DEBUG);
+    }
+
+    /**
+     *  set up to run the named project
+     *
+     * @param  filename name of project file to run
+     */
+    protected void configureProject(String filename, int logLevel)
+        throws BuildException {
+        logBuffer = new StringBuffer();
+        fullLogBuffer = new StringBuffer();
+        project = new Project();
+        project.init();
+        project.setUserProperty( "ant.file" , new File(filename).getAbsolutePath() );
+        project.addBuildListener(new AntTestListener(logLevel));
+        ProjectHelper.configureProject(project, new File(filename));
+    }
+
+    /**
+     *  execute a target we have set up
+     * @pre configureProject has been called
+     * @param  targetName  target to run
+     */
+    protected void executeTarget(String targetName) {
+        PrintStream sysOut = System.out;
+        PrintStream sysErr = System.err;
+        try {
+            sysOut.flush();
+            sysErr.flush();
+            outBuffer = new StringBuffer();
+            PrintStream out = new PrintStream(new AntOutputStream(outBuffer));
+            System.setOut(out);
+            errBuffer = new StringBuffer();
+            PrintStream err = new PrintStream(new AntOutputStream(errBuffer));
+            System.setErr(err);
+            logBuffer = new StringBuffer();
+            fullLogBuffer = new StringBuffer();
+            buildException = null;
+            project.executeTarget(targetName);
+        } finally {
+            System.setOut(sysOut);
+            System.setErr(sysErr);
+        }
+
+    }
+
+    /**
+     * Get the project which has been configured for a test.
+     *
+     * @return the Project instance for this test.
+     */
+    protected Project getProject() {
+        return project;
+    }
+
+    /**
+     * get the directory of the project
+     * @return the base dir of the project
+     */
+    protected File getProjectDir() {
+        return project.getBaseDir();
+    }
+
+    /**
+     *  run a target, wait for a build exception
+     *
+     *@param  target target to run
+     *@param  cause  information string to reader of report
+     *@param  msg    the message value of the build exception we are waiting for
+              set to null for any build exception to be valid
+     */
+    protected void expectSpecificBuildException(String target, String cause, String msg) {
+        try {
+            executeTarget(target);
+        } catch (org.apache.tools.ant.BuildException ex) {
+            buildException = ex;
+            if ((null != msg) && (!ex.getMessage().equals(msg))) {
+                fail("Should throw BuildException because '" + cause
+                        + "' with message '" + msg
+                        + "' (actual message '" + ex.getMessage() + "' instead)");
+            }
+            return;
+        }
+        fail("Should throw BuildException because: " + cause);
+    }
+
+    /**
+     *  run a target, expect an exception string
+     *  containing the substring we look for (case sensitive match)
+     *
+     *@param  target target to run
+     *@param  cause  information string to reader of report
+     *@param  contains  substring of the build exception to look for
+     */
+    protected void expectBuildExceptionContaining(String target, String cause, String contains) {
+        try {
+            executeTarget(target);
+        } catch (org.apache.tools.ant.BuildException ex) {
+            buildException = ex;
+            if ((null != contains) && (ex.getMessage().indexOf(contains) == -1)) {
+                fail("Should throw BuildException because '" + cause + "' with message containing '" + contains + "' (actual message '" + ex.getMessage() + "' instead)");
+            }
+            return;
+        }
+        fail("Should throw BuildException because: " + cause);
+    }
+
+
+    /**
+     * call a target, verify property is as expected
+     *
+     * @param target build file target
+     * @param property property name
+     * @param value expected value
+     */
+
+    protected void expectPropertySet(String target, String property, String value) {
+        executeTarget(target);
+        assertPropertyEquals(property, value);
+    }
+
+    /**
+     * assert that a property equals a value; comparison is case sensitive.
+     * @param property property name
+     * @param value expected value
+     */
+    protected void assertPropertyEquals(String property, String value) {
+        String result = project.getProperty(property);
+        assertEquals("property " + property,value,result);
+    }
+
+    /**
+     * assert that a property equals &quot;true&quot;
+     * @param property property name
+     */
+    protected void assertPropertySet(String property) {
+        assertPropertyEquals(property, "true");
+    }
+
+    /**
+     * assert that a property is null
+     * @param property property name
+     */
+    protected void assertPropertyUnset(String property) {
+        assertPropertyEquals(property, null);
+    }
+
+
+    /**
+     * call a target, verify named property is "true".
+     *
+     * @param target build file target
+     * @param property property name
+     */
+    protected void expectPropertySet(String target, String property) {
+        expectPropertySet(target, property, "true");
+    }
+
+
+    /**
+     * call a target, verify property is null
+     * @param target build file target
+     * @param property property name
+     */
+    protected void expectPropertyUnset(String target, String property) {
+        expectPropertySet(target, property, null);
+    }
+
+    /**
+     * Retrieve a resource from the caller classloader to avoid
+     * assuming a vm working directory. The resource path must be
+     * relative to the package name or absolute from the root path.
+     * @param resource the resource to retrieve its url.
+     * @throws AssertionFailureException if resource is not found.
+     */
+    protected URL getResource(String resource){
+        URL url = getClass().getResource(resource);
+        assertNotNull("Could not find resource :" + resource, url);
+        return url;
+    }
+
+    /**
+     * an output stream which saves stuff to our buffer.
+     */
+    private static class AntOutputStream extends java.io.OutputStream {
+        private StringBuffer buffer;
+
+        public AntOutputStream( StringBuffer buffer ) {
+            this.buffer = buffer;
+        }
+
+        public void write(int b) {
+            buffer.append((char)b);
+        }
+    }
+
+    /**
+     * our own personal build listener
+     */
+    private class AntTestListener implements BuildListener {
+        private int logLevel;
+
+        /**
+         * Constructs a test listener which will ignore log events
+         * above the given level
+         */
+        public AntTestListener(int logLevel) {
+            this.logLevel = logLevel;
+        }
+
+        /**
+         *  Fired before any targets are started.
+         */
+        public void buildStarted(BuildEvent event) {
+        }
+
+        /**
+         *  Fired after the last target has finished. This event
+         *  will still be thrown if an error occured during the build.
+         *
+         *  @see BuildEvent#getException()
+         */
+        public void buildFinished(BuildEvent event) {
+        }
+
+        /**
+         *  Fired when a target is started.
+         *
+         *  @see BuildEvent#getTarget()
+         */
+        public void targetStarted(BuildEvent event) {
+            //System.out.println("targetStarted " + event.getTarget().getName());
+        }
+
+        /**
+         *  Fired when a target has finished. This event will
+         *  still be thrown if an error occured during the build.
+         *
+         *  @see BuildEvent#getException()
+         */
+        public void targetFinished(BuildEvent event) {
+            //System.out.println("targetFinished " + event.getTarget().getName());
+        }
+
+        /**
+         *  Fired when a task is started.
+         *
+         *  @see BuildEvent#getTask()
+         */
+        public void taskStarted(BuildEvent event) {
+            //System.out.println("taskStarted " + event.getTask().getTaskName());
+        }
+
+        /**
+         *  Fired when a task has finished. This event will still
+         *  be throw if an error occured during the build.
+         *
+         *  @see BuildEvent#getException()
+         */
+        public void taskFinished(BuildEvent event) {
+            //System.out.println("taskFinished " + event.getTask().getTaskName());
+        }
+
+        /**
+         *  Fired whenever a message is logged.
+         *
+         *  @see BuildEvent#getMessage()
+         *  @see BuildEvent#getPriority()
+         */
+        public void messageLogged(BuildEvent event) {
+            if (event.getPriority() > logLevel) {
+                // ignore event
+                return;
+            }
+
+            if (event.getPriority() == Project.MSG_INFO ||
+                event.getPriority() == Project.MSG_WARN ||
+                event.getPriority() == Project.MSG_ERR) {
+                logBuffer.append(event.getMessage());
+            }
+            fullLogBuffer.append(event.getMessage());
+
+        }
+    }
+
+
+}


Property changes on: labs/jbossrules/trunk/drools-ant/src/test/java/org/drools/contrib/BuildFileTest.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:keywords
   + id author date revision
Name: svn:eol-style
   + native

Added: labs/jbossrules/trunk/drools-ant/src/test/java/org/drools/contrib/DroolsAntTaskTest.java
===================================================================
--- labs/jbossrules/trunk/drools-ant/src/test/java/org/drools/contrib/DroolsAntTaskTest.java	2006-12-10 19:57:12 UTC (rev 8184)
+++ labs/jbossrules/trunk/drools-ant/src/test/java/org/drools/contrib/DroolsAntTaskTest.java	2006-12-10 20:14:52 UTC (rev 8185)
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.contrib;
+
+import junit.framework.Assert;
+
+/**
+ * DroolsAntTask test case
+ */
+public class DroolsAntTaskTest extends BuildFileTest {
+
+    public DroolsAntTaskTest() {
+        super("DroolsAntTest");
+    }
+
+    public void setUp() {
+        configureProject("src/test/resources/DroolsAntTask.xml");
+    }
+
+    public void testRules() {
+        try {
+            executeTarget("rules");
+        } catch( Exception e ) {
+            e.printStackTrace();
+            Assert.fail("Should not throw any exception: " + e.getMessage() );
+        }
+    }
+
+    public void tearDown() {
+    }
+    
+}


Property changes on: labs/jbossrules/trunk/drools-ant/src/test/java/org/drools/contrib/DroolsAntTaskTest.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:keywords
   + id author date revision
Name: svn:eol-style
   + native

Added: labs/jbossrules/trunk/drools-ant/src/test/resources/DroolsAntTask.xml
===================================================================
--- labs/jbossrules/trunk/drools-ant/src/test/resources/DroolsAntTask.xml	2006-12-10 19:57:12 UTC (rev 8184)
+++ labs/jbossrules/trunk/drools-ant/src/test/resources/DroolsAntTask.xml	2006-12-10 20:14:52 UTC (rev 8185)
@@ -0,0 +1,28 @@
+<project default="rules">
+	
+	<path id="cheese.classpath">
+		<pathelement location="../../../lib/cheese.jar"/>
+	</path>	
+	
+	<path id="drools.classpath">
+		<fileset dir="../../../../repository">
+			<include name="**/*.jar"/>
+		</fileset>		
+		<pathelement location="target"/>
+	</path>	
+	
+	<taskdef name="rulebase" 
+		classname="org.drools.contrib.DroolsAntTask"
+		classpathref="drools.classpath" />  
+
+ 	
+	<target name="rules" >
+		<rulebase 
+			srcdir="../../../src/test/resources/rules" 		
+			tofile="../../../target/cheese.rules"
+			classpathref="cheese.classpath" > 
+			<include name="*.drl" />
+		</rulebase>
+	</target>
+
+</project>
\ No newline at end of file


Property changes on: labs/jbossrules/trunk/drools-ant/src/test/resources/DroolsAntTask.xml
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:eol-style
   + native

Added: labs/jbossrules/trunk/drools-ant/src/test/resources/rules/cheese.drl
===================================================================
--- labs/jbossrules/trunk/drools-ant/src/test/resources/rules/cheese.drl	2006-12-10 19:57:12 UTC (rev 8184)
+++ labs/jbossrules/trunk/drools-ant/src/test/resources/rules/cheese.drl	2006-12-10 20:14:52 UTC (rev 8185)
@@ -0,0 +1,12 @@
+#created on: Jul 21, 2006
+package org.drools.test;
+
+import org.drools.contrib.Cheese;
+
+rule "Cheese"
+	when
+		Cheese( type == "stilton" )
+	then 
+	    System.out.println("STILTON");
+end
+


Property changes on: labs/jbossrules/trunk/drools-ant/src/test/resources/rules/cheese.drl
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:eol-style
   + native




More information about the jboss-svn-commits mailing list