Skip to content

Commit 86f2afd

Browse files
mlbiscocdsmiley
andauthored
SOLR-16903: Migrate all tests using File to NIO Path (#3263)
* Remove unnecessary toAbsolutePath() calls * Remove some callers of FileSystems.getDefault().getSeparator() * Remove Path.toString in where it could easily be avoided. * Use Path.toFile().setReadable in TestCoreDiscovery * Throw AssumptionViolatedException for file permissions on windows Co-authored-by: David Smiley <[email protected]>
1 parent a93c091 commit 86f2afd

File tree

207 files changed

+1867
-1970
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

207 files changed

+1867
-1970
lines changed

Diff for: gradle/testing/randomization/policies/solr-tests.policy

+3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ grant {
198198
permission "java.net.URLPermission" "http://[::1]:*/solr/-", "HEAD,GET,PUT,POST:*";
199199
permission "java.net.URLPermission" "https://[::1]:*/solr/-", "HEAD,GET,PUT,POST:*";
200200
permission "java.net.URLPermission" "socket://[::1]:*", "CONNECT:*";
201+
202+
// Needed by TestCoreDiscovery
203+
permission java.lang.RuntimePermission "accessUserInformation";
201204
};
202205

203206
// additional permissions based on system properties set by /bin/solr

Diff for: solr/benchmark/src/java/org/apache/solr/bench/MiniClusterState.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
import static org.apache.solr.bench.BaseBenchState.log;
2121

2222
import com.codahale.metrics.Meter;
23-
import java.io.File;
2423
import java.io.IOException;
2524
import java.io.OutputStream;
2625
import java.io.PrintStream;
2726
import java.lang.management.ManagementFactory;
2827
import java.net.URL;
2928
import java.nio.charset.StandardCharsets;
29+
import java.nio.file.FileSystems;
3030
import java.nio.file.Files;
3131
import java.nio.file.Path;
3232
import java.nio.file.Paths;
@@ -130,8 +130,7 @@ public void tearDown(BenchmarkParams benchmarkParams) throws Exception {
130130
benchmarkParams.getBenchmark() + ".txt");
131131
Files.createDirectories(metricsResults.getParent());
132132

133-
cluster.dumpMetrics(
134-
metricsResults.getParent().toFile(), metricsResults.getFileName().toString());
133+
cluster.dumpMetrics(metricsResults.getParent(), metricsResults.getFileName().toString());
135134
}
136135

137136
/**
@@ -170,8 +169,14 @@ private void logClusterDirectorySize() throws IOException {
170169
long clusterSize =
171170
Files.walk(node)
172171
.filter(Files::isRegularFile)
173-
.map(Path::toFile)
174-
.mapToLong(File::length)
172+
.mapToLong(
173+
file -> {
174+
try {
175+
return Files.size(file);
176+
} catch (IOException e) {
177+
throw new RuntimeException(e);
178+
}
179+
})
175180
.sum();
176181
log("mini cluster node size (bytes) " + node + " " + clusterSize);
177182
} catch (IOException e) {
@@ -553,7 +558,9 @@ public static ModifiableSolrParams params(ModifiableSolrParams params, String...
553558
*/
554559
public static Path getFile(String name) {
555560
final URL url =
556-
MiniClusterState.class.getClassLoader().getResource(name.replace(File.separatorChar, '/'));
561+
MiniClusterState.class
562+
.getClassLoader()
563+
.getResource(name.replace(FileSystems.getDefault().getSeparator(), "/"));
557564
if (url != null) {
558565
try {
559566
return Path.of(url.toURI());

Diff for: solr/core/src/java/org/apache/solr/blockcache/BlockDirectory.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
*/
1717
package org.apache.solr.blockcache;
1818

19-
import java.io.File;
2019
import java.io.FileNotFoundException;
2120
import java.io.IOException;
2221
import java.lang.invoke.MethodHandles;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
2324
import java.util.Set;
2425
import org.apache.lucene.index.IndexFileNames;
2526
import org.apache.lucene.store.Directory;
@@ -289,12 +290,12 @@ String getFileCacheName(String name) throws IOException {
289290

290291
private long getFileModified(String name) throws IOException {
291292
if (in instanceof FSDirectory) {
292-
File directory = ((FSDirectory) in).getDirectory().toFile();
293-
File file = new File(directory, name);
294-
if (!file.exists()) {
293+
Path directory = ((FSDirectory) in).getDirectory();
294+
Path file = directory.resolve(name);
295+
if (!Files.exists(file)) {
295296
throw new FileNotFoundException("File [" + name + "] not found");
296297
}
297-
return file.lastModified();
298+
return Files.getLastModifiedTime(file).toMillis();
298299
} else {
299300
throw new UnsupportedOperationException();
300301
}

Diff for: solr/core/src/java/org/apache/solr/cli/CreateTool.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
import java.nio.file.Files;
2121
import java.nio.file.Path;
2222
import java.nio.file.Paths;
23+
import java.nio.file.StandardCopyOption;
2324
import java.util.Locale;
2425
import java.util.Set;
2526
import java.util.concurrent.TimeUnit;
2627
import org.apache.commons.cli.CommandLine;
2728
import org.apache.commons.cli.Option;
2829
import org.apache.commons.cli.Options;
29-
import org.apache.commons.io.FileUtils;
3030
import org.apache.commons.io.file.PathUtils;
3131
import org.apache.solr.cli.CommonCLIOptions.DefaultValues;
3232
import org.apache.solr.client.solrj.SolrClient;
@@ -185,7 +185,7 @@ protected void createCore(CommandLine cli, SolrClient solrClient) throws Excepti
185185
"Failed to create new core instance directory: " + coreInstanceDir.toAbsolutePath());
186186
}
187187

188-
FileUtils.copyDirectoryToDirectory(confDir.toFile(), coreInstanceDir.toFile());
188+
PathUtils.copyDirectory(confDir, coreInstanceDir, StandardCopyOption.COPY_ATTRIBUTES);
189189

190190
echoIfVerbose(
191191
"\nCopying configuration to new core instance directory:\n"

Diff for: solr/core/src/test/org/apache/solr/AnalysisAfterCoreReloadTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
2020

21-
import java.io.File;
2221
import java.io.IOException;
2322
import java.nio.charset.StandardCharsets;
2423
import java.nio.file.Files;
2524
import java.nio.file.Path;
26-
import org.apache.commons.io.FileUtils;
25+
import java.nio.file.StandardCopyOption;
26+
import org.apache.commons.io.file.PathUtils;
2727
import org.apache.solr.client.solrj.SolrClient;
2828
import org.apache.solr.client.solrj.SolrQuery;
2929
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
@@ -41,9 +41,9 @@ public class AnalysisAfterCoreReloadTest extends SolrTestCaseJ4 {
4141

4242
@BeforeClass
4343
public static void beforeClass() throws Exception {
44-
String tmpSolrHome = createTempDir().toFile().getAbsolutePath();
45-
FileUtils.copyDirectory(new File(TEST_HOME()), new File(tmpSolrHome).getAbsoluteFile());
46-
initCore("solrconfig.xml", "schema.xml", new File(tmpSolrHome).getAbsolutePath());
44+
Path tmpSolrHome = createTempDir();
45+
PathUtils.copyDirectory(TEST_HOME(), tmpSolrHome, StandardCopyOption.COPY_ATTRIBUTES);
46+
initCore("solrconfig.xml", "schema.xml", tmpSolrHome);
4747
}
4848

4949
@AfterClass

Diff for: solr/core/src/test/org/apache/solr/SolrInfoBeanTest.java

+24-15
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
*/
1717
package org.apache.solr;
1818

19-
import java.io.File;
2019
import java.net.URI;
2120
import java.net.URL;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
2223
import java.util.ArrayList;
2324
import java.util.Enumeration;
2425
import java.util.List;
26+
import java.util.stream.Stream;
2527
import org.apache.lucene.tests.util.TestUtil;
2628
import org.apache.solr.core.SolrInfoBean;
2729
import org.apache.solr.handler.admin.LukeRequestHandler;
@@ -88,30 +90,37 @@ public void testCallMBeanInfo() throws Exception {
8890
}
8991

9092
private static List<Class<?>> getClassesForPackage(String pckgname) throws Exception {
91-
ArrayList<File> directories = new ArrayList<>();
93+
ArrayList<Path> directories = new ArrayList<>();
9294
ClassLoader cld = h.getCore().getResourceLoader().getClassLoader();
9395
String path = pckgname.replace('.', '/');
9496
Enumeration<URL> resources = cld.getResources(path);
9597
while (resources.hasMoreElements()) {
9698
final URI uri = resources.nextElement().toURI();
9799
if (!"file".equalsIgnoreCase(uri.getScheme())) continue;
98-
final File f = new File(uri);
100+
final Path f = Path.of(uri);
99101
directories.add(f);
100102
}
101103

102104
ArrayList<Class<?>> classes = new ArrayList<>();
103-
for (File directory : directories) {
104-
if (directory.exists()) {
105-
String[] files = directory.list();
106-
for (String file : files) {
107-
if (file.endsWith(".class")) {
108-
String clazzName = file.substring(0, file.length() - 6);
109-
// exclude Test classes that happen to be in these packages.
110-
// class.ForName'ing some of them can cause trouble.
111-
if (!clazzName.endsWith("Test") && !clazzName.startsWith("Test")) {
112-
classes.add(Class.forName(pckgname + '.' + clazzName));
113-
}
114-
}
105+
for (Path directory : directories) {
106+
if (Files.exists(directory)) {
107+
try (Stream<Path> files = Files.list(directory)) {
108+
files.forEach(
109+
(file) -> {
110+
String fileName = file.getFileName().toString();
111+
if (fileName.endsWith(".class")) {
112+
String clazzName = fileName.substring(0, fileName.length() - 6);
113+
// exclude Test classes that happen to be in these packages.
114+
// class.ForName'ing some of them can cause trouble.
115+
if (!clazzName.endsWith("Test") && !clazzName.startsWith("Test")) {
116+
try {
117+
classes.add(Class.forName(pckgname + '.' + clazzName));
118+
} catch (ClassNotFoundException e) {
119+
throw new RuntimeException(e);
120+
}
121+
}
122+
}
123+
});
115124
}
116125
}
117126
}

Diff for: solr/core/src/test/org/apache/solr/SolrTestCaseJ4Test.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
*/
1717
package org.apache.solr;
1818

19-
import java.io.File;
2019
import java.nio.file.Files;
2120
import java.nio.file.Path;
22-
import org.apache.commons.io.FileUtils;
21+
import org.apache.commons.io.file.PathUtils;
2322
import org.apache.solr.common.params.ModifiableSolrParams;
2423
import org.junit.AfterClass;
2524
import org.junit.BeforeClass;
@@ -31,24 +30,24 @@ public class SolrTestCaseJ4Test extends SolrTestCaseJ4 {
3130
public static void beforeClass() throws Exception {
3231
// Create a temporary directory that holds a core NOT named "collection1". Use the smallest
3332
// configuration sets we can, so we don't copy that much junk around.
34-
String tmpSolrHome = createTempDir().toFile().getAbsolutePath();
33+
Path tmpSolrHome = createTempDir();
3534

36-
Path subHome = Path.of(tmpSolrHome, "core0", "conf");
35+
Path subHome = tmpSolrHome.resolve("core0/conf");
3736
Files.createDirectories(subHome);
38-
String top = SolrTestCaseJ4.TEST_HOME() + "/collection1/conf";
39-
Files.copy(Path.of(top, "schema-tiny.xml"), subHome.resolve("schema-tiny.xml"));
40-
Files.copy(Path.of(top, "solrconfig-minimal.xml"), subHome.resolve("solrconfig-minimal.xml"));
37+
Path top = SolrTestCaseJ4.TEST_HOME().resolve("collection1/conf");
38+
Files.copy(top.resolve("schema-tiny.xml"), subHome.resolve("schema-tiny.xml"));
39+
Files.copy(top.resolve("solrconfig-minimal.xml"), subHome.resolve("solrconfig-minimal.xml"));
4140
Files.copy(
42-
Path.of(top, "solrconfig.snippet.randomindexconfig.xml"),
41+
top.resolve("solrconfig.snippet.randomindexconfig.xml"),
4342
subHome.resolve("solrconfig.snippet.randomindexconfig.xml"));
4443

45-
FileUtils.copyDirectory(new File(tmpSolrHome, "core0"), new File(tmpSolrHome, "core1"));
44+
PathUtils.copyDirectory(tmpSolrHome.resolve("core0"), tmpSolrHome.resolve("core1"));
4645
// Core discovery will default to the name of the dir the core.properties file is in. So if
4746
// everything else is OK as defaults, just the _presence_ of this file is sufficient.
48-
FileUtils.touch(new File(tmpSolrHome, "core0/core.properties"));
49-
FileUtils.touch(new File(tmpSolrHome, "core1/core.properties"));
47+
PathUtils.touch(tmpSolrHome.resolve("core0/core.properties"));
48+
PathUtils.touch(tmpSolrHome.resolve("core1/core.properties"));
5049

51-
Files.copy(getFile("solr/solr.xml"), Path.of(tmpSolrHome, "solr.xml"));
50+
Files.copy(getFile("solr/solr.xml"), tmpSolrHome.resolve("solr.xml"));
5251

5352
initCore("solrconfig-minimal.xml", "schema-tiny.xml", tmpSolrHome, "core1");
5453
}

Diff for: solr/core/src/test/org/apache/solr/TestCpuTimeSearch.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static void setupSolr() throws Exception {
4848
System.setProperty(AllowListUrlChecker.DISABLE_URL_ALLOW_LIST, "true");
4949

5050
Path configSet = createTempDir("configSet");
51-
copyMinConf(configSet.toFile());
51+
copyMinConf(configSet);
5252
solrRule.startSolr(LuceneTestCase.createTempDir());
5353

5454
solrRule.newCollection("core1").withConfigSet(configSet.toString()).create();

Diff for: solr/core/src/test/org/apache/solr/TestSolrCoreProperties.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static void beforeTest() throws Exception {
4949
Files.createDirectories(dataDir);
5050
Files.createDirectories(confDir);
5151

52-
Files.copy(Path.of(SolrTestCaseJ4.TEST_HOME(), "solr.xml"), homeDir.resolve("solr.xml"));
52+
Files.copy(SolrTestCaseJ4.TEST_HOME().resolve("solr.xml"), homeDir.resolve("solr.xml"));
5353
String src_dir = TEST_HOME() + "/collection1/conf";
5454
Files.copy(Path.of(src_dir, "schema-tiny.xml"), confDir.resolve("schema.xml"));
5555
Files.copy(
@@ -71,7 +71,7 @@ public static void beforeTest() throws Exception {
7171
Properties nodeProperties = new Properties();
7272
// this sets the property for jetty starting SolrDispatchFilter
7373
if (System.getProperty("solr.data.dir") == null) {
74-
nodeProperties.setProperty("solr.data.dir", createTempDir().toFile().getCanonicalPath());
74+
nodeProperties.setProperty("solr.data.dir", createTempDir().toRealPath().toString());
7575
}
7676

7777
solrClientTestRule.startSolr(homeDir, nodeProperties, JettyConfig.builder().build());

Diff for: solr/core/src/test/org/apache/solr/TestTolerantSearch.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.apache.solr;
1818

19-
import java.io.File;
2019
import java.io.IOException;
2120
import java.io.OutputStream;
2221
import java.nio.file.Files;
@@ -45,23 +44,23 @@ public class TestTolerantSearch extends SolrJettyTestBase {
4544
private static String shard1;
4645
private static String shard2;
4746

48-
private static File createSolrHome() throws Exception {
47+
private static Path createSolrHome() throws Exception {
4948
Path workDir = createTempDir();
50-
setupJettyTestHome(workDir.toFile(), "collection1");
49+
setupJettyTestHome(workDir, "collection1");
5150
Files.copy(
5251
Path.of(SolrTestCaseJ4.TEST_HOME() + "/collection1/conf/solrconfig-tolerant-search.xml"),
5352
workDir.resolve("collection1").resolve("conf").resolve("solrconfig.xml"),
5453
StandardCopyOption.REPLACE_EXISTING);
5554
FileUtils.copyDirectory(
5655
workDir.resolve("collection1").toFile(), workDir.resolve("collection2").toFile());
57-
return workDir.toFile();
56+
return workDir;
5857
}
5958

6059
@BeforeClass
6160
public static void createThings() throws Exception {
6261
systemSetPropertySolrDisableUrlAllowList("true");
63-
File solrHome = createSolrHome();
64-
createAndStartJetty(solrHome.getAbsolutePath());
62+
Path solrHome = createSolrHome();
63+
createAndStartJetty(solrHome);
6564
String url = getBaseUrl();
6665
collection1 = getHttpSolrClient(url, "collection1");
6766
collection2 = getHttpSolrClient(url, "collection2");

Diff for: solr/core/src/test/org/apache/solr/blockcache/BlockDirectoryTest.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package org.apache.solr.blockcache;
1818

1919
import com.github.benmanes.caffeine.cache.Caffeine;
20-
import java.io.File;
2120
import java.io.IOException;
2221
import java.lang.invoke.MethodHandles;
22+
import java.nio.file.Path;
2323
import java.util.Map;
2424
import java.util.Random;
2525
import org.apache.lucene.store.Directory;
@@ -99,16 +99,16 @@ public void releaseResources() {}
9999
private static final int MAX_BUFFER_SIZE = 12000;
100100
private static final int MAX_NUMBER_OF_READS = 20000;
101101
private BlockDirectory directory;
102-
private File file;
102+
private Path file;
103103
private Random random;
104104
private MapperCache mapperCache;
105105

106106
@Override
107107
@Before
108108
public void setUp() throws Exception {
109109
super.setUp();
110-
file = createTempDir().toFile();
111-
FSDirectory dir = FSDirectory.open(new File(file, "base").toPath());
110+
file = createTempDir();
111+
FSDirectory dir = FSDirectory.open(file.resolve("base"));
112112
mapperCache = new MapperCache();
113113

114114
if (random().nextBoolean()) {
@@ -138,7 +138,7 @@ public void tearDown() throws Exception {
138138

139139
@Test
140140
public void testEOF() throws IOException {
141-
Directory fsDir = FSDirectory.open(new File(file, "normal").toPath());
141+
Directory fsDir = FSDirectory.open(file.resolve("normal"));
142142
String name = "test.eof";
143143
createFile(name, fsDir, directory);
144144
long fsLength = fsDir.fileLength(name);
@@ -170,7 +170,7 @@ public void testRandomAccessWrites() throws IOException {
170170
int i = 0;
171171
try {
172172
for (; i < 10; i++) {
173-
Directory fsDir = FSDirectory.open(new File(file, "normal").toPath());
173+
Directory fsDir = FSDirectory.open(file.resolve("normal"));
174174
String name = getName();
175175
createFile(name, fsDir, directory);
176176
assertInputsEquals(name, fsDir, directory);
@@ -253,9 +253,9 @@ private String getName() {
253253
return Long.toUnsignedString(random.nextLong());
254254
}
255255

256-
public static void rm(File file) {
256+
public static void rm(Path file) {
257257
try {
258-
IOUtils.rm(file.toPath());
258+
IOUtils.rm(file);
259259
} catch (Throwable ignored) {
260260
// TODO: should this class care if a file couldnt be deleted?
261261
// this just emulates previous behavior, where only SecurityException would be handled.

0 commit comments

Comments
 (0)