Skip to content

Commit f719b58

Browse files
committed
add test
1 parent dd93c12 commit f719b58

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

opengrok-indexer/src/test/java/org/opengrok/indexer/util/ExecutorTest.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2019, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.util;
@@ -30,9 +30,12 @@
3030
import java.io.File;
3131
import java.io.IOException;
3232
import java.io.InputStream;
33+
import java.io.Reader;
3334
import java.util.ArrayList;
3435
import java.util.Arrays;
36+
import java.util.HashMap;
3537
import java.util.List;
38+
import java.util.Map;
3639

3740
import static org.junit.jupiter.api.Assertions.assertEquals;
3841
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -64,10 +67,14 @@ void testReader() throws IOException {
6467
cmdList.add("testing org.opengrok.indexer.util.Executor");
6568
Executor instance = new Executor(cmdList);
6669
assertEquals(0, instance.exec());
67-
BufferedReader in = new BufferedReader(instance.getOutputReader());
70+
Reader outputReader = instance.getOutputReader();
71+
assertNotNull(outputReader);
72+
BufferedReader in = new BufferedReader(outputReader);
6873
assertEquals("testing org.opengrok.indexer.util.Executor", in.readLine());
6974
in.close();
70-
in = new BufferedReader(instance.getErrorReader());
75+
Reader errorReader = instance.getErrorReader();
76+
assertNotNull(errorReader);
77+
in = new BufferedReader(errorReader);
7178
assertNull(in.readLine());
7279
in.close();
7380
}
@@ -81,14 +88,44 @@ void testStream() throws IOException {
8188
assertEquals(0, instance.exec());
8289
assertNotNull(instance.getOutputStream());
8390
assertNotNull(instance.getErrorStream());
84-
BufferedReader in = new BufferedReader(instance.getOutputReader());
91+
Reader outputReader = instance.getOutputReader();
92+
assertNotNull(outputReader);
93+
BufferedReader in = new BufferedReader(outputReader);
8594
assertEquals("testing org.opengrok.indexer.util.Executor", in.readLine());
8695
in.close();
87-
in = new BufferedReader(instance.getErrorReader());
96+
Reader errorReader = instance.getErrorReader();
97+
assertNotNull(errorReader);
98+
in = new BufferedReader(errorReader);
8899
assertNull(in.readLine());
89100
in.close();
90101
}
91102

103+
/**
104+
* Test setting environment variable. Assumes the {@code env} program exists
105+
* and reports list of environment variables.
106+
*/
107+
@Test
108+
void testEnv() throws IOException {
109+
List<String> cmdList = List.of("env");
110+
final Map<String, String> env = new HashMap<>();
111+
env.put("foo", "bar");
112+
Executor instance = new Executor(cmdList, null, env);
113+
assertEquals(0, instance.exec());
114+
Reader outputReader = instance.getOutputReader();
115+
assertNotNull(outputReader);
116+
BufferedReader in = new BufferedReader(outputReader);
117+
String line;
118+
boolean found = false;
119+
while ((line = in.readLine()) != null) {
120+
if (line.equals("foo=bar")) {
121+
found = true;
122+
break;
123+
}
124+
}
125+
assertTrue(found);
126+
in.close();
127+
}
128+
92129
/**
93130
* {@link Executor.StreamHandler} implementation that always fails with {@link IOException}.
94131
*/

0 commit comments

Comments
 (0)