summaryrefslogtreecommitdiffstats
path: root/sandbox/ant/container.python/src/test/java/org/apache/tuscany/container/python/PythonComponentTestCase.java
blob: 3a1f6081bac34ee5cfe0e3a74e0b09acb26544d4 (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
package org.apache.tuscany.container.python;

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

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

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.TargetInvoker;
import org.easymock.IAnswer;

public class PythonComponentTestCase extends TestCase {
    
    private ScopeContainer scopeContainer;

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

    @SuppressWarnings("unchecked")
    public void testCreateInstance() throws IOException {
        PythonComponent pc = new PythonComponent(null,createPythonScript(),null, null, scopeContainer, null, null);
        Object o = pc.createInstance();
        assertNotNull(o);
    }

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

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

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

    @Override
    @SuppressWarnings("unchecked")
    protected void setUp() {
        this.scopeContainer = createMock(ScopeContainer.class);
        expect(scopeContainer.getScope()).andStubAnswer(new IAnswer() {
            public Object answer() throws Throwable {
                return Scope.MODULE;
            }
        });
    }

    public PythonScript createPythonScript() throws IOException {
        URL scriptURL = getClass().getResource("PythonScriptTestCase.py");
        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();
        PythonScript pythonScript = new PythonScript("PythonScriptTestCase", "hello", script, null);
        pythonScript.setServiceInterface(HelloWorldService.class);
        return pythonScript;
    }
}