1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.rcp.application.uitest;
  34 
  35 import java.io.File;
  36 import java.io.FileInputStream;
  37 import java.io.IOException;
  38 import java.io.InputStream;
  39 import java.text.MessageFormat;
  40 import java.util.ArrayList;
  41 import java.util.List;
  42 import java.util.Properties;
  43 
  44 import org.junit.Assert;
  45 import org.junit.Assume;
  46 import org.junit.Test;
  47 import org.openjdk.jmc.test.jemmy.MCJemmyTestBase;
  48 import org.openjdk.jmc.test.jemmy.misc.base.wrappers.MCJemmyBase;
  49 import org.openjdk.jmc.test.jemmy.misc.wrappers.MCButton;
  50 import org.openjdk.jmc.test.jemmy.misc.wrappers.MCDialog;
  51 import org.openjdk.jmc.test.jemmy.misc.wrappers.MCMenu;
  52 import org.openjdk.jmc.test.jemmy.misc.wrappers.MCTable;
  53 
  54 /**
  55  * Class testing update site related components
  56  */
  57 public class UpdateSiteTest extends MCJemmyTestBase {
  58         private static final String UPDATESITE_PROP_PREFIX = "updatesite.";
  59         private static final String JMC_VERSION = System.getProperty("jmc.test.jmc.version", "7.0.0");
  60         private static final String KEY_UPDATE_PROPERTIES_PATH = "org.openjdk.jmc.updatesites.properties";
  61 
  62         /**
  63          * This test verifies that, when the property "org.openjdk.jmc.updatesites.properties" is set to
  64          * something we're supposed to add the update sites from the properties file to the ones
  65          * available for updates. Note that this only tests that, at least, the sites in the properties
  66          * file or the predefined external sites (specified in the default properties file shipped with
  67          * JMC) are present.
  68          */
  69         @Test
  70         public void testUpdateSiteProperty() {
  71                 Assume.assumeTrue("Update site properties file property not set",
  72                                 System.getProperty(KEY_UPDATE_PROPERTIES_PATH) != null);
  73                 String updateSitePropertiesPath = System.getProperty(KEY_UPDATE_PROPERTIES_PATH);
  74                 if (updateSitePropertiesPath != null) {
  75                         List<String> propsUpdateSites = getUpdateSitesFromPropsFile(updateSitePropertiesPath);
  76                         Assert.assertTrue(
  77                                         "Update site properties file at " + updateSitePropertiesPath + " is empty or incorrectly formatted",
  78                                         propsUpdateSites.size() > 0);
  79                         List<String> updateSites = getUpdateSitesFromPrefs();
  80                         for (String site : propsUpdateSites) {
  81                                 Assert.assertTrue(
  82                                                 "Update site \"" + site + "\" is missing from the list of update sites. Expected sites: "
  83                                                                 + propsUpdateSites + ", Found sites (in preferences): " + updateSites,
  84                                                 updateSites.contains(site));
  85                         }
  86                 }
  87         }
  88 
  89         private static List<String> getUpdateSitesFromPrefs() {
  90                 MCDialog preferences = MCMenu.openPreferencesDialog();
  91                 preferences.selectTreeItem("Install/Update", "Available Software Sites");
  92                 MCTable siteTable = preferences.getFirstTable();
  93                 List<String> updateSiteURLs = siteTable.getColumnItemTexts("Location");
  94                 // Saving picture of preferences page in case we find no update sites (for easier debugging)
  95                 if (updateSiteURLs.size() == 0) {
  96                         MCJemmyBase.saveMcImage("UpdateSitePrefs");
  97                 }
  98                 preferences.clickButton(MCButton.Labels.APPLY_AND_CLOSE);
  99                 return updateSiteURLs;
 100         }
 101 
 102         private static List<String> getUpdateSitesFromPropsFile(String propsFilePath) {
 103                 Properties props = readProperties(propsFilePath);
 104                 List<String> updateSites = new ArrayList<>();
 105                 int i = 0;
 106                 String url;
 107                 do {
 108                         url = props.getProperty(UPDATESITE_PROP_PREFIX + i++);
 109                         if (url != null) {
 110                                 updateSites.add((MessageFormat.format(url, JMC_VERSION)));
 111                         }
 112                 } while (url != null);
 113                 return updateSites;
 114         }
 115 
 116         private static Properties readProperties(String propertiesPath) {
 117                 if (propertiesPath != null) {
 118                         File propertiesFile = new File(propertiesPath);
 119                         if (propertiesFile.isFile() && propertiesFile.canRead()) {
 120                                 try (InputStream is = new FileInputStream(propertiesFile)) {
 121                                         Properties props = new Properties();
 122                                         props.load(is);
 123                                         return props;
 124                                 } catch (IOException e) {
 125                                         Assert.fail("Could not load update sites properties file: " + e);
 126                                 }
 127                         } else {
 128                                 Assert.fail(
 129                                                 "Update sites properties file " + propertiesPath + " isn't readable or doesn't seem to exist");
 130                         }
 131                 }
 132                 return null;
 133         }
 134 }