View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.testtools.common;
17  
18  import org.junit.Assert;
19  import org.junit.BeforeClass;
20  import org.junit.Test;
21  
22  import java.io.IOException;
23  import java.util.Iterator;
24  import java.util.Properties;
25  
26  /**
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   */
29  public class PropertiesUtilsTest {
30  
31      private static final String JIRA_AWARE_CONTAINS_FAILURES_PROPERTIES = "JiraAwareContainsFailures.properties";
32  
33      private static final String PATH_TO_TEST_PROPERTIES_FILE = "org/kuali/rice/testtools/common/PropertiesUtilsTest.properties";
34  
35      private static final String PROPERTIES_DIR_INTELLIJ = "rice-tools-test/src/main/resources/";
36  
37      private static final String PROPERTIES_DIR_MAVEN = "src/main/resources/";
38  
39      private static final String PROPERTY_DOESNT_EXIST_IN_FILE = "PROPERTY_DOESNT_EXIST_IN_FILE";
40  
41      private static final String REMOTE_DRIVER_SAUCELABS_PROPERTY = "remote.driver.saucelabs";
42  
43      protected static PropertiesUtils propUtils;
44  
45      @BeforeClass
46      public static void setUp() {
47          System.setProperty(REMOTE_DRIVER_SAUCELABS_PROPERTY, "true");
48          System.setProperty(PROPERTY_DOESNT_EXIST_IN_FILE, "true");
49          propUtils = new PropertiesUtils();
50  //        printSystemProperties();
51      }
52  
53      private void assertPropertiesKeysCountNotZero(Properties props) {
54          Assert.assertTrue(props.keySet().size() > 0);
55      }
56  
57      private void assertProperityDoesntExistInFilePropertyTrue(Properties props) {
58          Assert.assertTrue(props.getProperty(REMOTE_DRIVER_SAUCELABS_PROPERTY).equals("true"));
59      }
60  
61      private void assertRemoteDriverSaucelabsPropertyTrue(Properties props) {
62          Assert.assertTrue(props.getProperty(REMOTE_DRIVER_SAUCELABS_PROPERTY).equals("true"));
63      }
64  
65      private static void printSystemProperties() {
66          Iterator iter = System.getProperties().stringPropertyNames().iterator();
67          while (iter.hasNext()) {
68              String key = (String) iter.next();
69              System.out.println(key + " = " + System.getProperty(key));
70          }
71      }
72  
73      @Test
74      public void testLoadPropertiesResource() throws IOException {
75          Properties props = propUtils.loadProperties(null, JIRA_AWARE_CONTAINS_FAILURES_PROPERTIES);
76          assertPropertiesKeysCountNotZero(props);
77      }
78  
79      @Test
80      public void testLoadPropertiesFile() throws IOException {
81          Properties props = propUtils.loadProperties(PROPERTIES_DIR_INTELLIJ + JIRA_AWARE_CONTAINS_FAILURES_PROPERTIES, null); // intellij
82          if (props == null) { // mvn
83              props = propUtils.loadProperties(PROPERTIES_DIR_MAVEN + JIRA_AWARE_CONTAINS_FAILURES_PROPERTIES, null);
84          }
85          Assert.assertNotNull(props);
86          assertPropertiesKeysCountNotZero(props);
87      }
88  
89      @Test
90      public void testLoadProperties() throws IOException {
91          Properties props = propUtils.loadProperties(null, PATH_TO_TEST_PROPERTIES_FILE);
92          assertPropertiesKeysCountNotZero(props);
93          Assert.assertTrue(props.keySet().contains(REMOTE_DRIVER_SAUCELABS_PROPERTY));
94          Assert.assertTrue(props.getProperty(REMOTE_DRIVER_SAUCELABS_PROPERTY).equals(""));
95      }
96  
97      @Test
98      public void testSystemPropertiesOverrides() throws IOException {
99          assertProperityDoesntExistInFilePropertyTrue(System.getProperties());
100         assertRemoteDriverSaucelabsPropertyTrue(System.getProperties());
101         Properties props = propUtils.loadPropertiesWithSystemOverrides(PATH_TO_TEST_PROPERTIES_FILE);
102         assertPropertiesKeysCountNotZero(props);
103         assertRemoteDriverSaucelabsPropertyTrue(props);
104         // PROPERTY_DOESNT_EXIST_IN_FILE but was set in System should not be in props
105         Assert.assertFalse(props.containsKey(PROPERTY_DOESNT_EXIST_IN_FILE));
106     }
107 
108     @Test
109     public void testSystemPropertiesAndOverrides() throws IOException {
110         assertProperityDoesntExistInFilePropertyTrue(System.getProperties());
111         assertRemoteDriverSaucelabsPropertyTrue(System.getProperties());
112         Properties props = propUtils.loadPropertiesWithSystemAndOverrides(PATH_TO_TEST_PROPERTIES_FILE);
113         assertPropertiesKeysCountNotZero(props);
114         assertRemoteDriverSaucelabsPropertyTrue(props);
115         // PROPERTY_DOESNT_EXIST_IN_FILE but was set in System should be in props
116         assertProperityDoesntExistInFilePropertyTrue(props);
117     }
118 
119     @Test
120     public void testLoadPropertiesWithSystemAndOverridesIntoSystem() throws IOException {
121         Assert.assertNull(System.getProperty("saucelabs.browser"));
122         assertRemoteDriverSaucelabsPropertyTrue(System.getProperties());
123         Properties props = propUtils.loadPropertiesWithSystemAndOverridesIntoSystem(PATH_TO_TEST_PROPERTIES_FILE);
124         //saucelabs.browser=ff defined in PATH_TO_TEST_PROPERTIES_FILE should now be in System properties
125         Assert.assertTrue(System.getProperty("saucelabs.browser").equals("ff"));
126     }
127 }