1+ package com .github .dockerjava .api .async ;
2+
3+ import org .junit .Assert ;
4+ import org .junit .Test ;
5+
6+ import java .io .File ;
7+ import java .net .MalformedURLException ;
8+ import java .net .URL ;
9+ import java .net .URLClassLoader ;
10+ import java .util .Set ;
11+ import java .util .concurrent .atomic .AtomicBoolean ;
12+ import java .util .stream .Collectors ;
13+ import java .util .stream .Stream ;
14+
15+ public class ResultCallbackTemplateTest {
16+
17+ static Set <URL > CLASSPATH = Stream .of (System .getProperty ("java.class.path" ).split (":" ))
18+ .map (it -> {
19+ try {
20+ return new File (it ).toURI ().toURL ();
21+ } catch (MalformedURLException e ) {
22+ throw new RuntimeException (e );
23+ }
24+ })
25+ .collect (Collectors .toSet ());
26+
27+ @ Test
28+ public void shouldNotFailIfSlf4jIsNotOnClasspath () throws Exception {
29+ try (
30+ URLClassLoader classLoader = new URLClassLoader (
31+ CLASSPATH .stream ().filter (it -> !it .getPath ().contains ("slf4j" )).toArray (URL []::new ),
32+ null
33+ )
34+ ) {
35+ Class <?> clazz = classLoader .loadClass (DummyResultCallbackTemplate .class .getName ());
36+ Object template = clazz .newInstance ();
37+ AtomicBoolean classNotFound = new AtomicBoolean ();
38+ clazz .getMethod ("onNext" , AtomicBoolean .class ).invoke (template , classNotFound );
39+ Assert .assertTrue ("Slf4J is not on classpath" , classNotFound .get ());
40+ clazz .getMethod ("onError" , Throwable .class ).invoke (template , new Exception ("Boom" ));
41+ }
42+ }
43+
44+ public static class DummyResultCallbackTemplate extends ResultCallbackTemplate <DummyResultCallbackTemplate , AtomicBoolean > {
45+
46+ @ Override
47+ public void onNext (AtomicBoolean atomicBoolean ) {
48+ try {
49+ atomicBoolean .set (Class .forName ("org.slf4j.LoggerFactory" ) == null );
50+ } catch (ClassNotFoundException ignored ) {
51+ atomicBoolean .set (true );
52+ }
53+ }
54+ }
55+ }
0 commit comments