Skip to content

Commit bb33cfe

Browse files
committed
Add ResourceUtil to load resource related files
1 parent a89041f commit bb33cfe

File tree

20 files changed

+113
-67
lines changed

20 files changed

+113
-67
lines changed

config-generator/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dependencies {
1919
implementation group: 'org.jetbrains', name: 'annotations', version: ver.'jetbrains-annotations'
2020

2121
testImplementation platform(dep.junit5bom)
22+
testImplementation project(':core-test-utils')
2223
testImplementation dep.junit5jupiter
2324
testImplementation dep.junit5migration
2425
testImplementation group: 'com.google.testing.compile', name: 'compile-testing', version: ver.'compile-testing'

config-generator/src/test/java/org/neo4j/gds/proc/ConfigurationProcessorTest.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,15 @@
2727
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
2828
import org.junit.jupiter.params.ParameterizedTest;
2929
import org.junit.jupiter.params.provider.ValueSource;
30+
import org.neo4j.gds.ResourceUtil;
3031

3132
import javax.tools.JavaFileObject;
32-
import java.io.IOException;
33-
import java.io.UncheckedIOException;
34-
import java.net.URISyntaxException;
35-
import java.nio.file.Files;
36-
import java.nio.file.Paths;
3733
import java.util.Locale;
38-
import java.util.Objects;
3934

4035
import static com.google.common.truth.Truth.assertAbout;
4136
import static com.google.testing.compile.JavaFileObjects.forResource;
4237
import static com.google.testing.compile.JavaFileObjects.forSourceLines;
4338
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
44-
import static java.nio.charset.StandardCharsets.UTF_8;
4539
import static org.junit.jupiter.api.condition.JRE.JAVA_12;
4640

4741
// Need to disable this for Java versions > 11
@@ -251,21 +245,12 @@ private void runNegativeTest(String className, ErrorCheck... expectations) {
251245
}
252246

253247
private JavaFileObject loadExpectedFile(String resourceName) {
254-
try {
255-
var sourceLines = Files.readAllLines(
256-
Paths.get(Objects.requireNonNull(getClass().getResource(resourceName)).toURI()),
257-
UTF_8
258-
);
259-
260-
String binaryName = resourceName
261-
.replace('/', '.')
262-
.replace(".java", "");
263-
return forSourceLines(binaryName, sourceLines);
264-
} catch (IOException e) {
265-
throw new UncheckedIOException(e);
266-
} catch (URISyntaxException e) {
267-
throw new RuntimeException(e);
268-
}
248+
var sourceLines = ResourceUtil.lines(resourceName);
249+
250+
String binaryName = resourceName
251+
.replace('/', '.')
252+
.replace(".java", "");
253+
return forSourceLines(binaryName, sourceLines);
269254
}
270255

271256
private static ErrorCheck e(String error, int line, int column) {

core-test-utils/build.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: 'java-library'
2+
3+
description = 'Neo4j Graph Data Science :: Core Test Utils'
4+
5+
group = 'org.neo4j.gds'
6+
7+
dependencies {
8+
api (
9+
platform(dep.junit5bom),
10+
dep.junit5jupiter,
11+
[group: 'org.assertj', name: 'assertj-core', version: ver.'assertj'],
12+
[group: 'org.hamcrest', name: 'hamcrest-library', version: ver.'hamcrest'],
13+
14+
// let annotation dependencies be transitive to avoid compiler warnings for users of this project on missing annotation methods
15+
[group: 'org.jetbrains', name: 'annotations', version: ver.'jetbrains-annotations'],
16+
[group: 'org.immutables', name: 'value-annotations', version: ver.'immutables'],
17+
)
18+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds;
21+
22+
import java.io.IOException;
23+
import java.io.UncheckedIOException;
24+
import java.net.URI;
25+
import java.net.URISyntaxException;
26+
import java.nio.file.Files;
27+
import java.nio.file.Paths;
28+
import java.util.List;
29+
import java.util.Locale;
30+
import java.util.Objects;
31+
32+
import static java.nio.charset.StandardCharsets.UTF_8;
33+
34+
public final class ResourceUtil {
35+
36+
public static List<String> lines(String resourceName) {
37+
var classLoader = Objects.requireNonNullElse(
38+
Thread.currentThread().getContextClassLoader(),
39+
ResourceUtil.class.getClassLoader()
40+
);
41+
42+
var resourceUrl = classLoader.getResource(resourceName);
43+
Objects.requireNonNull(resourceUrl, () -> String.format(Locale.ENGLISH, "The resource %s cannot be found.", resourceName));
44+
45+
final URI resourceUri;
46+
try {
47+
resourceUri = resourceUrl.toURI();
48+
} catch (URISyntaxException e) {
49+
throw new RuntimeException(e);
50+
}
51+
52+
var resourcePath = Paths.get(resourceUri);
53+
try {
54+
return Files.readAllLines(resourcePath, UTF_8);
55+
} catch (IOException e) {
56+
throw new UncheckedIOException(e);
57+
}
58+
}
59+
60+
private ResourceUtil() {}
61+
}

test-utils/src/main/java/org/neo4j/gds/core/ExceptionMessageMatcher.java renamed to core-test-utils/src/main/java/org/neo4j/gds/core/ExceptionMessageMatcher.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,3 @@ public void describeTo(final Description description) {
5959
}
6060

6161
}
62-

test-utils/src/main/java/org/neo4j/gds/core/PortFinder.java renamed to core-test-utils/src/main/java/org/neo4j/gds/core/PortFinder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@
3939
import java.net.InetSocketAddress;
4040
import java.net.ServerSocket;
4141
import java.util.Arrays;
42+
import java.util.Locale;
4243
import java.util.Objects;
4344
import java.util.Random;
4445
import java.util.stream.IntStream;
4546
import java.util.stream.Stream;
4647

47-
import static org.neo4j.gds.utils.StringFormatting.formatWithLocale;
48-
4948
public class PortFinder implements ParameterResolver, TestInstancePostProcessor {
5049

5150
@Target({ElementType.PARAMETER, ElementType.TYPE, ElementType.FIELD})
@@ -97,7 +96,8 @@ private void setValueToField(Object testInstance, ExtensionContext context, Fiel
9796
trySetValueToField(testInstance, context, field);
9897
} catch (SecurityException | InaccessibleObjectException | IllegalAccessException illegalAccessException) {
9998
throw new ExtensionConfigurationException(
100-
formatWithLocale(
99+
String.format(
100+
Locale.ENGLISH,
101101
"Field %s cannot be set, please make it either public or accessible to reflection.",
102102
field.getName()
103103
));
@@ -112,7 +112,7 @@ private void trySetValueToField(Object testInstance, ExtensionContext extensionC
112112

113113
if (existingValue != null && !Objects.equals(existingValue, 0)) {
114114
throw new ExtensionConfigurationException(
115-
formatWithLocale("Field %s should not have any manually assigned value.", field.getName()));
115+
String.format(Locale.ENGLISH,"Field %s should not have any manually assigned value.", field.getName()));
116116
}
117117

118118
var annotation = field.getAnnotation(FreePort.class);

test-utils/src/main/java/org/neo4j/gds/datasets/EnvironmentReporting.java renamed to core-test-utils/src/main/java/org/neo4j/gds/datasets/EnvironmentReporting.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,30 @@
1919
*/
2020
package org.neo4j.gds.datasets;
2121

22-
import org.apache.commons.io.IOUtils;
23-
22+
import java.io.BufferedReader;
2423
import java.io.IOException;
24+
import java.io.InputStreamReader;
2525
import java.nio.charset.Charset;
2626
import java.nio.file.Path;
27+
import java.util.stream.Collectors;
2728

2829
public final class EnvironmentReporting {
2930
private EnvironmentReporting() {}
3031

3132
public static String diskUsage() throws IOException {
3233
Process p = new ProcessBuilder("df", "-h").start();
33-
String stdout = IOUtils.toString(p.getInputStream(), Charset.defaultCharset());
34+
String stdout = new BufferedReader(new InputStreamReader(p.getInputStream(), Charset.defaultCharset()))
35+
.lines()
36+
.collect(Collectors.joining(System.lineSeparator()));
3437
String diskUsage = "$ df -h\n" + stdout;
3538
return asMultilineCloudWatchLogMessageIfAppropriate(diskUsage);
3639
}
3740

3841
public static String directoryContents(Path directory) throws IOException {
3942
Process p = new ProcessBuilder("ls", "-l", directory.toAbsolutePath().toString()).start();
40-
String stdout = IOUtils.toString(p.getInputStream(), Charset.defaultCharset());
43+
String stdout = new BufferedReader(new InputStreamReader(p.getInputStream(), Charset.defaultCharset()))
44+
.lines()
45+
.collect(Collectors.joining(System.lineSeparator()));
4146
String directoryContents = "$ ls -l " + directory + "\n" + stdout;
4247
return asMultilineCloudWatchLogMessageIfAppropriate(directoryContents);
4348
}

0 commit comments

Comments
 (0)