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 OverrideFileConfigTests {
42  	@Test
43  	public void createDynamicModule() {
44  		StartupModule startup = StartupModule.create(ASMClasspathScanner.class,
45  			PackageFilter.create(OverrideFileConfigTests.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(OverrideFileConfigTests.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() - overriden yeahh!!".equals(instance
63  			.sayHello()));
64  	}
65  
66  	@Configuration(name = @Named("override"), location = @PathConfig(value = "src/test/resources/configuration.properties", type = PathType.FILE), alternative = @PathConfig(value = "src/test/resources/configuration.override.properties", type = PathType.FILE))
67  	public interface OverrideConfiguration {
68  	}
69  
70  	public static interface TestInterface {
71  		String sayHello();
72  	}
73  
74  	@Bind
75  	public static class DirectImplementations implements TestInterface {
76  		@Inject
77  		@Named("override")
78  		private Properties config;
79  
80  		@Override
81  		public String sayHello() {
82  			return "sayHello() - " + config.getProperty("message");
83  		}
84  	}
85  }