KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > factory > TestBeanFactoryObjectProvider


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.lib.factory;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.Location;
19 import org.apache.hivemind.internal.Module;
20 import org.apache.hivemind.lib.BeanFactory;
21 import org.apache.hivemind.service.ObjectProvider;
22 import org.apache.hivemind.test.HiveMindTestCase;
23 import org.easymock.MockControl;
24
25 /**
26  * Tests for {@link org.apache.hivemind.lib.factory.BeanObjectProvider}.
27  *
28  * @author Howard Lewis Ship
29  */

30 public class TestBeanFactoryObjectProvider extends HiveMindTestCase
31 {
32     public void testNullInput()
33     {
34         ObjectProvider op = new BeanFactoryObjectProvider();
35
36         assertNull(op.provideObject(null, null, null, null));
37     }
38
39     public void testNoServiceId()
40     {
41         ObjectProvider op = new BeanFactoryObjectProvider();
42         Location l = newLocation();
43
44         try
45         {
46             op.provideObject(null, null, "foo", l);
47             unreachable();
48         }
49         catch (ApplicationRuntimeException ex)
50         {
51             assertEquals(
52                 "'foo' is not formatted correctly, it should be 'service-id:name[,initializer]'.",
53                 ex.getMessage());
54
55             assertSame(l, ex.getLocation());
56         }
57     }
58
59     public void testNoLocator()
60     {
61         ObjectProvider op = new BeanFactoryObjectProvider();
62         Location l = newLocation();
63
64         try
65         {
66             op.provideObject(null, null, "foo:", l);
67             unreachable();
68         }
69         catch (ApplicationRuntimeException ex)
70         {
71             assertEquals(
72                 "'foo:' is not formatted correctly, it should be 'service-id:name[,initializer]'.",
73                 ex.getMessage());
74             assertSame(l, ex.getLocation());
75         }
76     }
77
78     public void testSuccess()
79     {
80         String JavaDoc result = "Obtained via BeanFactory.";
81
82         MockControl factoryControl = newControl(BeanFactory.class);
83         BeanFactory factory = (BeanFactory) factoryControl.getMock();
84
85         MockControl moduleControl = newControl(Module.class);
86         Module module = (Module) moduleControl.getMock();
87
88         module.getService("factory", BeanFactory.class);
89         moduleControl.setReturnValue(factory);
90
91         factory.get("my-bean,initialized");
92         factoryControl.setReturnValue(result);
93
94         replayControls();
95
96         ObjectProvider op = new BeanFactoryObjectProvider();
97
98         assertSame(result, op.provideObject(module, null, "factory:my-bean,initialized", null));
99
100         verifyControls();
101     }
102 }
103
Popular Tags