1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.spi;
20
21 import java.io.IOException;
22
23 import org.apache.maven.surefire.providerapi.ServiceLoader;
24 import org.junit.Test;
25
26 import static java.lang.Thread.currentThread;
27 import static org.assertj.core.api.Assertions.assertThat;
28
29
30
31
32
33 public class SPITest {
34 private final ServiceLoader spi = new ServiceLoader();
35 private final ClassLoader ctx = currentThread().getContextClassLoader();
36
37 @Test
38 public void shouldNotLoadSpiDoesNotExist() throws IOException {
39 assertThat(spi.lookup(NoServiceInterface.class, ctx)).isEmpty();
40
41 assertThat(spi.load(NoServiceInterface.class, ctx)).isEmpty();
42 }
43
44 @Test
45 public void shouldNotLoadEmptySpi() throws IOException {
46 assertThat(spi.lookup(EmptyServiceInterface.class, ctx)).isEmpty();
47
48 assertThat(spi.load(EmptyServiceInterface.class, ctx)).isEmpty();
49 }
50
51 @Test
52 public void shouldLoad2SpiObjects() throws IOException {
53 assertThat(spi.lookup(ExistingServiceInterface.class, ctx)).hasSize(2);
54
55 assertThat(spi.lookup(ExistingServiceInterface.class, ctx))
56 .containsOnly(SPImpl1.class.getName(), SPImpl2.class.getName());
57
58 assertThat(spi.load(ExistingServiceInterface.class, ctx)).hasSize(2);
59
60 assertThat(spi.load(ExistingServiceInterface.class, ctx)).contains(new SPImpl1(), new SPImpl2());
61 }
62 }