summaryrefslogtreecommitdiffstats
path: root/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/implementation/system/component/SystemAtomicComponentTestCase.java
blob: b8789cb8b4bcd5067aab15223b625be039b543fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.tuscany.core.implementation.system.component;

import org.apache.tuscany.spi.wire.OutboundWire;

import junit.framework.TestCase;
import org.apache.tuscany.core.implementation.PojoConfiguration;
import org.apache.tuscany.core.injection.EventInvoker;
import org.apache.tuscany.core.injection.MethodEventInvoker;
import org.apache.tuscany.core.injection.PojoObjectFactory;
import org.apache.tuscany.core.injection.SingletonObjectFactory;
import org.easymock.EasyMock;

/**
 * Verifies a system atomic component can be started and initialized
 *
 * @version $$Rev$$ $$Date$$
 */
public class SystemAtomicComponentTestCase extends TestCase {

    private EventInvoker<Object> initInvoker;
    private EventInvoker<Object> destroyInvoker;

    public void testDefaultCreationAndInit() throws Exception {
        PojoObjectFactory<Foo> factory = new PojoObjectFactory<Foo>(Foo.class.getConstructor((Class[]) null));
        PojoConfiguration configuration = new PojoConfiguration();
        configuration.setInstanceFactory(factory);
        configuration.setInitInvoker(initInvoker);
        configuration.setName("foo");
        SystemAtomicComponentImpl component = new SystemAtomicComponentImpl(configuration);
        Foo foo = (Foo) component.createInstance();
        component.init(foo);
        assertTrue(foo.initialized);
    }

    public void testDestroy() throws Exception {
        PojoObjectFactory<Foo> factory = new PojoObjectFactory<Foo>(Foo.class.getConstructor((Class[]) null));
        PojoConfiguration configuration = new PojoConfiguration();
        configuration.setInstanceFactory(factory);
        configuration.setDestroyInvoker(destroyInvoker);
        configuration.setName("foo");
        SystemAtomicComponentImpl component = new SystemAtomicComponentImpl(configuration);
        Foo foo = (Foo) component.createInstance();
        component.destroy(foo);
        assertTrue(foo.destroyed);
    }

    public void testReferenceAndPropertyConstructor() throws Exception {
        PojoObjectFactory<Bar> factory = new PojoObjectFactory<Bar>(Bar.class.getConstructor(String.class, Foo.class));
        PojoConfiguration configuration = new PojoConfiguration();
        configuration.setInstanceFactory(factory);
        configuration.setInitInvoker(initInvoker);
        configuration.addConstructorParamName("foo");
        configuration.addConstructorParamType(String.class);
        configuration.addConstructorParamName("ref");
        configuration.addConstructorParamType(Foo.class);
        configuration.setName("foo");
        SystemAtomicComponentImpl component = new SystemAtomicComponentImpl(configuration);
        component.addPropertyFactory("foo", new SingletonObjectFactory<String>("baz"));
        Foo target = new Foo();
        OutboundWire wire = EasyMock.createMock(OutboundWire.class);
        EasyMock.expect(wire.getTargetService()).andReturn(target);
        EasyMock.expect(wire.getReferenceName()).andReturn("ref").anyTimes();
        EasyMock.replay(wire);
        component.addOutboundWire(wire);
        Bar bar = (Bar) component.createInstance();
        assertEquals("baz", bar.foo);
        assertEquals(target, bar.ref);
        EasyMock.verify(wire);
    }

    protected void setUp() throws Exception {
        super.setUp();
        initInvoker = new MethodEventInvoker<Object>(Foo.class.getMethod("init"));
        destroyInvoker = new MethodEventInvoker<Object>(Foo.class.getMethod("destroy"));
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public static class Foo {
        private boolean initialized;
        private boolean destroyed;

        public void init() {
            initialized = true;
        }

        public void destroy() {
            destroyed = true;
        }
    }

    public static class Bar {

        private String foo;
        private Foo ref;

        public Bar(String foo, Foo ref) {
            this.foo = foo;
            this.ref = ref;
        }
    }


}