1   package com.atlassian.config;
2   
3   import junit.framework.Assert;
4   import junit.framework.TestCase;
5   
6   import java.io.File;
7   import java.util.ArrayList;
8   import java.util.List;
9   import java.util.Map;
10  
11  public class ApplicationConfigurationTestCase extends TestCase
12  {
13      protected ApplicationConfiguration cfg;
14  
15      protected void setUp() throws Exception
16      {
17          super.setUp();
18          cfg = new ApplicationConfig();
19      }
20  
21      public void testApplicationConfigurationProperties() throws Exception
22      {
23  
24          cfg.setProperty("testObject", new ArrayList());
25          cfg.setProperty("testInteger", 4);
26          cfg.setProperty("testBoolean", true);
27  
28          assertTrue(cfg.getProperty("testObject") instanceof List);
29          Assert.assertEquals(4, cfg.getIntegerProperty("testInteger"));
30          Assert.assertEquals(true, cfg.getBooleanProperty("testBoolean"));
31          assertNull(cfg.getProperty("doesNotExist"));
32          Assert.assertEquals(Integer.MIN_VALUE, cfg.getIntegerProperty("doesNotExist"));
33          Assert.assertEquals(false, cfg.getBooleanProperty("doesNotExist"));
34  
35          assertTrue(!cfg.isSetupComplete());
36          cfg.setSetupComplete(true);
37          assertTrue(cfg.isSetupComplete());
38  
39          cfg.setProperty("prefix.property1", "value1");
40          cfg.setProperty("prefix.property2", "value2");
41          cfg.setProperty("prefix.property3", "value3");
42  
43          Assert.assertEquals(6, cfg.getProperties().size());
44  
45          Map prefixProps = cfg.getPropertiesWithPrefix("prefix.");
46          assertEquals(3, prefixProps.size());
47          assertEquals("value2", prefixProps.get("prefix.property2"));
48  
49          cfg.setProperty("prefix.property2", null);
50          assertNull(cfg.getProperty("prefix.property2"));
51  
52      }
53  
54      public void testApplicationConfigurationVersion() throws Exception
55      {
56          cfg.setBuildNumber("62");
57          cfg.setMajorVersion(1);
58          cfg.setMinorVersion(6);
59  
60          Assert.assertEquals("1.6 build: 62", cfg.getApplicationVersion());
61      }
62  
63      public void testApplicationConfigurationHome() throws Exception
64      {
65          cfg.setApplicationHome(".");
66  
67          Assert.assertEquals(new File(".").getCanonicalPath(), cfg.getApplicationHome());
68          assertTrue(cfg.isApplicationHomeValid());
69  
70          //Should create path automatically
71          String tempDir = System.getProperty("java.io.tmpdir");
72          File f = new File(tempDir + "_does_not_exist_");
73          if (f.exists()) f.delete();
74  
75          cfg.setApplicationHome(tempDir + "/_does_not_exist_");
76          assertTrue(cfg.isApplicationHomeValid());
77  
78          try
79          {
80              cfg.setApplicationHome(getInvalidHomeForOperatingSystem());
81              fail("should throw exception if home dir cannot be created");
82          }
83          catch (ConfigurationException e)
84          {
85              //expected
86          }
87          assertTrue(!cfg.isApplicationHomeValid());
88      }
89  
90      private String getInvalidHomeForOperatingSystem()
91      {
92          // On OS X, we need to try to create the home in /private, because / is writable by default for admin
93          // users.
94          if ("Mac OS X".equals(System.getProperty("os.name")))
95              return "/private/bad-confluence-home";
96  
97          // This should fail on Linux and Windows.
98          return "/|does|not|exist/cheese";
99      }
100 }