Modified attempt to delete files on Windows without failing the test.

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2935 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Simone Bordet 2011-03-30 13:51:50 +00:00
parent e9e7a72b08
commit da0160029d
1 changed files with 250 additions and 235 deletions

View File

@ -1,11 +1,8 @@
package org.eclipse.jetty.servlet; package org.eclipse.jetty.servlet;
import static org.junit.Assert.assertTrue;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import javax.servlet.Filter; import javax.servlet.Filter;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
import javax.servlet.FilterConfig; import javax.servlet.FilterConfig;
@ -14,7 +11,6 @@ import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
import org.eclipse.jetty.server.LocalConnector; import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.toolchain.test.FS; import org.eclipse.jetty.toolchain.test.FS;
@ -29,6 +25,8 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class DefaultServletTest public class DefaultServletTest
{ {
@Rule @Rule
@ -113,7 +111,8 @@ public class DefaultServletTest
assertTrue(new File(resBase, "one").mkdir()); assertTrue(new File(resBase, "one").mkdir());
assertTrue(new File(resBase, "two").mkdir()); assertTrue(new File(resBase, "two").mkdir());
assertTrue(new File(resBase, "three").mkdir()); assertTrue(new File(resBase, "three").mkdir());
if ( !OS.IS_WINDOWS ) { if (!OS.IS_WINDOWS)
{
assertTrue("Creating dir 'f??r' (Might not work in Windows)", new File(resBase, "f??r").mkdir()); assertTrue("Creating dir 'f??r' (Might not work in Windows)", new File(resBase, "f??r").mkdir());
} }
@ -134,7 +133,8 @@ public class DefaultServletTest
assertResponseContains("/one/", response); assertResponseContains("/one/", response);
assertResponseContains("/two/", response); assertResponseContains("/two/", response);
assertResponseContains("/three/", response); assertResponseContains("/three/", response);
if ( !OS.IS_WINDOWS ) { if (!OS.IS_WINDOWS)
{
assertResponseContains("/f%3F%3Fr", response); assertResponseContains("/f%3F%3Fr", response);
} }
@ -211,7 +211,8 @@ public class DefaultServletTest
createFile(index, "<h1>Hello Index</h1>"); createFile(index, "<h1>Hello Index</h1>");
File wackyDir = new File(resBase, "dir?"); File wackyDir = new File(resBase, "dir?");
if ( !OS.IS_WINDOWS ) { if (!OS.IS_WINDOWS)
{
FS.ensureDirExists(wackyDir); FS.ensureDirExists(wackyDir);
} }
@ -584,11 +585,14 @@ public class DefaultServletTest
private void createFile(File file, String str) throws IOException private void createFile(File file, String str) throws IOException
{ {
FileOutputStream out = null; FileOutputStream out = null;
try { try
{
out = new FileOutputStream(file); out = new FileOutputStream(file);
out.write(str.getBytes(StringUtil.__UTF8)); out.write(str.getBytes(StringUtil.__UTF8));
out.flush(); out.flush();
} finally { }
finally
{
IO.close(out); IO.close(out);
} }
} }
@ -624,14 +628,25 @@ public class DefaultServletTest
return idx; return idx;
} }
private void deleteFile(File file) throws IOException { private void deleteFile(File file) throws IOException
if(OS.IS_WINDOWS) { {
// Since Windows doesn't seem to like to delete content that was recently created. if (OS.IS_WINDOWS)
File deleted = MavenTestingUtils.getTargetFile(".deleted"); {
FS.ensureDirExists(deleted); // Windows doesn't seem to like to delete content that was recently created
File dest = File.createTempFile(file.getName(), "deleted", deleted); // Attempt a delete and if it fails, attempt a rename
Assert.assertTrue("Unable to move file out of the way: " + file.getName(), file.renameTo(dest)); boolean deleted = file.delete();
} else { if (!deleted)
{
File deletedDir = MavenTestingUtils.getTargetFile(".deleted");
FS.ensureDirExists(deletedDir);
File dest = File.createTempFile(file.getName(), "deleted", deletedDir);
boolean renamed = file.renameTo(dest);
if (!renamed)
System.err.println("WARNING: unable to move file out of the way: " + file.getName());
}
}
else
{
Assert.assertTrue("Deleting: " + file.getName(), file.delete()); Assert.assertTrue("Deleting: " + file.getName(), file.delete());
} }
} }