/*
* 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.java;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import org.apache.tuscany.spi.component.AtomicComponent;
import org.apache.tuscany.spi.component.CompositeComponent;
import org.apache.tuscany.spi.component.ScopeContainer;
import org.apache.tuscany.spi.deployer.DeploymentContext;
import org.apache.tuscany.spi.idl.java.JavaServiceContract;
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.model.ComponentDefinition;
import org.apache.tuscany.spi.model.Scope;
import org.apache.tuscany.spi.model.ServiceContract;
import junit.framework.TestCase;
import org.apache.tuscany.core.deployer.RootDeploymentContext;
import org.apache.tuscany.core.implementation.composite.CompositeComponentImpl;
import org.apache.tuscany.core.implementation.java.mock.components.Source;
import org.apache.tuscany.core.implementation.java.mock.components.SourceImpl;
import org.apache.tuscany.core.implementation.java.mock.components.Target;
import org.apache.tuscany.core.wire.jdk.JDKWireService;
import org.easymock.EasyMock;
import org.easymock.IAnswer;
/**
* @version $$Rev: 451895 $$ $$Date: 2006-10-01 23:58:18 -0700 (Sun, 01 Oct 2006) $$
*/
public class JavaComponentBuilderTestCase extends TestCase {
private DeploymentContext deploymentContext;
public void testBuild() throws Exception {
CompositeComponent parent = new CompositeComponentImpl(null, null, null, null);
PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> sourceType =
new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
sourceType.setImplementationScope(Scope.MODULE);
JavaMappedReference reference = new JavaMappedReference();
reference.setName("target");
reference.setMember(SourceImpl.class.getMethod("setTarget", Target.class));
sourceType.add(reference);
ServiceContract<?> sourceContract = new JavaServiceContract(Source.class);
JavaMappedService sourceServiceDefinition = new JavaMappedService();
sourceServiceDefinition.setName("Source");
sourceServiceDefinition.setServiceContract(sourceContract);
sourceType.add(sourceServiceDefinition);
Constructor<SourceImpl> constructor = SourceImpl.class.getConstructor((Class[]) null);
sourceType.setConstructorDefinition(new ConstructorDefinition<SourceImpl>(constructor));
JavaImplementation sourceImpl = new JavaImplementation();
sourceImpl.setComponentType(sourceType);
sourceImpl.setImplementationClass(SourceImpl.class);
ComponentDefinition<JavaImplementation> sourceComponentDefinition =
new ComponentDefinition<JavaImplementation>(sourceImpl);
JavaComponentBuilder builder = new JavaComponentBuilder();
builder.setWireService(new JDKWireService());
JavaAtomicComponent component =
(JavaAtomicComponent) builder.build(parent, sourceComponentDefinition, deploymentContext);
deploymentContext.getModuleScope().start();
component.start();
Source source = (Source) component.getServiceInstance();
assertNotNull(source);
component.stop();
}
protected void setUp() throws Exception {
super.setUp();
deploymentContext = new RootDeploymentContext(null, null, createMock(), null);
}
private ScopeContainer createMock() {
ScopeContainer scope = EasyMock.createMock(ScopeContainer.class);
scope.start();
scope.stop();
scope.register(EasyMock.isA(AtomicComponent.class));
EasyMock.expectLastCall().atLeastOnce();
EasyMock.expect(scope.getScope()).andReturn(Scope.MODULE).atLeastOnce();
scope.getInstance(EasyMock.isA(AtomicComponent.class));
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
private Map<AtomicComponent, Object> cache = new HashMap<AtomicComponent, Object>();
public Object answer() throws Throwable {
AtomicComponent component = (AtomicComponent) EasyMock.getCurrentArguments()[0];
Object instance = cache.get(component);
if (instance == null) {
instance = component.createInstance();
cache.put(component, instance);
}
return instance;
}
}).anyTimes();
EasyMock.replay(scope);
return scope;
}
}