Index: src/main/java/org/apache/kitty/client/Client.groovy =================================================================== --- src/main/java/org/apache/kitty/client/Client.groovy (revision 1141612) +++ src/main/java/org/apache/kitty/client/Client.groovy (revision ) @@ -22,7 +22,6 @@ import javax.management.Attribute import javax.management.ObjectName -import javax.management.remote.JMXConnector import javax.management.remote.JMXConnectorFactory import javax.management.remote.JMXServiceURL @@ -39,7 +38,7 @@ *

* * - * @version $Id: Client.groovy 1141612 2011-06-30 16:35:41Z msacks $ + * @version $Id: Client.groovy 1103602 2011-05-16 03:53:41Z msacks $ * */ class Client { @@ -57,24 +56,24 @@ public connect(def _host, def _port) { connect(_host, _port, null, null) } - + public connect(def _host, def _port, def _username, def _password) { def serviceURL def properties = new HashMap() - + if (remote != null) { disconnect() } - + if ((_username != null) && (_password != null)) { properties.put JMXConnector.CREDENTIALS, [_username, _password] as String[] } try { - serviceURL = "service:jmx:rmi:///jndi/rmi://$_host:$_port/jmxrmi" + serviceURL = "service:jmx:rmi:///jndi/rmi://$_host:$_port/jmxrmi" this.url = new JMXServiceURL(serviceURL) - + // TODO add auth & credentials to properties this.connector = JMXConnectorFactory.connect(this.url, properties) this.remote = this.connector.getMBeanServerConnection() @@ -142,7 +141,6 @@ if (it.equals(_domain)) { this.domain = _domain this.mBeansPath = [] - println "The domain is set to $domain" return } } @@ -157,7 +155,7 @@ * @return */ public ls() { - + if (!this.remote) { println "No remote is set!" return @@ -171,10 +169,9 @@ if (this.remote) { if (this.domain) { def objectName = this.domain + ":" - def objectName2 - if (objectName.length() > 0) { - objectName += ",".concat(this.mBeansPath.join()) // make sure mBeansPath is a list, otherwise remove the .join() command + if (mBeansPath.size() > 0) { + objectName += this.mBeansPath.join(',') // make sure mBeansPath is a list, otherwise remove the .join() command objectName2 = objectName + "," } else { @@ -183,18 +180,20 @@ println "objectName: " + objectName println "objectName2: " + objectName2 - + def pool = new ObjectName(objectName2 + "*") - def paths = {} + def paths = [:] println objectName println "-----" + + // List the MBeans names def qNames = this.remote.queryNames(pool, null) try { qNames.each { mbean -> def p = mbean.toString().split(objectName2)[1].split(',')[0] paths[p] = p } - paths.each { p -> println "M " + p } + paths.each { p, dummy -> println "M " + p } } catch(Exception e) { throw new DomainIsNoneException() @@ -231,7 +230,7 @@ } } catch(Exception e) { - // + // ObjectName not found } try { @@ -245,7 +244,7 @@ } } catch(Exception e) { - throw new DomainIsNoneException() + // ObjectName not found } } } @@ -259,13 +258,13 @@ if (this.remote) { if (this.domain) { if (path == "..") { - if (this.mBeansPath.size()) { + if (this.mBeansPath.length()) { this.mBeansPath.pop() } } else { for (p in path.split(',')) { - this.mBeansPath.append(p) + this.mBeansPath << p } } } @@ -342,7 +341,7 @@ def objectName = this.domain + ":" def attr - if (this.mBeansPath.size() > 0) { + if (this.mBeansPath.length() > 0) { objectName = objectName + ','.concat(this.mBeansPath.join()) } try { @@ -386,7 +385,7 @@ if (this.remote) { if (this.domain) { def objectName = this.domain + ":" - if (this.mBeansPath.size()) { + if (this.mBeansPath.length()) { objectName = objectName + ','.concat(this.mBeansPath.join()) } try{ @@ -423,7 +422,7 @@ public pwd() { def name if (this.domain) { - name = this.domain + ":" + ",".concat(this.mBeansPath.join()) // may need to change this + name = this.domain + ":" + this.mBeansPath.join(',') } return name Index: src/main/java/org/apache/kitty/client/rmi/RMIClient.groovy =================================================================== --- src/main/java/org/apache/kitty/client/rmi/RMIClient.groovy (revision 1041209) +++ src/main/java/org/apache/kitty/client/rmi/RMIClient.groovy (revision 1041209) @@ -1,37 +0,0 @@ -/* -* Licensed to the Apache Software Foundation (ASF) under one or more -* contributor license agreements. See the NOTICE file distributed with -* this work for additional information regarding copyright ownership. -* The ASF licenses this file to You 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.kitty.client.rmi - -import org.apache.kitty.client.Client; - -/** - * - * @version $Id: RMIClient.groovy 1041209 2010-12-01 23:46:24Z pidster $ - * - */ -class RMIClient extends Client { - - def host - def port - def domain - def mBeansPath - def remote - def connector - -} Index: src/test/java/org/apache/kitty/client/ClientTest.groovy =================================================================== --- src/test/java/org/apache/kitty/client/ClientTest.groovy (revision 1132791) +++ src/test/java/org/apache/kitty/client/ClientTest.groovy (revision ) @@ -20,7 +20,10 @@ import static org.junit.Assert.*; import org.apache.kitty.client.Client import javax.management.MBeanServerConnection -import org.junit.Test; +import org.junit.Test +import groovy.mock.interceptor.MockFor +import javax.management.ObjectName +import org.apache.kitty.exceptions.DomainIsNoneException; class ClientTest { @@ -55,4 +58,40 @@ assert client.domain == 'a' } + + @Test + public void should_list_names_for_domain() throws Exception { + Client client = new Client() + + // Mock the remote connection + def mockForRemote = new MockFor(MBeanServerConnection) + mockForRemote.ignore.asBoolean { + true -} + } + mockForRemote.ignore.getDomains { + ['a_domain'] + } + + // Prepare the names list + mockForRemote.ignore.queryNames { a,b -> + def set = new HashSet() + set.add(new ObjectName("a_domain", "test1", "1")) + set.add(new ObjectName("a_domain", "test2", "2")) + set.add(new ObjectName("a_domain", "test3", "3")) + return set + } + + def theProxyInstance = mockForRemote.proxyInstance() + + // Verify "ls" is working + mockForRemote.use { + client.remote = theProxyInstance + client.setDomain('a_domain') + try { + client.ls() + } catch(DomainIsNoneException dine) { + fail("DomainIsNoneException thrown") + } + } + } +}