Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author: daigle $'
8
 *     '$Date: 2008-07-18 10:06:00 -0700 (Fri, 18 Jul 2008) $'
9
 * '$Revision: 4146 $'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

    
26
package edu.ucsb.nceas.metacattest;
27

    
28
import java.util.Map;
29
import java.util.Random;
30
import java.util.Vector;
31

    
32
import edu.ucsb.nceas.MCTestCase;
33

    
34
import edu.ucsb.nceas.metacat.properties.PropertyService;
35
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
36
import junit.framework.Test;
37
import junit.framework.TestSuite;
38

    
39
/**
40
 * A JUnit test for testing Access Control in Metacat
41
 */
42
public class PropertyServiceTest extends MCTestCase {
43

    
44
    /**
45
     * Constructor to build the test
46
     *
47
     * @param name the name of the test method
48
     */
49
    public PropertyServiceTest(String name) {
50
        super(name);
51
    }
52

    
53
    /**
54
     * Establish a testing framework by initializing appropriate objects
55
     */
56
    public void setUp() {
57

    
58
    }
59

    
60
    /**
61
     * Release any objects after tests are complete
62
     */
63
    public void tearDown() {
64
    }
65

    
66
    /**
67
     * Create a suite of tests to be run together
68
     */
69
    public static Test suite() {
70
        TestSuite suite = new TestSuite();
71
        suite.addTest(new PropertyServiceTest("initialize"));
72
        // Test basic functions
73
        suite.addTest(new PropertyServiceTest("mainPropertiesTest"));
74

    
75
        return suite;
76
    }
77

    
78
    /**
79
     * Run an initial test that always passes to check that the test
80
     * harness is working.
81
     */
82
    public void initialize() {
83
        assertTrue(1 == 1);
84
    }
85
    
86
    
87
    /**
88
     * Tests reading and writing from properties using the PropertyService class
89
     *1. read a single property from the properties file that should exist
90
     *   Read a test property from the PropertyService.  The value should be read 
91
     *   successfully.  If PropertyNotFoundException is thrown, the test case fails.
92
     *   Also, if the value returned is null, the test case fails.  Strictly speaking,
93
     *   it is not a failure for getProperty to return null, but we know that 
94
     *   'test.metacatURL' should have a value.
95
     *2. read a single property from the main properties that shouldn't exist
96
     *   Expect a PropertyNotFoundException here.  If the exception is not thrown,
97
     *   the test case fails.
98
     *3. read group property names from the main properties
99
     *   read an existing group of property names.  If the result set is zero
100
     *   length, the test case fails.
101
     *4. read group properties from the main properties
102
     *   Get the property values for the group names retrieved in test case 3.  If
103
     *   the properties map retrieved is empty, the test case fails.
104
     *5. write property to main properties
105
     *   Write a property that already exists to the metacat.properties file.  The 
106
     *   value is randomly generated.  The value is then retrieved from the 
107
     *   metacat.properties file.  If the retrieved value does not match the 
108
     *   generated value, or if an exception is thrown, the test case fails.
109
     *6. set property and persist to main properties
110
     *   This does the same as test case 5, but instead of using setProperty which
111
     *   sets the property in memory and persists it to file, this case uses 
112
     *   setPropertyNoPersist and persistProperties to do the set and persist
113
     *   atomically. 
114
     *7. try to write property to main properties that doesn't exist
115
     *   Attempt to set a property that doesn't exist in the properties
116
     *   file.  This should throw a PropertyNotFoundException.  Anything else
117
     *   causes the test case to fail.
118
     *8. try to write property nonPersistant to main properties that doesn't exist
119
     *   This does the same as test case 7, but uses the setPropertyNoPersist method.
120
     */
121
    public void mainPropertiesTest()
122
    {
123
    	Random random = new Random();
124
    	try {
125
    		debug("\nRunning: mainPropertiesTest test");
126
      
127
	        //====1 read a single property from the properties file that should exist
128
        	debug("Test 1: read a single property from the properties file that should exist");
129
	        try {
130
	        	String metacatURL = PropertyService.getProperty("test.metacatUrl");
131
	        	if (metacatURL == null || metacatURL.equals("")) {
132
	        		fail("Test 1: Reading property 'test.metacatURL' returned no value.");
133
	        	}
134
	        	debug("Test 1: read property 'test.metacatURL': " + metacatURL);
135
	        } catch (PropertyNotFoundException pnfe) {
136
	        	fail("Test 1: Could not read property 'test.metacatURL' : " + pnfe.getMessage());
137
	        }
138
	        
139
	        //====2 read a single property from the main properties  that shouldn't exist
140
	        debug("Test 2: read a single property from the main properties  that shouldn't exist");
141
	        try {
142
	        	String metacatURL = PropertyService.getProperty("test.this.doesn't.exist");
143
	        	fail("Test 2: shouldn't have successfully read property: test.this.doesn't.exist : " 
144
	        			+ metacatURL);
145
	        } catch (PropertyNotFoundException pnfe) {
146
	        	debug("Test 2: expected failure reading property:'test.this.doesn't.exist' : " 
147
	        			+ pnfe.getMessage());
148
	        }
149
	        
150
	        //====3 read group property names from the main properties  
151
	        debug("Test 3: read property group names 'organization.org'");
152
			Vector<String> orgList = 
153
				PropertyService.getPropertyNamesByGroup("organization.org");
154
			if (orgList == null || orgList.size() == 0) {
155
				fail("Test 3: Empty vector returned when reading property group names 'organization.org'");
156
			}
157
   
158
	        // ====4 read group properties from the main properties
159
	        try {
160
	        	debug("Test 4: read property group 'organization.org'");
161
	        	Map<String, String> metacatProps = PropertyService.getPropertiesByGroup("organization.org");
162
	        	if (metacatProps == null || metacatProps.size() == 0) {
163
	        		fail("Test 4: Empty map returned when reading property group names 'organization.org'");
164
	        	}
165
	        } catch (PropertyNotFoundException pnfe) {
166
	        	fail("Test 4: Could not read property group names 'organization.org': " + pnfe.getMessage());
167
	        }  
168
	        
169
	        // ====5 write property to main properties
170
	        try {
171
	        	String testValue = "testing" + random.nextInt();
172
	        	debug("Test 5: set property 'test.testProperty' : " + testValue);
173
	        	PropertyService.setProperty("test.testProperty", testValue);
174
	        	String testValue2 = PropertyService.getProperty("test.testProperty");
175
	        	if (!testValue.equals(testValue2)) {
176
	        		fail("Test 5: couldn't set 'test.testProperty' to " + testValue);
177
	        	}
178
	        } catch (PropertyNotFoundException pnfe) {
179
	        	fail("Test 5: Could not set property 'test.testProperty' : " + pnfe.getMessage());
180
	        }  
181
	        	        
182
	        // ====6 set property and persist to main properties
183
	        try {
184
	        	String testValue = "testing" + random.nextInt();
185
	        	debug("Test 6: set property 'test.testProperty' : " + testValue);
186
	        	PropertyService.setPropertyNoPersist("test.testProperty", testValue);
187
	        	PropertyService.persistProperties();
188
	        	String testValue2 = PropertyService.getProperty("test.testProperty");
189
	        	if (!testValue.equals(testValue2)) {
190
	        		fail("Test 6: couldn't set 'test.testProperty' to " + testValue);
191
	        	}
192
	        } catch (PropertyNotFoundException pnfe) {
193
	        	fail("Test 6: Could not set property 'test.testProperty' : " + pnfe.getMessage());
194
	        }  
195
	        
196
	        // ====7 try to write property to main properties that doesn't exist
197
	        String testValue;
198
	        try {
199
	        	testValue = "testing" + random.nextInt();
200
	        	debug("Test 7: set property 'test.property.nonexistant' : " + testValue);
201
	        	PropertyService.setProperty("test.property.nonexistant", testValue);
202
		        fail("Test 7: shouldn't have been able to set 'test.property.nonexistant' to "
203
		        		+ testValue + " since 'test.property.nonexistant' doesn't exist.");
204
	        } catch (PropertyNotFoundException pnfe) {
205
	        	debug("Test 7: expected failure writing to property:'test.property.nonexistant' : " 
206
                        + pnfe.getMessage());
207
	        }  
208
	        
209
	        // ====8 try to write property nonPersistant to main properties that doesn't exist
210
	        try {
211
	        	testValue = "testing" + random.nextInt();
212
	        	debug("Test 8: set property 'test.property.nonexistant' : " + testValue);
213
	        	PropertyService.setPropertyNoPersist("test.property.nonexistant", testValue);
214
		        fail("Test 8: shouldn't have been able to set 'test.property.nonexistant' to "
215
		        		+ testValue + " since 'test.property.nonexistant' doesn't exist.");
216
	        } catch (PropertyNotFoundException pnfe) {
217
	        	debug("Test 8: expected failure writing to property:'test.property.nonexistant' : " 
218
                        + pnfe.getMessage());
219
	        }  
220
	        
221
	        
222
	    } catch (Exception e) {
223
	        fail("General exception:\n" + e.getMessage());
224
	    }
225
    }
226
}
(19-19/30)