Mercurial > hg > release > thermostat-0.7
view agent/cli/src/test/java/com/redhat/thermostat/agent/cli/impl/db/StorageCommandTest.java @ 1049:3f0416e9db80
Don't use System.exit anywhere other than in Laucher.
Reviewed-by: omajid
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-March/006029.html
author | Severin Gehwolf <sgehwolf@redhat.com> |
---|---|
date | Fri, 08 Mar 2013 14:28:35 +0100 |
parents | 8ea56b57334c |
children |
line wrap: on
line source
/* * Copyright 2012, 2013 Red Hat, Inc. * * This file is part of Thermostat. * * Thermostat is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2, or (at your * option) any later version. * * Thermostat 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 * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Thermostat; see the file COPYING. If not see * <http://www.gnu.org/licenses/>. * * Linking this code with other modules is making a combined work * based on this code. Thus, the terms and conditions of the GNU * General Public License cover the whole combination. * * As a special exception, the copyright holders of this code give * you permission to link this code with independent modules to * produce an executable, regardless of the license terms of these * independent modules, and to copy and distribute the resulting * executable under terms of your choice, provided that you also * meet, for each linked independent module, the terms and conditions * of the license of that module. An independent module is a module * which is not derived from or based on this code. If you modify * this code, you may extend this exception to your version of the * library, but you are not obligated to do so. If you do not wish * to do so, delete this exception statement from your version. */ package com.redhat.thermostat.agent.cli.impl.db; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import java.util.Random; import java.util.concurrent.CountDownLatch; import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.redhat.thermostat.common.ActionEvent; import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.common.ExitStatus; import com.redhat.thermostat.common.cli.CommandContext; import com.redhat.thermostat.common.cli.CommandException; import com.redhat.thermostat.common.cli.SimpleArguments; import com.redhat.thermostat.common.config.InvalidConfigurationException; import com.redhat.thermostat.common.tools.ApplicationException; import com.redhat.thermostat.common.tools.ApplicationState; public class StorageCommandTest { private static final String PORT = "27518"; private static final String BIND = "127.0.0.1"; private static final String DB = "storage/db"; private String tmpDir; private ExitStatus exitStatus; @Before public void setup() { exitStatus = mock(ExitStatus.class); // need to create a dummy config file for the test try { Random random = new Random(); tmpDir = System.getProperty("java.io.tmpdir") + File.separatorChar + Math.abs(random.nextInt()) + File.separatorChar; System.setProperty("THERMOSTAT_HOME", tmpDir); File base = new File(tmpDir + "storage"); base.mkdirs(); File tmpConfigs = new File(base, "db.properties"); new File(base, "run").mkdirs(); new File(base, "logs").mkdirs(); new File(base, "db").mkdirs(); Properties props = new Properties(); props.setProperty(DBConfig.BIND.name(), BIND); props.setProperty(DBConfig.PORT.name(), PORT); props.store(new FileOutputStream(tmpConfigs), "thermostat test properties"); } catch (IOException e) { Assert.fail("cannot setup tests: " + e); } } @After public void tearDown() { exitStatus = null; } @Test public void testConfig() throws CommandException { SimpleArguments args = new SimpleArguments(); args.addArgument("quiet", null); args.addArgument("start", null); args.addArgument("dryRun", null); CommandContext ctx = mock(CommandContext.class); when(ctx.getArguments()).thenReturn(args); StorageCommand service = new StorageCommand(exitStatus) { @Override MongoProcessRunner createRunner() { throw new AssertionError("dry run should never create an actual runner"); } }; service.run(ctx); DBStartupConfiguration conf = service.getConfiguration(); Assert.assertEquals(tmpDir + DB, conf.getDBPath().getPath()); Assert.assertEquals(Integer.parseInt(PORT), conf.getPort()); Assert.assertEquals("mongodb://" + BIND + ":" + PORT , conf.getDBConnectionString()); } private StorageCommand prepareService(boolean startSuccess) throws IOException, InterruptedException, InvalidConfigurationException, ApplicationException { final MongoProcessRunner runner = mock(MongoProcessRunner.class); if (!startSuccess) { doThrow(new ApplicationException("mock exception")).when(runner).startService(); } // TODO: stop not tested yet, but be sure it's not called from the code doThrow(new ApplicationException("mock exception")).when(runner).stopService(); StorageCommand service = new StorageCommand(exitStatus) { @Override MongoProcessRunner createRunner() { return runner; } }; return service; } @Test public void testListeners() throws InterruptedException, IOException, ApplicationException, InvalidConfigurationException, CommandException { StorageCommand service = prepareService(true); final CountDownLatch latch = new CountDownLatch(2); final boolean[] result = new boolean[2]; service.getNotifier().addActionListener(new ActionListener<ApplicationState>() { @SuppressWarnings("incomplete-switch") @Override public void actionPerformed(ActionEvent<ApplicationState> actionEvent) { switch (actionEvent.getActionId()) { case FAIL: result[0] = false; latch.countDown(); latch.countDown(); break; case SUCCESS: result[0] = true; latch.countDown(); break; case START: result[1] = true; latch.countDown(); break; } } }); service.run(prepareContext()); latch.await(); Assert.assertTrue(result[0]); Assert.assertTrue(result[1]); } @Test public void testListenersFail() throws InterruptedException, IOException, ApplicationException, CommandException, InvalidConfigurationException { StorageCommand service = prepareService(false); final CountDownLatch latch = new CountDownLatch(1); final boolean[] result = new boolean[1]; service.getNotifier().addActionListener(new ActionListener<ApplicationState>() { @SuppressWarnings("incomplete-switch") @Override public void actionPerformed(ActionEvent<ApplicationState> actionEvent) { switch (actionEvent.getActionId()) { case FAIL: result[0] = true; break; case SUCCESS: result[0] = false; break; } latch.countDown(); } }); service.run(prepareContext()); latch.await(); Assert.assertTrue(result[0]); } @Test public void exceptionSetsExitStatusOnFailure() throws Exception { this.exitStatus = new ExitStatus() { private int exitStatus = -1; @Override public void setExitStatus(int newExitStatus) { exitStatus = newExitStatus; } @Override public int getExitStatus() { return exitStatus; } }; assertEquals(-1, this.exitStatus.getExitStatus()); StorageCommand command = prepareService(false); final CountDownLatch latch = new CountDownLatch(1); final boolean[] result = new boolean[1]; command.getNotifier().addActionListener(new ActionListener<ApplicationState>() { @SuppressWarnings("incomplete-switch") @Override public void actionPerformed(ActionEvent<ApplicationState> actionEvent) { switch (actionEvent.getActionId()) { case FAIL: result[0] = true; break; case SUCCESS: result[0] = false; break; } latch.countDown(); } }); command.run(prepareContext()); latch.await(); // should have failed assertTrue(result[0]); assertEquals(ExitStatus.EXIT_ERROR, this.exitStatus.getExitStatus()); } @Test public void exitStatusRemainsUntouchedOnSuccess() throws Exception { this.exitStatus = new ExitStatus() { private int exitStatus = -1; @Override public void setExitStatus(int newExitStatus) { exitStatus = newExitStatus; } @Override public int getExitStatus() { return exitStatus; } }; StorageCommand command = prepareService(true); final CountDownLatch latch = new CountDownLatch(1); final boolean[] result = new boolean[1]; command.getNotifier().addActionListener(new ActionListener<ApplicationState>() { @SuppressWarnings("incomplete-switch") @Override public void actionPerformed(ActionEvent<ApplicationState> actionEvent) { switch (actionEvent.getActionId()) { case FAIL: result[0] = false; break; case SUCCESS: result[0] = true; break; } latch.countDown(); } }); command.run(prepareContext()); latch.await(); // should have worked assertTrue(result[0]); // this impl of ExitStatus has a default value of -1 assertEquals(-1, this.exitStatus.getExitStatus()); } private CommandContext prepareContext() { SimpleArguments args = new SimpleArguments(); args.addArgument("quiet", "--quiet"); args.addArgument("start", "--start"); CommandContext ctx = mock(CommandContext.class); when(ctx.getArguments()).thenReturn(args); return ctx; } @Test public void testName() { StorageCommand dbService = new StorageCommand(exitStatus); String name = dbService.getName(); assertEquals("storage", name); } @Test public void testDescAndUsage() { StorageCommand dbService = new StorageCommand(exitStatus); assertNotNull(dbService.getDescription()); assertNotNull(dbService.getUsage()); } }