learn tuscany by example I

I.最简单的(计算器实例)

Cal.composite(*.composite文件必需位于classLoader能找到的位置 )

<?xml version="1.0" encoding="UTF-8"?>
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0" xmlns:c="http://Cal" targetNamespace="http://Cal" name="Cal">
	<component name="CalServiceComponent">
		<implementation.java class="cal.CalService" />
		<reference name="addService" target="AddServiceComponent" />
	</component>
	<component name="AddServiceComponent">
		<implementation.java class="cal.AddService" />
	</component>
</composite>

 CalService.java(需要设置addService为reference )

package cal;

import org.osoa.sca.annotations.Reference;

public class CalService {
	private AddService addService;
	public int add(int n, int m){
		return addService.add(n, m);
	}
	
	@Reference//must have this annotation
	public void setAddService(AddService addService){
		this.addService = addService;
	}
}

 AddService.java

package cal;

public class AddService {
	public int add(int n, int m){
		return n + m;
	}
}

  CalServiceTest.java(可以采用SCADomain方式或SCANode2方式启动,其它方式? )

package cal;

import static org.junit.Assert.*;

import org.apache.tuscany.sca.host.embedded.SCADomain;
import org.junit.Before;
import org.junit.Test;

import cal.CalService;

public class CalServiceTest {
	private SCADomain scaDomain;
	private SCANode2 node;
	private CalService calService;

	@Before
	public void init(){
		//setupViaSCADomain();
		setupViaNode2();
	}
	
	@Test
	public void testAdd() {
		assertEquals(3, calService.add(1, 2));
	}
	
	private void setupViaSCADomain(){
		scaDomain = SCADomain.newInstance("Cal.composite");
		calService = scaDomain.getService(CalService.class, "CalServiceComponent");
	}
	
	private void setupViaNode2(){
		node = SCANode2Factory.newInstance().createSCANodeFromClassLoader("Cal.composite", null);
		node.start();
		calService = ((SCAClient)node).getService(CalService.class, "CalServiceComponent");
	}
}
 

<!----><!----> <!----><!----><!----><!---->

你可能感兴趣的:(java,apache,c,xml,JUnit)