View Javadoc
1   package org.apache.maven.surefire.spi;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
33   * @since 2.20
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  }