/*
* 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.util;
import junit.framework.TestCase;
import org.apache.agila.model.BusinessProcess;
import org.apache.agila.model.Variable;
import org.apache.agila.model.NodeID;
import org.apache.agila.model.Node;
import org.apache.agila.example.LeaveApplicationTask;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class XMLUtilTestCase extends TestCase {
// NOTE: If additional features are added to the xml, this file should also be updated
// and more test cases should be added for those features
private String workflowXML = "org/apache/agila/LeaveApplication.xml";
private BusinessProcess businessProcess;
public void testName_NotNull() {
assertNotNull( businessProcess.getName() );
}
public void testGraphAsXML_NotNull() {
assertNotNull( businessProcess.getGraphAsXML() );
}
public void testGraphAsXML_Same() {
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read( getWorkflow() );
assertEquals( document.asXML(), businessProcess.getGraphAsXML() );
} catch( DocumentException e ) {
fail( e.getMessage() );
}
}
public void testNodeCount() {
assertEquals( getElementCount( "//graph/nodes/*" ), businessProcess.getNodeCount() );
}
public void testVariableCount() {
assertEquals( getElementCount( "//variables/*" ), businessProcess.getVariables().size() );
}
public void testRequiredVariables() {
assertTrue( getRequiredVariables() == 1 );
}
public void testPropertiesSet() {
Node node = businessProcess.getNode(new NodeID(3));
assertTrue("Not found node!", node instanceof LeaveApplicationTask);
LeaveApplicationTask task = (LeaveApplicationTask) node;
assertEquals("foo property", "hello", task.getFoo());
assertEquals("bar property", 123, task.getBar());
}
private int getRequiredVariables() {
int retVal = 0;
Map variables = businessProcess.getVariables();
for( Iterator iterator = variables.keySet().iterator(); iterator.hasNext(); ) {
String key = (String)iterator.next();
Variable variable = (Variable)variables.get( key );
if( variable.isRequiredForInput() ) {
retVal++;
}
}
return( retVal );
}
private int getElementCount( String xpath ) {
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read( getWorkflow() );
List elements = document.getRootElement().selectNodes( xpath );
return( elements.size() );
} catch( DocumentException e ) {
fail( e.getMessage() );
}
return( -1 );
}
// TODO add a test case to validate the xml
public void testValidateXML() {
}
private BufferedReader getWorkflow() {
return( new BufferedReader( new InputStreamReader( getClass().getClassLoader().getResourceAsStream( workflowXML ) ) ) );
}
public void setUp() throws Exception {
businessProcess = XMLUtil.deserializeXML( getWorkflow() );
}
public void tearDown() throws Exception {
businessProcess = null;
}
}