/*
* Embedded Jopr Project
* Copyright (C) 2006-2009 Red Hat, Inc.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.jboss.on.embedded.ui.test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.rhq.core.domain.resource.ResourceType;
import org.testng.annotations.Test;
import org.jboss.seam.Component;
import org.jboss.on.embedded.ResourceTypes;
import org.jboss.on.embedded.ui.NavigationAction;
import org.jboss.on.embedded.ui.SummaryAction;
import org.jboss.on.embedded.ui.nav.JONTreeNode;
import org.jboss.on.embedded.ui.nav.ResourceTypeTreeNode;
import org.jboss.on.embedded.util.EmbeddedSeamHelper;
public class TreeNodeTest extends EmbeddedSeamHelper
{
private final Log log = LogFactory.getLog(TreeNodeTest.class);
@Override
protected String getScenario()
{
return "scenario2.xml";
}
@Test
// just make sure we can at least visit each node in the tree
public void testVisitEachNode() throws Exception
{
new ComponentTest()
{
@Override
protected void testComponents() throws Exception
{
visitNodes("noOp");
}
}.run();
}
public void noOp(JONTreeNode node, int level)
{
}
int numberOfNodes;
public void countNode(JONTreeNode node, int level)
{
numberOfNodes++;
}
@Test(dependsOnMethods = {"testVisitEachNode"})
// make sure we have the expected number
public void testCountNodes() throws Exception
{
new ComponentTest()
{
@Override
protected void testComponents() throws Exception
{
numberOfNodes = 0;
visitNodes("countNode");
assert numberOfNodes == 28 : "Incorrect number of nodes identified, was ["
+ numberOfNodes + "] should have been 28.";
}
}.run();
}
@Test(dependsOnMethods = {"testVisitEachNode"})
// just draw the tree to system.out
public void logNodes() throws Exception
{
new ComponentTest()
{
@Override
protected void testComponents() throws Exception
{
visitNodes("logNode");
}
}.run();
}
public void logNode(JONTreeNode node, int level)
{
for (int j = 0; j < level; j++)
{
System.out.print(" ");
}
System.out.println(node.getName() + ": [" + node.getClass().getName() + "] "
+ node.getPath());
}
@Test(dependsOnMethods = {"testVisitEachNode"})
// executes SummaryAction.getOutcome for each node
public void testGetOutcome() throws Exception
{
new ComponentTest()
{
@Override
protected void testComponents() throws Exception
{
visitNodes("getOutcomeForNode");
}
}.run();
}
public void getOutcomeForNode(JONTreeNode node, int level)
{
NavigationAction navAction = getNavigationAction();
navAction.setCurrentPath(node.getPath());
SummaryAction action = new SummaryAction();
action.setNavigationAction(navAction);
String outcome = action.view();
log.debug("Name=[" + node.getName() + ", Path=[" + node.getPath() + "], outcome=[" + outcome + "].");
}
@Test(dependsOnMethods = {"testVisitEachNode"})
// executes NavigationAction.getBreadcrumb for each node
public void testGetBreadcrumb() throws Exception
{
new ComponentTest()
{
@Override
protected void testComponents() throws Exception
{
visitNodes("getBreadcrumbForNode");
}
}.run();
}
public void getBreadcrumbForNode(JONTreeNode node, int level)
{
NavigationAction navAction = getNavigationAction();
navAction.setCurrentPath(node.getPath());
List breadcrumbs = navAction.getBreadCrumbs();
assert breadcrumbs != null;
assert !breadcrumbs.isEmpty();
}
@Test(dependsOnMethods = {"testVisitEachNode"})
// tries to find each node from the root using the nodes path
public void testFindNodeByPath() throws Exception
{
new ComponentTest()
{
@Override
protected void testComponents() throws Exception
{
visitNodes("findNode");
}
}.run();
}
public void findNode(JONTreeNode node, int level)
{
NavigationAction navAction = getNavigationAction();
JONTreeNode retrievedNode = navAction.findNode(node.getPath());
assert retrievedNode != null;
assert retrievedNode.getName().equals(node.getName());
assert retrievedNode.getPath().equals(node.getPath());
}
@Test(dependsOnMethods = {"testVisitEachNode"})
// gets a ResourceType from the Manager and then search for it in the tree
// finally checking how many children it has
// should test with LocalTXDS and AppServer
public void testfindNodeByResourceType() throws Exception
{
new ComponentTest()
{
@Override
protected void testComponents() throws Exception
{
ResourceType localDS = getManager().getResourceType(ResourceTypes.RESOURCE_TYPE_LOCAL_DATASOURCE);
assert localDS != null;
NavigationAction navAction = getNavigationAction();
ResourceTypeTreeNode retrievedNode = navAction.findNodeByResourceTypeAndParent(localDS, getPlatformNode());
assert retrievedNode != null;
assert localDS.equals(retrievedNode.getResourceType());
assert retrievedNode.getChildNodes().size() == 6;
}
}.run();
}
// Utility methods
private void visitNodes(String methodName)
throws NoSuchMethodException, IllegalAccessException
{
JONTreeNode root = getPlatformNode();
Method method = this.getClass().getMethod(methodName, JONTreeNode.class, Integer.TYPE);
try
{
visitNodes(method, root, 0);
}
catch (InvocationTargetException e)
{
log.error("child call failed", e);
assert false;
}
}
// method which calls the specified method on every node in the tree
private void visitNodes(Method method, JONTreeNode node, int level) throws IllegalAccessException, InvocationTargetException
{
assert node != null;
method.invoke(this, node, level);
level++;
for (JONTreeNode childNode : node.getChildNodes())
{
visitNodes(method, childNode, level);
}
}
private NavigationAction getNavigationAction()
{
return (NavigationAction)Component.getInstance("navigationAction", true);
}
private JONTreeNode getPlatformNode()
{
NavigationAction navAction = getNavigationAction();
navAction.setCurrentPath("bad_path");
// if the path doesn't match anything the root node is returned
JONTreeNode root = navAction.getSelectedNode();
return root;
}
}