Commit 92150b8b authored by Rodrigo Carneiro Henrique's avatar Rodrigo Carneiro Henrique
Browse files

[SCS-230]: Refactoring da API, demos e testes Java

(commit parcial) Adição dos primeiros testes unitários


git-svn-id: https://subversion.tecgraf.puc-rio.br/engdist/scs/core/java/trunk@114702 ae0415b3-e90b-0410-900d-d0be9363c56b
parent 474e5820
......@@ -12,15 +12,25 @@
<properties>
<java.version>1.5</java.version>
<!-- comentei pela ausencia de testes
<junit.version>4.6</junit.version>
-->
<jacorb.version>2.3.0</jacorb.version>
<source.encoding>ISO-8859-1</source.encoding>
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
<testSourceDirectory>test/src</testSourceDirectory>
<testResources>
<testResource>
<directory>test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
......@@ -57,13 +67,11 @@
<version>${jacorb.version}</version>
</dependency>
<!-- comentei pela ausencia de testes
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
-->
</dependencies>
</project>
package scs.core;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
......@@ -16,9 +17,6 @@ public class ComponentContext {
private ORB orb;
private POA poa;
private ComponentId componentId;
// facetDescs contain CORBA objects (field facet_ref)
// facets contain JAVA objects (servants)
private Map<String, Facet> facets;
private Map<String, Receptacle> receptacles;
......@@ -39,15 +37,22 @@ public class ComponentContext {
* @throws SCSException
*/
public ComponentContext(ORB orb, POA poa, ComponentId id) throws SCSException {
if ((id == null) || (orb == null) || (poa == null)) {
throw new IllegalArgumentException();
if (orb == null) {
throw new IllegalArgumentException("The ORB can't be null");
}
if (poa == null) {
throw new IllegalArgumentException("The POA can't be null");
}
if (id == null) {
throw new IllegalArgumentException("The component's id can't be null");
}
this.orb = orb;
this.poa = poa;
componentId = id;
facets = new HashMap<String, Facet>();
receptacles = new HashMap<String, Receptacle>();
componentId = id;
addBasicFacets();
}
......@@ -61,24 +66,36 @@ public class ComponentContext {
new IMetaInterfaceServant(this));
}
public void checkForGetComponent(Class<?> c, String name) {
private static void checkForGetComponentMethod(Servant facet) {
try {
c.getMethod("_get_component", (Class<?>[]) null);
Method getComponentMethod =
facet.getClass().getMethod("_get_component", (Class<?>[]) null);
if (getComponentMethod == null) {
//TODO: logar como warn que _get_component no foi definida.
/*
* System.err.println("_get_component nao foi definida para a classe " +
* name + "!");
*/
}
}
catch (Exception e) {
catch (NoSuchMethodException e) {
//TODO: logar como warn que _get_component no foi definida.
/*
* System.err.println("_get_component nao foi definida para a classe " +
* name + "!");
*/
}
catch (SecurityException e) {
//TODO: logar erro.
}
}
private void deactivateFacet(Facet facet) throws SCSException {
if (facet != null) {
try {
poa.deactivate_object(poa
.reference_to_id(facet.getDescription().facet_ref));
poa
.deactivate_object(poa.reference_to_id(facet.getDescription().facet_ref));
}
catch (UserException e) {
throw new SCSException(e);
......@@ -92,7 +109,7 @@ public class ComponentContext {
public void putFacet(String name, String interface_name, Servant servant)
throws SCSException {
checkForGetComponent(servant.getClass(), name);
checkForGetComponentMethod(servant);
try {
Facet facet =
new Facet(name, interface_name, poa.servant_to_reference(servant),
......@@ -169,8 +186,8 @@ public class ComponentContext {
Map<String, SCSException> errMsgs = new HashMap<String, SCSException>();
for (Facet facet : facets.values()) {
try {
poa.deactivate_object(poa
.reference_to_id(facet.getDescription().facet_ref));
poa
.deactivate_object(poa.reference_to_id(facet.getDescription().facet_ref));
}
catch (UserException e) {
//TODO: logar erro ao desativar faceta como warn
......@@ -185,6 +202,9 @@ public class ComponentContext {
}
public Facet getFacetByName(String name) {
if (name == null) {
throw new IllegalArgumentException("The facet's name can't be null");
}
return facets.get(name);
}
......@@ -210,14 +230,18 @@ public class ComponentContext {
/**
* Returns a stringified version of the component's id, concatenating its name
* with the version numbers.
* with the version number and its platform spec.
*
* @return String The stringified ComponentId.
* @return The stringified component's id.
*/
public String stringifiedComponentId() {
return componentId.name + String.valueOf(componentId.major_version)
+ String.valueOf(componentId.minor_version)
+ String.valueOf(componentId.patch_version);
public String getComponentIdAsString() {
StringBuilder builder = new StringBuilder();
builder.append(componentId.name);
builder.append(componentId.major_version);
builder.append(componentId.minor_version);
builder.append(componentId.patch_version);
builder.append(componentId.platform_spec);
return builder.toString();
}
/**
......
......@@ -17,7 +17,7 @@ public final class Facet {
private org.omg.CORBA.Object facet_ref;
private Servant servant;
public Facet(String name, String interface_name,
Facet(String name, String interface_name,
org.omg.CORBA.Object facet_ref, Servant servant) {
this.name = name;
this.interface_name = interface_name;
......
......@@ -21,7 +21,7 @@ public class Receptacle {
* retornar como ConnectionDescription[] (mapeamento padro de sequence
* CORBA).
*/
List<ConnectionDescription> connections;
private List<ConnectionDescription> connections;
/**
* Construtor a partir de uma descrio.
......@@ -33,6 +33,17 @@ public class Receptacle {
*/
Receptacle(ComponentContext context, String name, String interface_name,
boolean is_multiplex) {
if (context == null) {
throw new IllegalArgumentException(
"The component's context can't be null");
}
if (name == null) {
throw new IllegalArgumentException("The name can't be null");
}
if (interface_name == null) {
throw new IllegalArgumentException("The interface's name can't be null");
}
this.myComponent = context;
this.name = name;
this.interface_name = interface_name;
......@@ -84,6 +95,10 @@ public class Receptacle {
*/
public synchronized int addConnection(org.omg.CORBA.Object obj)
throws AlreadyConnected, ExceededConnectionLimit, InvalidConnection {
if (obj == null) {
throw new IllegalArgumentException("The connection can't be null");
}
if ((!is_multiplex) && (getConnectionsSize() > 0)) {
throw new AlreadyConnected();
}
......
package scs.core;
import java.util.Collection;
import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.omg.CORBA.ORB;
import org.omg.CORBA.UserException;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import scs.core.exception.SCSException;
public final class ComponentContextTest {
private static ORB orb;
private static POA poa;
private static ComponentId componentId;
@BeforeClass
public static void beforeClass() throws UserException {
orb = ORB.init((String[]) null, null);
org.omg.CORBA.Object obj = orb.resolve_initial_references("RootPOA");
poa = POAHelper.narrow(obj);
componentId =
new ComponentId("componente", (byte) 1, (byte) 0, (byte) 0, "java");
}
@AfterClass
public static void afterClass() {
componentId = null;
poa.destroy(true, true);
orb.destroy();
}
@Test
public void constructContext() throws SCSException {
new ComponentContext(orb, poa, componentId);
}
@Test(expected = IllegalArgumentException.class)
public void constructContext2() throws SCSException {
new ComponentContext(null, poa, componentId);
}
@Test(expected = IllegalArgumentException.class)
public void constructContext3() throws SCSException {
new ComponentContext(orb, null, componentId);
}
@Test(expected = IllegalArgumentException.class)
public void constructContext4() throws SCSException {
new ComponentContext(orb, poa, null);
}
@Test
public void getORB() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
Assert.assertNotNull(component.getORB());
}
@Test
public void getPOA() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
Assert.assertNotNull(component.getPOA());
}
@Test
public void getPOA2() throws SCSException, UserException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
Assert.assertNotNull(component.getPOA());
org.omg.CORBA.Object obj =
component.getORB().resolve_initial_references("RootPOA");
Assert.assertNotNull(obj);
POA poa = POAHelper.narrow(obj);
Assert.assertEquals(poa, component.getPOA());
}
@Test
public void getFacets() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
Collection<Facet> facets = component.getFacets();
Assert.assertNotNull(facets);
Assert.assertEquals(3, facets.size());
}
@Test
public void getReceptacles() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
Collection<Receptacle> receptacles = component.getReceptacles();
Assert.assertNotNull(receptacles);
Assert.assertEquals(0, receptacles.size());
}
@Test
public void getComponentId() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
Assert.assertNotNull(component.getComponentIdAsString());
ComponentId otherComponentId = component.getComponentId();
Assert.assertNotNull(otherComponentId);
Assert.assertEquals(componentId.name, otherComponentId.name);
Assert.assertEquals(componentId.major_version,
otherComponentId.major_version);
Assert.assertEquals(componentId.minor_version,
otherComponentId.minor_version);
Assert.assertEquals(componentId.patch_version,
otherComponentId.patch_version);
Assert.assertEquals(componentId.platform_spec,
otherComponentId.platform_spec);
}
@Test(expected = IllegalArgumentException.class)
public void getFacetByName() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
component.getFacetByName(null);
}
@Test
public void getIComponent() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
IComponent iComponent = component.getIComponent();
Assert.assertNotNull(iComponent);
String iComponentFacetName = "IComponent";
Facet facet = component.getFacetByName(iComponentFacetName);
Assert.assertNotNull(facet);
FacetDescription description = facet.getDescription();
Assert.assertEquals(iComponent, description.facet_ref);
Assert.assertEquals(IComponentHelper.id(), facet.getInterfaceName());
Assert.assertEquals(IComponentHelper.id(), description.interface_name);
Assert.assertEquals(iComponentFacetName, facet.getName());
Assert.assertEquals(iComponentFacetName, description.name);
}
@Test
public void getIReceptacles() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
String iReceptaclesFacetName = "IReceptacles";
Facet facet = component.getFacetByName(iReceptaclesFacetName);
Assert.assertNotNull(facet);
FacetDescription description = facet.getDescription();
Assert.assertEquals(IReceptaclesHelper.id(), facet.getInterfaceName());
Assert.assertEquals(IReceptaclesHelper.id(), description.interface_name);
Assert.assertEquals(iReceptaclesFacetName, facet.getName());
Assert.assertEquals(iReceptaclesFacetName, description.name);
}
@Test
public void getIMetaInterface() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
String iMetaInterfaceFacetName = "IMetaInterface";
Facet facet = component.getFacetByName(iMetaInterfaceFacetName);
Assert.assertNotNull(facet);
FacetDescription description = facet.getDescription();
Assert.assertEquals(IMetaInterfaceHelper.id(), facet.getInterfaceName());
Assert.assertEquals(IMetaInterfaceHelper.id(), description.interface_name);
Assert.assertEquals(iMetaInterfaceFacetName, facet.getName());
Assert.assertEquals(iMetaInterfaceFacetName, description.name);
}
}
package scs.core;
import java.util.List;
import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.omg.CORBA.ORB;
import org.omg.CORBA.UserException;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import scs.core.exception.SCSException;
public final class IReceptacleTest {
private static ComponentContext context;
private static String name;
private static String interfaceName;
private static boolean multiplex;
@BeforeClass
public static void beforeClass() throws UserException, SCSException {
ORB orb = ORB.init((String[]) null, null);
org.omg.CORBA.Object obj = orb.resolve_initial_references("RootPOA");
POA poa = POAHelper.narrow(obj);
ComponentId componentId =
new ComponentId("componente", (byte) 1, (byte) 0, (byte) 0, "java");
context = new ComponentContext(orb, poa, componentId);
name = "Receptculo";
interfaceName = IReceptaclesHelper.id();
multiplex = false;
}
@AfterClass
public static void afterClass() {
context = null;
}
@Test(expected = IllegalArgumentException.class)
public void constructReceptacle() {
new Receptacle(null, name, interfaceName, multiplex);
}
@Test(expected = IllegalArgumentException.class)
public void constructReceptacle2() {
new Receptacle(context, null, interfaceName, multiplex);
}
@Test(expected = IllegalArgumentException.class)
public void constructReceptacle3() {
new Receptacle(context, name, null, multiplex);
}
@Test
public void constructReceptacle4() {
new Receptacle(context, name, interfaceName, multiplex);
}
@Test
public void getName() {
Receptacle receptacle =
new Receptacle(context, name, interfaceName, multiplex);
Assert.assertNotNull(receptacle.getName());
Assert.assertEquals(name, receptacle.getName());
}
@Test
public void getInterfaceName() {
Receptacle receptacle =
new Receptacle(context, name, interfaceName, multiplex);
Assert.assertNotNull(receptacle.getInterfaceName());
Assert.assertEquals(interfaceName, receptacle.getInterfaceName());
}
@Test
public void isMultiplex() {
Receptacle receptacle =
new Receptacle(context, name, interfaceName, multiplex);
Assert.assertEquals(multiplex, receptacle.isMultiplex());
}
@Test
public void getConnections() {
Receptacle receptacle =
new Receptacle(context, name, interfaceName, multiplex);
List<ConnectionDescription> connections = receptacle.getConnections();
Assert.assertNotNull(connections);
Assert.assertEquals(0, connections.size());
}
@Test
public void getConnectionSize() {
Receptacle receptacle =
new Receptacle(context, name, interfaceName, multiplex);
Assert.assertEquals(0, receptacle.getConnectionsSize());
}
@Test
public void getDescription() {
Receptacle receptacle =
new Receptacle(context, name, interfaceName, multiplex);
ReceptacleDescription description = receptacle.getDescription();
Assert.assertNotNull(description.name);
Assert.assertEquals(name, description.name);
Assert.assertNotNull(description.interface_name);
Assert.assertEquals(interfaceName, description.interface_name);
Assert.assertEquals(multiplex, description.is_multiplex);
ConnectionDescription[] connections = description.connections;
Assert.assertNotNull(connections);
Assert.assertEquals(0, connections.length);
}
@Test(expected = IllegalArgumentException.class)
public void addConnection() throws UserException {
Receptacle receptacle =
new Receptacle(context, name, interfaceName, multiplex);
receptacle.addConnection(null);
}
@Test
public void addConnection2() throws UserException {
Receptacle receptacle =
new Receptacle(context, name, interfaceName, multiplex);
POA poa = context.getPOA();
byte[] id =
poa.servant_to_id(new IReceptaclesServant(context));
org.omg.CORBA.Object obj = poa.id_to_reference(id);
receptacle.addConnection(obj);
Assert.assertEquals(1, receptacle.getConnectionsSize());
poa.deactivate_object(id);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment