diff options
Diffstat (limited to 'dropwizard-ioc-container/src/test')
6 files changed, 192 insertions, 0 deletions
diff --git a/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/IOCBundleTest.java b/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/IOCBundleTest.java new file mode 100644 index 0000000..b0cac65 --- /dev/null +++ b/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/IOCBundleTest.java @@ -0,0 +1,49 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.dropwizard.ioc.test.service; + +import org.glassfish.hk2.api.ServiceLocator; +import org.junit.Before; +import org.junit.Test; +import org.openo.dropwizard.ioc.test.service.app.ExampleApp; +import org.openo.dropwizard.ioc.utils.ServiceLocatorHolder; + +import junit.framework.Assert; + +public class IOCBundleTest { + + + + @Before + public void setUp() throws Exception { + ExampleApp.main(null); + } + + @Test + public void test() { + + ServiceLocator locator = ServiceLocatorHolder.getLocator(); + + TestService testService = locator.getService(TestService.class); + + Assert.assertNotNull(testService); + + Assert.assertEquals(TestService.HELLO, testService.sayHello()); + + Assert.assertEquals(PreTestService.HELLO, testService.sayHelloToPreTestService()); + } + +} diff --git a/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/PreTestService.java b/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/PreTestService.java new file mode 100644 index 0000000..51cc386 --- /dev/null +++ b/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/PreTestService.java @@ -0,0 +1,33 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.dropwizard.ioc.test.service; + +import org.openo.dropwizard.ioc.annotation.PreServiceLoad; + +/** + * @author hu.rui + * + */ +@PreServiceLoad +public class PreTestService { + + public static final String HELLO = "hello Pre Service"; + + public String sayHello(){ + return HELLO; + } + +} diff --git a/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/TestService.java b/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/TestService.java new file mode 100644 index 0000000..3d0f945 --- /dev/null +++ b/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/TestService.java @@ -0,0 +1,41 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.dropwizard.ioc.test.service; + +import javax.inject.Inject; + +import org.jvnet.hk2.annotations.Service; + +/** + * @author hu.rui + * + */ +@Service +public class TestService { + + @Inject + private PreTestService preTestService; + + public static final String HELLO = "hello Service"; + + public String sayHello(){ + return HELLO; + } + + public String sayHelloToPreTestService(){ + return preTestService.sayHello(); + } +} diff --git a/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/app/ExampleApp.java b/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/app/ExampleApp.java new file mode 100644 index 0000000..1e1f3ea --- /dev/null +++ b/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/app/ExampleApp.java @@ -0,0 +1,35 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.dropwizard.ioc.test.service.app; + +import org.openo.dropwizard.ioc.bundle.IOCApplication; +import org.openo.dropwizard.ioc.test.service.conf.TestConfiguration; + +public class ExampleApp extends IOCApplication<TestConfiguration>{ + + private static final String CONFIGURATION_FILE = "example.yml"; + + public static void main(String[] args) throws Exception { + + String configFile = ExampleApp.class.getClassLoader().getResource(CONFIGURATION_FILE).getFile(); + + args = new String[]{"server",configFile}; + + new ExampleApp().run(args); + } + + +} diff --git a/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/conf/TestConfiguration.java b/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/conf/TestConfiguration.java new file mode 100644 index 0000000..82c30cd --- /dev/null +++ b/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/conf/TestConfiguration.java @@ -0,0 +1,26 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.dropwizard.ioc.test.service.conf; + +import io.dropwizard.Configuration; + +/** + * @author hu.rui + * + */ +public class TestConfiguration extends Configuration { + +} diff --git a/dropwizard-ioc-container/src/test/resources/example.yml b/dropwizard-ioc-container/src/test/resources/example.yml new file mode 100644 index 0000000..c3ef0f0 --- /dev/null +++ b/dropwizard-ioc-container/src/test/resources/example.yml @@ -0,0 +1,8 @@ +server: + type: simple + rootPath: '/service/*' + applicationContextPath: / + connector: + type: http + port: 45673 +
\ No newline at end of file |