001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.BuildFileTest;
023:
024: /**
025: */
026: public class FailTest extends BuildFileTest {
027:
028: public FailTest(String name) {
029: super (name);
030: }
031:
032: public void setUp() {
033: configureProject("src/etc/testcases/taskdefs/fail.xml");
034: }
035:
036: public void test1() {
037: expectBuildExceptionContaining("test1",
038: "it is required to fail :-)", "No message");
039: }
040:
041: public void test2() {
042: expectSpecificBuildException("test2",
043: "it is required to fail :-)", "test2");
044: }
045:
046: public void testText() {
047: expectSpecificBuildException("testText",
048: "it is required to fail :-)", "testText");
049: }
050:
051: public void testIf() {
052: try {
053: executeTarget("testIf");
054: } catch (BuildException be) {
055: fail("foo has not been defined, testIf must not fail");
056: }
057: project.setProperty("foo", "");
058: expectBuildException("testIf",
059: "testIf must fail if foo has been set");
060: }
061:
062: public void testUnless() {
063: expectBuildException("testUnless",
064: "testUnless must fail unless foo has been set");
065: project.setProperty("foo", "");
066: try {
067: executeTarget("testUnless");
068: } catch (BuildException be) {
069: fail("foo has been defined, testUnless must not fail");
070: }
071: }
072:
073: /**
074: * see that the different combinations work, and
075: * that the autogenerated text contains information
076: * about which condition was not met
077: */
078: public void testIfAndUnless() {
079: //neither
080: executeTarget("testIfAndUnless");
081: project.setProperty("if", "");
082: expectBuildExceptionContaining("testIfAndUnless",
083: "expect fail on defined(if)", "if=if and unless=unless");
084: project.setProperty("unless", "");
085: //this call should succeed as unless overrides if
086: executeTarget("testIfAndUnless");
087: }
088:
089: /**
090: * see that the different combinations work, and
091: * that the autogenerated text contains information
092: * about which condition was not met
093: */
094: public void testIfAndUnless2() {
095: project.setProperty("unless", "");
096: try {
097: executeTarget("testIfAndUnless");
098: } catch (BuildException be) {
099: fail("defined(if) && !defined(unless); testIfAndUnless must not fail");
100: }
101: }
102:
103: public void testNested1() {
104: expectSpecificBuildException("testNested1",
105: "it is required to fail :-)", "condition satisfied");
106: }
107:
108: public void testNested2() {
109: try {
110: executeTarget("testNested2");
111: } catch (BuildException be) {
112: fail("condition not satisfied; testNested2 must not fail");
113: }
114: }
115:
116: public void testNested3() {
117: expectSpecificBuildException("testNested3",
118: "it is required to fail :-)", "testNested3");
119: }
120:
121: public void testNested4() {
122: String specificMessage = "Nested conditions "
123: + "not permitted in conjunction with if/unless attributes";
124:
125: char[] c = { 'a', 'b', 'c' };
126: StringBuffer target = new StringBuffer("testNested4x");
127:
128: for (int i = 0; i < c.length; i++) {
129: target.setCharAt(target.length() - 1, c[i]);
130: expectSpecificBuildException(target.toString(),
131: "it is required to fail :-)", specificMessage);
132: }
133: }
134:
135: public void testNested5() {
136: expectSpecificBuildException("testNested5",
137: "it is required to fail :-)",
138: "Only one nested condition is allowed.");
139: }
140:
141: public void testNested6() {
142: expectSpecificBuildException("testNested6",
143: "it is required to fail :-)",
144: "testNested6\ntestNested6\ntestNested6");
145: }
146:
147: public void testNested7() {
148: String specificMessage = "A single nested condition is required.";
149:
150: char[] c = { 'a', 'b' };
151: StringBuffer target = new StringBuffer("testNested7x");
152:
153: for (int i = 0; i < c.length; i++) {
154: target.setCharAt(target.length() - 1, c[i]);
155: expectSpecificBuildException(target.toString(),
156: "it is required to fail :-)", specificMessage);
157: }
158: }
159:
160: }
|