KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > impl > TestObjectTranslator


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.service.impl;
16
17 import java.util.Collections JavaDoc;
18
19 import org.apache.hivemind.ErrorLog;
20 import org.apache.hivemind.internal.Module;
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.service.impl.ObjectTranslator}.
27  *
28  * @author Howard Lewis Ship
29  */

30 public class TestObjectTranslator extends HiveMindTestCase
31 {
32
33     public void testBadLocator()
34     {
35         ObjectTranslator ot = new ObjectTranslator();
36
37         Module module = (Module) newMock(Module.class);
38
39         ErrorLog el = (ErrorLog) newMock(ErrorLog.class);
40
41         ot.setErrorLog(el);
42         ot.setContributions(Collections.EMPTY_MAP);
43
44         el.error("Object provider selector 'badprefix' is not properly formatted.", null, null);
45
46         replayControls();
47
48         Object JavaDoc result = ot.translate(module, Object JavaDoc.class, "badprefix", null);
49
50         assertNull(result);
51
52         verifyControls();
53     }
54
55     public void testUnknownPrefix()
56     {
57         ObjectTranslator ot = new ObjectTranslator();
58
59         Module module = (Module) newMock(Module.class);
60
61         ErrorLog el = (ErrorLog) newMock(ErrorLog.class);
62
63         ot.setErrorLog(el);
64         ot.setContributions(Collections.EMPTY_MAP);
65
66         el.error("No object provider exists for prefix 'zap'.", null, null);
67
68         replayControls();
69
70         Object JavaDoc result = ot.translate(module, Object JavaDoc.class, "zap:foo", null);
71
72         assertNull(result);
73
74         verifyControls();
75     }
76
77     public void testSuccess()
78     {
79         MockControl c = newControl(ObjectProvider.class);
80         ObjectProvider p = (ObjectProvider) c.getMock();
81
82         ObjectTranslator ot = new ObjectTranslator();
83         ot.setContributions(Collections.singletonMap("fetch", p));
84
85         Module module = (Module) newMock(Module.class);
86
87         p.provideObject(module, Integer JavaDoc.class, "zap", null);
88         Object JavaDoc value = new Integer JavaDoc(13);
89
90         c.setReturnValue(value);
91
92         replayControls();
93
94         Object JavaDoc result = ot.translate(module, Integer JavaDoc.class, "fetch:zap", null);
95
96         assertSame(value, result);
97
98         verifyControls();
99     }
100
101     public void testNullInput()
102     {
103         ObjectTranslator ot = new ObjectTranslator();
104
105         assertNull(ot.translate(null, null, null, null));
106     }
107 }
Popular Tags