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.url.both;
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.Configuration.Type;
30  import org.apache.onami.autobind.configuration.PathConfig;
31  import org.apache.onami.autobind.configuration.PathConfig.PathType;
32  import org.apache.onami.autobind.configuration.StartupModule;
33  import org.apache.onami.autobind.configuration.features.ConfigurationFeature;
34  import org.apache.onami.autobind.scanner.PackageFilter;
35  import org.apache.onami.autobind.scanner.asm.ASMClasspathScanner;
36  import org.junit.Test;
37  
38  import com.google.inject.Guice;
39  import com.google.inject.Injector;
40  
41  
42  public class BothURLConfigTests {
43  	@Test
44  	public void createDynamicModule() {
45  		StartupModule startup = StartupModule.create(ASMClasspathScanner.class,
46  			PackageFilter.create(BothURLConfigTests.class));
47  		startup.addFeature(ConfigurationFeature.class);
48  
49  		Injector injector = Guice.createInjector(startup);
50  		assertNotNull(injector);
51  	}
52  
53  	@Test
54  	public void createPListConfiguration() {
55  		StartupModule startup = StartupModule.create(ASMClasspathScanner.class,
56  			PackageFilter.create(BothURLConfigTests.class));
57  		startup.addFeature(ConfigurationFeature.class);
58  
59  		Injector injector = Guice.createInjector(startup);
60  		assertNotNull(injector);
61  
62  		TestInterface instance = injector.getInstance(TestInterface.class);
63  		Assert.assertTrue("sayHello() - yeahh!!".equals(instance.sayHello()));
64  	}
65  
66  	@Configuration(name = @Named("config"), location = @PathConfig(value = "http://devsurf.de/guice/configuration.properties", type = PathType.URL), type=Type.BOTH)
67  	public interface TestConfiguration {
68  	}
69  
70  	public static interface TestInterface {
71  		String sayHello();
72  	}
73  
74  	@Bind
75  	public static class TestImplementations implements TestInterface {
76  		@Inject
77  		@Named("message")
78  		private String message;
79  		
80  		@Inject
81  		@Named("config")
82  		private Properties config;
83  
84  		@Override
85  		public String sayHello() {
86  			String msg = config.getProperty("message","");
87  			if(msg.equals(message)){
88  				return "sayHello() - " + message;	
89  			}
90  			return "message in Properties-Map isn't equal to direct injected Message";
91  		}
92  	}
93  }