1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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  
17  package org.apache.jetspeed.util;
18  
19  import java.io.*;
20  
21  // Junit imports
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.apache.turbine.util.StringUtils;
26  import org.apache.tools.ant.util.FileUtils;
27  
28  // Jetspeed imports
29  import org.apache.jetspeed.test.JetspeedTestCase;
30  
31  /***
32   *
33   * @author <a href="epugh@upstate.com">Eric Pugh</a>
34   */
35  
36  public class TestOverwriteProperties extends JetspeedTestCase
37  {
38  
39      private static String SRC_JETSPEED_RESOURCES = "test/testdata/conf/SrcJetspeedResources.properties";
40      private static String JETSPEED_RESOURCES = "test/testdata/conf/JetspeedResources.properties";
41      private static String MERGE_JETSPEED_RESOURCES = "test/testdata/conf/MergeJetspeedResources.properties";
42      private static String MERGE_REMOVE_JETSPEED_RESOURCES = "test/testdata/conf/MergeRemoveJetspeedResources.properties";
43      private static String MERGE_ADD_JETSPEED_RESOURCES = "test/testdata/conf/MergeAddJetspeedResources.properties";
44      private static String RESULT_MERGE_JETSPEED_RESOURCES = "test/testdata/conf/ResultMergeJetspeedResources.properties";
45      private static String RESULT_MERGE_REMOVE_JETSPEED_RESOURCES = "test/testdata/conf/ResultMergeRemoveJetspeedResources.properties";
46      private static String RESULT_MERGE_ADD_JETSPEED_RESOURCES = "test/testdata/conf/ResultMergeAddJetspeedResources.properties";
47  
48      private static String INCLUDE_ROOT = "test/testdata/conf/";
49  
50      private File SRC_JETSPEED_RESOURCES_FILE = null;
51      private File JETSPEED_RESOURCES_FILE = null;
52      private File MERGE_JETSPEED_RESOURCES_FILE = null;
53      private File MERGE_REMOVE_JETSPEED_RESOURCES_FILE = null;
54      private File MERGE_ADD_JETSPEED_RESOURCES_FILE = null;
55      private File RESULT_MERGE_JETSPEED_RESOURCES_FILE = null;
56      private File RESULT_MERGE_REMOVE_JETSPEED_RESOURCES_FILE = null;
57      private File RESULT_MERGE_ADD_JETSPEED_RESOURCES_FILE = null;
58  
59      private File INCLUDE_ROOT_DIR = null;
60  
61      private FileUtils fileUtils = FileUtils.newFileUtils();
62  
63      /***
64       * Defines the testcase name for JUnit.
65       *
66       * @param name the testcase's name.
67       */
68      public TestOverwriteProperties( String name )
69      {
70          super( name );
71      }
72  
73      /***
74       * Start the tests.
75       *
76       * @param args the arguments. Not used
77       */
78      public static void main(String args[])
79      {
80          junit.awtui.TestRunner.main( new String[] { TestOverwriteProperties.class.getName()} );
81      }
82  
83      /***
84       * Creates the test suite.
85       *
86       * @return a test suite (<code>TestSuite</code>) that includes all methods
87       *         starting with "test"
88       */
89      public static Test suite()
90      {
91          // All methods starting with "test" will be executed in the test suite.
92          return new TestSuite( TestOverwriteProperties.class );
93      }
94  
95      public void setUp() throws Exception{
96  
97          SRC_JETSPEED_RESOURCES_FILE = new File(SRC_JETSPEED_RESOURCES);
98  
99          assertTrue("Check SRC_JETSPEED_RESOURCES_FILE exists:" + SRC_JETSPEED_RESOURCES_FILE,SRC_JETSPEED_RESOURCES_FILE.exists());
100 
101         JETSPEED_RESOURCES_FILE = new File(JETSPEED_RESOURCES);
102 
103         if (JETSPEED_RESOURCES_FILE.exists())
104         {
105             assertTrue("Check JETSPEED_RESOURCES_FILE exists, then delete it:" + JETSPEED_RESOURCES_FILE,JETSPEED_RESOURCES_FILE.delete());
106         }
107 
108         fileUtils.copyFile(SRC_JETSPEED_RESOURCES_FILE,JETSPEED_RESOURCES_FILE);
109 
110         assertTrue("Check JETSPEED_RESOURCES_FILE exists:" + JETSPEED_RESOURCES_FILE,JETSPEED_RESOURCES_FILE.exists());
111 
112         MERGE_JETSPEED_RESOURCES_FILE = new File(MERGE_JETSPEED_RESOURCES);
113 
114         assertTrue("Check MERGE_JETSPEED_RESOURCES_FILE exists:" + MERGE_JETSPEED_RESOURCES_FILE,MERGE_JETSPEED_RESOURCES_FILE.exists());
115 
116         MERGE_REMOVE_JETSPEED_RESOURCES_FILE = new File(MERGE_REMOVE_JETSPEED_RESOURCES);
117 
118         assertTrue("Check MERGE_REMOVE_JETSPEED_RESOURCES_FILE exists:" + MERGE_REMOVE_JETSPEED_RESOURCES_FILE,MERGE_REMOVE_JETSPEED_RESOURCES_FILE.exists());
119 
120         MERGE_ADD_JETSPEED_RESOURCES_FILE = new File(MERGE_ADD_JETSPEED_RESOURCES);
121 
122         assertTrue("Check MERGE_ADD_JETSPEED_RESOURCES_FILE exists:" + MERGE_ADD_JETSPEED_RESOURCES_FILE,MERGE_ADD_JETSPEED_RESOURCES_FILE.exists());
123 
124         RESULT_MERGE_JETSPEED_RESOURCES_FILE = new File(RESULT_MERGE_JETSPEED_RESOURCES);
125 
126         assertTrue("Check RESULT_MERGE_JETSPEED_RESOURCES_FILE exists:" + RESULT_MERGE_JETSPEED_RESOURCES_FILE,RESULT_MERGE_JETSPEED_RESOURCES_FILE.exists());
127 
128         RESULT_MERGE_REMOVE_JETSPEED_RESOURCES_FILE = new File(RESULT_MERGE_REMOVE_JETSPEED_RESOURCES);
129 
130         assertTrue("Check RESULT_MERGE_REMOVE_JETSPEED_RESOURCES_FILE exists:" + RESULT_MERGE_REMOVE_JETSPEED_RESOURCES_FILE,RESULT_MERGE_REMOVE_JETSPEED_RESOURCES_FILE.exists());
131 
132         RESULT_MERGE_ADD_JETSPEED_RESOURCES_FILE = new File(RESULT_MERGE_ADD_JETSPEED_RESOURCES);
133 
134         assertTrue("Check RESULT_MERGE_ADD_JETSPEED_RESOURCES_FILE exists:" + RESULT_MERGE_ADD_JETSPEED_RESOURCES_FILE,RESULT_MERGE_ADD_JETSPEED_RESOURCES_FILE.exists());
135 
136         INCLUDE_ROOT_DIR = new File(INCLUDE_ROOT);
137 
138         assertTrue("Check include root dir exists:" + INCLUDE_ROOT_DIR,INCLUDE_ROOT_DIR.exists());
139 
140         assertTrue(INCLUDE_ROOT_DIR.isDirectory());
141 
142     }
143 
144     /***
145      * Tests overwriting property
146      * @throws Exception
147      */
148     public void testOverwriteProperty() throws Exception
149     {
150 
151         try
152         {
153             String args[] = new String[]{JETSPEED_RESOURCES_FILE.toString(),MERGE_JETSPEED_RESOURCES.toString(),INCLUDE_ROOT.toString()};
154             OverwriteProperties.main(args);
155             assertTrue("Make sure our merge worked properly.",fileUtils.contentEquals(JETSPEED_RESOURCES_FILE,RESULT_MERGE_JETSPEED_RESOURCES_FILE));
156         }
157         catch (Exception e)
158         {
159             fail(StringUtils.stackTrace(e));
160         }
161     }
162 
163 
164 
165     /***
166      * Tests removing properties
167      * @throws Exception
168      */
169     public void testRemoveProperties() throws Exception
170     {
171         try
172         {
173             String args[] = new String[]{JETSPEED_RESOURCES_FILE.toString(),MERGE_REMOVE_JETSPEED_RESOURCES.toString(),INCLUDE_ROOT.toString()};
174             OverwriteProperties.main(args);
175             assertTrue("Make sure our merge worked properly.",fileUtils.contentEquals(JETSPEED_RESOURCES_FILE,RESULT_MERGE_REMOVE_JETSPEED_RESOURCES_FILE));
176         }
177         catch (Exception e)
178         {
179             fail(StringUtils.stackTrace(e));
180         }
181     }
182 
183     /***
184      * Tests adding properties
185      * @throws Exception
186      */
187     public void testAddProperties() throws Exception
188     {
189         try
190         {
191             String args[] = new String[]{JETSPEED_RESOURCES_FILE.toString(),MERGE_ADD_JETSPEED_RESOURCES.toString(),INCLUDE_ROOT.toString()};
192             OverwriteProperties.main(args);
193             assertTrue("Make sure our merge worked properly.",fileUtils.contentEquals(JETSPEED_RESOURCES_FILE,RESULT_MERGE_ADD_JETSPEED_RESOURCES_FILE));
194         }
195         catch (Exception e)
196         {
197             fail(StringUtils.stackTrace(e));
198         }
199     }
200 
201     public void tearDown()
202     {
203         if (JETSPEED_RESOURCES_FILE.exists())
204         {
205             //JETSPEED_RESOURCES_FILE.delete();
206         }
207     }
208 }