KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > selectors > DepthSelectorTest


1 /*
2  * Copyright 2000-2002,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.types.selectors;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.Project;
22 import org.apache.tools.ant.util.*;
23 import org.apache.tools.ant.BuildFileTest;
24 import org.apache.tools.ant.types.Parameter;
25
26 import junit.framework.TestCase;
27 import junit.framework.AssertionFailedError;
28
29 /**
30  * Tests Depth Selectors
31  *
32  */

33 public class DepthSelectorTest extends BaseSelectorTest {
34
35     private Project project;
36
37     public DepthSelectorTest(String JavaDoc name) {
38         super(name);
39     }
40
41     /**
42      * Factory method from base class. This is overriden in child
43      * classes to return a specific Selector class.
44      */

45     public BaseSelector getInstance() {
46         return new DepthSelector();
47     }
48
49     /**
50      * Test the code that validates the selector.
51      */

52     public void testValidate() {
53         DepthSelector s = (DepthSelector)getInstance();
54         try {
55             s.isSelected(basedir,filenames[0],files[0]);
56             fail("DepthSelector did not check for required fields");
57         } catch (BuildException be1) {
58             assertEquals("You must set at least one of the min or the " +
59                     "max levels.", be1.getMessage());
60         }
61
62         s = (DepthSelector)getInstance();
63         s.setMin(5);
64         s.setMax(2);
65         try {
66             s.isSelected(basedir,filenames[0],files[0]);
67             fail("DepthSelector did not check for maximum being higher "
68                     + "than minimum");
69         } catch (BuildException be2) {
70             assertEquals("The maximum depth is lower than the minimum.",
71                     be2.getMessage());
72         }
73
74         s = (DepthSelector)getInstance();
75         Parameter param = new Parameter();
76         param.setName("garbage in");
77         param.setValue("garbage out");
78         Parameter[] params = new Parameter[1];
79         params[0] = param;
80         s.setParameters(params);
81         try {
82             s.isSelected(basedir,filenames[0],files[0]);
83             fail("DepthSelector did not check for valid parameter element");
84         } catch (BuildException be3) {
85             assertEquals("Invalid parameter garbage in", be3.getMessage());
86         }
87
88         s = (DepthSelector)getInstance();
89         param = new Parameter();
90         param.setName("min");
91         param.setValue("garbage out");
92         params[0] = param;
93         s.setParameters(params);
94         try {
95             s.isSelected(basedir,filenames[0],files[0]);
96             fail("DepthSelector accepted bad minimum as parameter");
97         } catch (BuildException be4) {
98             assertEquals("Invalid minimum value garbage out",
99                     be4.getMessage());
100         }
101
102         s = (DepthSelector)getInstance();
103         param = new Parameter();
104         param.setName("max");
105         param.setValue("garbage out");
106         params[0] = param;
107         s.setParameters(params);
108         try {
109             s.isSelected(basedir,filenames[0],files[0]);
110             fail("DepthSelector accepted bad maximum as parameter");
111         } catch (BuildException be5) {
112             assertEquals("Invalid maximum value garbage out",
113                     be5.getMessage());
114         }
115
116     }
117
118     /**
119      * Tests to make sure that the selector is selecting files correctly.
120      */

121     public void testSelectionBehaviour() {
122         DepthSelector s;
123         String JavaDoc results;
124
125         try {
126             makeBed();
127
128             s = (DepthSelector)getInstance();
129             s.setMin(20);
130             s.setMax(25);
131             results = selectionString(s);
132             assertEquals("FFFFFFFFFFFF", results);
133
134             s = (DepthSelector)getInstance();
135             s.setMin(0);
136             results = selectionString(s);
137             assertEquals("TTTTTTTTTTTT", results);
138
139             s = (DepthSelector)getInstance();
140             s.setMin(1);
141             results = selectionString(s);
142             assertEquals("FFFFFTTTTTTT", results);
143
144             s = (DepthSelector)getInstance();
145             s.setMax(0);
146             results = selectionString(s);
147             assertEquals("TTTTTFFFFFFF", results);
148
149             s = (DepthSelector)getInstance();
150             s.setMin(1);
151             s.setMax(1);
152             results = selectionString(s);
153             assertEquals("FFFFFTTTFFFT", results);
154
155         }
156         finally {
157             cleanupBed();
158         }
159
160     }
161
162 }
163
Popular Tags