Skip to content

Commit

Permalink
first spike of a junit plugin for hawtio for #817 ; we can now visual…
Browse files Browse the repository at this point in the history
…ise any unit tests found on the classpath
  • Loading branch information
jstrachan committed Dec 5, 2013
1 parent 8426fa4 commit ab4ddb8
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 8 deletions.
18 changes: 10 additions & 8 deletions hawtio-web/src/main/webapp/app/jmx/js/jmxHelpers.ts
Expand Up @@ -129,21 +129,23 @@ module Jmx {
return typeNames;
}

export function enableTree($scope, $location: ng.ILocationService, workspace: Workspace, treeElement, children, redraw = false) {
export function enableTree($scope, $location: ng.ILocationService, workspace: Workspace, treeElement, children, redraw = false, onActivateFn = null) {
//$scope.workspace = workspace;

if (treeElement.length) {
if (!onActivateFn) {
onActivateFn = (node:DynaTreeNode) => {
var data = node.data;
//$scope.select(data);
workspace.updateSelectionNode(data);
Core.$apply($scope);
};
}
workspace.treeElement = treeElement;
treeElement.dynatree({
/*
* The event handler called when a different node in the tree is selected
*/
onActivate: function (node:DynaTreeNode) {
var data = node.data;
//$scope.select(data);
workspace.updateSelectionNode(data);
Core.$apply($scope);
},
onActivate: onActivateFn,
onLazyRead: function(treeNode) {
var folder = treeNode.data;
var plugin = null;
Expand Down
16 changes: 16 additions & 0 deletions hawtio-web/src/main/webapp/app/junit/html/layoutJUnitTree.html
@@ -0,0 +1,16 @@
<div class="row-fluid" ng-controller="JUnit.TreeController">
<div id="tree-container" class="span3">
<ul id="tree-ctrl" class="nav">
<li>
<a data-toggle="collapse" data-target="#junittree">
<span>Unit Tests</span>
</a>
</li>
</ul>
<div id="junittree" class="in collapse"></div>
</div>

<div class="span9">
<div id="properties" ng-view></div>
</div>
</div>
5 changes: 5 additions & 0 deletions hawtio-web/src/main/webapp/app/junit/html/tests.html
@@ -0,0 +1,5 @@
<h1>Tests</h1>

<ul>
<li ng-repeat="test in selectedTests">{{test}}</li>
</ul>
16 changes: 16 additions & 0 deletions hawtio-web/src/main/webapp/app/junit/js/junitHelpers.ts
@@ -0,0 +1,16 @@
/**
* @module JUnit
*/
module JUnit {

export var log:Logging.Logger = Logger.get("JUnit");

export function isJUnitPluginEnabled(workspace:Workspace) {
return getIntrospectorMBean(workspace);
}

export function getIntrospectorMBean(workspace: Workspace) {
return Core.getMBeanTypeObjectName(workspace, "io.hawt.introspect", "Introspector");
}

}
39 changes: 39 additions & 0 deletions hawtio-web/src/main/webapp/app/junit/js/junitPlugin.ts
@@ -0,0 +1,39 @@
/**
* @module JUnit
* @main JUnit
*/
module JUnit {
var pluginName = 'junit';
angular.module(pluginName, ['bootstrap', 'ngResource', 'ngGrid', 'datatable', 'hawtioCore']).
config(($routeProvider) => {
$routeProvider.
when('/junit/tests', {templateUrl: 'app/junit/html/tests.html', reloadOnSearch: false})
}).
run(($location:ng.ILocationService, workspace:Workspace, viewRegistry, layoutFull, helpRegistry) => {

viewRegistry['junit'] = 'app/junit/html/layoutJUnitTree.html';
/*
helpRegistry.addUserDoc('junit', 'app/junit/doc/help.md', () => {
return isJUnitPluginEnabled(workspace);
});
*/

workspace.topLevelTabs.push({
content: "JUnit",
title: "View and run test cases in this process",
isValid: (workspace:Workspace) => isJUnitPluginEnabled(workspace),
href: () => "#/junit/tests"
});

/*
workspace.subLevelTabs.push({
content: '<i class="icon-list-alt"></i> JUnit',
title: "View the logs in this process",
isValid: (workspace:Workspace) => workspace.hasDomainAndProperties('org.fusesource.insight', {type: 'JUnitQuery'}),
href: () => "#/logs"
});
*/
});

hawtioPluginLoader.addModule(pluginName);
}
100 changes: 100 additions & 0 deletions hawtio-web/src/main/webapp/app/junit/js/tree.ts
@@ -0,0 +1,100 @@
module JUnit {

export function TreeController($scope, $location:ng.ILocationService, workspace:Workspace, jolokia) {

var log:Logging.Logger = Logger.get("JUnit");

$scope.testClasses = [];
$scope.testClassMap = {};

$scope.$on("$routeChangeSuccess", function (event, current, previous) {
// lets do this asynchronously to avoid Error: $digest already in progress
setTimeout(updateSelectionFromURL, 50);
});

$scope.$on('jmxTreeUpdated', function () {
reloadTree();
});

function updateSelectionFromURL() {
Jmx.updateTreeSelectionFromURL($location, $("#junittree"), true);
}

reloadTree();

function selectionChanged(data) {
var selectionKey = data ? data.key : null;
log.debug("Selection is now: " + selectionKey);
var selectedTests = $scope.testClasses;
$scope.selectionKey = selectionKey;
if (selectionKey) {
selectedTests = $scope.testClassMap[selectionKey] || [selectionKey];
}
$scope.selectedTests = selectedTests;
Core.$apply($scope);
}

function reloadTree() {
var mbean = JUnit.getIntrospectorMBean(workspace);
var domain = "org.unit";
var rootFolder = new Folder("Test Cases");
rootFolder.addClass = "testCases";
rootFolder.typeName = "testCases";
rootFolder.domain = domain;
rootFolder.key = "all";
var children = [rootFolder];

if (mbean) {
function render(results) {
$scope.testClasses = results;
$scope.testClassMap = {};

angular.forEach(results, (className) => {
var paths = className.split(".");
var last = paths.length - 1;
var folder = rootFolder;
var prefix = "";
for (var i = 0; i < last; i++) {
var path = paths[i];
if (prefix) {
prefix += ".";
}
prefix += path;
var list = $scope.testClassMap[prefix];
if (!list) {
list = [];
$scope.testClassMap[prefix] = list;
}
list.push(className);
folder = workspace.folderGetOrElse(folder, path);
folder.key = prefix;
}
var lastPath = paths[last];

// lets add the test case...
var testClass = new Folder(lastPath);
testClass.addClass = "testClass";
testClass.domain = domain;
testClass.key = className;
folder.children.push(testClass);
});

Core.$apply($scope);

var treeElement = $("#junittree");
Jmx.enableTree($scope, $location, workspace, treeElement, children, true, (selectedNode) => {
var data = selectedNode.data;
//$scope.select(data);
//workspace.updateSelectionNode(data);
selectionChanged(data);
Core.$apply($scope);
});
// lets do this asynchronously to avoid Error: $digest already in progress
setTimeout(updateSelectionFromURL, 50);
}

jolokia.execute(mbean, "findJUnitTestClassNames", onSuccess(render));
}
}
}
}

0 comments on commit ab4ddb8

Please sign in to comment.