Skip to content

Commit 4d56458

Browse files
committed
Merge branch 'release-0.0.11'
2 parents 9a3b973 + c7fb829 commit 4d56458

File tree

18 files changed

+279
-82
lines changed

18 files changed

+279
-82
lines changed

java-io-base/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>be.nbb.rd</groupId>
77
<artifactId>java-io-parent</artifactId>
8-
<version>0.0.10</version>
8+
<version>0.0.11</version>
99
</parent>
1010

1111
<artifactId>java-io-base</artifactId>

java-io-base/src/main/java/internal/io/text/InternalFormatter.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
/*
22
* Copyright 2017 National Bank of Belgium
3-
*
4-
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved
3+
*
4+
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved
55
* by the European Commission - subsequent versions of the EUPL (the "Licence");
66
* You may not use this work except in compliance with the Licence.
77
* You may obtain a copy of the Licence at:
8-
*
8+
*
99
* http://ec.europa.eu/idabc/eupl
10-
*
11-
* Unless required by applicable law or agreed to in writing, software
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
1212
* distributed under the Licence is distributed on an "AS IS" basis,
1313
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the Licence for the specific language governing permissions and
14+
* See the Licence for the specific language governing permissions and
1515
* limitations under the Licence.
1616
*/
1717
package internal.io.text;
1818

1919
import java.io.File;
20+
import java.net.URL;
2021
import java.nio.charset.Charset;
2122
import java.text.DateFormat;
2223
import java.text.NumberFormat;
@@ -30,7 +31,6 @@
3031
import java.util.stream.Stream;
3132

3233
/**
33-
*
3434
* @author Philippe Charles
3535
*/
3636
@lombok.experimental.UtilityClass
@@ -119,4 +119,8 @@ public <T> CharSequence formatString(String value) {
119119
public <T> CharSequence formatObjectToString(Object value) {
120120
return value != null ? value.toString() : null;
121121
}
122+
123+
public CharSequence formatURL(URL value) {
124+
return value != null ? value.toString() : null;
125+
}
122126
}

java-io-base/src/main/java/internal/io/text/InternalParser.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package internal.io.text;
1818

1919
import java.io.File;
20+
import java.net.MalformedURLException;
21+
import java.net.URL;
2022
import java.nio.charset.Charset;
2123
import java.nio.charset.UnsupportedCharsetException;
2224
import java.text.DateFormat;
@@ -273,4 +275,14 @@ private boolean isLocaleLetter(char c) {
273275
private boolean isLocaleSeparator(char c) {
274276
return c == '_' || c == '-';
275277
}
278+
279+
public URL parseURL(CharSequence input) {
280+
if (input != null) {
281+
try {
282+
return new URL(input.toString());
283+
} catch (MalformedURLException ex) {
284+
}
285+
}
286+
return null;
287+
}
276288
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package nbbrd.io.sys;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.nio.charset.Charset;
7+
import java.util.stream.Collectors;
8+
9+
public final class EndOfProcessException extends IOException {
10+
11+
public static EndOfProcessException of(Process process) throws IOException {
12+
return new EndOfProcessException(process.exitValue(), readErrorStream(process));
13+
}
14+
15+
@lombok.Getter
16+
private final int exitValue;
17+
18+
@lombok.Getter
19+
private final String errorMessage;
20+
21+
private EndOfProcessException(int exitValue, String errorMessage) {
22+
super("Invalid exit value: " + exitValue + " " + errorMessage);
23+
this.exitValue = exitValue;
24+
this.errorMessage = errorMessage;
25+
}
26+
27+
private static String readErrorStream(Process process) throws IOException {
28+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream(), Charset.defaultCharset()))) {
29+
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
30+
}
31+
}
32+
}

java-io-base/src/main/java/nbbrd/io/sys/ProcessReader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
public class ProcessReader {
3030

3131
public static @NonNull BufferedReader newReader(@NonNull String... args) throws IOException {
32-
return newReader(new ProcessBuilder(args).redirectError(ProcessBuilder.Redirect.INHERIT).start());
32+
return newReader(new ProcessBuilder(args).start());
3333
}
3434

3535
public static @NonNull BufferedReader newReader(@NonNull Process process) throws IOException {
3636
return new BufferedReader(new InputStreamReader(new ProcessInputStream(process), Charset.defaultCharset()));
3737
}
3838

3939
public static @NonNull String readToString(@NonNull String... args) throws IOException {
40-
return readToString(new ProcessBuilder(args).redirectError(ProcessBuilder.Redirect.INHERIT).start());
40+
return readToString(new ProcessBuilder(args).start());
4141
}
4242

4343
public static @NonNull String readToString(@NonNull Process process) throws IOException {
@@ -81,7 +81,7 @@ private void waitForEndOfProcess() throws IOException {
8181
throw new IOException(ex);
8282
}
8383
if (process.exitValue() != 0) {
84-
throw new IOException("Invalid exit value: " + process.exitValue());
84+
throw EndOfProcessException.of(process);
8585
}
8686
}
8787
}

java-io-base/src/main/java/nbbrd/io/text/Formatter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.checkerframework.checker.nullness.qual.Nullable;
2323

2424
import java.io.File;
25+
import java.net.URL;
2526
import java.nio.charset.Charset;
2627
import java.text.DateFormat;
2728
import java.text.NumberFormat;
@@ -201,4 +202,9 @@ public interface Formatter<T> {
201202
Objects.requireNonNull(joiner);
202203
return o -> InternalFormatter.formatStringList(joiner, o);
203204
}
205+
206+
@StaticFactoryMethod
207+
static @NonNull Formatter<URL> onURL() {
208+
return InternalFormatter::formatURL;
209+
}
204210
}

java-io-base/src/main/java/nbbrd/io/text/Parser.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.checkerframework.checker.nullness.qual.Nullable;
2323

2424
import java.io.File;
25+
import java.net.URL;
2526
import java.nio.charset.Charset;
2627
import java.text.DateFormat;
2728
import java.text.NumberFormat;
@@ -191,4 +192,9 @@ public interface Parser<T> {
191192
static @NonNull Parser<Locale> onLocale() {
192193
return InternalParser::parseLocale;
193194
}
195+
196+
@StaticFactoryMethod
197+
static @NonNull Parser<URL> onURL() {
198+
return InternalParser::parseURL;
199+
}
194200
}

java-io-base/src/test/java/nbbrd/io/sys/ProcessReaderTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,28 @@ public void testContent() throws IOException {
4343
public void testExitCode() {
4444
switch (OS.NAME) {
4545
case WINDOWS:
46-
assertThatIOException()
46+
assertThatExceptionOfType(EndOfProcessException.class)
4747
.isThrownBy(() -> ProcessReader.readToString("where", UUID.randomUUID().toString()))
48-
.withMessageContaining("Invalid exit value");
48+
.withMessageStartingWith("Invalid exit value")
49+
.withNoCause()
50+
.matches(ex -> ex.getExitValue() != 0)
51+
.matches(ex -> !ex.getErrorMessage().isEmpty());
52+
53+
assertThatExceptionOfType(EndOfProcessException.class)
54+
.isThrownBy(() -> ProcessReader.readToString("where", "/Q", UUID.randomUUID().toString()))
55+
.withMessageStartingWith("Invalid exit value")
56+
.withNoCause()
57+
.matches(ex -> ex.getExitValue() != 0)
58+
.matches(ex -> ex.getErrorMessage().isEmpty());
4959
break;
5060
case LINUX:
5161
case MACOS:
5262
case SOLARIS:
53-
assertThatIOException()
63+
assertThatExceptionOfType(EndOfProcessException.class)
5464
.isThrownBy(() -> ProcessReader.readToString("which", UUID.randomUUID().toString()))
55-
.withMessageContaining("Invalid exit value");
65+
.withMessageStartingWith("Invalid exit value")
66+
.withNoCause()
67+
.matches(ex -> ex.getExitValue() != 0);
5668
break;
5769
}
5870
}

java-io-base/src/test/java/nbbrd/io/sys/SystemPropertiesTest.java

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,100 @@
33
import com.google.common.base.StandardSystemProperty;
44
import org.junit.Test;
55

6+
import java.nio.file.FileSystems;
67
import java.nio.file.Path;
78
import java.nio.file.Paths;
89
import java.util.List;
10+
import java.util.Properties;
911
import java.util.stream.Collectors;
1012
import java.util.stream.Stream;
1113

1214
import static org.assertj.core.api.Assertions.assertThat;
15+
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
1316

1417
public class SystemPropertiesTest {
1518

1619
@Test
17-
public void test() {
20+
public void testFactories() {
21+
assertThatNullPointerException()
22+
.isThrownBy(() -> SystemProperties.of(null, FileSystems.getDefault()));
23+
24+
assertThatNullPointerException()
25+
.isThrownBy(() -> SystemProperties.of(System.getProperties(), null));
26+
27+
assertThat(SystemProperties.DEFAULT)
28+
.isNotNull();
29+
}
30+
31+
@Test
32+
public void testGetUserHome() {
33+
Properties p = new Properties();
34+
SystemProperties x = SystemProperties.of(p, FileSystems.getDefault());
35+
36+
assertThat(x.getUserHome())
37+
.isNull();
38+
39+
p.put(SystemProperties.USER_HOME, "C:\\Temp");
40+
assertThat(x.getUserHome())
41+
.isEqualByComparingTo(Paths.get("C:\\Temp"));
42+
}
43+
44+
@Test
45+
public void testGetUserDir() {
46+
Properties p = new Properties();
47+
SystemProperties x = SystemProperties.of(p, FileSystems.getDefault());
48+
49+
assertThat(x.getUserDir())
50+
.isNull();
51+
52+
p.put(SystemProperties.USER_DIR, "C:\\Temp");
53+
assertThat(x.getUserDir())
54+
.isEqualByComparingTo(Paths.get("C:\\Temp"));
55+
}
56+
57+
@Test
58+
public void testGetJavaClassPath() {
59+
Properties p = new Properties();
60+
SystemProperties x = SystemProperties.of(p, FileSystems.getDefault());
61+
62+
assertThat(x.getJavaClassPath())
63+
.isEmpty();
64+
65+
p.put(SystemProperties.PATH_SEPARATOR, ";");
66+
assertThat(x.getJavaClassPath())
67+
.isEmpty();
68+
69+
p.put(SystemProperties.JAVA_CLASS_PATH, "C:\\Temp\\x.jar");
70+
assertThat(x.getJavaClassPath())
71+
.containsExactly(Paths.get("C:\\Temp\\x.jar"));
72+
73+
p.put(SystemProperties.JAVA_CLASS_PATH, "C:\\Temp\\x.jar;C:\\Temp\\y.jar");
74+
assertThat(x.getJavaClassPath())
75+
.containsExactly(
76+
Paths.get("C:\\Temp\\x.jar"),
77+
Paths.get("C:\\Temp\\y.jar")
78+
);
79+
}
80+
81+
@Test
82+
public void testGetPathSeparator() {
83+
Properties p = new Properties();
84+
SystemProperties x = SystemProperties.of(p, FileSystems.getDefault());
85+
86+
assertThat(x.getPathSeparator()).isNull();
87+
88+
p.put(SystemProperties.PATH_SEPARATOR, "");
89+
assertThat(x.getPathSeparator()).isNull();
90+
91+
p.put(SystemProperties.PATH_SEPARATOR, "; ");
92+
assertThat(x.getPathSeparator()).isNull();
93+
94+
p.put(SystemProperties.PATH_SEPARATOR, ";");
95+
assertThat(x.getPathSeparator()).isEqualTo(';');
96+
}
97+
98+
@Test
99+
public void testDEFAULT() {
18100
SystemProperties p = SystemProperties.DEFAULT;
19101

20102
assertThat(p.getJavaVersion()).isEqualTo(StandardSystemProperty.JAVA_VERSION.value());

java-io-base/src/test/java/nbbrd/io/text/FormatterTest.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
*/
1717
package nbbrd.io.text;
1818

19-
import static nbbrd.io.text.Formatter.*;
19+
import org.assertj.core.util.DateUtil;
20+
import org.junit.Test;
21+
2022
import java.io.File;
23+
import java.net.MalformedURLException;
24+
import java.net.URL;
2125
import java.nio.charset.Charset;
2226
import java.nio.charset.StandardCharsets;
2327
import java.text.NumberFormat;
@@ -32,9 +36,9 @@
3236
import java.util.Locale;
3337
import java.util.concurrent.TimeUnit;
3438
import java.util.stream.Collectors;
39+
40+
import static nbbrd.io.text.Formatter.*;
3541
import static org.assertj.core.api.Assertions.*;
36-
import org.assertj.core.util.DateUtil;
37-
import org.junit.Test;
3842

3943
/**
4044
*
@@ -190,6 +194,12 @@ public void testStringList() {
190194
assertCompliance(f, Arrays.asList(), "");
191195
}
192196

197+
@Test
198+
public void testURL() throws MalformedURLException {
199+
Formatter<URL> f = onURL();
200+
assertCompliance(f, new URL("file:/C:/temp/x.xml"), "file:/C:/temp/x.xml");
201+
}
202+
193203
private static <T> void assertCompliance(Formatter<T> f, T value, CharSequence text) {
194204
assertThatCode(() -> f.format(null)).doesNotThrowAnyException();
195205
assertThatCode(() -> f.formatAsString(null)).doesNotThrowAnyException();

0 commit comments

Comments
 (0)