| 1 | /* | |
| 2 |  * Copyright (c) 2014 Maxim Yunusov | |
| 3 |  *    Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 |  *    you may not use this file except in compliance with the License. | |
| 5 |  *    You may obtain a copy of the License at | |
| 6 |  * | |
| 7 |  *        http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 |  * | |
| 9 |  *    Unless required by applicable law or agreed to in writing, software | |
| 10 |  *    distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 |  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 |  *    See the License for the specific language governing permissions and | |
| 13 |  *    limitations under the License. | |
| 14 |  */ | |
| 15 | ||
| 16 | package org.maxur.perfmodel.backend.service.impl; | |
| 17 | ||
| 18 | import org.jvnet.hk2.annotations.Service; | |
| 19 | import org.maxur.perfmodel.backend.service.Application; | |
| 20 | import org.slf4j.Logger; | |
| 21 | import org.slf4j.LoggerFactory; | |
| 22 | ||
| 23 | import javax.inject.Named; | |
| 24 | import javax.swing.*; | |
| 25 | import java.awt.*; | |
| 26 | import java.awt.image.BufferedImage; | |
| 27 | import java.io.IOException; | |
| 28 | import java.net.URI; | |
| 29 | import java.net.URL; | |
| 30 | import java.util.Optional; | |
| 31 | ||
| 32 | import static java.lang.String.format; | |
| 33 | import static java.util.Optional.empty; | |
| 34 | import static javax.swing.SwingUtilities.invokeLater; | |
| 35 | import static org.maxur.perfmodel.backend.utils.OsUtils.isWindows; | |
| 36 | ||
| 37 | /** | |
| 38 |  * This class represents Performance Model Calculator Application | |
| 39 |  * It's GUI Implementation with Tray Icon | |
| 40 |  * | |
| 41 |  * @author Maxim | |
| 42 |  * @version 1.0 | |
| 43 |  * @since <pre>24.10.2014</pre> | |
| 44 |  * <p> | |
| 45 |  * see http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/TrayIconDemoProject/src/misc/TrayIconDemo.java | |
| 46 |  */ | |
| 47 | @Service | |
| 48 | public class TrayIconApplication extends Application { | |
| 49 | ||
| 50 |     private static final Logger LOGGER = LoggerFactory.getLogger(TrayIconApplication.class); | |
| 51 | ||
| 52 |     public static final String IMG_FAVICON_PATH = "/img/favicon.png"; | |
| 53 | ||
| 54 |     private TrayIcon trayIcon; | |
| 55 | ||
| 56 |     @SuppressWarnings("unused") | |
| 57 |     @Named("webapp.folderName") | |
| 58 |     private String webappUrl; | |
| 59 | ||
| 60 |     public TrayIconApplication() { | |
| 61 |     } | |
| 62 | ||
| 63 |     @Override | |
| 64 |     public boolean isApplicable() { | |
| 65 |         //Check the SystemTray support | |
| 66 | 
1
1. isApplicable : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE | 
        return SystemTray.isSupported(); | 
| 67 |     } | |
| 68 | ||
| 69 |     @Override | |
| 70 |     protected void onInit() { | |
| 71 |         try { | |
| 72 | 
1
1. onInit : negated conditional → NO_COVERAGE | 
            final String className = isWindows() ? | 
| 73 |                     "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" : | |
| 74 |                     "javax.swing.plaf.metal.MetalLookAndFeel"; | |
| 75 | 
1
1. onInit : removed call to javax/swing/UIManager::setLookAndFeel → NO_COVERAGE | 
            UIManager.setLookAndFeel(className); | 
| 76 |         } catch (UnsupportedLookAndFeelException | IllegalAccessException | |
| 77 |                 | InstantiationException | ClassNotFoundException ex | |
| 78 |                 ) { | |
| 79 |             LOGGER.error("Tray application is not created", ex); | |
| 80 |         } | |
| 81 |         UIManager.put("swing.boldMetal", Boolean.FALSE); | |
| 82 |     } | |
| 83 | ||
| 84 |     @Override | |
| 85 |     protected void onStart() { | |
| 86 | 
1
1. onStart : removed call to javax/swing/SwingUtilities::invokeLater → NO_COVERAGE | 
        invokeLater(this::run); | 
| 87 |     } | |
| 88 | ||
| 89 |     private void run() { | |
| 90 |         final PopupMenu popup = new PopupMenu(); | |
| 91 |         final Optional<Image> image = createImage(IMG_FAVICON_PATH, "tray icon"); | |
| 92 |         final Image img; | |
| 93 | 
1
1. run : negated conditional → NO_COVERAGE | 
        if (image.isPresent()) { | 
| 94 |             img = image.get(); | |
| 95 |         } else { | |
| 96 |             img = createImageFrom(); | |
| 97 |             LOGGER.error(format("Resource '%s' is not found", IMG_FAVICON_PATH)); | |
| 98 |         } | |
| 99 |         trayIcon = new TrayIcon(img); | |
| 100 | 
1
1. run : removed call to java/awt/TrayIcon::setToolTip → NO_COVERAGE | 
        trayIcon.setToolTip("Performance Model Calculator"); | 
| 101 | 
1
1. run : removed call to java/awt/TrayIcon::setImageAutoSize → NO_COVERAGE | 
        trayIcon.setImageAutoSize(true); | 
| 102 | ||
| 103 |         MenuItem aboutItem = new MenuItem("About"); | |
| 104 |         MenuItem startClientItem = new MenuItem("Start Client"); | |
| 105 |         MenuItem manageServiceItem = new MenuItem("Stop Service"); | |
| 106 |         MenuItem exitItem = new MenuItem("Exit"); | |
| 107 | ||
| 108 |         //Add components to popup menu | |
| 109 |         popup.add(startClientItem); | |
| 110 |         popup.add(manageServiceItem); | |
| 111 |         popup.add(aboutItem); | |
| 112 | 
1
1. run : removed call to java/awt/PopupMenu::addSeparator → NO_COVERAGE | 
        popup.addSeparator(); | 
| 113 |         popup.add(exitItem); | |
| 114 | ||
| 115 | 
1
1. run : removed call to java/awt/TrayIcon::setPopupMenu → NO_COVERAGE | 
        trayIcon.setPopupMenu(popup); | 
| 116 | ||
| 117 |         try { | |
| 118 | 
1
1. run : removed call to java/awt/SystemTray::add → NO_COVERAGE | 
            SystemTray.getSystemTray().add(trayIcon); | 
| 119 |         } catch (AWTException e) { | |
| 120 |             LOGGER.debug("TrayIcon could not be added.", e); | |
| 121 |             LOGGER.error("TrayIcon could not be added."); | |
| 122 |             return; | |
| 123 |         } | |
| 124 | ||
| 125 | 
2
1. lambda$run$0 : removed call to org/maxur/perfmodel/backend/service/impl/TrayIconApplication::openBrowser → NO_COVERAGE 2. run : removed call to java/awt/TrayIcon::addActionListener → NO_COVERAGE  | 
        trayIcon.addActionListener(e -> openBrowser()); | 
| 126 | ||
| 127 | 
2
1. lambda$run$1 : removed call to org/maxur/perfmodel/backend/service/impl/TrayIconApplication::openBrowser → NO_COVERAGE 2. run : removed call to java/awt/MenuItem::addActionListener → NO_COVERAGE  | 
        startClientItem.addActionListener(e -> openBrowser()); | 
| 128 | ||
| 129 | 
1
1. run : removed call to java/awt/MenuItem::addActionListener → NO_COVERAGE | 
        manageServiceItem.addActionListener(e -> { | 
| 130 | 
1
1. lambda$run$2 : negated conditional → NO_COVERAGE | 
            if (webServer().isStarted()) { | 
| 131 | 
1
1. lambda$run$2 : removed call to org/maxur/perfmodel/backend/service/WebServer::stop → NO_COVERAGE | 
                webServer().stop(); | 
| 132 |             } else { | |
| 133 | 
1
1. lambda$run$2 : removed call to org/maxur/perfmodel/backend/service/WebServer::restart → NO_COVERAGE | 
                webServer().restart(); | 
| 134 |             } | |
| 135 | 
1
1. lambda$run$2 : negated conditional → NO_COVERAGE | 
            final String label = webServer().isStarted() ? "Stop Service" : "Start Service"; | 
| 136 | 
1
1. lambda$run$2 : removed call to java/awt/MenuItem::setLabel → NO_COVERAGE | 
            manageServiceItem.setLabel(label); | 
| 137 | 
1
1. lambda$run$2 : negated conditional → NO_COVERAGE | 
            final String message = webServer().isStarted() ? "Web Service was started" : "Web Service was stopped"; | 
| 138 | 
1
1. lambda$run$2 : removed call to java/awt/TrayIcon::displayMessage → NO_COVERAGE | 
            trayIcon.displayMessage("Performance Model Calculator", | 
| 139 |                     message, | |
| 140 |                     TrayIcon.MessageType.INFO); | |
| 141 | 
1
1. lambda$run$2 : removed call to java/awt/MenuItem::setEnabled → NO_COVERAGE | 
            startClientItem.setEnabled(webServer().isStarted()); | 
| 142 |         }); | |
| 143 | ||
| 144 | 
2
1. lambda$run$3 : removed call to javax/swing/JOptionPane::showMessageDialog → NO_COVERAGE 2. run : removed call to java/awt/MenuItem::addActionListener → NO_COVERAGE  | 
        aboutItem.addActionListener(e -> JOptionPane.showMessageDialog(null, | 
| 145 |                 "Performance Model Calculator. Version: " + this.getClass().getPackage().getImplementationVersion())); | |
| 146 | ||
| 147 | ||
| 148 | 
1
1. run : removed call to java/awt/MenuItem::addActionListener → NO_COVERAGE | 
        exitItem.addActionListener(e -> { | 
| 149 | 
1
1. lambda$run$4 : removed call to org/maxur/perfmodel/backend/service/impl/TrayIconApplication::stop → NO_COVERAGE | 
            stop(); | 
| 150 |         }); | |
| 151 |     } | |
| 152 | ||
| 153 |     @Override | |
| 154 |     protected void onStop() { | |
| 155 | 
1
1. onStop : removed call to java/awt/SystemTray::remove → NO_COVERAGE | 
        SystemTray.getSystemTray().remove(trayIcon); | 
| 156 |     } | |
| 157 | ||
| 158 |     private static Image createImageFrom() { | |
| 159 |         BufferedImage bufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); | |
| 160 |         Graphics graphics = bufferedImage.getGraphics(); | |
| 161 | 
1
1. createImageFrom : removed call to java/awt/Graphics::setColor → NO_COVERAGE | 
        graphics.setColor(Color.RED); | 
| 162 | 
1
1. createImageFrom : removed call to java/awt/Graphics::fillRect → NO_COVERAGE | 
        graphics.fillRect(0, 0, 100, 100); | 
| 163 | 
1
1. createImageFrom : mutated return of Object value for org/maxur/perfmodel/backend/service/impl/TrayIconApplication::createImageFrom to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE | 
        return bufferedImage; | 
| 164 |     } | |
| 165 | ||
| 166 |     //Obtain the image URL | |
| 167 |     private static Optional<Image> createImage(String path, String description) { | |
| 168 |         final URL imageURL = TrayIconApplication.class.getResource(path); | |
| 169 | 
2
1. createImage : negated conditional → NO_COVERAGE 2. createImage : mutated return of Object value for org/maxur/perfmodel/backend/service/impl/TrayIconApplication::createImage to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE  | 
        return imageURL == null ? empty() : Optional.of((new ImageIcon(imageURL, description)).getImage()); | 
| 170 |     } | |
| 171 | ||
| 172 |     private void openBrowser() { | |
| 173 |         URI uri = URI.create(webappUrl); | |
| 174 | 
1
1. openBrowser : negated conditional → NO_COVERAGE | 
        Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null; | 
| 175 | 
2
1. openBrowser : negated conditional → NO_COVERAGE 2. openBrowser : negated conditional → NO_COVERAGE  | 
        if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) { | 
| 176 |             try { | |
| 177 | 
1
1. openBrowser : removed call to java/awt/Desktop::browse → NO_COVERAGE | 
                desktop.browse(uri); | 
| 178 |             } catch (IOException e) { | |
| 179 |                 LOGGER.error("Cannot open browser", e); | |
| 180 |             } | |
| 181 |         } | |
| 182 |     } | |
| 183 | ||
| 184 | ||
| 185 | } | |
| 186 | ||
| 187 | ||
Mutations | ||
| 66 | 
 
 1.1  | 
|
| 72 | 
 
 1.1  | 
|
| 75 | 
 
 1.1  | 
|
| 86 | 
 
 1.1  | 
|
| 93 | 
 
 1.1  | 
|
| 100 | 
 
 1.1  | 
|
| 101 | 
 
 1.1  | 
|
| 112 | 
 
 1.1  | 
|
| 115 | 
 
 1.1  | 
|
| 118 | 
 
 1.1  | 
|
| 125 | 
 
 1.1 2.2  | 
|
| 127 | 
 
 1.1 2.2  | 
|
| 129 | 
 
 1.1  | 
|
| 130 | 
 
 1.1  | 
|
| 131 | 
 
 1.1  | 
|
| 133 | 
 
 1.1  | 
|
| 135 | 
 
 1.1  | 
|
| 136 | 
 
 1.1  | 
|
| 137 | 
 
 1.1  | 
|
| 138 | 
 
 1.1  | 
|
| 141 | 
 
 1.1  | 
|
| 144 | 
 
 1.1 2.2  | 
|
| 148 | 
 
 1.1  | 
|
| 149 | 
 
 1.1  | 
|
| 155 | 
 
 1.1  | 
|
| 161 | 
 
 1.1  | 
|
| 162 | 
 
 1.1  | 
|
| 163 | 
 
 1.1  | 
|
| 169 | 
 
 1.1 2.2  | 
|
| 174 | 
 
 1.1  | 
|
| 175 | 
 
 1.1 2.2  | 
|
| 177 | 
 
 1.1  |