1   /*
2    * $Id: BexeePropertiesTest.java,v 1.1 2004/12/15 14:18:21 patforna Exp $
3    *
4    * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
5    * Berne University of Applied Sciences
6    * School of Engineering and Information Technology
7    * All rights reserved.
8    */
9   package bexee.util;
10  
11  import java.io.File;
12  import java.util.Properties;
13  
14  import junit.framework.TestCase;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  /***
20   * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:21 $
21   * @author Patric Fornasier
22   * 
23   * FIXME JVM crash cause
24   */
25  public class BexeePropertiesTest extends TestCase {
26  
27      private static Log log = LogFactory.getLog(BexeePropertiesTest.class);
28  
29      private static final String SYS_PROP_KEY = "sys.prop";
30  
31      private static final String SYS_PROP_VAL = "piff";
32  
33      private static final String PROP_KEY_1 = "bexee.prop.1";
34  
35      private static final String PROP_VAL_1 = "paff";
36  
37      private static final String PROP_KEY_2 = SYS_PROP_KEY;
38  
39      private static final String PROP_VAL_2 = "peff";
40  
41      private static final String PROP_KEY_3 = "bexee.prop.3";
42  
43      private static final String PROP_VAL_3 = "puff";
44  
45      private File file;
46  
47      protected void setUp() throws Exception {
48          super.setUp();
49      }
50  
51      protected void tearDown() throws Exception {
52          super.tearDown();
53  
54          // make sure to remove any system properties we introduced
55          System.getProperties().remove(SYS_PROP_KEY);
56      }
57  
58      /***
59       * Test if the properties were read from the bexee.properties file on the
60       * classpath.
61       */
62      public void testGetPropertyString() {
63          assertEquals(PROP_VAL_1, BexeeProperties.getProperty(PROP_KEY_1));
64          assertEquals(PROP_VAL_2, BexeeProperties.getProperty(PROP_KEY_2));
65      }
66  
67      /***
68       * Test if system properties have precedence over application properties.
69       */
70      public void testSystemProperty() {
71  
72          /*
73           * PROP_KEY_2 and SYS_PROP_KEY have the same values, let's see which one
74           * has precedence if we set SYS_PROP_KEY as a system property.
75           */
76          assertEquals(PROP_VAL_2, BexeeProperties.getProperty(PROP_KEY_2));
77  
78          // make sure that user.name system property is set
79          System.setProperty(SYS_PROP_KEY, SYS_PROP_VAL);
80  
81          // let's see if the system property we set has precedence
82          assertEquals(SYS_PROP_VAL, BexeeProperties.getProperty(PROP_KEY_2));
83      }
84  
85      /***
86       * Test if we are able to retrieve a default value.
87       */
88      public void testGetPropertyStringString() {
89          assertNull(BexeeProperties.getProperty(PROP_KEY_3));
90          assertEquals(PROP_VAL_3, BexeeProperties.getProperty(PROP_KEY_3,
91                  PROP_VAL_3));
92      }
93  
94      /***
95       * Test if all the properties are there.
96       */
97      public void testGetProperties() {
98  
99          Properties sysProps = System.getProperties();
100         Properties bexeeProps = BexeeProperties.getProperties();
101 
102         // there should be n system properties plus 2 from bexee.properties
103         assertEquals(sysProps.size() + 2, bexeeProps.size());
104 
105     }
106 
107     /***
108      * Test if we are able to set new properties.
109      */
110     public void testSetProperty() {
111         assertNull(BexeeProperties.getProperty(PROP_KEY_3));
112         BexeeProperties.setProperty(PROP_KEY_3, PROP_VAL_3);
113         assertEquals(PROP_VAL_3, BexeeProperties.getProperty(PROP_KEY_3));
114     }
115 
116 }