[14627] | 1 | /*
|
---|
| 2 | * Copyright 2003-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.filters;
|
---|
| 19 |
|
---|
| 20 | import java.io.File;
|
---|
| 21 | import java.io.Reader;
|
---|
| 22 | import java.io.FileReader;
|
---|
| 23 | import java.io.IOException;
|
---|
| 24 |
|
---|
| 25 | import org.apache.tools.ant.BuildFileTest;
|
---|
| 26 | import org.apache.tools.ant.util.FileUtils;
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | */
|
---|
| 30 | public class TokenFilterTest extends BuildFileTest {
|
---|
| 31 |
|
---|
| 32 | public TokenFilterTest(String name) {
|
---|
| 33 | super(name);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public void setUp() {
|
---|
| 37 | configureProject("src/etc/testcases/filters/tokenfilter.xml");
|
---|
| 38 | executeTarget("init");
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public void tearDown() {
|
---|
| 42 | executeTarget("cleanup");
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | /** make sure tokenfilter exists */
|
---|
| 46 | public void testTokenfilter() throws IOException {
|
---|
| 47 | executeTarget("tokenfilter");
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public void testTrimignore() throws IOException {
|
---|
| 51 | expectLogContaining("trimignore", "Hello-World");
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public void testStringTokenizer() throws IOException {
|
---|
| 55 | expectLogContaining(
|
---|
| 56 | "stringtokenizer", "#This#is#a#number#of#words#");
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public void testUnixLineOutput() throws IOException {
|
---|
| 60 | expectFileContains(
|
---|
| 61 | "unixlineoutput", "result/unixlineoutput",
|
---|
| 62 | "\nThis\nis\na\nnumber\nof\nwords\n");
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public void testDosLineOutput() throws IOException {
|
---|
| 66 | expectFileContains(
|
---|
| 67 | "doslineoutput", "result/doslineoutput",
|
---|
| 68 | "\r\nThis\r\nis\r\na\r\nnumber\r\nof\r\nwords\r\n");
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public void testFileTokenizer() throws IOException {
|
---|
| 72 | String contents = getFileString(
|
---|
| 73 | "filetokenizer", "result/filetokenizer");
|
---|
| 74 | assertStringContains(contents, " of words");
|
---|
| 75 | assertStringNotContains(contents, " This is");
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public void testReplaceString() throws IOException {
|
---|
| 79 | expectFileContains(
|
---|
| 80 | "replacestring", "result/replacestring",
|
---|
| 81 | "this is the moon");
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | public void testReplaceStrings() throws IOException {
|
---|
| 85 | expectLogContaining("replacestrings", "bar bar bar");
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | public void testContainsString() throws IOException {
|
---|
| 89 | String contents = getFileString(
|
---|
| 90 | "containsstring", "result/containsstring");
|
---|
| 91 | assertStringContains(contents, "this is a line contains foo");
|
---|
| 92 | assertStringNotContains(contents, "this line does not");
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | public void testReplaceRegex() throws IOException {
|
---|
| 96 | if (! hasRegex("testReplaceRegex"))
|
---|
| 97 | return;
|
---|
| 98 | String contents = getFileString(
|
---|
| 99 | "replaceregex", "result/replaceregex");
|
---|
| 100 | assertStringContains(contents, "world world world world");
|
---|
| 101 | assertStringContains(contents, "dog Cat dog");
|
---|
| 102 | assertStringContains(contents, "moon Sun Sun");
|
---|
| 103 | assertStringContains(contents, "found WhiteSpace");
|
---|
| 104 | assertStringContains(contents, "Found digits [1234]");
|
---|
| 105 | assertStringNotContains(contents, "This is a line with digits");
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | public void testFilterReplaceRegex() throws IOException {
|
---|
| 109 | if (! hasRegex("testFilterReplaceRegex"))
|
---|
| 110 | return;
|
---|
| 111 | String contents = getFileString(
|
---|
| 112 | "filterreplaceregex", "result/filterreplaceregex");
|
---|
| 113 | assertStringContains(contents, "world world world world");
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | public void testHandleDollerMatch() throws IOException {
|
---|
| 117 | if (! hasRegex("testFilterReplaceRegex"))
|
---|
| 118 | return;
|
---|
| 119 | executeTarget("dollermatch");
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | public void testTrimFile() throws IOException {
|
---|
| 123 | String contents = getFileString(
|
---|
| 124 | "trimfile", "result/trimfile");
|
---|
| 125 | assertTrue("no ws at start", contents.startsWith("This is th"));
|
---|
| 126 | assertTrue("no ws at end", contents.endsWith("second line."));
|
---|
| 127 | assertStringContains(contents, " This is the second");
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | public void testTrimFileByLine() throws IOException {
|
---|
| 131 | String contents = getFileString(
|
---|
| 132 | "trimfilebyline", "result/trimfilebyline");
|
---|
| 133 | assertFalse("no ws at start", contents.startsWith("This is th"));
|
---|
| 134 | assertFalse("no ws at end", contents.endsWith("second line."));
|
---|
| 135 | assertStringNotContains(contents, " This is the second");
|
---|
| 136 | assertStringContains(contents, "file.\nThis is the second");
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | public void testFilterReplaceString() throws IOException {
|
---|
| 140 | String contents = getFileString(
|
---|
| 141 | "filterreplacestring", "result/filterreplacestring");
|
---|
| 142 | assertStringContains(contents, "This is the moon");
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | public void testFilterReplaceStrings() throws IOException {
|
---|
| 146 | expectLogContaining("filterreplacestrings", "bar bar bar");
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | public void testContainsRegex() throws IOException {
|
---|
| 150 | if (! hasRegex("testContainsRegex"))
|
---|
| 151 | return;
|
---|
| 152 | String contents = getFileString(
|
---|
| 153 | "containsregex", "result/containsregex");
|
---|
| 154 | assertStringContains(contents, "hello world");
|
---|
| 155 | assertStringNotContains(contents, "this is the moon");
|
---|
| 156 | assertStringContains(contents, "World here");
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | public void testFilterContainsRegex() throws IOException {
|
---|
| 160 | if (! hasRegex("testFilterContainsRegex"))
|
---|
| 161 | return;
|
---|
| 162 | String contents = getFileString(
|
---|
| 163 | "filtercontainsregex", "result/filtercontainsregex");
|
---|
| 164 | assertStringContains(contents, "hello world");
|
---|
| 165 | assertStringNotContains(contents, "this is the moon");
|
---|
| 166 | assertStringContains(contents, "World here");
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | public void testContainsRegex2() throws IOException {
|
---|
| 170 | if (! hasRegex("testContainsRegex2"))
|
---|
| 171 | return;
|
---|
| 172 | String contents = getFileString(
|
---|
| 173 | "containsregex2", "result/containsregex2");
|
---|
| 174 | assertStringContains(contents, "void register_bits();");
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | public void testDeleteCharacters() throws IOException {
|
---|
| 178 | String contents = getFileString(
|
---|
| 179 | "deletecharacters", "result/deletechars");
|
---|
| 180 | assertStringNotContains(contents, "#");
|
---|
| 181 | assertStringNotContains(contents, "*");
|
---|
| 182 | assertStringContains(contents, "This is some ");
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | public void testScriptFilter() throws IOException {
|
---|
| 186 | if (! hasScript("testScriptFilter"))
|
---|
| 187 | return;
|
---|
| 188 |
|
---|
| 189 | expectFileContains("scriptfilter", "result/scriptfilter",
|
---|
| 190 | "HELLO WORLD");
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 | public void testScriptFilter2() throws IOException {
|
---|
| 195 | if (! hasScript("testScriptFilter"))
|
---|
| 196 | return;
|
---|
| 197 |
|
---|
| 198 | expectFileContains("scriptfilter2", "result/scriptfilter2",
|
---|
| 199 | "HELLO MOON");
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | public void testCustomTokenFilter() throws IOException {
|
---|
| 203 | expectFileContains("customtokenfilter", "result/custom",
|
---|
| 204 | "Hello World");
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | // ------------------------------------------------------
|
---|
| 208 | // Helper methods
|
---|
| 209 | // -----------------------------------------------------
|
---|
| 210 | private boolean hasScript(String test) {
|
---|
| 211 | try {
|
---|
| 212 | executeTarget("hasscript");
|
---|
| 213 | }
|
---|
| 214 | catch (Throwable ex) {
|
---|
| 215 | System.out.println(
|
---|
| 216 | test + ": skipped - script not present ");
|
---|
| 217 | return false;
|
---|
| 218 | }
|
---|
| 219 | return true;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | private boolean hasRegex(String test) {
|
---|
| 223 | try {
|
---|
| 224 | executeTarget("hasregex");
|
---|
| 225 | expectFileContains("result/replaceregexp", "bye world");
|
---|
| 226 | }
|
---|
| 227 | catch (Throwable ex) {
|
---|
| 228 | System.out.println(test + ": skipped - regex not present "
|
---|
| 229 | + ex);
|
---|
| 230 | return false;
|
---|
| 231 | }
|
---|
| 232 | return true;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | private void assertStringContains(String string, String contains) {
|
---|
| 236 | assertTrue("[" + string + "] does not contain [" + contains +"]",
|
---|
| 237 | string.indexOf(contains) > -1);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | private void assertStringNotContains(String string, String contains) {
|
---|
| 241 | assertTrue("[" + string + "] does contain [" + contains +"]",
|
---|
| 242 | string.indexOf(contains) == -1);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | private String getFileString(String filename)
|
---|
| 246 | throws IOException
|
---|
| 247 | {
|
---|
| 248 | Reader r = null;
|
---|
| 249 | try {
|
---|
| 250 | r = new FileReader(getProject().resolveFile(filename));
|
---|
| 251 | return FileUtils.newFileUtils().readFully(r);
|
---|
| 252 | }
|
---|
| 253 | finally {
|
---|
| 254 | try {r.close();} catch (Throwable ignore) {}
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | private String getFileString(String target, String filename)
|
---|
| 260 | throws IOException
|
---|
| 261 | {
|
---|
| 262 | executeTarget(target);
|
---|
| 263 | return getFileString(filename);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | private void expectFileContains(String name, String contains)
|
---|
| 267 | throws IOException
|
---|
| 268 | {
|
---|
| 269 | String content = getFileString(name);
|
---|
| 270 | assertTrue(
|
---|
| 271 | "expecting file " + name + " to contain " + contains +
|
---|
| 272 | " but got " + content, content.indexOf(contains) > -1);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | private void expectFileContains(
|
---|
| 276 | String target, String name, String contains)
|
---|
| 277 | throws IOException
|
---|
| 278 | {
|
---|
| 279 | executeTarget(target);
|
---|
| 280 | expectFileContains(name, contains);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | public static class Capitalize
|
---|
| 284 | implements TokenFilter.Filter
|
---|
| 285 | {
|
---|
| 286 | public String filter(String token) {
|
---|
| 287 | if (token.length() == 0)
|
---|
| 288 | return token;
|
---|
| 289 | return token.substring(0, 1).toUpperCase() +
|
---|
| 290 | token.substring(1);
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | }
|
---|