View Javadoc

1   package org.apache.onami.spi.services;
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 static org.junit.Assert.assertEquals;
23  
24  import org.apache.onami.test.OnamiRunner;
25  import org.apache.onami.test.annotation.GuiceProvidedModules;
26  import org.junit.Test;
27  import org.junit.runner.RunWith;
28  
29  import com.google.inject.Inject;
30  import com.google.inject.Module;
31  
32  @RunWith( OnamiRunner.class )
33  public final class FromSystemPropertiesTestCase
34  {
35  
36      @GuiceProvidedModules
37      public static Module createTestModule()
38      {
39          // This simulates the SPI specification via Java System Properties,
40          // equivalent to java -Dorg.apache.onami.spi.services.FooService=org.apac...
41          System.setProperty( "org.apache.onami.spi.services.FooService",
42                              "org.apache.onami.spi.services.FooServiceImpl1," +
43                              "org.apache.onami.spi.services.FooServiceImpl2");
44  
45          return new ServiceLoaderModule()
46          {
47  
48              @Override
49              protected void configureServices()
50              {
51                  discover( FooService.class );
52              }
53  
54          };
55      }
56  
57      @Inject
58      @BarBindingAnnotation( 1 )
59      private FooService fooService1;
60  
61      @Inject
62      @BarBindingAnnotation( 2 )
63      private FooService fooService2;
64  
65      public void setFooService1( FooService fooService1 )
66      {
67          this.fooService1 = fooService1;
68      }
69  
70      public void setFooService2( FooService fooService2 )
71      {
72          this.fooService2 = fooService2;
73      }
74  
75      @Test
76      public void injectedServicesCaughtFromSystemProperties()
77      {
78          assertEquals( FooServiceImpl1.class, fooService1.getClass() );
79          assertEquals( FooServiceImpl2.class, fooService2.getClass() );
80      }
81  
82  }