1 package org.apache.maven.surefire.spi;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.surefire.booterclient.ProviderDetector;
23 import org.apache.maven.surefire.providerapi.ServiceLoader;
24 import org.junit.Test;
25
26 import java.io.IOException;
27
28 import static java.lang.Thread.currentThread;
29 import static org.fest.assertions.Assertions.assertThat;
30
31
32
33
34
35 public class SPITest
36 {
37 private final ServiceLoader spi = new ServiceLoader();
38 private final ProviderDetector providerDetector = new ProviderDetector();
39 private final ClassLoader ctx = currentThread().getContextClassLoader();
40
41 @Test
42 public void shouldNotLoadSpiDoesNotExist() throws IOException
43 {
44 assertThat( spi.lookup( NoServiceInterface.class, ctx ) )
45 .isEmpty();
46
47 assertThat( spi.load( NoServiceInterface.class, ctx ) )
48 .isEmpty();
49
50 assertThat( providerDetector.lookupServiceNames( NoServiceInterface.class, ctx ) )
51 .isEmpty();
52 }
53
54 @Test
55 public void shouldNotLoadEmptySpi() throws IOException
56 {
57 assertThat( spi.lookup( EmptyServiceInterface.class, ctx ) )
58 .isEmpty();
59
60 assertThat( spi.load( EmptyServiceInterface.class, ctx ) )
61 .isEmpty();
62
63 assertThat( providerDetector.lookupServiceNames( EmptyServiceInterface.class, ctx ) )
64 .isEmpty();
65 }
66
67 @Test
68 public void shouldLoad2SpiObjects() throws IOException
69 {
70 assertThat( spi.lookup( ExistingServiceInterface.class, ctx ) )
71 .hasSize( 2 );
72
73 assertThat( spi.lookup( ExistingServiceInterface.class, ctx ) )
74 .containsOnly( SPImpl1.class.getName(), SPImpl2.class.getName() );
75
76
77 assertThat( spi.load( ExistingServiceInterface.class, ctx ) )
78 .hasSize( 2 );
79
80 assertThat( spi.load( ExistingServiceInterface.class, ctx ) )
81 .contains( new SPImpl1(), new SPImpl2() );
82
83
84 assertThat( providerDetector.lookupServiceNames( ExistingServiceInterface.class, ctx ) )
85 .hasSize( 2 );
86
87 assertThat( providerDetector.lookupServiceNames( ExistingServiceInterface.class, ctx ) )
88 .containsOnly( SPImpl1.class.getName(), SPImpl2.class.getName() );
89 }
90 }