Skip to content

Commit 71f9252

Browse files
committed
add protocol string + tests on LoggerFactory
1 parent 46c7e46 commit 71f9252

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.lecousin.framework.protocols.string;
2+
3+
import java.io.IOException;
4+
import java.net.URL;
5+
import java.net.URLConnection;
6+
import java.net.URLStreamHandler;
7+
8+
import net.lecousin.framework.io.buffering.ByteArrayIO;
9+
import net.lecousin.framework.io.util.ReadableAsURLConnection;
10+
11+
/**
12+
* URL Stream Handler for string protocol.
13+
*/
14+
public class Handler extends URLStreamHandler {
15+
16+
@Override
17+
protected URLConnection openConnection(URL u) throws IOException {
18+
String encoding = u.getHost();
19+
String str = u.getPath().substring(1);
20+
byte[] bytes = str.getBytes(encoding);
21+
ByteArrayIO io = new ByteArrayIO(bytes, "string protocol");
22+
return new ReadableAsURLConnection(io, u, true);
23+
}
24+
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* String protocol string:/xxx/yyy with xxx the encoding to use and yyy the string.
3+
*/
4+
package net.lecousin.framework.protocols.string;

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/log/TestLoggers.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package net.lecousin.framework.core.tests.log;
22

33
import java.io.File;
4+
import java.io.InputStream;
5+
import java.net.MalformedURLException;
6+
import java.net.URL;
47
import java.nio.charset.StandardCharsets;
58
import java.util.Calendar;
69

7-
import org.junit.Assert;
8-
import org.junit.Test;
9-
1010
import net.lecousin.framework.application.Application;
1111
import net.lecousin.framework.application.LCCore;
1212
import net.lecousin.framework.concurrent.Task;
@@ -26,6 +26,9 @@
2626
import net.lecousin.framework.log.appenders.RollingFileAppender;
2727
import net.lecousin.framework.util.StringUtil;
2828

29+
import org.junit.Assert;
30+
import org.junit.Test;
31+
2932
public class TestLoggers extends LCCoreAbstractTest {
3033

3134
@Test
@@ -102,7 +105,7 @@ public void test() throws Exception {
102105
expected.append(' ');
103106
expected.append("test");
104107
expected.append(' ');
105-
expected.append(79);
108+
expected.append(82);
106109
expected.append(' ');
107110
expected.append("TestLoggers.java");
108111
expected.append(' ');
@@ -193,4 +196,38 @@ private static void produceLogs(Logger log) {
193196
log.trace("this is trace");
194197
}
195198

199+
@Test
200+
public void testInvalidConfig() throws MalformedURLException {
201+
testInvalidConfig("");
202+
testInvalidConfig("<!-- -->");
203+
testInvalidConfig("<root></root>");
204+
testInvalidConfig("<LoggingConfiguration><unexpected/></LoggingConfiguration>");
205+
testInvalidConfig("<LoggingConfiguration><Appender>");
206+
testInvalidConfig("<LoggingConfiguration><Appender></Appender></LoggingConfiguration>");
207+
testInvalidConfig("<LoggingConfiguration><Appender class=\"hello\"></Appender></LoggingConfiguration>");
208+
testInvalidConfig("<LoggingConfiguration><Appender name=\"hello\"></Appender></LoggingConfiguration>");
209+
testInvalidConfig("<LoggingConfiguration><Appender name=\"hello\" class=\"" + getClass().getName() + "\"></Appender></LoggingConfiguration>");
210+
testInvalidConfig("<LoggingConfiguration><Logger>");
211+
testInvalidConfig("<LoggingConfiguration><Logger toto=\"1\"></Logger></LoggingConfiguration>");
212+
testInvalidConfig("<LoggingConfiguration><Logger level=\"UNKNOWN\"></Logger></LoggingConfiguration>");
213+
testInvalidConfig("<LoggingConfiguration><Logger name=\"test\" level=\"WARN\"></Logger></LoggingConfiguration>");
214+
testInvalidConfig("<LoggingConfiguration><Logger name=\"test\" level=\"WARN\" appender=\"unknown\"></Logger></LoggingConfiguration>");
215+
testInvalidConfig("<LoggingConfiguration><Logger name=\"test\" level=\"WARN\" appender=\"console\"><!----><unexpected/></Logger></LoggingConfiguration>");
216+
testInvalidConfig("<LoggingConfiguration><Default>");
217+
testInvalidConfig("<LoggingConfiguration><Default></Default></LoggingConfiguration>");
218+
testInvalidConfig("<LoggingConfiguration><Default toto=\"1\"></Default></LoggingConfiguration>");
219+
testInvalidConfig("<LoggingConfiguration><Default appender=\"unknown\"></Default></LoggingConfiguration>");
220+
testInvalidConfig("<LoggingConfiguration><Default appender=\"console\"><!----><unexpected/></Default></LoggingConfiguration>");
221+
}
222+
223+
private static void testInvalidConfig(String config) throws MalformedURLException {
224+
URL url = new URL("string://UTF-8/" + config);
225+
try (InputStream in = url.openStream()) {
226+
LCCore.getApplication().getLoggerFactory().configure(in);
227+
throw new AssertionError("Error expected to configure loggers: " + config);
228+
} catch (Exception e) {
229+
// ok
230+
}
231+
}
232+
196233
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.lecousin.framework.core.tests.protocols;
2+
3+
import java.io.InputStream;
4+
import java.net.URL;
5+
6+
import net.lecousin.framework.core.test.LCCoreAbstractTest;
7+
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
public class TestStringProtocol extends LCCoreAbstractTest {
12+
13+
@Test
14+
public void testUTF8() throws Exception {
15+
URL url = new URL("string://UTF-8/this is a test");
16+
InputStream in = url.openStream();
17+
byte[] b = new byte[1024];
18+
int off = 0;
19+
do {
20+
int nb = in.read(b, off, b.length - off);
21+
if (nb <= 0) break;
22+
off += nb;
23+
} while (true);
24+
in.close();
25+
Assert.assertEquals(14, off);
26+
byte[] b2 = new byte[14];
27+
System.arraycopy(b, 0, b2, 0, 14);
28+
Assert.assertArrayEquals(b2, new byte[] { 't','h','i','s',' ','i','s',' ','a',' ','t','e','s','t' });
29+
}
30+
31+
}

0 commit comments

Comments
 (0)