summaryrefslogtreecommitdiffstats
path: root/sandbox/ant/container.script/src/test/java/org/apache/tuscany/container/script/helper/ScriptHelperComponentTestCase.java
blob: a1e300051d26458c533b91a46d92259ed74cce88 (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
package org.apache.tuscany.container.script.helper;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import junit.framework.TestCase;

import org.apache.tuscany.container.script.helper.ScriptHelperComponent;
import org.apache.tuscany.container.script.helper.ScriptHelperInstance;
import org.apache.tuscany.container.script.helper.ScriptHelperInstanceFactory;
import org.apache.tuscany.container.script.helper.mock.MockInstanceFactory;
import org.apache.tuscany.core.wire.InboundWireImpl;
import org.apache.tuscany.core.wire.OutboundWireImpl;
import org.apache.tuscany.spi.component.ScopeContainer;
import org.apache.tuscany.spi.component.TargetException;
import org.apache.tuscany.spi.model.Operation;
import org.apache.tuscany.spi.model.Scope;
import org.apache.tuscany.spi.model.ServiceContract;
import org.apache.tuscany.spi.wire.InboundWire;
import org.apache.tuscany.spi.wire.OutboundWire;
import org.apache.tuscany.spi.wire.RuntimeWire;
import org.apache.tuscany.spi.wire.TargetInvoker;
import org.apache.tuscany.spi.wire.WireService;
import org.easymock.IAnswer;

public class ScriptHelperComponentTestCase extends TestCase {
    
    private ScopeContainer scopeContainer;

    @SuppressWarnings("unchecked")
    public void testCreateTargetInvoker() {
        ScriptHelperComponent component = new ScriptHelperComponent(null,null, null, null, null, scopeContainer, null, null, null);
        
        Operation operation = new Operation("hashCode", null,null,null,false,null);
        ServiceContract contract = new ServiceContract(List.class){};
        operation.setServiceContract(contract);
        TargetInvoker invoker = component.createTargetInvoker("hashCode", operation);
        
        assertNotNull(invoker);
    }

    @SuppressWarnings("unchecked")
    public void testCreateInstance() throws IOException {
        ScriptHelperComponent pc = new ScriptHelperComponent(null,createBSFEasy(), new HashMap(), null, null, scopeContainer, null, null, null);
        Object o = pc.createInstance();
        assertNotNull(o);
        assertTrue(o instanceof ScriptHelperInstance);
    }

    @SuppressWarnings("unchecked")
    public void testCreateInstanceWithRef() throws IOException {
        WireService wireService = createMock(WireService.class);
        expect(wireService.createProxy(isA(RuntimeWire.class))).andStubAnswer(new IAnswer() {
            public Object answer() throws Throwable {
                return Scope.MODULE;
            }
        });
       
        ScriptHelperComponent pc = new ScriptHelperComponent(null,createBSFEasy(), new HashMap(), null, null, scopeContainer, wireService, null, null);
        OutboundWire wire = new OutboundWireImpl();
        wire.setReferenceName("foo");
        pc.addOutboundWire(wire);
        Object o = pc.createInstance();
        assertNotNull(o);
        assertTrue(o instanceof ScriptHelperInstance);
    }

    @SuppressWarnings("unchecked")
    public void testGetServiceInstance() {
        WireService wireService = createMock(WireService.class);
        expect(wireService.createProxy(isA(RuntimeWire.class))).andStubAnswer(new IAnswer() {
            public Object answer() throws Throwable {
                return "foo";
            }
        });
        replay(wireService);
        ScriptHelperComponent pc = new ScriptHelperComponent(null,null, null, null, null, scopeContainer, wireService, null, null);
        InboundWire wire = new InboundWireImpl();
        pc.addInboundWire(wire);
        assertEquals("foo", pc.getServiceInstance());
    }

    @SuppressWarnings("unchecked")
    public void testGetServiceInstanceFail() {
        ScriptHelperComponent pc = new ScriptHelperComponent(null,null, null, null, null, scopeContainer, null, null, null);
        try {
            pc.getServiceInstance();
            fail();
        } catch (TargetException e) {
            // expected
        }
    }

    @SuppressWarnings("unchecked")
    public void testGetproperties() {
        ScriptHelperComponent pc = new ScriptHelperComponent(null,null, new HashMap(), null, null, scopeContainer, null, null, null);
        assertNotNull(pc.getProperties());
    }

    @SuppressWarnings("unchecked")
    public void testGetServiceInterfaces() {
        List services = new ArrayList();
        ScriptHelperComponent pc = new ScriptHelperComponent(null,null,null, services, null, scopeContainer, null, null, null);
        assertEquals(services, pc.getServiceInterfaces());
    }

    @SuppressWarnings("unchecked")
    public void testCreateAsyncTargetInvoker() {
        ScriptHelperComponent pc = new ScriptHelperComponent(null,null,null, new ArrayList<Class<?>>(), null, scopeContainer, null, null, null);
        assertNotNull(pc.createAsyncTargetInvoker(null, new Operation("foo", null,null,null)));
    }
    
    @Override
    @SuppressWarnings("unchecked")
    protected void setUp() throws Exception {
        super.setUp();
        this.scopeContainer = createMock(ScopeContainer.class);
        expect(scopeContainer.getScope()).andStubAnswer(new IAnswer() {
            public Object answer() throws Throwable {
                return Scope.MODULE;
            }
        });
    }

    public ScriptHelperInstanceFactory createBSFEasy() throws IOException {
//        URL scriptURL = getClass().getResource("foo.mock");
//        InputStream is = scriptURL.openStream();
//        StringBuilder sb = new StringBuilder();
//        int i = 0;
//        while ((i = is.read()) != -1) {
//            sb.append((char) i);
//        }
//        is.close();
//        String script = sb.toString();
        MockInstanceFactory bsfEasy = new MockInstanceFactory("foo.mock", null);
        return bsfEasy;
    }
}