View Javadoc

1   /**
2    * Copyright (C) 2010 Daniel Manzke <daniel.manzke@googlemail.com>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.onami.autobind.test.configuration.file.override;
17  
18  import static org.junit.Assert.assertNotNull;
19  
20  import java.util.Properties;
21  
22  import javax.inject.Inject;
23  import javax.inject.Named;
24  
25  import junit.framework.Assert;
26  
27  import org.apache.onami.autobind.annotations.Bind;
28  import org.apache.onami.autobind.configuration.Configuration;
29  import org.apache.onami.autobind.configuration.PathConfig;
30  import org.apache.onami.autobind.configuration.PathConfig.PathType;
31  import org.apache.onami.autobind.configuration.StartupModule;
32  import org.apache.onami.autobind.configuration.features.ConfigurationFeature;
33  import org.apache.onami.autobind.scanner.PackageFilter;
34  import org.apache.onami.autobind.scanner.asm.ASMClasspathScanner;
35  import org.junit.Test;
36  
37  import com.google.inject.Guice;
38  import com.google.inject.Injector;
39  
40  
41  public class DirectFileConfigTests {
42  	@Test
43  	public void createDynamicModule() {
44  		StartupModule startup = StartupModule.create(ASMClasspathScanner.class,
45  			PackageFilter.create(DirectFileConfigTests.class));
46  		startup.addFeature(ConfigurationFeature.class);
47  
48  		Injector injector = Guice.createInjector(startup);
49  		assertNotNull(injector);
50  	}
51  
52  	@Test
53  	public void createPListConfiguration() {
54  		StartupModule startup = StartupModule.create(ASMClasspathScanner.class,
55  			PackageFilter.create(DirectFileConfigTests.class));
56  		startup.addFeature(ConfigurationFeature.class);
57  
58  		Injector injector = Guice.createInjector(startup);
59  		assertNotNull(injector);
60  
61  		TestInterface instance = injector.getInstance(TestInterface.class);
62  		Assert.assertTrue(instance.sayHello(), "sayHello() - yeahh!!".equals(instance.sayHello()));
63  	}
64  
65  	@Configuration(name = @Named("direct"), location = @PathConfig(value = "src/test/resources/configuration.properties", type = PathType.FILE), alternative = @PathConfig(value = "src/test/resources/configuration.nonexisting.properties", type = PathType.FILE))
66  	public interface DirectConfiguration {
67  	}
68  
69  	public static interface TestInterface {
70  		String sayHello();
71  	}
72  
73  	@Bind
74  	public static class DirectImplementations implements TestInterface {
75  		@Inject
76  		@Named("direct")
77  		private Properties config;
78  
79  		@Override
80  		public String sayHello() {
81  			return "sayHello() - " + config.getProperty("message");
82  		}
83  	}
84  }