From 2275e2d6435ca746636e289d41c018cd1c26abc7 Mon Sep 17 00:00:00 2001 From: Charles Bell Date: Sat, 4 Feb 2017 00:38:33 +0100 Subject: [PATCH 1/4] Move command to separate file --- bin/jmx.php | 68 +------------------ .../JMX/Command/ReadCommand.php | 62 +++++++++++++++++ 2 files changed, 65 insertions(+), 65 deletions(-) create mode 100644 src/CentralDesktop/JMX/Command/ReadCommand.php diff --git a/bin/jmx.php b/bin/jmx.php index ae4c8ba..6c2ed7d 100755 --- a/bin/jmx.php +++ b/bin/jmx.php @@ -1,70 +1,8 @@ #!/usr/bin/env php setName('read') - ->setDescription("Get's JMX attributes") - ->addArgument( - 'uri', - InputArgument::REQUIRED, - 'URL for the JMX Jolokia endpoint' - ) - ->addArgument( - 'object', - InputArgument::REQUIRED, - 'Name of the object you are fetching' - ) - ->addOption('user', '', InputOption::VALUE_REQUIRED, "Username") - ->addOption('password', '', InputOption::VALUE_REQUIRED, "Password"); - } - - protected - function execute(InputInterface $input, OutputInterface $output) { - $client = new Client($input->getArgument("uri"), - $input->getOption("user"), - $input->getOption("password")); - $bean = $client->bean($input->getArgument("object")); - - $bean->read(); - - $attributes = $bean->getAttributes(); - $keys = array_keys($attributes); - - foreach ($keys as $key) { - $value = $attributes[$key]; - - if (is_array($value)) { - $output->writeln("$key : array(" . count($value) . ")"); - foreach ($value as $el) { - $output->writeln(" --> $el "); - } - - } - else { - $output->writeln("$key : "); - } - - } - - } -} - - -$application = new Application(); -$application->add(new ReadCommand()); +$application = new Symfony\Component\Console\Application(); +$application->add(new CentralDesktop\JMX\Command\ReadCommand()); $application->run(); diff --git a/src/CentralDesktop/JMX/Command/ReadCommand.php b/src/CentralDesktop/JMX/Command/ReadCommand.php new file mode 100644 index 0000000..27cf1c0 --- /dev/null +++ b/src/CentralDesktop/JMX/Command/ReadCommand.php @@ -0,0 +1,62 @@ +setName('read') + ->setDescription("Displays JMX attributes") + ->addArgument( + 'uri', + InputArgument::REQUIRED, + 'URL for the JMX Jolokia endpoint' + ) + ->addArgument( + 'object', + InputArgument::REQUIRED, + 'Name of the object you are fetching' + ) + ->addOption('user', '', InputOption::VALUE_REQUIRED, "Username") + ->addOption('password', '', InputOption::VALUE_REQUIRED, "Password"); + } + + protected + function execute(InputInterface $input, OutputInterface $output) { + $client = new Client($input->getArgument("uri"), + $input->getOption("user"), + $input->getOption("password")); + $bean = $client->bean($input->getArgument("object")); + + $bean->read(); + + $attributes = $bean->getAttributes(); + $keys = array_keys($attributes); + + foreach ($keys as $key) { + $value = $attributes[$key]; + + if (is_array($value)) { + $output->writeln("$key : array(" . count($value) . ")"); + foreach ($value as $el) { + $output->writeln(" --> $el "); + } + + } + else { + $output->writeln("$key : "); + } + + } + + } +} From 65a07b520f535c7256402718ac8b0c776d1e703c Mon Sep 17 00:00:00 2001 From: Charles Bell Date: Sat, 4 Feb 2017 00:38:38 +0100 Subject: [PATCH 2/4] Update test classes to PSR-4 --- composer.json | 8 ++++++-- phpunit.xml => phpunit.xml.dist | 4 ++-- test/bootstrap.php | 6 ------ {test/src/CentralDesktop/JMX/Test => tests}/BeanTest.php | 4 ++-- .../src/CentralDesktop/JMX/Test => tests}/ClientTest.php | 4 ++-- 5 files changed, 12 insertions(+), 14 deletions(-) rename phpunit.xml => phpunit.xml.dist (77%) delete mode 100644 test/bootstrap.php rename {test/src/CentralDesktop/JMX/Test => tests}/BeanTest.php (98%) rename {test/src/CentralDesktop/JMX/Test => tests}/ClientTest.php (92%) diff --git a/composer.json b/composer.json index 898a273..77218b8 100644 --- a/composer.json +++ b/composer.json @@ -11,17 +11,21 @@ "symfony/console": "~2.3" }, "require-dev": { - "phpunit/phpunit": ">=3.7.0", + "phpunit/phpunit": ">=3.7.0,<6.0", "phpmd/phpmd": "1.5.*", "pdepend/pdepend": "1.1.*", "squizlabs/php_codesniffer": "1.4.*", "mockery/mockery": "0.8.0" }, - "autoload": { "psr-0": { "CentralDesktop": "src/" } + }, + "autoload-dev": { + "psr-4": { + "CentralDesktop\\JMX\\Tests\\": "tests/" + } } } diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 77% rename from phpunit.xml rename to phpunit.xml.dist index 0d493a3..909ee9c 100644 --- a/phpunit.xml +++ b/phpunit.xml.dist @@ -1,9 +1,9 @@ - + - test/src/CentralDesktop/JMX + tests/ diff --git a/test/bootstrap.php b/test/bootstrap.php deleted file mode 100644 index c022ff8..0000000 --- a/test/bootstrap.php +++ /dev/null @@ -1,6 +0,0 @@ -add('CentralDesktop\JMX\Test', __DIR__); diff --git a/test/src/CentralDesktop/JMX/Test/BeanTest.php b/tests/BeanTest.php similarity index 98% rename from test/src/CentralDesktop/JMX/Test/BeanTest.php rename to tests/BeanTest.php index 4a89425..11ab168 100644 --- a/test/src/CentralDesktop/JMX/Test/BeanTest.php +++ b/tests/BeanTest.php @@ -7,7 +7,7 @@ * To change this template use File | Settings | File Templates. */ -namespace CentralDesktop\JMX\Test; +namespace CentralDesktop\JMX\Tests; use CentralDesktop\JMX\Bean; @@ -101,4 +101,4 @@ function testToString() { $this->assertSame("$bean", "mbean:{$obj}"); $this->assertSame("{$bean}", "mbean:{$obj}"); } -} \ No newline at end of file +} diff --git a/test/src/CentralDesktop/JMX/Test/ClientTest.php b/tests/ClientTest.php similarity index 92% rename from test/src/CentralDesktop/JMX/Test/ClientTest.php rename to tests/ClientTest.php index 1a15667..b87776a 100644 --- a/test/src/CentralDesktop/JMX/Test/ClientTest.php +++ b/tests/ClientTest.php @@ -7,7 +7,7 @@ * To change this template use File | Settings | File Templates. */ -namespace CentralDesktop\JMX\Test; +namespace CentralDesktop\JMX\Tests; @@ -26,4 +26,4 @@ function testBeanMe() { $this->assertInstanceOf('\CentralDesktop\JMX\Bean', $b); $this->assertSame($b->getName(), "1234"); } -} \ No newline at end of file +} From 8e7c27256a218c17483ffde26694da2b1ea86dbc Mon Sep 17 00:00:00 2001 From: Charles Bell Date: Sat, 4 Feb 2017 00:38:43 +0100 Subject: [PATCH 3/4] Update test dependencies --- composer.json | 16 +++++++++++----- phpunit.xml.dist | 4 ---- tests/BeanTest.php | 5 +++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 77218b8..f99b6fb 100644 --- a/composer.json +++ b/composer.json @@ -11,11 +11,11 @@ "symfony/console": "~2.3" }, "require-dev": { - "phpunit/phpunit": ">=3.7.0,<6.0", - "phpmd/phpmd": "1.5.*", - "pdepend/pdepend": "1.1.*", - "squizlabs/php_codesniffer": "1.4.*", - "mockery/mockery": "0.8.0" + "phpunit/phpunit": "^5.7", + "phpmd/phpmd": "^2.6", + "pdepend/pdepend": "^2.5", + "squizlabs/php_codesniffer": "^2.8", + "mockery/mockery": "^0.9.7" }, "autoload": { @@ -27,5 +27,11 @@ "psr-4": { "CentralDesktop\\JMX\\Tests\\": "tests/" } + }, + + "config": { + "platform": { + "php": "5.6" + } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 909ee9c..787f4fc 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -12,8 +12,4 @@ src/CentralDesktop/ - - - - diff --git a/tests/BeanTest.php b/tests/BeanTest.php index 11ab168..e1b56c3 100644 --- a/tests/BeanTest.php +++ b/tests/BeanTest.php @@ -13,8 +13,9 @@ use CentralDesktop\JMX\Bean; use Mockery as m; -class BeanTest extends \PHPUnit_Framework_TestCase { - +class BeanTest extends \PHPUnit_Framework_TestCase +{ + use \Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; public function testRead() { From bc7f9b52ad126ef4b1118ccd2a9811b7390cc944 Mon Sep 17 00:00:00 2001 From: Charles Bell Date: Sat, 4 Feb 2017 00:38:47 +0100 Subject: [PATCH 4/4] Implement command to list MBeans --- bin/jmx.php | 1 + src/CentralDesktop/JMX/Client.php | 59 ++++++++++--------- src/CentralDesktop/JMX/Command/Command.php | 32 ++++++++++ .../JMX/Command/ListCommand.php | 34 +++++++++++ .../JMX/Command/ReadCommand.php | 38 +++++------- tests/ClientTest.php | 44 +++++++++----- 6 files changed, 144 insertions(+), 64 deletions(-) create mode 100644 src/CentralDesktop/JMX/Command/Command.php create mode 100644 src/CentralDesktop/JMX/Command/ListCommand.php diff --git a/bin/jmx.php b/bin/jmx.php index 6c2ed7d..43a7b1b 100755 --- a/bin/jmx.php +++ b/bin/jmx.php @@ -4,5 +4,6 @@ require_once __DIR__.'/../vendor/autoload.php'; $application = new Symfony\Component\Console\Application(); +$application->add(new CentralDesktop\JMX\Command\ListCommand()); $application->add(new CentralDesktop\JMX\Command\ReadCommand()); $application->run(); diff --git a/src/CentralDesktop/JMX/Client.php b/src/CentralDesktop/JMX/Client.php index 967c890..e278db5 100644 --- a/src/CentralDesktop/JMX/Client.php +++ b/src/CentralDesktop/JMX/Client.php @@ -1,14 +1,14 @@ uri = $uri; $this->username = $username; $this->password = $password; - $this->logger = new \Psr\Log\NullLogger(); - - + $this->logger = new NullLogger(); $client = new Http\Client($uri); - - $client->setDefaultOption('auth', - array($username, $password, 'Basic')); - + $client->setDefaultOption('auth', array($username, $password, 'Basic')); $client->setDefaultOption('timeout', 5); $client->setDefaultOption('connect_timeout', 1); $this->setClient($client); } - public function setClient(\Guzzle\Http\Client $client){ $this->g = $client; } - - public - function bean($name) { + public function bean($name) { $bean = new Bean($this, $name); $bean->setLogger($this->logger); return $bean; } - /** * Sets a logger instance on the object * @@ -67,17 +56,33 @@ function bean($name) { * * @return null */ - public - function setLogger(LoggerInterface $logger) { + public function setLogger(LoggerInterface $logger) { $this->logger = $logger; } + public function read($name) + { + return $this->sendRequest("read/{$name}"); + } + + public function list() + { + $response = $this->sendRequest('list'); + + $beans = []; + foreach($response['value'] as $domain => $list) { + foreach($list as $name => $metadata) { + $beans[] = $this->bean($domain.':'.$name); + } + } + + return $beans; + } - public - function read($name) { - $request = $this->g->get("read/{$name}"); - $response = $request->send(); + protected function sendRequest($path) + { + $request = $this->g->get($path); - return $response->json(); + return $request->send()->json(); } -} \ No newline at end of file +} diff --git a/src/CentralDesktop/JMX/Command/Command.php b/src/CentralDesktop/JMX/Command/Command.php new file mode 100644 index 0000000..7235020 --- /dev/null +++ b/src/CentralDesktop/JMX/Command/Command.php @@ -0,0 +1,32 @@ +addArgument('uri', InputArgument::REQUIRED, 'URL for the JMX Jolokia endpoint') + ->addOption('user', '', InputOption::VALUE_REQUIRED, 'Username') + ->addOption('password', '', InputOption::VALUE_REQUIRED, 'Password') + ; + } + + protected function buildClient(InputInterface $input) + { + return new Client( + $input->getArgument('uri'), + $input->getOption('user'), + $input->getOption('password') + ); + } +} diff --git a/src/CentralDesktop/JMX/Command/ListCommand.php b/src/CentralDesktop/JMX/Command/ListCommand.php new file mode 100644 index 0000000..f645d4a --- /dev/null +++ b/src/CentralDesktop/JMX/Command/ListCommand.php @@ -0,0 +1,34 @@ +setName('beans') + ->setDescription('Lists MBeans') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $client = $this->buildClient($input); + + $beans = $client->list(); + + foreach ($beans as $bean) { + $output->writeln($bean->getName()); + } + } +} diff --git a/src/CentralDesktop/JMX/Command/ReadCommand.php b/src/CentralDesktop/JMX/Command/ReadCommand.php index 27cf1c0..885a360 100644 --- a/src/CentralDesktop/JMX/Command/ReadCommand.php +++ b/src/CentralDesktop/JMX/Command/ReadCommand.php @@ -4,43 +4,36 @@ use CentralDesktop\JMX\Client; use Symfony\Component\Console\Application; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -class ReadCommand extends Command { - protected - function configure() { +class ReadCommand extends Command +{ + protected function configure() + { + parent::configure(); + $this - ->setName('read') - ->setDescription("Displays JMX attributes") - ->addArgument( - 'uri', - InputArgument::REQUIRED, - 'URL for the JMX Jolokia endpoint' - ) - ->addArgument( + ->setName('read') + ->setDescription('Displays MBean attributes') + ->addArgument( 'object', InputArgument::REQUIRED, 'Name of the object you are fetching' ) - ->addOption('user', '', InputOption::VALUE_REQUIRED, "Username") - ->addOption('password', '', InputOption::VALUE_REQUIRED, "Password"); + ; } - protected - function execute(InputInterface $input, OutputInterface $output) { - $client = new Client($input->getArgument("uri"), - $input->getOption("user"), - $input->getOption("password")); - $bean = $client->bean($input->getArgument("object")); + protected function execute(InputInterface $input, OutputInterface $output) + { + $client = $this->buildClient($input); - $bean->read(); + $bean = $client->bean($input->getArgument('object')); $attributes = $bean->getAttributes(); - $keys = array_keys($attributes); + $keys = array_keys($attributes); foreach ($keys as $key) { $value = $attributes[$key]; @@ -57,6 +50,5 @@ function execute(InputInterface $input, OutputInterface $output) { } } - } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index b87776a..e3bae98 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -1,29 +1,45 @@ bean("1234"); $this->assertInstanceOf('\CentralDesktop\JMX\Bean', $b); $this->assertSame($b->getName(), "1234"); } + + public function testList() + { + $client = m::mock('\CentralDesktop\JMX\Client[sendRequest]', [''])->shouldAllowMockingProtectedMethods(); + + $client->shouldReceive('sendRequest')->once()->andReturn([ + 'value' => [ + 'java.lang' => [ + 'name=Foo' => [], + 'name=Bar' => [], + ], + 'java.nio' => [ + 'type=BufferPool' => [], + ], + ], + ]); + + $beans = $client->list(); + + $this->assertCount(3, $beans); + $this->assertInstanceOf('\CentralDesktop\JMX\Bean', $beans[0]); + $this->assertSame('java.lang:name=Foo', $beans[0]->getName()); + } }