aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/tests/antunit/taskdefs/copy-test.xml
blob: 51c6277c1971fb85605c16c09f736fa05166d7f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?xml version="1.0"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<project default="antunit" xmlns:au="antlib:org.apache.ant.antunit">
  <import file="../antunit-base.xml" />

  <target name="-fileResourceSetup">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <touch file="${input}/file.txt"/>
  </target>

  <target name="testCopyFileFlatten" depends="-fileResourceSetup">
    <au:assertFileDoesntExist file="${output}/file.txt"/>
    <copy todir="${output}" flatten="true">
      <file file="${input}/file.txt"/>
    </copy>
    <au:assertFileExists file="${output}/file.txt"/>
  </target>

  <target name="testCopyFileInResourcesFlatten" depends="-fileResourceSetup">
    <au:assertFileDoesntExist file="${output}/file.txt"/>
    <copy todir="${output}" flatten="true">
      <resources>
        <file file="${input}/file.txt"/>
      </resources>
    </copy>
    <au:assertFileExists file="${output}/file.txt"/>
  </target>

  <target name="-setupNullByteStreamResource">
    <mkdir dir="${input}"/>
    <echo file="${input}/NullByteStreamResource.java"><![CDATA[
import org.apache.tools.ant.types.Resource;
import java.io.*;
public class NullByteStreamResource extends Resource {
    private long length = 1024;

    public boolean isExists() {
        return true;
    }

    public long getLastModified() {
        return UNKNOWN_DATETIME;
    }

    public void setLength(long length) {
        this.length = length;
    }

    public InputStream getInputStream() {
        return new InputStream() {
            int readSoFar = 0;

            public int read() {
                return readSoFar++ > length ? -1 : 0;
            }
        };
    }
}
]]></echo>
    <mkdir dir="${output}"/>
    <javac srcdir="${input}" destdir="${output}"/>
    <typedef name="nullstream" classname="NullByteStreamResource">
      <classpath>
        <pathelement location="${output}"/>
      </classpath>
    </typedef>
  </target>

  <target name="testResourceWithoutName"
          depends="-setupNullByteStreamResource">
    <au:expectfailure>
      <copy todir="${output}">
        <nullstream/>
      </copy>
    </au:expectfailure>
  </target>

  <target name="testResourceWithoutNameWithMergeMapper"
          depends="-setupNullByteStreamResource">
    <copy todir="${output}">
      <nullstream/>
      <mergemapper to="foo"/>
    </copy>
    <au:assertFileExists file="${output}/foo"/>
  </target>

  <target name="testMappedResources">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <echo file="${input}/foo.txt">Hello, world!</echo>
    <copy todir="${output}">
      <mappedresources>
        <fileset dir="${input}"/>
        <globmapper from="foo.*" to="bar.*"/>
      </mappedresources>
    </copy>
    <au:assertFileDoesntExist file="${output}/foo.txt"/>
    <au:assertFileExists file="${output}/bar.txt"/>
    <au:assertFilesMatch expected="${input}/foo.txt"
                         actual="${output}/bar.txt"/>
  </target>

  <target name="testMappedResourcesMultipleTrue">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <echo file="${input}/foo.txt">Hello, world!</echo>
    <copy todir="${output}">
      <mappedresources enableMultipleMappings="true">
        <fileset dir="${input}"/>
        <compositemapper>
          <globmapper from="foo.*" to="bar.*"/>
          <globmapper from="foo.*" to="baz.*"/>
        </compositemapper>
      </mappedresources>
    </copy>
    <au:assertFileDoesntExist file="${output}/foo.txt"/>
    <au:assertFileExists file="${output}/bar.txt"/>
    <au:assertFileExists file="${output}/baz.txt"/>
    <au:assertFilesMatch expected="${input}/foo.txt"
                         actual="${output}/bar.txt"/>
    <au:assertFilesMatch expected="${input}/foo.txt"
                         actual="${output}/baz.txt"/>
  </target>

  <target name="testMappedResourcesMultipleFalse">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <echo file="${input}/foo.txt">Hello, world!</echo>
    <copy todir="${output}">
      <mappedresources enableMultipleMappings="false">
        <fileset dir="${input}"/>
        <compositemapper>
          <globmapper from="foo.*" to="bar.*"/>
          <globmapper from="foo.*" to="baz.*"/>
        </compositemapper>
      </mappedresources>
    </copy>
    <au:assertFileDoesntExist file="${output}/foo.txt"/>
    <au:assertFileExists file="${output}/bar.txt"/>
    <au:assertFileDoesntExist file="${output}/baz.txt"/>
    <au:assertFilesMatch expected="${input}/foo.txt"
                         actual="${output}/bar.txt"/>
  </target>

  <target name="testIncludeEmptyDirsDefaultsToTrue"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=47168">
    <mkdir dir="${input}/foo"/>
    <mkdir dir="${output}"/>
    <copy todir="${output}">
      <fileset dir="${input}"/>
    </copy>
    <au:assertFileExists file="${output}/foo"/>
  </target>

  <target name="XtestIncludeEmptyDirsAndZipfileset"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=47168">
    <mkdir dir="${input}/foo"/>
    <mkdir dir="${output}/final"/>
    <zip destfile="${output}/zipfile.zip">
      <fileset dir="${input}"/>
    </zip>
    <copy todir="${output}/final">
      <zipfileset src="${output}/zipfile.zip"/>
    </copy>
    <au:assertFileExists file="${output}/final/foo"/>
  </target>

  <target name="testFailOnURLConnectionError"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=47362">
    <mkdir dir="${output}"/>
    <au:expectfailure>
      <copy todir="${output}" failonerror="true" flatten="true">
        <resources>
          <url url="http://i-do-not-exist/"/>
        </resources>
      </copy>
    </au:expectfailure>
  </target>

  <target name="testNotModifiedSelector"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=43574"
          >
    <mkdir dir="${input}/images"/>
    <mkdir dir="${input}/cache"/>
    <touch file="${input}/images/foo.jpg"/>
    <mkdir dir="${output}"/>
    <selector id="cache.selector">
      <not>
        <modified update="true"
                  seldirs="false"
                  cache="propertyfile"
                  algorithm="digest"
                  comparator="equal">
          <param name="cache.cachefile"
                 value="${input}/cache/cache.properties"/>
          <param name="algorithm.algorithm" value="MD5"/>
        </modified>
      </not>
    </selector>
    <au:assertFileDoesntExist file="${input}/cache/cache.properties"/>
    <copy todir="${output}" overwrite="true">
      <fileset dir="${input}/images">
        <include name="*.jpg" />
        <selector refid="cache.selector" />
      </fileset>
    </copy>
    <au:assertFileExists file="${input}/cache/cache.properties"/>
    <au:assertFileDoesntExist file="${output}/foo.jpg"/>
    <copy todir="${output}" overwrite="true">
      <fileset dir="${input}/images">
        <include name="*.jpg" />
        <selector refid="cache.selector" />
      </fileset>
    </copy>
    <au:assertFileExists file="${output}/foo.jpg"/>
  </target>

  <target name="testMissingFileUsingFileAttribute">
    <mkdir dir="${output}"/>
    <mkdir dir="${input}"/>
    <au:expectfailure expectedMessage="Could not find file">
      <copy file="${input}/not-there.txt" todir="${output}"/>
    </au:expectfailure>
    <copy file="${input}/not-there.txt" todir="${output}"
          failonerror="false"/>
  </target>

  <target name="testQuiet">
    <mkdir dir="${output}"/>
    <mkdir dir="${input}"/>
    <copy file="${input}/not-there.txt" todir="${output}" failonerror="false" quiet="true" />
  	<au:assertLogDoesntContain text="Could not find file" />
  </target>

  <target name="testMissingFilesetRoot">
    <mkdir dir="${output}"/>
    <au:expectfailure expectedMessage="does not exist">
      <copy todir="${output}">
        <fileset dir="${input}">
          <include name="not-there.txt"/>
        </fileset>
      </copy>
    </au:expectfailure>
    <copy todir="${output}" failonerror="false">
      <fileset dir="${input}">
        <include name="not-there.txt"/>
      </fileset>
    </copy>
  </target>

  <target name="testMissingFileUsingFilesetInclude"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=49070">
    <mkdir dir="${output}"/>
    <mkdir dir="${input}"/>
    <au:expectfailure
       expectedMessage="Cannot perform operation from directory to file.">
      <copy tofile="${output}/foo.txt">
        <fileset dir="${input}">
          <include name="not-there.txt"/>
        </fileset>
      </copy>
    </au:expectfailure>
    <copy tofile="${output}/foo.txt" failonerror="false">
      <fileset dir="${input}">
        <include name="not-there.txt"/>
      </fileset>
    </copy>
  </target>

  <target name="testMissingFileUsingFilesetFilename"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=49070">
    <mkdir dir="${output}"/>
    <mkdir dir="${input}"/>
    <au:expectfailure
       expectedMessage="Cannot perform operation from directory to file.">
      <copy tofile="${output}/foo.txt">
        <fileset dir="${input}">
          <filename name="not-there.txt"/>
        </fileset>
      </copy>
    </au:expectfailure>
    <copy  tofile="${output}/foo.txt" failonerror="false">
      <fileset dir="${input}">
        <filename name="not-there.txt"/>
      </fileset>
    </copy>
  </target>

  <!-- stolen from ../types/readwrite-test.xml - create a read-only file -->
  <property name="file" value="testfile"/>
  <condition property="unix">
    <os family="unix"/>
  </condition>
  <target name="createTestdir">
    <mkdir dir="${output}"/>
    <mkdir dir="${input}"/>
    <touch file="${output}/${file}"/>
  </target>
  <target name="makeFileUnwritable"
          depends="createTestdir,makeFileUnwritable-Unix,makeFileUnwritable-Windows"/>
  <target name="makeFileUnwritable-Unix" id="unix">
    <chmod file="${output}/${file}" perm="444"/>
  </target>
  <target name="makeFileUnwritable-Windows" unless="unix">
    <attrib file="${output}/${file}" readonly="true"/>
  </target>

  <target name="testCopyOverReadOnlyFile" depends="makeFileUnwritable">
    <sleep seconds="2"/>
    <touch file="${input}/${file}"/>
    <au:expectfailure
        expectedMessage="can't write to read-only destination file ">
      <copy toDir="${output}">
        <fileset dir="${input}"/>
      </copy>
    </au:expectfailure>
  </target>

  <target name="testFilteredCopyOverReadOnlyFile" depends="makeFileUnwritable">
    <sleep seconds="2"/>
    <touch file="${input}/${file}"/>
    <au:expectfailure
        expectedMessage="can't write to read-only destination file ">
      <copy toDir="${output}">
        <fileset dir="${input}"/>
        <filterset>
          <filter token="foo" value="bar"/>
        </filterset>
      </copy>
    </au:expectfailure>
  </target>

  <target name="testCopyOverReadOnlyFileWithOverwrite"
          depends="makeFileUnwritable">
    <touch file="${input}/${file}"/>
    <au:expectfailure
        expectedMessage="can't write to read-only destination file ">
      <copy toDir="${output}" overwrite="true">
        <fileset dir="${input}"/>
      </copy>
    </au:expectfailure>
  </target>

  <target name="testFilteredCopyOverReadOnlyFileWithOverwrite"
          depends="makeFileUnwritable">
    <touch file="${input}/${file}"/>
    <au:expectfailure
        expectedMessage="can't write to read-only destination file ">
      <copy toDir="${output}" overwrite="true">
        <fileset dir="${input}"/>
        <filterset>
          <filter token="foo" value="bar"/>
        </filterset>
      </copy>
    </au:expectfailure>
  </target>

  <target name="testForcedCopyOverReadOnlyFile" depends="makeFileUnwritable">
    <sleep seconds="2"/>
    <touch file="${input}/${file}"/>
    <copy toDir="${output}" force="true">
      <fileset dir="${input}"/>
    </copy>
  </target>

  <target name="testForcedFilteredCopyOverReadOnlyFile"
          depends="makeFileUnwritable">
    <sleep seconds="2"/>
    <touch file="${input}/${file}"/>
    <copy toDir="${output}" force="true">
      <fileset dir="${input}"/>
      <filterset>
        <filter token="foo" value="bar"/>
      </filterset>
    </copy>
  </target>

  <target name="testForcedCopyOverReadOnlyFileWithOverwrite"
          depends="makeFileUnwritable">
    <touch file="${input}/${file}"/>
    <copy toDir="${output}" overwrite="true" force="true">
      <fileset dir="${input}"/>
    </copy>
  </target>

  <target name="testForcedFilteredCopyOverReadOnlyFileWithOverwrite"
          depends="makeFileUnwritable">
    <touch file="${input}/${file}"/>
    <copy toDir="${output}" overwrite="true" force="true">
      <fileset dir="${input}"/>
      <filterset>
        <filter token="foo" value="bar"/>
      </filterset>
    </copy>
  </target>

  <target name="testCopyWithResourceAndFile"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=49756"
          >
    <mkdir dir="${input}"/>
    <au:assertFileDoesntExist file="${input}/somefile"/>
    <copy tofile="${input}/somefile">
      <first>
        <union>
          <restrict>
            <exists/>
            <fileset file="${input}/somefile"/>
          </restrict>
          <string value="default contents"/>
        </union>
      </first>
    </copy>
    <au:assertFileExists file="${input}/somefile"/>
    <au:assertResourceContains resource="${input}/somefile"
                               value="default contents"/>
    <delete file="${input}/somefile"/>
    <touch file="${input}/somefile"/>
    <copy tofile="${input}/somefile">
      <first>
        <union>
          <restrict>
            <exists/>
            <fileset file="${input}/somefile"/>
          </restrict>
          <string value="default contents"/>
        </union>
      </first>
    </copy>
    <au:assertFileExists file="${input}/somefile"/>
    <au:assertResourceDoesntContain resource="${input}/somefile"
                                    value="default contents"/>
  </target>

  <target name="testCopyDoesntDeleteReadonlyTargetWhenCopyFails"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=53095">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <touch file="${input}/somefile"/>
    <touch file="${output}/somefile"/>
    <exec executable="chmod" osfamily="unix">
      <arg value="-w"/>
      <arg file="${output}/somefile"/>
    </exec>
    <exec executable="attrib" osfamily="dos">
      <arg value="+r"/>
      <arg file="${output}/somefile"/>
    </exec>
    <au:expectfailure>
      <copy todir="${output}" file="${input}/somefile"
            overwrite="true"/>
    </au:expectfailure>
    <au:assertFileExists file="${input}/somefile"/>
    <au:assertFileExists file="${output}/somefile"/>
  </target>
</project>