/*
* Copyright 2004 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.agila.services;
import org.apache.agila.engine.InstanceID;
import org.apache.agila.engine.Token;
import org.apache.agila.engine.TokenID;
import org.apache.agila.impl.ConnectionImpl;
import org.apache.agila.impl.memory.TokenServiceImpl;
import org.apache.agila.model.Connection;
import org.apache.agila.model.Node;
import org.apache.agila.model.NodeContext;
import org.apache.agila.model.NodeID;
import org.apache.agila.model.node.BaseNodeImpl;
import junit.framework.TestCase;
/**
* @author Glenn
*
*/
public class TokenServiceTestCase extends TestCase {
TokenService tokenService;
int instanceCounter = 1;
int nodeCounter = 1;
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
tokenService = new TokenServiceImpl();
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
public void testSaveToken() {
// retrieve a token from the service given the ff. instance and node id's
InstanceID instanceId = new InstanceID(instanceCounter++);
int nodeId = nodeCounter++;
Token token = tokenService.newToken(instanceId, new NodeID(nodeId), Token.PRE);
TokenID tokenId = token.getTokenID();
// check the retrieved token like in testNewToken()
assertNotNull(token);
assertEquals(instanceId, token.getInstanceID());
assertEquals(nodeId, token.getCurrentNodeID().getID());
assertEquals(Token.PRE, token.getCurrentState());
assertTrue(token.isActive());
// set the token data to new values
int newNodeId = nodeCounter++;
token.setActive(false);
token.setCurrentNodeID(new NodeID(newNodeId));
token.setCurrentState(Token.MID);
tokenService.saveToken(token);
token = tokenService.getTokenByID(tokenId);
// now check for the new values
assertNotNull(token);
assertEquals(instanceId, token.getInstanceID());
assertEquals(newNodeId, token.getCurrentNodeID().getID());
assertEquals(Token.MID, token.getCurrentState());
assertFalse(token.isActive());
}
public void testNewToken() {
// retrieve a token from the service given the ff. instance and node id's
InstanceID instanceId = new InstanceID(instanceCounter++);
int nodeId = nodeCounter++;
Token token = tokenService.newToken(instanceId, new NodeID(nodeId), Token.PRE);
assertNotNull(token);
assertEquals(instanceId, token.getInstanceID());
assertEquals(nodeId, token.getCurrentNodeID().getID());
assertEquals(Token.PRE, token.getCurrentState());
}
public void testNextToken() {
// retrieve a token from the service given the ff. instance and node id's
InstanceID instanceId = new InstanceID(instanceCounter++);
Node parentNode = new FullBaseNode();
parentNode.setDisplayName("parent_node");
parentNode.setNodeId(new NodeID(nodeCounter++));
Token parentToken = tokenService.newToken(instanceId, parentNode.getNodeId(), Token.PRE);
TokenID tokenId = parentToken.getTokenID();
// create n child nodes
Node[] childrenNode = new Node[5];
for(int i = 0; i < childrenNode.length; i++) {
Node childNode = new FullBaseNode();
childNode.setDisplayName("child_node_"+ i);
childNode.setNodeId(new NodeID(nodeCounter++));
childrenNode[i] = childNode;
}
// create childrenNode.length connections
Connection[] connections = new Connection[childrenNode.length];
for(int i = 0; i < connections.length; i++) {
Connection connection = new ConnectionImpl("connection_"+ i);
connection.setParentNode(parentNode);
connection.setChildNode(childrenNode[i]);
parentNode.addOutboundConnection(connection);
childrenNode[i].addInboundConnection(connection);
connections[i] = connection;
}
// tokens are created in the order of connections -- this test will be using that fact.
// question: would it be proper to do so? Is that a property of the engine?
Token[] tokens = tokenService.nextToken(connections, parentToken);
for(int i = 0; i < tokens.length; i++) {
Token token = tokens[i];
InstanceID instance_id = token.getInstanceID();
NodeID currentNodeId = token.getCurrentNodeID();
int currentState = token.getCurrentState();
Node currentNode = childrenNode[i];
Connection[] inbound = currentNode.getInboundConnections();
assertNotNull(instance_id);
assertEquals(instanceId, instance_id);
// here is where we use the ordering
assertEquals(currentNode.getNodeId(), currentNodeId);
assertEquals(Token.PRE, currentState);
assertNotNull(inbound);
assertEquals(1, inbound.length);
assertNotNull(inbound[0].getParentNode());
assertSame(parentNode, inbound[0].getParentNode());
}
}
private class FullBaseNode extends BaseNodeImpl {
/* (non-Javadoc)
* @see org.apache.agila.model.Node#doEnd(org.apache.agila.model.NodeContext)
*/
public Connection[] doEnd(NodeContext ctx) {
return null;
}
}
}