Mercurial > hg > release > thermostat-0.13
changeset 202:7d7f9dd102bb
move ApplicationContext to common
reviewed-by: rkennke
review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/000705.html
line wrap: on
line diff
--- a/client/src/main/java/com/redhat/thermostat/client/Main.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/Main.java Wed Apr 04 17:03:52 2012 -0400 @@ -47,7 +47,6 @@ import javax.swing.JPopupMenu; import javax.swing.SwingUtilities; -import com.redhat.thermostat.client.appctx.ApplicationContext; import com.redhat.thermostat.client.config.ConnectionConfiguration; import com.redhat.thermostat.client.locale.LocaleResources; import com.redhat.thermostat.client.ui.ConnectionSelectionDialog; @@ -55,6 +54,7 @@ import com.redhat.thermostat.common.Constants; import com.redhat.thermostat.common.ThreadPoolTimerFactory; import com.redhat.thermostat.common.TimerFactory; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.config.StartupConfiguration; import com.redhat.thermostat.common.dao.Connection; import com.redhat.thermostat.common.dao.Connection.ConnectionListener;
--- a/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java Wed Apr 04 17:03:52 2012 -0400 @@ -40,10 +40,10 @@ import java.util.concurrent.TimeUnit; import java.util.logging.Logger; -import com.redhat.thermostat.client.appctx.ApplicationContext; import com.redhat.thermostat.common.ActionEvent; import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.common.Timer; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef;
--- a/client/src/main/java/com/redhat/thermostat/client/SummaryPanelFacadeImpl.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/SummaryPanelFacadeImpl.java Wed Apr 04 17:03:52 2012 -0400 @@ -40,9 +40,9 @@ import java.util.List; import java.util.concurrent.TimeUnit; -import com.redhat.thermostat.client.appctx.ApplicationContext; import com.redhat.thermostat.common.Timer; import com.redhat.thermostat.common.Timer.SchedulingType; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.VmInfoDAO;
--- a/client/src/main/java/com/redhat/thermostat/client/appctx/ApplicationContext.java Wed Apr 04 15:51:48 2012 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,79 +0,0 @@ -/* - * Copyright 2012 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.client.appctx; - -import com.redhat.thermostat.common.TimerFactory; -import com.redhat.thermostat.common.dao.DAOFactory; - -public class ApplicationContext { - - private static ApplicationContext instance = new ApplicationContext(); - - private DAOFactory daoFactory; - - private TimerFactory timerFactory; - - public static ApplicationContext getInstance() { - return instance; - } - - static void reset() { - instance = new ApplicationContext(); - } - - private ApplicationContext() { - // Nothing to do here, just prevent instantiation of this class outside - // the factory method. - } - - public void setDAOFactory(DAOFactory daoFactory) { - this.daoFactory = daoFactory; - } - - public DAOFactory getDAOFactory() { - return daoFactory; - } - - public void setTimerFactory(TimerFactory timerFactory) { - this.timerFactory = timerFactory; - } - - public TimerFactory getTimerFactory() { - return timerFactory; - } - -}
--- a/client/src/main/java/com/redhat/thermostat/client/ui/HostCpuController.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/ui/HostCpuController.java Wed Apr 04 17:03:52 2012 -0400 @@ -42,9 +42,9 @@ import java.util.concurrent.TimeUnit; import com.redhat.thermostat.client.AsyncUiFacade; -import com.redhat.thermostat.client.appctx.ApplicationContext; import com.redhat.thermostat.common.Timer; import com.redhat.thermostat.common.Timer.SchedulingType; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.dao.CpuStatDAO; import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO;
--- a/client/src/main/java/com/redhat/thermostat/client/ui/HostMemoryController.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/ui/HostMemoryController.java Wed Apr 04 17:03:52 2012 -0400 @@ -46,9 +46,9 @@ import java.util.concurrent.TimeUnit; import com.redhat.thermostat.client.AsyncUiFacade; -import com.redhat.thermostat.client.appctx.ApplicationContext; import com.redhat.thermostat.client.locale.LocaleResources; import com.redhat.thermostat.client.ui.HostMemoryView.GraphVisibilityChangeListener; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef;
--- a/client/src/main/java/com/redhat/thermostat/client/ui/HostOverviewController.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/ui/HostOverviewController.java Wed Apr 04 17:03:52 2012 -0400 @@ -52,8 +52,8 @@ import javax.swing.SwingWorker; import com.redhat.thermostat.client.AsyncUiFacade; -import com.redhat.thermostat.client.appctx.ApplicationContext; import com.redhat.thermostat.client.locale.LocaleResources; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef;
--- a/client/src/main/java/com/redhat/thermostat/client/ui/VmClassStatController.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/ui/VmClassStatController.java Wed Apr 04 17:03:52 2012 -0400 @@ -44,7 +44,7 @@ import java.util.concurrent.TimeUnit; import com.redhat.thermostat.client.AsyncUiFacade; -import com.redhat.thermostat.client.appctx.ApplicationContext; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.dao.VmClassStatDAO; import com.redhat.thermostat.common.dao.VmRef; import com.redhat.thermostat.common.model.DiscreteTimeData;
--- a/client/src/main/java/com/redhat/thermostat/client/ui/VmCpuController.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/ui/VmCpuController.java Wed Apr 04 17:03:52 2012 -0400 @@ -44,7 +44,7 @@ import java.util.concurrent.TimeUnit; import com.redhat.thermostat.client.AsyncUiFacade; -import com.redhat.thermostat.client.appctx.ApplicationContext; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.dao.VmCpuStatDAO; import com.redhat.thermostat.common.dao.VmRef; import com.redhat.thermostat.common.model.DiscreteTimeData;
--- a/client/src/main/java/com/redhat/thermostat/client/ui/VmGcController.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/ui/VmGcController.java Wed Apr 04 17:03:52 2012 -0400 @@ -50,8 +50,8 @@ import java.util.concurrent.TimeUnit; import com.redhat.thermostat.client.AsyncUiFacade; -import com.redhat.thermostat.client.appctx.ApplicationContext; import com.redhat.thermostat.client.locale.LocaleResources; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.VmGcStatDAO; import com.redhat.thermostat.common.dao.VmMemoryStatDAO;
--- a/client/src/main/java/com/redhat/thermostat/client/ui/VmMemoryController.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/ui/VmMemoryController.java Wed Apr 04 17:03:52 2012 -0400 @@ -43,7 +43,7 @@ import java.util.concurrent.TimeUnit; import com.redhat.thermostat.client.AsyncUiFacade; -import com.redhat.thermostat.client.appctx.ApplicationContext; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.dao.VmMemoryStatDAO; import com.redhat.thermostat.common.dao.VmRef; import com.redhat.thermostat.common.model.VmMemoryStat;
--- a/client/src/main/java/com/redhat/thermostat/client/ui/VmOverviewController.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/ui/VmOverviewController.java Wed Apr 04 17:03:52 2012 -0400 @@ -46,8 +46,8 @@ import java.util.concurrent.TimeUnit; import com.redhat.thermostat.client.AsyncUiFacade; -import com.redhat.thermostat.client.appctx.ApplicationContext; import com.redhat.thermostat.client.locale.LocaleResources; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.dao.VmInfoDAO; import com.redhat.thermostat.common.dao.VmRef; import com.redhat.thermostat.common.model.VmInfo;
--- a/client/src/test/java/com/redhat/thermostat/client/MainWindowControllerImplTest.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/test/java/com/redhat/thermostat/client/MainWindowControllerImplTest.java Wed Apr 04 17:03:52 2012 -0400 @@ -54,12 +54,12 @@ import org.junit.Test; import org.mockito.ArgumentCaptor; -import com.redhat.thermostat.client.appctx.ApplicationContext; -import com.redhat.thermostat.client.appctx.ApplicationContextUtil; import com.redhat.thermostat.common.ActionEvent; import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.common.Timer; import com.redhat.thermostat.common.TimerFactory; +import com.redhat.thermostat.common.appctx.ApplicationContext; +import com.redhat.thermostat.common.appctx.ApplicationContextUtil; import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef;
--- a/client/src/test/java/com/redhat/thermostat/client/SummaryPanelFacadeImplTest.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/test/java/com/redhat/thermostat/client/SummaryPanelFacadeImplTest.java Wed Apr 04 17:03:52 2012 -0400 @@ -47,11 +47,11 @@ import org.junit.Before; import org.junit.Test; -import com.redhat.thermostat.client.appctx.ApplicationContext; -import com.redhat.thermostat.client.appctx.ApplicationContextUtil; import com.redhat.thermostat.common.Timer; import com.redhat.thermostat.common.Timer.SchedulingType; import com.redhat.thermostat.common.TimerFactory; +import com.redhat.thermostat.common.appctx.ApplicationContext; +import com.redhat.thermostat.common.appctx.ApplicationContextUtil; import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.MongoDAOFactory;
--- a/client/src/test/java/com/redhat/thermostat/client/VmClassStatControllerTest.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/test/java/com/redhat/thermostat/client/VmClassStatControllerTest.java Wed Apr 04 17:03:52 2012 -0400 @@ -47,9 +47,9 @@ import org.junit.Test; -import com.redhat.thermostat.client.appctx.ApplicationContext; import com.redhat.thermostat.client.ui.VmClassStatController; import com.redhat.thermostat.client.ui.VmClassStatView; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.MongoDAOFactory; import com.redhat.thermostat.common.dao.VmClassStatDAO;
--- a/client/src/test/java/com/redhat/thermostat/client/appctx/ApplicationContextTest.java Wed Apr 04 15:51:48 2012 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,125 +0,0 @@ -/* - * Copyright 2012 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.client.appctx; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.redhat.thermostat.client.appctx.ApplicationContext; -import com.redhat.thermostat.common.TimerFactory; -import com.redhat.thermostat.common.dao.DAOFactory; - -public class ApplicationContextTest { - - @Before - public void setUp() { - ApplicationContext.reset(); - } - - @After - public void tearDown() { - ApplicationContext.reset(); - } - - @Test - public void verifyGetInstanceNotNull() { - ApplicationContext ctx = ApplicationContext.getInstance(); - assertNotNull(ctx); - } - - @Test - public void testDAOFactorySetGet() { - DAOFactory daoFactory = mock(DAOFactory.class); - ApplicationContext ctx = ApplicationContext.getInstance(); - ctx.setDAOFactory(daoFactory); - - DAOFactory actual1 = ctx.getDAOFactory(); - assertSame(daoFactory, actual1); - } - - @Test - public void verifyDAOFactoryIsNullWhenNotInitialized() { - ApplicationContext ctx = ApplicationContext.getInstance(); - - DAOFactory actual = ctx.getDAOFactory(); - assertNull(actual); - } - - @Test - public void verifyDAOFactoryStaysSame() { - DAOFactory daoFactory = mock(DAOFactory.class); - ApplicationContext ctx = ApplicationContext.getInstance(); - ctx.setDAOFactory(daoFactory); - - ApplicationContext ctx2 = ApplicationContext.getInstance(); - DAOFactory actual2 = ctx2.getDAOFactory(); - assertSame(daoFactory, actual2); - } - - @Test - public void testTimerFactorySetGet() { - TimerFactory timerFactory = mock(TimerFactory.class); - ApplicationContext ctx = ApplicationContext.getInstance(); - ctx.setTimerFactory(timerFactory); - - TimerFactory actual1 = ctx.getTimerFactory(); - assertSame(timerFactory, actual1); - } - - @Test - public void verifyTimerFactoryIsNullWhenNotInitialized() { - ApplicationContext ctx = ApplicationContext.getInstance(); - - TimerFactory actual = ctx.getTimerFactory(); - assertNull(actual); - } - - @Test - public void verifyTimerFactoryStaysSame() { - TimerFactory timerFactory = mock(TimerFactory.class); - ApplicationContext ctx = ApplicationContext.getInstance(); - ctx.setTimerFactory(timerFactory); - - ApplicationContext ctx2 = ApplicationContext.getInstance(); - TimerFactory actual2 = ctx2.getTimerFactory(); - assertSame(timerFactory, actual2); - } -}
--- a/client/src/test/java/com/redhat/thermostat/client/appctx/ApplicationContextUtil.java Wed Apr 04 15:51:48 2012 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -/* - * Copyright 2012 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.client.appctx; - -import com.redhat.thermostat.client.appctx.ApplicationContext; - -public class ApplicationContextUtil { - - /** - * This is here to allow tests to reset the ApplicationContext, while - * preventing real code the same (ApplicationContext.reset() is package private). - * - * It is vital that tests call this from their setUp() and tearDown() methods, - * to avoid leaking mocks and stuff from test to test. - */ - public static void resetApplicationContext() { - ApplicationContext.reset(); - } -}
--- a/client/src/test/java/com/redhat/thermostat/client/ui/HostCpuControllerTest.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/test/java/com/redhat/thermostat/client/ui/HostCpuControllerTest.java Wed Apr 04 17:03:52 2012 -0400 @@ -53,13 +53,13 @@ import org.junit.Test; import org.mockito.ArgumentCaptor; -import com.redhat.thermostat.client.appctx.ApplicationContext; -import com.redhat.thermostat.client.appctx.ApplicationContextUtil; import com.redhat.thermostat.client.ui.HostCpuController; import com.redhat.thermostat.client.ui.HostCpuView; import com.redhat.thermostat.common.Timer; import com.redhat.thermostat.common.Timer.SchedulingType; import com.redhat.thermostat.common.TimerFactory; +import com.redhat.thermostat.common.appctx.ApplicationContext; +import com.redhat.thermostat.common.appctx.ApplicationContextUtil; import com.redhat.thermostat.common.dao.CpuStatDAO; import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO;
--- a/client/src/test/java/com/redhat/thermostat/client/ui/HostMemoryControllerTest.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/test/java/com/redhat/thermostat/client/ui/HostMemoryControllerTest.java Wed Apr 04 17:03:52 2012 -0400 @@ -47,7 +47,7 @@ import org.junit.Test; -import com.redhat.thermostat.client.appctx.ApplicationContext; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef;
--- a/client/src/test/java/com/redhat/thermostat/client/ui/VmCpuControllerTest.java Wed Apr 04 15:51:48 2012 -0400 +++ b/client/src/test/java/com/redhat/thermostat/client/ui/VmCpuControllerTest.java Wed Apr 04 17:03:52 2012 -0400 @@ -47,7 +47,7 @@ import org.junit.Test; -import com.redhat.thermostat.client.appctx.ApplicationContext; +import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.MongoDAOFactory; import com.redhat.thermostat.common.dao.VmCpuStatDAO;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/src/main/java/com/redhat/thermostat/common/appctx/ApplicationContext.java Wed Apr 04 17:03:52 2012 -0400 @@ -0,0 +1,79 @@ +/* + * Copyright 2012 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.common.appctx; + +import com.redhat.thermostat.common.TimerFactory; +import com.redhat.thermostat.common.dao.DAOFactory; + +public class ApplicationContext { + + private static ApplicationContext instance = new ApplicationContext(); + + private DAOFactory daoFactory; + + private TimerFactory timerFactory; + + public static ApplicationContext getInstance() { + return instance; + } + + static void reset() { + instance = new ApplicationContext(); + } + + private ApplicationContext() { + // Nothing to do here, just prevent instantiation of this class outside + // the factory method. + } + + public void setDAOFactory(DAOFactory daoFactory) { + this.daoFactory = daoFactory; + } + + public DAOFactory getDAOFactory() { + return daoFactory; + } + + public void setTimerFactory(TimerFactory timerFactory) { + this.timerFactory = timerFactory; + } + + public TimerFactory getTimerFactory() { + return timerFactory; + } + +}
--- a/common/src/main/java/com/redhat/thermostat/common/dao/CpuStatDAO.java Wed Apr 04 15:51:48 2012 -0400 +++ b/common/src/main/java/com/redhat/thermostat/common/dao/CpuStatDAO.java Wed Apr 04 17:03:52 2012 -0400 @@ -53,4 +53,6 @@ List<CpuStat> getLatestCpuStats(HostRef ref); + void putCpuStat(CpuStat stat); + }
--- a/common/src/main/java/com/redhat/thermostat/common/dao/CpuStatDAOImpl.java Wed Apr 04 15:51:48 2012 -0400 +++ b/common/src/main/java/com/redhat/thermostat/common/dao/CpuStatDAOImpl.java Wed Apr 04 17:03:52 2012 -0400 @@ -69,4 +69,9 @@ public long getCount() { return storage.getCount(cpuStatCategory); } + + @Override + public void putCpuStat(CpuStat stat) { + storage.putChunk(converter.toChunk(stat)); + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/src/test/java/com/redhat/thermostat/common/appctx/ApplicationContextTest.java Wed Apr 04 17:03:52 2012 -0400 @@ -0,0 +1,125 @@ +/* + * Copyright 2012 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.common.appctx; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.redhat.thermostat.common.TimerFactory; +import com.redhat.thermostat.common.appctx.ApplicationContext; +import com.redhat.thermostat.common.dao.DAOFactory; + +public class ApplicationContextTest { + + @Before + public void setUp() { + ApplicationContext.reset(); + } + + @After + public void tearDown() { + ApplicationContext.reset(); + } + + @Test + public void verifyGetInstanceNotNull() { + ApplicationContext ctx = ApplicationContext.getInstance(); + assertNotNull(ctx); + } + + @Test + public void testDAOFactorySetGet() { + DAOFactory daoFactory = mock(DAOFactory.class); + ApplicationContext ctx = ApplicationContext.getInstance(); + ctx.setDAOFactory(daoFactory); + + DAOFactory actual1 = ctx.getDAOFactory(); + assertSame(daoFactory, actual1); + } + + @Test + public void verifyDAOFactoryIsNullWhenNotInitialized() { + ApplicationContext ctx = ApplicationContext.getInstance(); + + DAOFactory actual = ctx.getDAOFactory(); + assertNull(actual); + } + + @Test + public void verifyDAOFactoryStaysSame() { + DAOFactory daoFactory = mock(DAOFactory.class); + ApplicationContext ctx = ApplicationContext.getInstance(); + ctx.setDAOFactory(daoFactory); + + ApplicationContext ctx2 = ApplicationContext.getInstance(); + DAOFactory actual2 = ctx2.getDAOFactory(); + assertSame(daoFactory, actual2); + } + + @Test + public void testTimerFactorySetGet() { + TimerFactory timerFactory = mock(TimerFactory.class); + ApplicationContext ctx = ApplicationContext.getInstance(); + ctx.setTimerFactory(timerFactory); + + TimerFactory actual1 = ctx.getTimerFactory(); + assertSame(timerFactory, actual1); + } + + @Test + public void verifyTimerFactoryIsNullWhenNotInitialized() { + ApplicationContext ctx = ApplicationContext.getInstance(); + + TimerFactory actual = ctx.getTimerFactory(); + assertNull(actual); + } + + @Test + public void verifyTimerFactoryStaysSame() { + TimerFactory timerFactory = mock(TimerFactory.class); + ApplicationContext ctx = ApplicationContext.getInstance(); + ctx.setTimerFactory(timerFactory); + + ApplicationContext ctx2 = ApplicationContext.getInstance(); + TimerFactory actual2 = ctx2.getTimerFactory(); + assertSame(timerFactory, actual2); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/src/test/java/com/redhat/thermostat/common/appctx/ApplicationContextUtil.java Wed Apr 04 17:03:52 2012 -0400 @@ -0,0 +1,53 @@ +/* + * Copyright 2012 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.common.appctx; + +import com.redhat.thermostat.common.appctx.ApplicationContext; + +public class ApplicationContextUtil { + + /** + * This is here to allow tests to reset the ApplicationContext, while + * preventing real code the same (ApplicationContext.reset() is package private). + * + * It is vital that tests call this from their setUp() and tearDown() methods, + * to avoid leaking mocks and stuff from test to test. + */ + public static void resetApplicationContext() { + ApplicationContext.reset(); + } +}
--- a/common/src/test/java/com/redhat/thermostat/common/dao/CpuStatDAOTest.java Wed Apr 04 15:51:48 2012 -0400 +++ b/common/src/test/java/com/redhat/thermostat/common/dao/CpuStatDAOTest.java Wed Apr 04 17:03:52 2012 -0400 @@ -134,6 +134,14 @@ } @Test + public void testPutCpuStat() { + Storage storage = mock(Storage.class); + CpuStat stat = new CpuStat(1, 5.0, 15.0, 10.0); + CpuStatDAO dao = new CpuStatDAOImpl(storage); + dao.putCpuStat(stat); + } + + @Test public void testGetCount() { Storage storage = mock(Storage.class); when(storage.getCount(any(Category.class))).thenReturn(5L);