Skip to content

Commit 45ff4a5

Browse files
committed
add tests
1 parent 65bf230 commit 45ff4a5

File tree

5 files changed

+177
-119
lines changed

5 files changed

+177
-119
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/util/GUIDUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
/**
44
* Utility methods for GUID.
55
*/
6-
public class GUIDUtil {
6+
public final class GUIDUtil {
7+
8+
private GUIDUtil() { /* no instance */ }
79

810
/** Create a GUID. */
911
public static byte[] toGUID(long p1, int p2, int p3, int p4, long p5) {
Lines changed: 120 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,120 @@
1-
package net.lecousin.framework.util;
2-
3-
import java.io.InputStream;
4-
import java.nio.charset.StandardCharsets;
5-
import java.util.concurrent.ThreadFactory;
6-
7-
import net.lecousin.framework.application.Application;
8-
import net.lecousin.framework.application.LCCore;
9-
import net.lecousin.framework.event.Listener;
10-
import net.lecousin.framework.mutable.Mutable;
11-
12-
/**
13-
* Utility methods with Process.
14-
*/
15-
public class ProcessUtil {
16-
17-
/**
18-
* Create a thread that wait for the given process to end, and call the given listener.
19-
*/
20-
public static void onProcessExited(Process process, Listener<Integer> exitValueListener) {
21-
Application app = LCCore.getApplication();
22-
Mutable<Thread> mt = new Mutable<>(null);
23-
Thread t = app.getThreadFactory().newThread(() -> {
24-
try { exitValueListener.fire(Integer.valueOf(process.waitFor())); }
25-
catch (InterruptedException e) { /* ignore and quit */ }
26-
app.interrupted(mt.get());
27-
});
28-
mt.set(t);
29-
t.setName("Waiting for process to exit");
30-
t.start();
31-
app.toInterruptOnShutdown(t);
32-
}
33-
34-
/** Launch 2 threads to consume both output and error streams, and call the listeners for each line read. */
35-
public static void consumeProcessConsole(Process process, Listener<String> outputListener, Listener<String> errorListener) {
36-
Application app = LCCore.getApplication();
37-
ThreadFactory factory = app.getThreadFactory();
38-
Thread t;
39-
ConsoleConsumer cc;
40-
cc = new ConsoleConsumer(process.getInputStream(), outputListener);
41-
t = factory.newThread(cc);
42-
t.setName("Process output console consumer");
43-
cc.app = app;
44-
cc.t = t;
45-
t.start();
46-
app.toInterruptOnShutdown(t);
47-
cc = new ConsoleConsumer(process.getErrorStream(), errorListener);
48-
t = factory.newThread(cc);
49-
t.setName("Process error console consumer");
50-
cc.app = app;
51-
cc.t = t;
52-
t.start();
53-
app.toInterruptOnShutdown(t);
54-
}
55-
56-
/** Thread that read from the given stream and call the listener for each line. */
57-
public static class ConsoleConsumer implements Runnable {
58-
59-
/** Constructor. */
60-
public ConsoleConsumer(InputStream input, Listener<String> listener) {
61-
this.input = input;
62-
this.listener = listener;
63-
}
64-
65-
private InputStream input;
66-
private Listener<String> listener;
67-
private Application app;
68-
private Thread t;
69-
70-
@Override
71-
public void run() {
72-
StringBuilder line = new StringBuilder();
73-
byte[] buffer = new byte[1024];
74-
do {
75-
int nb;
76-
try { nb = input.read(buffer); }
77-
catch (Exception e) { break; }
78-
if (nb < 0) break;
79-
if (nb == 0) continue;
80-
String s = new String(buffer, 0, nb, StandardCharsets.ISO_8859_1);
81-
while (s.length() > 0) {
82-
int i = s.indexOf('\r');
83-
int j = s.indexOf('\n');
84-
if (i < 0 && j < 0) {
85-
line.append(s);
86-
break;
87-
} else if (i < 0) {
88-
line.append(s.substring(0,j));
89-
listener.fire(line.toString());
90-
line = new StringBuilder();
91-
s = s.substring(j + 1);
92-
} else if (j < 0) {
93-
line.append(s.substring(0,i));
94-
listener.fire(line.toString());
95-
line = new StringBuilder();
96-
s = s.substring(i + 1);
97-
} else if (i == j - 1) {
98-
line.append(s.substring(0,i));
99-
listener.fire(line.toString());
100-
line = new StringBuilder();
101-
s = s.substring(j + 1);
102-
} else {
103-
if (j < i) i = j;
104-
line.append(s.substring(0,i));
105-
listener.fire(line.toString());
106-
line = new StringBuilder();
107-
s = s.substring(i + 1);
108-
}
109-
}
110-
} while (true);
111-
if (line.length() > 0)
112-
listener.fire(line.toString());
113-
try { input.close(); } catch (Throwable t) { /* ignore */ }
114-
app.interrupted(t);
115-
}
116-
}
117-
118-
}
1+
package net.lecousin.framework.util;
2+
3+
import java.io.InputStream;
4+
import java.nio.charset.StandardCharsets;
5+
import java.util.concurrent.ThreadFactory;
6+
7+
import net.lecousin.framework.application.Application;
8+
import net.lecousin.framework.application.LCCore;
9+
import net.lecousin.framework.event.Listener;
10+
import net.lecousin.framework.mutable.Mutable;
11+
12+
/**
13+
* Utility methods with Process.
14+
*/
15+
public final class ProcessUtil {
16+
17+
private ProcessUtil() { /* no instance */ }
18+
19+
/**
20+
* Create a thread that wait for the given process to end, and call the given listener.
21+
*/
22+
public static void onProcessExited(Process process, Listener<Integer> exitValueListener) {
23+
Application app = LCCore.getApplication();
24+
Mutable<Thread> mt = new Mutable<>(null);
25+
Thread t = app.getThreadFactory().newThread(() -> {
26+
try { exitValueListener.fire(Integer.valueOf(process.waitFor())); }
27+
catch (InterruptedException e) { /* ignore and quit */ }
28+
app.interrupted(mt.get());
29+
});
30+
mt.set(t);
31+
t.setName("Waiting for process to exit");
32+
t.start();
33+
app.toInterruptOnShutdown(t);
34+
}
35+
36+
/** Launch 2 threads to consume both output and error streams, and call the listeners for each line read. */
37+
public static void consumeProcessConsole(Process process, Listener<String> outputListener, Listener<String> errorListener) {
38+
Application app = LCCore.getApplication();
39+
ThreadFactory factory = app.getThreadFactory();
40+
Thread t;
41+
ConsoleConsumer cc;
42+
cc = new ConsoleConsumer(process.getInputStream(), outputListener);
43+
t = factory.newThread(cc);
44+
t.setName("Process output console consumer");
45+
cc.app = app;
46+
cc.t = t;
47+
t.start();
48+
app.toInterruptOnShutdown(t);
49+
cc = new ConsoleConsumer(process.getErrorStream(), errorListener);
50+
t = factory.newThread(cc);
51+
t.setName("Process error console consumer");
52+
cc.app = app;
53+
cc.t = t;
54+
t.start();
55+
app.toInterruptOnShutdown(t);
56+
}
57+
58+
/** Thread that read from the given stream and call the listener for each line. */
59+
public static class ConsoleConsumer implements Runnable {
60+
61+
/** Constructor. */
62+
public ConsoleConsumer(InputStream input, Listener<String> listener) {
63+
this.input = input;
64+
this.listener = listener;
65+
}
66+
67+
private InputStream input;
68+
private Listener<String> listener;
69+
private Application app;
70+
private Thread t;
71+
72+
@Override
73+
public void run() {
74+
StringBuilder line = new StringBuilder();
75+
byte[] buffer = new byte[1024];
76+
do {
77+
int nb;
78+
try { nb = input.read(buffer); }
79+
catch (Exception e) { break; }
80+
if (nb < 0) break;
81+
if (nb == 0) continue;
82+
String s = new String(buffer, 0, nb, StandardCharsets.ISO_8859_1);
83+
while (s.length() > 0) {
84+
int i = s.indexOf('\r');
85+
int j = s.indexOf('\n');
86+
if (i < 0 && j < 0) {
87+
line.append(s);
88+
break;
89+
} else if (i < 0) {
90+
line.append(s.substring(0,j));
91+
listener.fire(line.toString());
92+
line = new StringBuilder();
93+
s = s.substring(j + 1);
94+
} else if (j < 0) {
95+
line.append(s.substring(0,i));
96+
listener.fire(line.toString());
97+
line = new StringBuilder();
98+
s = s.substring(i + 1);
99+
} else if (i == j - 1) {
100+
line.append(s.substring(0,i));
101+
listener.fire(line.toString());
102+
line = new StringBuilder();
103+
s = s.substring(j + 1);
104+
} else {
105+
if (j < i) i = j;
106+
line.append(s.substring(0,i));
107+
listener.fire(line.toString());
108+
line = new StringBuilder();
109+
s = s.substring(i + 1);
110+
}
111+
}
112+
} while (true);
113+
if (line.length() > 0)
114+
listener.fire(line.toString());
115+
try { input.close(); } catch (Throwable t) { /* ignore */ }
116+
app.interrupted(t);
117+
}
118+
}
119+
120+
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/util/TestClassUtil.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public void testGetAllFieldsInheritedFirst() {
6060
//Assert.assertEquals("myInteger", fields.get(0).getName());
6161
}
6262

63+
@Test(timeout=30000)
64+
public void testGetAllMethodsInheritedFirst() {
65+
ClassUtil.getAllMethodsInheritedFirst(Class2.class);
66+
}
67+
6368
@Test(timeout=30000)
6469
public void testGettersAndSetters() {
6570
Assert.assertNotNull(ClassUtil.getGetter(Class2.class, "myBoolean").getAnnotation(Property.class));
@@ -118,4 +123,9 @@ public void testFieldPath() throws Exception {
118123
Assert.assertEquals(Integer.valueOf(11), ClassUtil.getFieldFromPath(root, "e2.sub2.i"));
119124
}
120125

126+
@Test(timeout=30000)
127+
public void testGetMethods() {
128+
Assert.assertEquals(1, ClassUtil.getMethods(Class2.class, "setMyBoolean", 1).size());
129+
}
130+
121131
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.lecousin.framework.core.tests.util;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import net.lecousin.framework.core.test.LCCoreAbstractTest;
7+
import net.lecousin.framework.util.GUIDUtil;
8+
9+
public class TestGUIDUtil extends LCCoreAbstractTest {
10+
11+
@Test(timeout=30000)
12+
public void test() {
13+
byte[] guid = GUIDUtil.toGUID(12345678, 753, 159, 85246, 987654321);
14+
String s = GUIDUtil.toString(guid);
15+
Assert.assertEquals("{00BC614E-02F1-009F-4CFE-00003ADE68B1}", s);
16+
}
17+
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.lecousin.framework.core.tests.util;
2+
3+
import java.io.IOException;
4+
5+
import org.junit.Test;
6+
7+
import net.lecousin.framework.application.LCCore;
8+
import net.lecousin.framework.concurrent.Console;
9+
import net.lecousin.framework.concurrent.synch.SynchronizationPoint;
10+
import net.lecousin.framework.core.test.LCCoreAbstractTest;
11+
import net.lecousin.framework.util.ProcessUtil;
12+
13+
public class TestProcessUtil extends LCCoreAbstractTest {
14+
15+
@Test(timeout=60000)
16+
public void test() throws IOException {
17+
ProcessBuilder pb = new ProcessBuilder(System.getProperty("java.home") + "/bin/java", "-version");
18+
Process p = pb.start();
19+
SynchronizationPoint<Exception> sp = new SynchronizationPoint<>();
20+
ProcessUtil.onProcessExited(p, (val) -> { sp.unblock(); });
21+
Console c = LCCore.getApplication().getConsole();
22+
ProcessUtil.consumeProcessConsole(p, (line) -> { c.out(line); }, (line) -> { c.err(line); });
23+
sp.block(0);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)