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.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 Filename Selectors
|
---|
31 | *
|
---|
32 | */
|
---|
33 | public class FilenameSelectorTest extends BaseSelectorTest {
|
---|
34 |
|
---|
35 | private Project project;
|
---|
36 |
|
---|
37 | public FilenameSelectorTest(String 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 FilenameSelector();
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Test the code that validates the selector.
|
---|
51 | */
|
---|
52 | public void testValidate() {
|
---|
53 | FilenameSelector s = (FilenameSelector)getInstance();
|
---|
54 | try {
|
---|
55 | s.isSelected(basedir,filenames[0],files[0]);
|
---|
56 | fail("FilenameSelector did not check for required fields");
|
---|
57 | } catch (BuildException be1) {
|
---|
58 | assertEquals("The name attribute is required", be1.getMessage());
|
---|
59 | }
|
---|
60 |
|
---|
61 | s = (FilenameSelector)getInstance();
|
---|
62 | Parameter param = new Parameter();
|
---|
63 | param.setName("garbage in");
|
---|
64 | param.setValue("garbage out");
|
---|
65 | Parameter[] params = {param};
|
---|
66 | s.setParameters(params);
|
---|
67 | try {
|
---|
68 | s.isSelected(basedir,filenames[0],files[0]);
|
---|
69 | fail("FilenameSelector did not check for valid parameter element");
|
---|
70 | } catch (BuildException be2) {
|
---|
71 | assertEquals("Invalid parameter garbage in", be2.getMessage());
|
---|
72 | }
|
---|
73 |
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Tests to make sure that the selector is selecting files correctly.
|
---|
78 | */
|
---|
79 | public void testSelectionBehaviour() {
|
---|
80 | FilenameSelector s;
|
---|
81 | String results;
|
---|
82 |
|
---|
83 | try {
|
---|
84 | makeBed();
|
---|
85 |
|
---|
86 | s = (FilenameSelector)getInstance();
|
---|
87 | s.setName("no match possible");
|
---|
88 | results = selectionString(s);
|
---|
89 | assertEquals("FFFFFFFFFFFF", results);
|
---|
90 |
|
---|
91 | s = (FilenameSelector)getInstance();
|
---|
92 | s.setName("*.gz");
|
---|
93 | results = selectionString(s);
|
---|
94 | // This is turned off temporarily. There appears to be a bug
|
---|
95 | // in SelectorUtils.matchPattern() where it is recursive on
|
---|
96 | // Windows even if no ** is in pattern.
|
---|
97 | //assertEquals("FFFTFFFFFFFF", results); // Unix
|
---|
98 | // vs
|
---|
99 | //assertEquals("FFFTFFFFTFFF", results); // Windows
|
---|
100 |
|
---|
101 | s = (FilenameSelector)getInstance();
|
---|
102 | s.setName("**/*.gz");
|
---|
103 | s.setNegate(true);
|
---|
104 | results = selectionString(s);
|
---|
105 | assertEquals("TTTFTTTFFTTT", results);
|
---|
106 |
|
---|
107 | s = (FilenameSelector)getInstance();
|
---|
108 | s.setName("**/*.GZ");
|
---|
109 | s.setCasesensitive(false);
|
---|
110 | results = selectionString(s);
|
---|
111 | assertEquals("FFFTFFFTTFFF", results);
|
---|
112 |
|
---|
113 | s = (FilenameSelector)getInstance();
|
---|
114 | Parameter param1 = new Parameter();
|
---|
115 | param1.setName("name");
|
---|
116 | param1.setValue("**/*.bz2");
|
---|
117 | Parameter[] params = {param1};
|
---|
118 | s.setParameters(params);
|
---|
119 | results = selectionString(s);
|
---|
120 | assertEquals("FFTFFFFFFTTF", results);
|
---|
121 |
|
---|
122 | }
|
---|
123 | finally {
|
---|
124 | cleanupBed();
|
---|
125 | }
|
---|
126 |
|
---|
127 | }
|
---|
128 |
|
---|
129 | }
|
---|