diff --git a/bin/jmx.php b/bin/jmx.php index ae4c8ba..43a7b1b 100755 --- a/bin/jmx.php +++ b/bin/jmx.php @@ -1,70 +1,9 @@ #!/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\ListCommand()); +$application->add(new CentralDesktop\JMX\Command\ReadCommand()); $application->run(); diff --git a/composer.json b/composer.json index 898a273..f99b6fb 100644 --- a/composer.json +++ b/composer.json @@ -11,17 +11,27 @@ "symfony/console": "~2.3" }, "require-dev": { - "phpunit/phpunit": ">=3.7.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": { "psr-0": { "CentralDesktop": "src/" } + }, + "autoload-dev": { + "psr-4": { + "CentralDesktop\\JMX\\Tests\\": "tests/" + } + }, + + "config": { + "platform": { + "php": "5.6" + } } } diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 55% rename from phpunit.xml rename to phpunit.xml.dist index 0d493a3..787f4fc 100644 --- a/phpunit.xml +++ b/phpunit.xml.dist @@ -1,9 +1,9 @@ - + - test/src/CentralDesktop/JMX + tests/ @@ -12,8 +12,4 @@ src/CentralDesktop/ - - - - 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 new file mode 100644 index 0000000..885a360 --- /dev/null +++ b/src/CentralDesktop/JMX/Command/ReadCommand.php @@ -0,0 +1,54 @@ +setName('read') + ->setDescription('Displays MBean attributes') + ->addArgument( + 'object', + InputArgument::REQUIRED, + 'Name of the object you are fetching' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $client = $this->buildClient($input); + + $bean = $client->bean($input->getArgument('object')); + + $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 : "); + } + + } + } +} 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/ClientTest.php b/test/src/CentralDesktop/JMX/Test/ClientTest.php deleted file mode 100644 index 1a15667..0000000 --- a/test/src/CentralDesktop/JMX/Test/ClientTest.php +++ /dev/null @@ -1,29 +0,0 @@ -bean("1234"); - - $this->assertInstanceOf('\CentralDesktop\JMX\Bean', $b); - $this->assertSame($b->getName(), "1234"); - } -} \ No newline at end of file diff --git a/test/src/CentralDesktop/JMX/Test/BeanTest.php b/tests/BeanTest.php similarity index 94% rename from test/src/CentralDesktop/JMX/Test/BeanTest.php rename to tests/BeanTest.php index 4a89425..e1b56c3 100644 --- a/test/src/CentralDesktop/JMX/Test/BeanTest.php +++ b/tests/BeanTest.php @@ -7,14 +7,15 @@ * To change this template use File | Settings | File Templates. */ -namespace CentralDesktop\JMX\Test; +namespace CentralDesktop\JMX\Tests; 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() { @@ -101,4 +102,4 @@ function testToString() { $this->assertSame("$bean", "mbean:{$obj}"); $this->assertSame("{$bean}", "mbean:{$obj}"); } -} \ No newline at end of file +} diff --git a/tests/ClientTest.php b/tests/ClientTest.php new file mode 100644 index 0000000..e3bae98 --- /dev/null +++ b/tests/ClientTest.php @@ -0,0 +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()); + } +}