/*
* The MIT License
*
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Alan Harder
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson;
import hudson.remoting.VirtualChannel;
import hudson.util.IOException2;
import hudson.util.NullStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.NullOutputStream;
import org.jvnet.hudson.test.Bug;
/**
* @author Kohsuke Kawaguchi
*/
public class FilePathTest extends ChannelTestCase {
public void testCopyTo() throws Exception {
File tmp = File.createTempFile("testCopyTo","");
FilePath f = new FilePath(french,tmp.getPath());
f.copyTo(new NullStream());
assertTrue("target does not exist", tmp.exists());
assertTrue("could not delete target " + tmp.getPath(), tmp.delete());
}
/**
* An attempt to reproduce the file descriptor leak.
* If this operation leaks a file descriptor, 2500 should be enough, I think.
*/
public void testCopyTo2() throws Exception {
for (int j=0; j<2500; j++) {
File tmp = File.createTempFile("testCopyFrom","");
FilePath f = new FilePath(tmp);
File tmp2 = File.createTempFile("testCopyTo","");
FilePath f2 = new FilePath(british,tmp2.getPath());
f.copyTo(f2);
f.delete();
f2.delete();
}
}
/**
* As we moved the I/O handling to another thread, there's a race condition in
* {@link FilePath#copyTo(OutputStream)} — this method can return before
* all the writes are delivered to {@link OutputStream}.
*
*
* To reproduce that problem, we use a large number of threads, so that we can
* maximize the chance of out-of-order execution, and make sure we are
* seeing the right byte count at the end.
*
* Also see JENKINS-7897
*/
@Bug(7871)
public void testCopyTo3() throws Exception {
final File tmp = File.createTempFile("testCopyTo3","");
FileOutputStream os = new FileOutputStream(tmp);
final int size = 90000;
byte[] buf = new byte[size];
for (int i=0; i> r = new ArrayList>();
for (int i=0; i<100; i++) {
r.add(es.submit(new Callable