01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.tapestry.ioc.internal.services;
16:
17: import org.apache.tapestry.ioc.internal.IOCInternalTestCase;
18: import org.apache.tapestry.ioc.services.DefaultImplementationBuilder;
19: import org.testng.annotations.AfterClass;
20: import org.testng.annotations.BeforeClass;
21: import org.testng.annotations.Test;
22:
23: public class DefaultImplementationBuilderImplTest extends
24: IOCInternalTestCase {
25: private DefaultImplementationBuilder _builder;
26:
27: @BeforeClass
28: public void setup_builder() {
29: _builder = getService("DefaultImplementationBuilder",
30: DefaultImplementationBuilder.class);
31: }
32:
33: @AfterClass
34: public void cleanup_builder() {
35: _builder = null;
36: }
37:
38: @Test
39: public void simple_interface() {
40: Runnable r = _builder
41: .createDefaultImplementation(Runnable.class);
42:
43: r.run();
44:
45: assertEquals(r.toString(), "<NoOp java.lang.Runnable>");
46: }
47:
48: public interface ToString {
49: String toString();
50: }
51:
52: @Test
53: public void interface_has_toString() {
54: ToString ts = _builder
55: .createDefaultImplementation(ToString.class);
56:
57: assertNull(ts.toString());
58: }
59:
60: @Test
61: public void instances_are_cached() {
62: Runnable r1 = null;
63: Runnable r2 = null;
64:
65: // With tests in parallel, there's a harmless race condition that can cause r1 != r2
66: // for one pass, so we give it a second chance to prove itself.
67:
68: for (int i = 0; i < 2; i++) {
69: r1 = _builder.createDefaultImplementation(Runnable.class);
70: r2 = _builder.createDefaultImplementation(Runnable.class);
71:
72: if (r1 == r2)
73: break;
74: }
75:
76: assertSame(r2, r1);
77: }
78: }
|