summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/loader/assembly/ComponentLoaderTestCase.java
blob: 37056b328300dce3bcc92fbe75eff599955ca928 (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
/**
 *
 * Copyright 2006 The Apache Software Foundation
 *
 *  Licensed 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.loader.assembly;

import java.util.List;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.tuscany.core.builder.ObjectFactory;
import org.apache.tuscany.core.config.ComponentTypeIntrospector;
import org.apache.tuscany.core.config.ConfigurationException;
import org.apache.tuscany.core.config.ConfigurationLoadException;
import org.apache.tuscany.core.extension.config.ImplementationProcessor;
import org.apache.tuscany.core.config.impl.Java5ComponentTypeIntrospector;
import org.apache.tuscany.core.config.processor.ProcessorUtils;
import org.apache.tuscany.core.injection.SingletonObjectFactory;
import org.apache.tuscany.core.loader.StAXPropertyFactory;
import org.apache.tuscany.core.loader.impl.StringParserPropertyFactory;
import org.apache.tuscany.core.system.assembly.SystemImplementation;
import org.apache.tuscany.model.assembly.Component;
import org.apache.tuscany.model.assembly.ConfiguredProperty;
import org.apache.tuscany.model.assembly.Property;
import org.apache.tuscany.model.assembly.AtomicComponent;

/**
 * @version $Rev$ $Date$
 */
public class ComponentLoaderTestCase extends LoaderTestSupport {
    private ComponentLoader loader;
    private ComponentTypeIntrospector introspector;

    public void testStringProperty() throws XMLStreamException, ConfigurationLoadException {
        String xml = "<properties><propString>HelloWorld</propString></properties>";
        Component component = createFooComponent();
        loadProperties(xml, component);
        ConfiguredProperty prop = component.getConfiguredProperty("propString");
        assertEquals("HelloWorld", prop.getValue());
    }

    public void testIntProperty() throws XMLStreamException, ConfigurationLoadException {
        String xml = "<properties><propInt>1234</propInt></properties>";
        Component component = createFooComponent();
        loadProperties(xml, component);
        ConfiguredProperty prop = component.getConfiguredProperty("propInt");
        assertEquals(1234, prop.getValue());
    }

    public void testIntegerProperty() throws XMLStreamException, ConfigurationLoadException {
        String xml = "<properties><propInteger>1234</propInteger></properties>";
        Component component = createFooComponent();
        loadProperties(xml, component);
        ConfiguredProperty prop = component.getConfiguredProperty("propInteger");
        assertEquals(Integer.valueOf(1234), prop.getValue());
    }

    public void testCustomProperty() throws XMLStreamException, ConfigurationLoadException {
        String xml = "<properties><propFoo factory='" + FooFactory.class.getName() + "'><name>Hello</name></propFoo></properties>";
        Component component = createFooComponent();
        loadProperties(xml, component);
        ConfiguredProperty prop = component.getConfiguredProperty("propFoo");
        Foo instance = (Foo) prop.getValue();
        assertEquals("Hello", instance.name);
    }

    private void loadProperties(String xml, Component component) throws XMLStreamException, ConfigurationLoadException {
        XMLStreamReader reader = getReader(xml);
        loader.loadProperties(reader, resourceLoader, component);
        component.initialize(modelContext);
    }

    private Component createFooComponent() {
        SystemImplementation impl = assemblyFactory.createSystemImplementation();
        impl.setImplementationClass(ServiceImpl.class);
        try {
            impl.setComponentType(introspector.introspect(ServiceImpl.class));
        } catch (ConfigurationException e) {
            throw new AssertionError();
        }
        impl.initialize(null);
        AtomicComponent component = assemblyFactory.createSimpleComponent();
        component.setImplementation(impl);
        return component;
    }

    protected void setUp() throws Exception {
        super.setUp();
        loader = new ComponentLoader();
        loader.setFactory(assemblyFactory);
        loader.setDefaultPropertyFactory(new StringParserPropertyFactory());
        introspector = ProcessorUtils.createCoreIntrospector(assemblyFactory);
    }

    public static interface Service {
    }

    public static class ServiceImpl implements Service {
        public String propString;
        public int propInt;
        public Integer propInteger;
        public Foo propFoo;
    }

    public static class Foo {
        public Foo() {
        }

        private String name;
        private Foo foo;

        public void setName(String val) {
            name = val;
        }

        public void setFoo(Foo val) {
            foo = val;
        }
/*

        private MyJaxBThing jaxBThing;

        public void setMyJaxBThing(MyJaxBThing thing) {
            jaxBthing = thing;
        }
*/
    }

    public static class FooFactory implements StAXPropertyFactory<Foo> {
        public ObjectFactory<Foo> createObjectFactory(XMLStreamReader reader, Property property) throws XMLStreamException, ConfigurationLoadException {
            reader.nextTag();
            String name = reader.getElementText();
            reader.next();
            Foo foo = new Foo();
            foo.setName(name);
            return new SingletonObjectFactory<Foo>(foo);
        }
    }
}