KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > FailTest


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.tools.ant.taskdefs;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.BuildFileTest;
22
23 /**
24  */

25 public class FailTest extends BuildFileTest {
26
27     public FailTest(String JavaDoc name) {
28         super(name);
29     }
30
31     public void setUp() {
32         configureProject("src/etc/testcases/taskdefs/fail.xml");
33     }
34
35     public void test1() {
36         expectBuildExceptionContaining("test1",
37                 "it is required to fail :-)",
38                 "No message");
39     }
40
41     public void test2() {
42         expectSpecificBuildException("test2",
43             "it is required to fail :-)",
44             "test2");
45     }
46
47     public void testText() {
48         expectSpecificBuildException("testText",
49             "it is required to fail :-)",
50             "testText");
51     }
52
53     public void testIf() {
54         try {
55             executeTarget("testIf");
56         } catch (BuildException be) {
57             fail("foo has not been defined, testIf must not fail");
58         }
59         project.setProperty("foo", "");
60         expectBuildException("testIf", "testIf must fail if foo has been set");
61     }
62
63     public void testUnless() {
64         expectBuildException("testUnless",
65                              "testUnless must fail unless foo has been set");
66         project.setProperty("foo", "");
67         try {
68             executeTarget("testUnless");
69         } catch (BuildException be) {
70             fail("foo has been defined, testUnless must not fail");
71         }
72     }
73
74     /**
75      * see that the different combinations work, and
76      * that the autogenerated text contains information
77      * about which condition was not met
78      */

79     public void testIfAndUnless() {
80         //neither
81
executeTarget("testIfAndUnless");
82         project.setProperty("if", "");
83         expectBuildExceptionContaining("testIfAndUnless",
84                 "expect fail on defined(if)",
85                 "if=if and unless=unless");
86         project.setProperty("unless", "");
87         //this call should succeed as unless overrides if
88
executeTarget("testIfAndUnless");
89     }
90     /**
91      * see that the different combinations work, and
92      * that the autogenerated text contains information
93      * about which condition was not met
94      */

95     public void testIfAndUnless2() {
96         project.setProperty("unless", "");
97         try {
98             executeTarget("testIfAndUnless");
99         } catch (BuildException be) {
100             fail("defined(if) && !defined(unless); testIfAndUnless must not fail");
101         }
102     }
103
104     public void testNested1() {
105         expectSpecificBuildException("testNested1",
106             "it is required to fail :-)",
107             "condition satisfied");
108     }
109
110     public void testNested2() {
111         try {
112             executeTarget("testNested2");
113         } catch (BuildException be) {
114             fail("condition not satisfied; testNested2 must not fail");
115         }
116     }
117
118     public void testNested3() {
119         expectSpecificBuildException("testNested3",
120             "it is required to fail :-)",
121             "testNested3");
122     }
123
124     public void testNested4() {
125         String JavaDoc specificMessage = "Nested conditions "
126           + "not permitted in conjunction with if/unless attributes";
127
128         char[] c = {'a', 'b', 'c'};
129         StringBuffer JavaDoc target = new StringBuffer JavaDoc("testNested4x");
130
131         for (int i = 0; i < c.length; i++) {
132             target.setCharAt(target.length() - 1, c[i]);
133             expectSpecificBuildException(target.toString(),
134                 "it is required to fail :-)", specificMessage);
135         }
136     }
137
138     public void testNested5() {
139         expectSpecificBuildException("testNested5",
140             "it is required to fail :-)",
141             "Only one nested condition is allowed.");
142     }
143
144     public void testNested6() {
145         expectSpecificBuildException("testNested6",
146             "it is required to fail :-)",
147             "testNested6\ntestNested6\ntestNested6");
148     }
149
150     public void testNested7() {
151         String JavaDoc specificMessage = "A single nested condition is required.";
152
153         char[] c = {'a', 'b'};
154         StringBuffer JavaDoc target = new StringBuffer JavaDoc("testNested7x");
155
156         for (int i = 0; i < c.length; i++) {
157             target.setCharAt(target.length() - 1, c[i]);
158             expectSpecificBuildException(target.toString(),
159                 "it is required to fail :-)", specificMessage);
160         }
161     }
162
163 }
164
Popular Tags