Mercurial > hg > release > thermostat-1.6
changeset 1740:4ef35bbc19f7
Make VM identifier distinguishable
PR1074
PR2601
Combined two patches contributed originally by Antonio Cesarano, then backported.
http://icedtea.classpath.org/hg/thermostat/rev/6883c36648ec
http://icedtea.classpath.org/hg/thermostat/rev/c6d64d516345
Original review at: http://icedtea.classpath.org/pipermail/thermostat/2015-July/014724.html
Contributed-by: acesaran
Reviewed-by: jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-August/015649.html
author | Andrew Azores <aazores@redhat.com> |
---|---|
date | Tue, 01 Sep 2015 10:22:40 -0400 |
parents | d4e7ccafce3f |
children | 2e98d55e5498 |
files | client/living-vm-filter/swing/src/main/java/com/redhat/thermostat/client/filter/host/swing/ThermostatVmMainLabelDecorator.java client/living-vm-filter/swing/src/main/java/com/redhat/thermostat/client/filter/vm/swing/VMFilterActivator.java client/living-vm-filter/swing/src/test/java/com/redhat/thermostat/client/filter/host/swing/ThermostatVmMainLabelDecoratorTest.java |
diffstat | 3 files changed, 207 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/living-vm-filter/swing/src/main/java/com/redhat/thermostat/client/filter/host/swing/ThermostatVmMainLabelDecorator.java Tue Sep 01 10:22:40 2015 -0400 @@ -0,0 +1,78 @@ +/* + * Copyright 2012-2015 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.filter.host.swing; + +import com.redhat.thermostat.client.ui.ReferenceFieldLabelDecorator; +import com.redhat.thermostat.storage.core.Ref; +import com.redhat.thermostat.storage.core.VmRef; +import com.redhat.thermostat.storage.dao.VmInfoDAO; +import com.redhat.thermostat.storage.model.VmInfo; + +public class ThermostatVmMainLabelDecorator implements ReferenceFieldLabelDecorator { + + private VmInfoDAO dao; + + public ThermostatVmMainLabelDecorator(VmInfoDAO dao) { + this.dao = dao; + } + + @Override + public String getLabel(String originalLabel, Ref reference) { + + if (!(reference instanceof VmRef) || + !reference.getName().contains("thermostat")) { + return originalLabel; + } + return processLabel(dao.getVmInfo((VmRef) reference)); + } + + @Override + public int getOrderValue() { + return ORDER_FIRST + 1; + } + + /** + * This method returns a short version for the VmInfo object command line. + * @param info the vm info object from which take information. + * @return the resulting string + */ + private String processLabel(VmInfo info) { + String[] s = info.getJavaCommandLine().split("\\."); //escaped dot + return s[s.length - 1]; + } +} +
--- a/client/living-vm-filter/swing/src/main/java/com/redhat/thermostat/client/filter/vm/swing/VMFilterActivator.java Tue Sep 01 13:27:40 2015 +0200 +++ b/client/living-vm-filter/swing/src/main/java/com/redhat/thermostat/client/filter/vm/swing/VMFilterActivator.java Tue Sep 01 10:22:40 2015 -0400 @@ -52,6 +52,7 @@ import com.redhat.thermostat.client.filter.host.swing.HostIconDecorator; import com.redhat.thermostat.client.filter.host.swing.HostInfoLabelDecorator; import com.redhat.thermostat.client.filter.host.swing.HostVmMainLabelDecorator; +import com.redhat.thermostat.client.filter.host.swing.ThermostatVmMainLabelDecorator; import com.redhat.thermostat.client.swing.ReferenceFieldDecoratorLayout; import com.redhat.thermostat.client.swing.UIDefaults; import com.redhat.thermostat.client.ui.ReferenceFieldIconDecorator; @@ -163,6 +164,17 @@ registration = context.registerService(ReferenceFieldLabelDecorator.class.getName(), mainDecorator, decoratorProperties); + + ThermostatVmMainLabelDecorator thermostatDecorator = new ThermostatVmMainLabelDecorator(vmDao); + decoratorProperties = new Hashtable<>(); + decoratorProperties.put(ReferenceFieldLabelDecorator.ID, + ReferenceFieldDecoratorLayout.LABEL_MAIN.name()); + + registration = context.registerService(ReferenceFieldLabelDecorator.class.getName(), + thermostatDecorator, decoratorProperties); + + registeredServices.add(registration); + } }); tracker.open();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/living-vm-filter/swing/src/test/java/com/redhat/thermostat/client/filter/host/swing/ThermostatVmMainLabelDecoratorTest.java Tue Sep 01 10:22:40 2015 -0400 @@ -0,0 +1,117 @@ +/* + * Copyright 2012-2015 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.filter.host.swing; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.junit.Before; +import org.junit.Test; + +import com.redhat.thermostat.storage.core.HostRef; +import com.redhat.thermostat.storage.core.Ref; +import com.redhat.thermostat.storage.core.VmRef; +import com.redhat.thermostat.storage.dao.VmInfoDAO; +import com.redhat.thermostat.storage.model.VmInfo; + +public class ThermostatVmMainLabelDecoratorTest { + + private ThermostatVmMainLabelDecorator decorator; + private VmInfoDAO dao; + private VmRef vmRef; + private VmInfo info; + + + @Before + public void setUp() { + dao = mock(VmInfoDAO.class); + vmRef = mock(VmRef.class); + info = mock(VmInfo.class); + } + + /** + * Testing using a Thermostat vm. The getLabel method must return + * the shorter version of the vm's command. + */ + @Test + public void getLabelTest1() { + + when(dao.getVmInfo(vmRef)).thenReturn(info); + when(vmRef.getName()).thenReturn("com.redhat.thermostat.main.Thermostat"); + when(info.getMainClass()).thenReturn("com.redhat.thermostat.main.Thermostat"); + when(info.getJavaCommandLine()).thenReturn("com.redhat.thermostat.main.Thermostat service"); + + decorator = new ThermostatVmMainLabelDecorator(dao); + String result = decorator.getLabel("originalLabel", vmRef); + assertTrue(result.equals("Thermostat service")); + } + + /** + * Testing using a non Thermostat vm. The getLabel method must return + * the vm's main class. + */ + @Test + public void getLabelTest2() { + + when(dao.getVmInfo(vmRef)).thenReturn(info); + when(vmRef.getName()).thenReturn("/opt/eclipse//plugin/org.eclipse.equinox.laucher.jar"); + when(info.getMainClass()).thenReturn("/opt/eclipse//plugin/org.eclipse.equinox.laucher.jar"); + when(info.getJavaCommandLine()).thenReturn("-os linux"); + + decorator = new ThermostatVmMainLabelDecorator(dao); + String result = decorator.getLabel("originalLabel", vmRef); + assertTrue(result.equals("originalLabel")); + } + + + /** + * Testing using a non VmRef object. The getLabel method must return + * the host's reference name. + */ + @Test + public void getLabelTest3() { + + Ref hostRef = mock(HostRef.class); + when(hostRef.getName()).thenReturn("localhost.localdomain"); + + decorator = new ThermostatVmMainLabelDecorator(dao); + String result = decorator.getLabel("localhost.localdomain", hostRef); + assertTrue(result.equals("localhost.localdomain")); + } + +}