Commit 52d4308e authored by Rodrigo Carneiro Henrique's avatar Rodrigo Carneiro Henrique
Browse files

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

- Inclusão dos testes unitários
- Correção de pequenos bugs encontrados durante a criação dos testes


git-svn-id: https://subversion.tecgraf.puc-rio.br/engdist/scs/core/java/trunk@114921 ae0415b3-e90b-0410-900d-d0be9363c56b
parent 6fe7a718
......@@ -13,15 +13,28 @@ import org.omg.PortableServer.Servant;
*/
public final class Facet {
private String name;
private String interface_name;
private org.omg.CORBA.Object facet_ref;
private String interfaceName;
private org.omg.CORBA.Object reference;
private Servant servant;
Facet(String name, String interface_name,
org.omg.CORBA.Object facet_ref, Servant servant) {
Facet(String name, String interfaceName, org.omg.CORBA.Object referece,
Servant servant) {
if (name == null) {
throw new IllegalArgumentException("The name can't be null");
}
if (interfaceName == null) {
throw new IllegalArgumentException("The interface's name can't be null");
}
if (referece == null) {
throw new IllegalArgumentException("The reference can't be null");
}
if (servant == null) {
throw new IllegalArgumentException("The servant can't be null");
}
this.name = name;
this.interface_name = interface_name;
this.facet_ref = facet_ref;
this.interfaceName = interfaceName;
this.reference = referece;
this.servant = servant;
}
......@@ -29,7 +42,7 @@ public final class Facet {
* @return FacetDescription
*/
public FacetDescription getDescription() {
return new FacetDescription(name, interface_name, facet_ref);
return new FacetDescription(name, interfaceName, reference);
}
public String getName() {
......@@ -37,7 +50,11 @@ public final class Facet {
}
public String getInterfaceName() {
return interface_name;
return interfaceName;
}
public org.omg.CORBA.Object getReference() {
return reference;
}
/**
......
......@@ -46,15 +46,16 @@ public class IComponentServant extends IComponentPOA {
/**
* Obtém o objeto CORBA a partir do nome fictício da faceta
*
* @param facet Nome fictício da faceta
* @param facetName Nome fictício da faceta
* @return Objeto CORBA que implementa a faceta
* @see scs.core.IComponentOperations#getFacetByName(java.lang.String)
*/
public org.omg.CORBA.Object getFacetByName(String facet) {
FacetDescription desc = myComponent.getFacetByName(facet).getDescription();
if (desc != null)
return desc.facet_ref;
return null;
public org.omg.CORBA.Object getFacetByName(String facetName) {
Facet facet = myComponent.getFacetByName(facetName);
if (facet == null) {
return null;
}
return facet.getReference();
}
/**
......
package scs.core;
import java.util.Collection;
import java.util.Iterator;
/**
* Servant da interface IDL {@link IMetaInterface}. Implementa as
......@@ -36,14 +37,12 @@ public class IMetaInterfaceServant extends scs.core.IMetaInterfacePOA {
*/
public FacetDescription[] getFacets() {
Collection<Facet> facets = myComponent.getFacets();
FacetDescription[] descs = new FacetDescription[facets.size()];
int i = 0;
for (Facet facet : facets) {
descs[i] = facet.getDescription();
i++;
FacetDescription[] descriptions = new FacetDescription[facets.size()];
Iterator<Facet> facetsIterator = facets.iterator();
for (int i = 0; i < descriptions.length; i++) {
descriptions[i] = facetsIterator.next().getDescription();
}
return descs;
return descriptions;
}
/**
......@@ -54,9 +53,11 @@ public class IMetaInterfaceServant extends scs.core.IMetaInterfacePOA {
public FacetDescription[] getFacetsByName(String[] names) throws InvalidName {
FacetDescription[] facets = new FacetDescription[names.length];
for (int i = 0; i < names.length; i++) {
facets[i] = myComponent.getFacetByName(names[i]).getDescription();
if (facets[i] == null)
Facet facet = myComponent.getFacetByName(names[i]);
if (facet == null) {
throw new InvalidName(names[i]);
}
facets[i] = facet.getDescription();
}
return facets;
}
......@@ -68,15 +69,13 @@ public class IMetaInterfaceServant extends scs.core.IMetaInterfacePOA {
*/
public ReceptacleDescription[] getReceptacles() {
Collection<Receptacle> receptacles = myComponent.getReceptacles();
ReceptacleDescription[] descs =
ReceptacleDescription[] descriptions =
new ReceptacleDescription[receptacles.size()];
int i = 0;
for (Receptacle receptacle : receptacles) {
descs[i] = receptacle.getDescription();
i++;
Iterator<Receptacle> receptaclesIterator = receptacles.iterator();
for (int i = 0; i < descriptions.length; i++) {
descriptions[i] = receptaclesIterator.next().getDescription();
}
return descs;
return descriptions;
}
/**
......@@ -90,10 +89,11 @@ public class IMetaInterfaceServant extends scs.core.IMetaInterfacePOA {
ReceptacleDescription[] receptacles =
new ReceptacleDescription[names.length];
for (int i = 0; i < names.length; i++) {
receptacles[i] =
myComponent.getReceptacleByName(names[i]).getDescription();
if (receptacles[i] == null)
Receptacle receptacle = myComponent.getReceptacleByName(names[i]);
if (receptacle == null) {
throw new InvalidName(names[i]);
}
receptacles[i] = receptacle.getDescription();
}
return receptacles;
}
......
......@@ -115,6 +115,7 @@ public final class ComponentContextTest {
public void getFacetByName() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
component.getFacetByName(null);
Assert.assertNull(component.getFacetByName(""));
}
@Test
......
package scs.core;
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 org.omg.PortableServer.Servant;
import scs.core.exception.SCSException;
public final class FacetTest {
private static ComponentContext context;
private static String name;
private static String interfaceName;
private static Servant servant;
private static org.omg.CORBA.Object facetReference;
@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 = "Facet";
interfaceName = IMetaInterfaceHelper.id();
servant = new IMetaInterfaceServant(context);
facetReference = context.getPOA().servant_to_reference(servant);
}
@AfterClass
public static void afterClass() {
context = null;
}
@Test(expected = IllegalArgumentException.class)
public void constructFacet() {
new Facet(null, interfaceName, facetReference, servant);
}
@Test(expected = IllegalArgumentException.class)
public void constructFacet2() {
new Facet(name, null, facetReference, servant);
}
@Test(expected = IllegalArgumentException.class)
public void constructFacet3() {
new Facet(name, interfaceName, null, servant);
}
@Test(expected = IllegalArgumentException.class)
public void constructFacet4() {
new Facet(name, interfaceName, facetReference, null);
}
@Test
public void constructFacet5() {
new Facet(name, interfaceName, facetReference, servant);
}
@Test
public void getName() {
Facet facet = new Facet(name, interfaceName, facetReference, servant);
Assert.assertNotNull(facet.getName());
Assert.assertEquals(name, facet.getName());
Assert.assertEquals(facet.getName(), facet.getDescription().name);
}
@Test
public void getInterfaceName() {
Facet facet = new Facet(name, interfaceName, facetReference, servant);
Assert.assertNotNull(facet.getInterfaceName());
Assert.assertEquals(interfaceName, facet.getInterfaceName());
Assert.assertEquals(facet.getInterfaceName(),
facet.getDescription().interface_name);
}
@Test
public void getReferece() {
Facet facet = new Facet(name, interfaceName, facetReference, servant);
Assert.assertNotNull(facet.getReference());
}
@Test
public void getServant() {
Facet facet = new Facet(name, interfaceName, facetReference, servant);
Assert.assertNotNull(facet.getServant());
}
}
package scs.core;
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 IComponentTest {
private static final String COMPONENT_NAME = "componente";
private static final String COMPONENT_PLATFORM_SPEC = "java";
private static ComponentContext context;
@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);
poa.the_POAManager().activate();
ComponentId componentId =
new ComponentId(COMPONENT_NAME, (byte) 1, (byte) 0, (byte) 0,
COMPONENT_PLATFORM_SPEC);
context = new ComponentContext(orb, poa, componentId);
Thread thread = new Thread(new Runnable() {
public void run() {
context.getORB().run();
}
});
thread.start();
}
@AfterClass
public static void afterClass() {
context.getORB().shutdown(true);
context = null;
}
@Test
public void getComponentId() {
Facet facet = context.getFacetByName("IComponent");
IComponent component = IComponentHelper.narrow(facet.getReference());
ComponentId id = component.getComponentId();
Assert.assertEquals(COMPONENT_NAME, id.name);
Assert.assertEquals(COMPONENT_PLATFORM_SPEC, id.platform_spec);
}
@Test
public void getFacet() {
Facet facet = context.getFacetByName("IComponent");
IComponent component = IComponentHelper.narrow(facet.getReference());
Assert.assertNotNull(component.getFacet(IComponentHelper.id()));
Assert.assertNotNull(component.getFacet(IReceptaclesHelper.id()));
Assert.assertNotNull(component.getFacet(IMetaInterfaceHelper.id()));
Assert.assertNull(component.getFacet(""));
}
@Test
public void getFacetByName() {
Facet facet = context.getFacetByName("IComponent");
IComponent component = IComponentHelper.narrow(facet.getReference());
Assert.assertNotNull(component.getFacetByName("IComponent"));
Assert.assertNotNull(component.getFacetByName("IReceptacles"));
Assert.assertNotNull(component.getFacetByName("IMetaInterface"));
Assert.assertNull(component.getFacetByName(""));
}
}
package scs.core;
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 IMetaInterfaceTest {
private static ComponentContext context;
@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);
poa.the_POAManager().activate();
ComponentId componentId =
new ComponentId("componente", (byte) 1, (byte) 0, (byte) 0, "java");
context = new ComponentContext(orb, poa, componentId);
Thread thread = new Thread(new Runnable() {
public void run() {
context.getORB().run();
}
});
thread.start();
}
@AfterClass
public static void afterClass() {
context.getORB().shutdown(true);
context = null;
}
@Test
public void getFacets() {
Facet facet = context.getFacetByName("IMetaInterface");
IMetaInterface meta = IMetaInterfaceHelper.narrow(facet.getReference());
FacetDescription[] facets = meta.getFacets();
Assert.assertEquals(3, facets.length);
}
@Test
public void getFacetsByName() throws InvalidName {
Facet facet = context.getFacetByName("IMetaInterface");
IMetaInterface meta = IMetaInterfaceHelper.narrow(facet.getReference());
FacetDescription[] facets =
meta.getFacetsByName(new String[] { "IComponent", "IReceptacles",
"IMetaInterface" });
Assert.assertEquals(3, facets.length);
}
@Test(expected = InvalidName.class)
public void getFacetsByName2() throws InvalidName {
Facet facet = context.getFacetByName("IMetaInterface");
IMetaInterface meta = IMetaInterfaceHelper.narrow(facet.getReference());
meta.getFacetsByName(new String[] { "" });
}
@Test
public void getReceptacles() {
Facet facet = context.getFacetByName("IMetaInterface");
IMetaInterface meta = IMetaInterfaceHelper.narrow(facet.getReference());
ReceptacleDescription[] receptacles = meta.getReceptacles();
Assert.assertEquals(0, receptacles.length);
}
@Test(expected = InvalidName.class)
public void getReceptacleByName() throws InvalidName {
Facet facet = context.getFacetByName("IMetaInterface");
IMetaInterface meta = IMetaInterfaceHelper.narrow(facet.getReference());
meta.getReceptaclesByName(new String[] { "" });
}
}
package scs.core;
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 IReceptaclesTest {
private static ComponentContext context;
@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);
poa.the_POAManager().activate();
ComponentId componentId =
new ComponentId("componente", (byte) 1, (byte) 0, (byte) 0, "java");
context = new ComponentContext(orb, poa, componentId);
Thread thread = new Thread(new Runnable() {
public void run() {
context.getORB().run();
}
});
thread.start();
}
@AfterClass
public static void afterClass() {
context.getORB().shutdown(true);
context = null;
}
@Test(expected = InvalidName.class)
public void getConnections() throws InvalidName {
Facet facet = context.getFacetByName("IReceptacles");
IReceptacles receptacles = IReceptaclesHelper.narrow(facet.getReference());
receptacles.getConnections("");
}
@Test(expected = InvalidConnection.class)
public void disconnect() throws InvalidName, InvalidConnection, NoConnection {
Facet facet = context.getFacetByName("IReceptacles");
IReceptacles receptacles = IReceptaclesHelper.narrow(facet.getReference());
receptacles.disconnect(0);
}
@Test(expected = NoConnection.class)
public void disconnect2() throws InvalidName, InvalidConnection, NoConnection {
Facet facet = context.getFacetByName("IReceptacles");
IReceptacles receptacles = IReceptaclesHelper.narrow(facet.getReference());
receptacles.disconnect(1);
}
}
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