summaryrefslogtreecommitdiffstats
path: root/branches/sca-java-integration/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/processor/MonitorProcessorTestCase.java
blob: 8982fa3991189e4729b8ceb3cd737a63d304c952 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * 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.processor;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

import org.apache.tuscany.spi.implementation.java.ConstructorDefinition;
import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
import org.apache.tuscany.spi.implementation.java.JavaMappedReference;
import org.apache.tuscany.spi.implementation.java.JavaMappedService;
import org.apache.tuscany.spi.implementation.java.PojoComponentType;
import org.apache.tuscany.spi.implementation.java.IllegalPropertyException;

import junit.framework.TestCase;
import org.apache.tuscany.api.annotation.Monitor;
import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl;
import org.apache.tuscany.core.injection.SingletonObjectFactory;
import org.apache.tuscany.host.MonitorFactory;
import org.easymock.EasyMock;

/**
 * @version $Rev$ $Date$
 */
public class MonitorProcessorTestCase extends TestCase {

    private MonitorProcessor processor;
    private MonitorFactory monitorFactory;

    public void testSetter() throws Exception {
        PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
            new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
        Method method = Foo.class.getMethod("setMonitor", Foo.class);
        EasyMock.expect(monitorFactory.getMonitor(EasyMock.eq(Foo.class))).andReturn(null);
        EasyMock.replay(monitorFactory);
        processor.visitMethod(null, method, type, null);
        Map<String, JavaMappedProperty<?>> properties = type.getProperties();
        assertTrue(properties.get("monitor").getDefaultValueFactory() instanceof SingletonObjectFactory);
        EasyMock.verify(monitorFactory);
    }


    public void testBadSetter() throws Exception {
        PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
            new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
        Method method = BadMonitor.class.getMethod("setMonitor");
        try {
            processor.visitMethod(null, method, type, null);
            fail();
        } catch (IllegalPropertyException e) {
            // expected
        }
    }

    public void testField() throws Exception {
        PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
            new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
        Field field = Foo.class.getDeclaredField("bar");
        EasyMock.expect(monitorFactory.getMonitor(EasyMock.eq(Foo.class))).andReturn(null);
        EasyMock.replay(monitorFactory);
        processor.visitField(null, field, type, null);
        Map<String, JavaMappedProperty<?>> properties = type.getProperties();
        assertTrue(properties.get("bar").getDefaultValueFactory() instanceof SingletonObjectFactory);
        EasyMock.verify(monitorFactory);
    }

    public void testConstructor() throws Exception {
        PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
            new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
        Constructor<Bar> ctor = Bar.class.getConstructor(BazMonitor.class);
        EasyMock.expect(monitorFactory.getMonitor(EasyMock.eq(BazMonitor.class))).andReturn(null);
        EasyMock.replay(monitorFactory);
        processor.visitConstructor(null, ctor, type, null);
        Map<String, JavaMappedProperty<?>> properties = type.getProperties();
        assertTrue(
            properties.get(BazMonitor.class.getName()).getDefaultValueFactory() instanceof SingletonObjectFactory);
        EasyMock.verify(monitorFactory);
    }

    /**
     * Verifies calling the monitor processor to evaluate a constructor can be done after a property parameter is
     * processed
     */
    public void testConstructorAfterProperty() throws Exception {
        PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
            new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
        Constructor<Bar> ctor = Bar.class.getConstructor(String.class, BazMonitor.class);
        EasyMock.expect(monitorFactory.getMonitor(EasyMock.eq(BazMonitor.class))).andReturn(null);
        EasyMock.replay(monitorFactory);
        ConstructorDefinition<Bar> definition = new ConstructorDefinition<Bar>(ctor);
        JavaMappedProperty prop = new JavaMappedProperty();
        definition.getInjectionNames().add("prop");
        type.setConstructorDefinition(definition);
        type.getProperties().put("prop", prop);
        processor.visitConstructor(null, ctor, type, null);
        Map<String, JavaMappedProperty<?>> properties = type.getProperties();
        assertEquals(BazMonitor.class.getName(), definition.getInjectionNames().get(1));
        assertEquals(2, type.getProperties().size());
        String name = BazMonitor.class.getName();
        assertTrue(properties.get(name).getDefaultValueFactory() instanceof SingletonObjectFactory);
        EasyMock.verify(monitorFactory);
    }

    /**
     * Verifies calling the monitor processor to evaluate a constructor can be done before a property parameter is
     * processed
     */
    public void testConstructorBeforeProperty() throws Exception {
        PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
            new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
        Constructor<Bar> ctor = Bar.class.getConstructor(String.class, BazMonitor.class);
        EasyMock.expect(monitorFactory.getMonitor(EasyMock.eq(BazMonitor.class))).andReturn(null);
        EasyMock.replay(monitorFactory);
        processor.visitConstructor(null, ctor, type, null);
        Map<String, JavaMappedProperty<?>> properties = type.getProperties();
        ConstructorDefinition definition = type.getConstructorDefinition();
        assertEquals(2, definition.getInjectionNames().size());
        assertEquals(BazMonitor.class.getName(), definition.getInjectionNames().get(1));
        String name = BazMonitor.class.getName();
        assertTrue(properties.get(name).getDefaultValueFactory() instanceof SingletonObjectFactory);
        EasyMock.verify(monitorFactory);
    }

    protected void setUp() throws Exception {
        super.setUp();
        monitorFactory = EasyMock.createMock(MonitorFactory.class);
        JavaInterfaceProcessorRegistryImpl registry = new JavaInterfaceProcessorRegistryImpl();
        ImplementationProcessorServiceImpl processor = new ImplementationProcessorServiceImpl(registry);
        this.processor = new MonitorProcessor(monitorFactory, processor);
    }

    private class Foo {

        @Monitor
        protected Foo bar;

        @Monitor
        public void setMonitor(Foo foo) {
        }
    }


    private class BadMonitor {

        @Monitor
        public void setMonitor() {
        }
    }

    private interface BazMonitor {

    }

    private static class Bar {

        public Bar(@Monitor BazMonitor monitor) {
        }

        public Bar(String prop, @Monitor BazMonitor monitor) {
        }
    }
}