Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOLR-16903: Migrate all tests using File to NIO Path #3263

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

mlbiscoc
Copy link
Contributor

https://issues.apache.org/jira/browse/SOLR-16903

Description

WIP - Leaving in draft state for now as there is still more work from refactor needed but most tests pass so can be semi-reviewed.

Migrate all imports using File to Path. There should be no more imports using File in the project. We are close to making File a forbidden API after this but still more work needed.

Checklist

Please review the following and check all that apply:

  • I have reviewed the guidelines for How to Contribute and my code conforms to the standards described there to the best of my ability.
  • I have created a Jira issue and added the issue ID to my pull request title.
  • I have given Solr maintainers access to contribute to my PR branch. (optional but recommended, not available for branches on forks living under an organisation)
  • I have developed this patch against the main branch.
  • I have run ./gradlew check.
  • I have added tests for my changes.
  • I have added documentation for the Reference Guide

File file = new File(directory, name);
if (!file.exists()) {
Path directory = ((FSDirectory) in).getDirectory();
Path file = directory.resolve(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just purely cosmentic, but should this really be path instead of file? In all the places?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. I like 'file' to differentiate from a 'dir'. 'path' is generic if could be either.

Copy link
Contributor

@epugh epugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, so much work! In general I love the removing of the indirection that using Path is showing....

ClassLoader cld = h.getCore().getResourceLoader().getClassLoader();
String path = pckgname.replace('.', '/');
Enumeration<URL> resources = cld.getResources(path);
while (resources.hasMoreElements()) {
final URI uri = resources.nextElement().toURI();
if (!"file".equalsIgnoreCase(uri.getScheme())) continue;
final File f = new File(uri);
final Path f = Path.of(uri);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path instead of f?

}
for (Path directory : directories) {
if (Files.exists(directory)) {
try (Stream<Path> files = Files.list(directory)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the try helping? Is this a try-with-resources pattern?

@@ -84,9 +83,11 @@ private static int findAvailablePort() throws IOException {
private static Pair<Integer, Process> createProcess(int port, boolean https) throws IOException {
// Get the path to the java executable from the current JVM
String classPath =
Arrays.stream(System.getProperty("java.class.path").split(File.pathSeparator))
Arrays.stream(
System.getProperty("java.class.path").split(System.getProperty("path.separator")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm suprised we don't have sort of nicer way of doing this than System.getProperty.. Is this common enough that we should have a helper?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "FileSystems.getDefault().getSeparator();" a possiblity?


StreamTool.LocalCatStream catStream =
new StreamTool.LocalCatStream(localFile.getAbsolutePath(), -1);
new StreamTool.LocalCatStream(localFile.toAbsolutePath().toString(), -1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just pass in a path here?


String[] toolArgs =
new String[] {
"-e", "techproducts",
"--server-dir", solrServerDir.getAbsolutePath(),
"--example-dir", solrExampleDir.getAbsolutePath(),
"--server-dir", solrServerDir.toAbsolutePath().toString(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shame that this requires all these .toString.. Is there a smarter way of handling this toolArgs? var toolArgs? And then internally we call .toString on anything passed in?????

Copy link
Contributor

@dsmiley dsmiley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed a subset of this but I'll pause until you react to this feedback.(1) Don't use Path.of if you can instead resolve() against the existing path.
(2) Don't rely on referring to the separator char for string concatenation if the scenario could simply be path.resolve()

@@ -279,12 +292,16 @@ public void testCp() throws Exception {
// src and cp3 are valid

String localNamed =
tmp.normalize() + File.separator + "localnamed" + File.separator + "renamed.txt";
tmp.normalize()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we use resolve() instead?

"file:" + srcPathCheck.normalize().toAbsolutePath() + File.separator + "solrconfig.xml",
"file:"
+ srcPathCheck.normalize().toAbsolutePath()
+ FileSystems.getDefault().getSeparator()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we use resolve instead? And everywhere else in this source file probably.

@@ -417,7 +434,7 @@ public void testCp() throws Exception {
res = cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool, args));
assertEquals("Copy should have succeeded.", 0, res);

Path locEmpty = Paths.get(tmp2.toAbsolutePath().toString(), "stopwords", "emptyfile");
Path locEmpty = Path.of(tmp2.toAbsolutePath().toString(), "stopwords", "emptyfile");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use resolve here instead? and in many other places similarly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants