Skip to content

Commit

Permalink
NbTestCase: use Files#delete for better exception on failure,
Browse files Browse the repository at this point in the history
 - "Cannot delete file" isn't very useful, Files#delete will state
   the cause if available
 - might help to debug some sporadic test failures
 - global refactoring to replace similar patterns resulted in some
   TestUtil/TestFileUtils cleanup which was duplicated everywhere
  • Loading branch information
mbien committed May 1, 2024
1 parent 5559d21 commit cbd9644
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 765 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public static void makePlatform(File d, String harnessSpecVersion) throws IOExce

@Deprecated
public static void delete(File f) throws IOException {
TestUtil.delete(f);
TestFileUtils.deleteFile(f);
}

private static File getTestNBRoot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,6 @@ public static void createJar(File jar, Map<String,String> contents, Manifest man
}
}

public static void delete(File f) throws IOException {
if (f.isDirectory()) {
File[] kids = f.listFiles();
for (int i = 0; i < kids.length; i++) {
delete(kids[i]);
}
}
if (!f.delete()) {
throw new IOException("Could not delete " + f);
}
}

/** @deprecated Use {@link TestFileUtils#writeFile} instead. */
@Deprecated
public static void dump(FileObject f, String contents) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.openide.filesystems.Repository;
import org.openide.filesystems.URLMapper;
import org.openide.util.Lookup;
import org.openide.util.test.TestFileUtils;

/**
* Help set up org.netbeans.api.project.*Test.
Expand Down Expand Up @@ -96,18 +97,7 @@ public static FileObject makeScratchDir(NbTestCase test) throws IOException {
* Delete a file and all subfiles.
*/
public static void deleteRec(File f) throws IOException {
if (f.isDirectory()) {
File[] kids = f.listFiles();
if (kids == null) {
throw new IOException("List " + f);
}
for (int i = 0; i < kids.length; i++) {
deleteRec(kids[i]);
}
}
if (!f.delete()) {
throw new IOException("Delete " + f);
}
TestFileUtils.deleteFile(f);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.openide.filesystems.URLMapper;
import org.openide.util.Lookup;
import org.openide.util.test.MockLookup;
import org.openide.util.test.TestFileUtils;

/**
* Help set up org.netbeans.api.project.*Test.
Expand Down Expand Up @@ -168,18 +169,7 @@ public static FileObject makeScratchDir(NbTestCase test) throws IOException {
* Delete a file and all subfiles.
*/
public static void deleteRec(File f) throws IOException {
if (f.isDirectory()) {
File[] kids = f.listFiles();
if (kids == null) {
throw new IOException("List " + f);
}
for (int i = 0; i < kids.length; i++) {
deleteRec(kids[i]);
}
}
if (!f.delete()) {
throw new IOException("Delete " + f);
}
TestFileUtils.deleteFile(f);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.WeakHashMap;
Expand All @@ -43,6 +44,7 @@
import org.openide.filesystems.URLMapper;
import org.openide.util.Lookup;
import org.openide.util.lookup.ProxyLookup;
import org.openide.util.test.TestFileUtils;

/**
* Help set up org.netbeans.api.project.*Test.
Expand Down Expand Up @@ -92,18 +94,7 @@ public static FileObject makeScratchDir(NbTestCase test) throws IOException {
* Delete a file and all subfiles.
*/
public static void deleteRec(File f) throws IOException {
if (f.isDirectory()) {
File[] kids = f.listFiles();
if (kids == null) {
throw new IOException("List " + f);
}
for (int i = 0; i < kids.length; i++) {
deleteRec(kids[i]);
}
}
if (!f.delete()) {
throw new IOException("Delete " + f);
}
TestFileUtils.deleteFile(f);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.netbeans.modules.web.core;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -32,18 +31,10 @@
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.modules.j2ee.metadata.model.support.JavaSourceTestCase;
import org.netbeans.modules.j2ee.metadata.model.support.TestUtilities;
import org.netbeans.modules.j2ee.core.api.support.java.SourceUtils;
import static org.netbeans.api.java.source.JavaSource.Phase;
import org.netbeans.modules.parsing.api.indexing.IndexingManager;



import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;

import org.netbeans.junit.NbTestCase;

import org.netbeans.modules.web.core.test.TestUtil;

/**
*
Expand All @@ -52,7 +43,6 @@
*/
public class WebInjectionTargetQueryImplementationTest extends JavaSourceTestCase {

private String serverID;
private FileObject ordinaryClass;
private FileObject fileSubclass;
private FileObject directServletSubclass;
Expand Down Expand Up @@ -144,8 +134,8 @@ protected void setUp() throws Exception {

}

@Override
protected void tearDown() {
serverID = null;
ordinaryClass = null;
fileSubclass = null;
directServletSubclass = null;
Expand Down
Loading

0 comments on commit cbd9644

Please sign in to comment.