bodewig 2005/04/14 04:16:55 Added: proposal/sandbox/antunit .cvsignore build.xml proposal/sandbox/antunit/src/etc/testcases assert.xml proposal/sandbox/antunit/src/main/org/apache/ant/antlib/antunit AssertTask.java AssertionFailedException.java antlib.xml proposal/sandbox/antunit/src/testcases/org/apache/ant/antlib/antunit AssertTest.java Log: Some assert tasks Revision Changes Path 1.1 ant/proposal/sandbox/antunit/.cvsignore Index: .cvsignore =================================================================== build 1.1 ant/proposal/sandbox/antunit/build.xml Index: build.xml =================================================================== <?xml version="1.0"?> <!-- Copyright 2005 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. --> <project default="compile"> <target name="setup"> <property name="build" value="build"/> <property name="build.classes" value="${build}/classes"/> <property name="build.testclasses" value="${build}/test-classes"/> <property name="build.lib" value="${build}/lib"/> <property name="jarname" value="${build.lib}/ant-antunit.jar"/> <mkdir dir="${build.classes}"/> <mkdir dir="${build.testclasses}"/> <mkdir dir="${build.lib}"/> </target> <target name="compile" depends="setup"> <javac srcdir="src/main" destdir="${build.classes}" debug="true" /> </target> <target name="antlib" depends="compile"> <copy todir="${build.classes}"> <fileset dir="src/main" includes="**/antlib.xml"/> </copy> <jar destfile="${jarname}" basedir="${build.classes}" /> </target> <target name="setup-for-tests" depends="setup"> <ant antfile="../../../build.xml" target="test-jar" inheritall="false" /> </target> <target name="compile-tests" depends="setup-for-tests, antlib"> <javac srcdir="src/testcases" destdir="${build.testclasses}" debug="true" > <classpath> <pathelement location="${jarname}"/> <pathelement location="../../../build/lib/ant-testutil.jar"/> </classpath> </javac> </target> <target name="test" depends="compile-tests"> <junit printsummary="false" haltonfailure="false" failureproperty="tests.failed" filtertrace="false" fork="true" forkmode="once" > <classpath> <pathelement location="${jarname}"/> <pathelement location="../../../build/lib/ant-testutil.jar"/> <pathelement location="${build.testclasses}"/> </classpath> <batchtest> <fileset dir="src/testcases"/> </batchtest> <formatter type="plain" usefile="false"/> </junit> <fail if="tests.failed">At least one test has failed.</fail> </target> </project> 1.1 ant/proposal/sandbox/antunit/src/etc/testcases/assert.xml Index: assert.xml =================================================================== <?xml version="1.0"?> <!-- Copyright 2005 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. --> <project name="assert-test" default="all" xmlns:au="antlib:org.apache.ant.antlib.antunit"> <target name="all"> <fail>Only run via JUnit</fail> </target> <target name="assertTruePass"> <au:assertTrue> <istrue value="true"/> </au:assertTrue> </target> <target name="assertTrueFail"> <au:assertTrue> <istrue value="false"/> </au:assertTrue> </target> <target name="assertFalsePass"> <au:assertFalse> <isfalse value="true"/> </au:assertFalse> </target> <target name="assertFalseFail"> <au:assertFalse> <isfalse value="false"/> </au:assertFalse> </target> <target name="assertEqualsPass"> <au:assertEquals expected="bar" actual="bar"/> </target> <target name="assertEqualsCasePass"> <au:assertEquals expected="bar" actual="BAR" casesensitive="false"/> </target> <target name="assertEqualsFail1"> <au:assertEquals expected="bar" actual="baz"/> </target> <target name="assertEqualsFail2"> <au:assertEquals expected="bar" actual="BAR"/> </target> <target name="assertPropertySetPass"> <property name="foo" value="bar"/> <au:assertPropertySet name="foo"/> </target> <target name="assertPropertySetFail"> <au:assertPropertySet name="foo"/> </target> <target name="assertPropertyEqualsPass"> <property name="foo" value="bar"/> <au:assertPropertyEquals name="foo" value="bar"/> </target> <target name="assertPropertyEqualsCasePass"> <property name="foo" value="bar"/> <au:assertPropertyEquals name="foo" value="BAR" casesensitive="false"/> </target> <target name="assertPropertyEqualsFail1"> <au:assertPropertyEquals name="foo" value="bar"/> </target> <target name="assertPropertyEqualsFail2"> <property name="foo" value="bar"/> <au:assertPropertyEquals name="foo" value="baz"/> </target> <target name="assertPropertyEqualsFail3"> <property name="foo" value="bar"/> <au:assertPropertyEquals name="foo" value="BAR"/> </target> <target name="assertFileExistsPass"> <au:assertFileExists file="assert.xml"/> </target> <target name="assertFileExistsFail"> <au:assertFileExists file="assert.txt"/> </target> <target name="assertFileDoesntExistPass"> <au:assertFileDoesntExist file="assert.txt"/> </target> <target name="assertFileDoesntExistFail"> <au:assertFileDoesntExist file="assert.xml"/> </target> <target name="assertDestIsUptodatePass"> <au:assertDestIsUptodate src="../../main/org/apache/ant/antlib/antunit/AssertTask.java" dest="../../../build/classes/org/apache/ant/antlib/antunit/AssertTask.class"/> </target> <target name="assertDestIsUptodateFail"> <au:assertDestIsUptodate dest="../../main/org/apache/ant/antlib/antunit/AssertTask.java" src="../../../build/classes/org/apache/ant/antlib/antunit/AssertTask.class"/> </target> <target name="assertDestIsOutofdatePass"> <au:assertDestIsOutofdate dest="../../main/org/apache/ant/antlib/antunit/AssertTask.java" src="../../../build/classes/org/apache/ant/antlib/antunit/AssertTask.class"/> </target> <target name="assertDestIsOutofdateFail"> <au:assertDestIsOutofdate src="../../main/org/apache/ant/antlib/antunit/AssertTask.java" dest="../../../build/classes/org/apache/ant/antlib/antunit/AssertTask.class"/> </target> </project> 1.1 ant/proposal/sandbox/antunit/src/main/org/apache/ant/antlib/antunit/AssertTask.java Index: AssertTask.java =================================================================== /* * Copyright 2005 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.apache.ant.antlib.antunit; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.taskdefs.condition.Condition; import org.apache.tools.ant.taskdefs.condition.ConditionBase; /** * Exits the active build, giving an additional message if the single * nested condition fails. * * <p>This one could as well be implemented as * * <pre> * <macrodef name="assertTrue"> * <attribute name="message" default="Assertion failed"/> * <element name="assertion" implicit="true"/> * <sequential> * <fail message="@{message}"> * <condition> * <assertion/> * </condition> * </fail> * </sequential> * </macrodef> * </pre> * * but wouldn't be able to throw a specialized exception that way - * and the macrodef would nest the exception in yet another * BuildException.</p> */ public class AssertTask extends ConditionBase { private String message = AssertionFailedException.DEFAULT_MESSAGE; public void setMessage(String value) { this.message = value; } public void execute() throws BuildException { int count = countConditions(); if (count > 1) { throw new BuildException("You must not specify more tha one " + "conditions"); } if (count < 1) { throw new BuildException("You must specify a condition"); } if (!((Condition) getConditions().nextElement()).eval()) { throw new AssertionFailedException(message); } } } 1.1 ant/proposal/sandbox/antunit/src/main/org/apache/ant/antlib/antunit/AssertionFailedException.java Index: AssertionFailedException.java =================================================================== /* * Copyright 2005 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.apache.ant.antlib.antunit; import org.apache.tools.ant.BuildException; /** * Specialized BuildException thrown by the AssertTask task. */ public class AssertionFailedException extends BuildException { public static final String DEFAULT_MESSAGE = "Assertion failed"; public AssertionFailedException(String message) { super(message); } } 1.1 ant/proposal/sandbox/antunit/src/main/org/apache/ant/antlib/antunit/antlib.xml Index: antlib.xml =================================================================== <?xml version="1.0"?> <!-- Copyright 2005 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. --> <antlib xmlns:au="antlib:org.apache.ant.antlib.antunit"> <taskdef name="assertTrue" classname="org.apache.ant.antlib.antunit.AssertTask"/> <macrodef name="assertFalse"> <attribute name="message" default="Assertion failed"/> <element name="assertion" implicit="true"/> <sequential> <au:assertTrue message="@{message}"> <not> <assertion/> </not> </au:assertTrue> </sequential> </macrodef> <macrodef name="assertEquals"> <attribute name="expected"/> <attribute name="actual"/> <attribute name="casesensitive" default="true"/> <attribute name="message" default="Expected '@{expected}' but was '@{actual}'"/> <sequential> <au:assertTrue message="@{message}"> <equals arg1="@{expected}" arg2="@{actual}" casesensitive="@{casesensitive}"/> </au:assertTrue> </sequential> </macrodef> <macrodef name="assertPropertySet"> <attribute name="name"/> <attribute name="message" default="Expected property '@{name}'"/> <sequential> <au:assertTrue message="@{message}"> <isset property="@{name}"/> </au:assertTrue> </sequential> </macrodef> <macrodef name="assertPropertyEquals"> <attribute name="name"/> <attribute name="value"/> <attribute name="casesensitive" default="true"/> <attribute name="message" default="Expected property '@{name}' to have value '@{value}' but was '[EMAIL PROTECTED]'"/> <sequential> <au:assertPropertySet message="@{message}" name="@{name}"/> <au:assertEquals message="@{message}" expected="@{value}" actual="[EMAIL PROTECTED]" casesensitive="@{casesensitive}"/> </sequential> </macrodef> <macrodef name="assertFileExists"> <attribute name="file"/> <attribute name="message" default="Expected file '@{file}' to exist"/> <sequential> <au:assertTrue message="@{message}"> <available file="@{file}"/> </au:assertTrue> </sequential> </macrodef> <macrodef name="assertFileDoesntExist"> <attribute name="file"/> <attribute name="message" default="Didn't expect file '@{file}' to exist"/> <sequential> <au:assertFalse message="@{message}"> <available file="@{file}"/> </au:assertFalse> </sequential> </macrodef> <macrodef name="assertDestIsUptodate"> <attribute name="src"/> <attribute name="dest"/> <attribute name="message" default="Expected '@{dest}' to be more recent than '@{src}'"/> <sequential> <au:assertTrue message="@{message}"> <uptodate srcfile="@{src}" targetfile="@{dest}"/> </au:assertTrue> </sequential> </macrodef> <macrodef name="assertDestIsOutofdate"> <attribute name="src"/> <attribute name="dest"/> <attribute name="message" default="Expected '@{src}' to be more recent than '@{dest}'"/> <sequential> <au:assertFalse message="@{message}"> <uptodate srcfile="@{src}" targetfile="@{dest}"/> </au:assertFalse> </sequential> </macrodef> </antlib> 1.1 ant/proposal/sandbox/antunit/src/testcases/org/apache/ant/antlib/antunit/AssertTest.java Index: AssertTest.java =================================================================== /* * Copyright 2005 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.apache.ant.antlib.antunit; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildFileTest; public class AssertTest extends BuildFileTest { public AssertTest(String name) { super(name); } public void setUp() { configureProject("src/etc/testcases/assert.xml"); } public void testTruePass() { testPass("assertTruePass"); } public void testFalsePass() { testPass("assertFalsePass"); } public void testEqualsPass() { testPass("assertEqualsPass"); } public void testEqualsCasePass() { testPass("assertEqualsCasePass"); } public void testPropertySetPass() { testPass("assertPropertySetPass"); } public void testPropertyEqualsPass() { testPass("assertPropertyEqualsPass"); } public void testPropertyEqualsCasePass() { testPass("assertPropertyEqualsCasePass"); } public void testFileExistsPass() { testPass("assertFileExistsPass"); } public void testFileDoesntExistPass() { testPass("assertFileDoesntExistPass"); } public void testDestIsUptodatePass() { testPass("assertDestIsUptodatePass"); } public void testDestIsOutofdatePass() { testPass("assertDestIsOutofdatePass"); } public void testTrueFail() { testFail("assertTrueFail"); } public void testFalseFail() { testFail("assertFalseFail"); } public void testEqualsFail1() { testFail("assertEqualsFail1", "Expected 'bar' but was 'baz'"); } public void testEqualsFail2() { testFail("assertEqualsFail2", "Expected 'bar' but was 'BAR'"); } public void testPropertySetFail() { testFail("assertPropertySetFail", "Expected property 'foo'"); } public void testPropertyEqualsFail1() { testFail("assertPropertyEqualsFail1", "Expected property 'foo' to have value 'bar' but was '${foo}'"); } public void testPropertyEqualsFail2() { testFail("assertPropertyEqualsFail2", "Expected property 'foo' to have value 'baz' but was 'bar'"); } public void testPropertyEqualsFail3() { testFail("assertPropertyEqualsFail3", "Expected property 'foo' to have value 'BAR' but was 'bar'"); } public void testFileExistsFail() { testFail("assertFileExistsFail", "Expected file 'assert.txt' to exist"); } public void testFileDoesntExistFail() { testFail("assertFileDoesntExistFail", "Didn't expect file 'assert.xml' to exist"); } public void testDestIsUptodateFail() { testFail("assertDestIsUptodateFail", "Expected '../../main/org/apache/ant/antlib/antunit/AssertTask.java' to be more recent than '../../../build/classes/org/apache/ant/antlib/antunit/AssertTask.class'"); } public void testDestIsOutofdateFail() { testFail("assertDestIsOutofdateFail", "Expected '../../main/org/apache/ant/antlib/antunit/AssertTask.java' to be more recent than '../../../build/classes/org/apache/ant/antlib/antunit/AssertTask.class'"); } private void testPass(String target) { executeTarget(target); } private void testFail(String target) { testFail(target, "Assertion failed"); } private void testFail(String target, String message) { try { executeTarget(target); fail("Expected failed assetion"); } catch (AssertionFailedException e) { assertEquals(message, e.getMessage()); } catch (BuildException e) { // depending on the number of macrodef indirections, this // can become arbitrarily deep while (true) { Throwable t = e.getCause(); assertNotNull(t); assertTrue("nested is a BuildException", t instanceof BuildException); if (t instanceof AssertionFailedException) { assertEquals(message, e.getMessage()); break; } e = (BuildException) t; } } // end of try-catch } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]