source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/conf/TestConfiguration.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 12.5 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.apache.hadoop.conf;
19
20import java.io.BufferedWriter;
21import java.io.File;
22import java.io.FileWriter;
23import java.io.IOException;
24import java.io.DataInputStream;
25import java.io.ByteArrayOutputStream;
26import java.io.ByteArrayInputStream;
27import java.io.DataOutputStream;
28import java.util.ArrayList;
29import java.util.Random;
30
31import junit.framework.TestCase;
32
33import org.apache.hadoop.fs.Path;
34
35
36public class TestConfiguration extends TestCase {
37
38  private Configuration conf;
39  final static String CONFIG = new File("./test-config.xml").getAbsolutePath();
40  final static String CONFIG2 = new File("./test-config2.xml").getAbsolutePath();
41  final static Random RAN = new Random();
42
43  @Override
44  protected void setUp() throws Exception {
45    super.setUp();
46    conf = new Configuration();
47  }
48 
49  @Override
50  protected void tearDown() throws Exception {
51    super.tearDown();
52    new File(CONFIG).delete();
53    new File(CONFIG2).delete();
54  }
55 
56  private void startConfig() throws IOException{
57    out.write("<?xml version=\"1.0\"?>\n");
58    out.write("<configuration>\n");
59  }
60
61  private void endConfig() throws IOException{
62    out.write("</configuration>\n");
63    out.close();
64  }
65
66  private void addInclude(String filename) throws IOException{
67    out.write("<xi:include href=\"" + filename + "\" xmlns:xi=\"http://www.w3.org/2001/XInclude\"  />\n ");
68  }
69
70  public void testVariableSubstitution() throws IOException {
71    out=new BufferedWriter(new FileWriter(CONFIG));
72    startConfig();
73    declareProperty("my.int", "${intvar}", "42");
74    declareProperty("intvar", "42", "42");
75    declareProperty("my.base", "/tmp/${user.name}", UNSPEC);
76    declareProperty("my.file", "hello", "hello");
77    declareProperty("my.suffix", ".txt", ".txt");
78    declareProperty("my.relfile", "${my.file}${my.suffix}", "hello.txt");
79    declareProperty("my.fullfile", "${my.base}/${my.file}${my.suffix}", UNSPEC);
80    // check that undefined variables are returned as-is
81    declareProperty("my.failsexpand", "a${my.undefvar}b", "a${my.undefvar}b");
82    endConfig();
83    Path fileResource = new Path(CONFIG);
84    conf.addResource(fileResource);
85
86    for (Prop p : props) {
87      System.out.println("p=" + p.name);
88      String gotVal = conf.get(p.name);
89      String gotRawVal = conf.getRaw(p.name);
90      assertEq(p.val, gotRawVal);
91      if (p.expectEval == UNSPEC) {
92        // expansion is system-dependent (uses System properties)
93        // can't do exact match so just check that all variables got expanded
94        assertTrue(gotVal != null && -1 == gotVal.indexOf("${"));
95      } else {
96        assertEq(p.expectEval, gotVal);
97      }
98    }
99     
100    // check that expansion also occurs for getInt()
101    assertTrue(conf.getInt("intvar", -1) == 42);
102    assertTrue(conf.getInt("my.int", -1) == 42);
103  }
104   
105  public static void assertEq(Object a, Object b) {
106    System.out.println("assertEq: " + a + ", " + b);
107    assertEquals(a, b);
108  }
109
110  static class Prop {
111    String name;
112    String val;
113    String expectEval;
114  }
115
116  final String UNSPEC = null;
117  ArrayList<Prop> props = new ArrayList<Prop>();
118
119  void declareProperty(String name, String val, String expectEval)
120    throws IOException {
121    declareProperty(name, val, expectEval, false);
122  }
123
124  void declareProperty(String name, String val, String expectEval,
125                       boolean isFinal)
126    throws IOException {
127    appendProperty(name, val, isFinal);
128    Prop p = new Prop();
129    p.name = name;
130    p.val = val;
131    p.expectEval = expectEval;
132    props.add(p);
133  }
134
135  void appendProperty(String name, String val) throws IOException {
136    appendProperty(name, val, false);
137  }
138 
139  void appendProperty(String name, String val, boolean isFinal)
140    throws IOException {
141    out.write("<property>");
142    out.write("<name>");
143    out.write(name);
144    out.write("</name>");
145    out.write("<value>");
146    out.write(val);
147    out.write("</value>");
148    if (isFinal) {
149      out.write("<final>true</final>");
150    }
151    out.write("</property>\n");
152  }
153 
154  public void testOverlay() throws IOException{
155    out=new BufferedWriter(new FileWriter(CONFIG));
156    startConfig();
157    appendProperty("a","b");
158    appendProperty("b","c");
159    appendProperty("d","e");
160    appendProperty("e","f", true);
161    endConfig();
162
163    out=new BufferedWriter(new FileWriter(CONFIG2));
164    startConfig();
165    appendProperty("a","b");
166    appendProperty("b","d");
167    appendProperty("e","e");
168    endConfig();
169   
170    Path fileResource = new Path(CONFIG);
171    conf.addResource(fileResource);
172   
173    //set dynamically something
174    conf.set("c","d");
175    conf.set("a","d");
176   
177    Configuration clone=new Configuration(conf);
178    clone.addResource(new Path(CONFIG2));
179   
180    assertEquals(clone.get("a"), "d"); 
181    assertEquals(clone.get("b"), "d"); 
182    assertEquals(clone.get("c"), "d"); 
183    assertEquals(clone.get("d"), "e"); 
184    assertEquals(clone.get("e"), "f"); 
185   
186  }
187 
188  public void testCommentsInValue() throws IOException {
189    out=new BufferedWriter(new FileWriter(CONFIG));
190    startConfig();
191    appendProperty("my.comment", "this <!--comment here--> contains a comment");
192    endConfig();
193    Path fileResource = new Path(CONFIG);
194    conf.addResource(fileResource);
195    //two spaces one after "this", one before "contains"
196    assertEquals("this  contains a comment", conf.get("my.comment"));
197  }
198 
199  public void testTrim() throws IOException {
200    out=new BufferedWriter(new FileWriter(CONFIG));
201    startConfig();
202    String[] whitespaces = {"", " ", "\n", "\t"};
203    String[] name = new String[100];
204    for(int i = 0; i < name.length; i++) {
205      name[i] = "foo" + i;
206      StringBuilder prefix = new StringBuilder(); 
207      StringBuilder postfix = new StringBuilder(); 
208      for(int j = 0; j < 3; j++) {
209        prefix.append(whitespaces[RAN.nextInt(whitespaces.length)]);
210        postfix.append(whitespaces[RAN.nextInt(whitespaces.length)]);
211      }
212     
213      appendProperty(prefix + name[i] + postfix, name[i] + ".value");
214    }
215    endConfig();
216
217    conf.addResource(new Path(CONFIG));
218    for(String n : name) {
219      assertEquals(n + ".value", conf.get(n));
220    }
221  }
222
223  public void testToString() throws IOException {
224    out=new BufferedWriter(new FileWriter(CONFIG));
225    startConfig();
226    endConfig();
227    Path fileResource = new Path(CONFIG);
228    conf.addResource(fileResource);
229   
230    String expectedOutput = 
231      "Configuration: core-default.xml, core-site.xml, " + 
232      fileResource.toString();
233    assertEquals(expectedOutput, conf.toString());
234  }
235 
236  public void testIncludes() throws Exception {
237    tearDown();
238    System.out.println("XXX testIncludes");
239    out=new BufferedWriter(new FileWriter(CONFIG2));
240    startConfig();
241    appendProperty("a","b");
242    appendProperty("c","d");
243    endConfig();
244
245    out=new BufferedWriter(new FileWriter(CONFIG));
246    startConfig();
247    addInclude(CONFIG2);
248    appendProperty("e","f");
249    appendProperty("g","h");
250    endConfig();
251
252    // verify that the includes file contains all properties
253    Path fileResource = new Path(CONFIG);
254    conf.addResource(fileResource);
255    assertEquals(conf.get("a"), "b"); 
256    assertEquals(conf.get("c"), "d"); 
257    assertEquals(conf.get("e"), "f"); 
258    assertEquals(conf.get("g"), "h"); 
259    tearDown();
260  }
261
262  BufferedWriter out;
263       
264  public void testIntegerRanges() {
265    Configuration conf = new Configuration();
266    conf.set("first", "-100");
267    conf.set("second", "4-6,9-10,27");
268    conf.set("third", "34-");
269    Configuration.IntegerRanges range = conf.getRange("first", null);
270    System.out.println("first = " + range);
271    assertEquals(true, range.isIncluded(0));
272    assertEquals(true, range.isIncluded(1));
273    assertEquals(true, range.isIncluded(100));
274    assertEquals(false, range.isIncluded(101));
275    range = conf.getRange("second", null);
276    System.out.println("second = " + range);
277    assertEquals(false, range.isIncluded(3));
278    assertEquals(true, range.isIncluded(4));
279    assertEquals(true, range.isIncluded(6));
280    assertEquals(false, range.isIncluded(7));
281    assertEquals(false, range.isIncluded(8));
282    assertEquals(true, range.isIncluded(9));
283    assertEquals(true, range.isIncluded(10));
284    assertEquals(false, range.isIncluded(11));
285    assertEquals(false, range.isIncluded(26));
286    assertEquals(true, range.isIncluded(27));
287    assertEquals(false, range.isIncluded(28));
288    range = conf.getRange("third", null);
289    System.out.println("third = " + range);
290    assertEquals(false, range.isIncluded(33));
291    assertEquals(true, range.isIncluded(34));
292    assertEquals(true, range.isIncluded(100000000));
293  }
294
295  public void testHexValues() throws IOException{
296    out=new BufferedWriter(new FileWriter(CONFIG));
297    startConfig();
298    appendProperty("test.hex1", "0x10");
299    appendProperty("test.hex2", "0xF");
300    appendProperty("test.hex3", "-0x10");
301    endConfig();
302    Path fileResource = new Path(CONFIG);
303    conf.addResource(fileResource);
304    assertEquals(16, conf.getInt("test.hex1", 0));
305    assertEquals(16, conf.getLong("test.hex1", 0));
306    assertEquals(15, conf.getInt("test.hex2", 0));
307    assertEquals(15, conf.getLong("test.hex2", 0));
308    assertEquals(-16, conf.getInt("test.hex3", 0));
309    assertEquals(-16, conf.getLong("test.hex3", 0));
310
311  }
312
313  public void testIntegerValues() throws IOException{
314    out=new BufferedWriter(new FileWriter(CONFIG));
315    startConfig();
316    appendProperty("test.int1", "20");
317    appendProperty("test.int2", "020");
318    appendProperty("test.int3", "-20");
319    endConfig();
320    Path fileResource = new Path(CONFIG);
321    conf.addResource(fileResource);
322    assertEquals(20, conf.getInt("test.int1", 0));
323    assertEquals(20, conf.getLong("test.int1", 0));
324    assertEquals(20, conf.getInt("test.int2", 0));
325    assertEquals(20, conf.getLong("test.int2", 0));
326    assertEquals(-20, conf.getInt("test.int3", 0));
327    assertEquals(-20, conf.getLong("test.int3", 0));
328  }
329       
330  public void testReload() throws IOException {
331    out=new BufferedWriter(new FileWriter(CONFIG));
332    startConfig();
333    appendProperty("test.key1", "final-value1", true);
334    appendProperty("test.key2", "value2");
335    endConfig();
336    Path fileResource = new Path(CONFIG);
337    conf.addResource(fileResource);
338   
339    out=new BufferedWriter(new FileWriter(CONFIG2));
340    startConfig();
341    appendProperty("test.key1", "value1");
342    appendProperty("test.key3", "value3");
343    endConfig();
344    Path fileResource1 = new Path(CONFIG2);
345    conf.addResource(fileResource1);
346   
347    // add a few values via set.
348    conf.set("test.key3", "value4");
349    conf.set("test.key4", "value5");
350   
351    assertEquals("final-value1", conf.get("test.key1"));
352    assertEquals("value2", conf.get("test.key2"));
353    assertEquals("value4", conf.get("test.key3"));
354    assertEquals("value5", conf.get("test.key4"));
355   
356    // change values in the test file...
357    out=new BufferedWriter(new FileWriter(CONFIG));
358    startConfig();
359    appendProperty("test.key1", "final-value1");
360    appendProperty("test.key3", "final-value3", true);
361    endConfig();
362   
363    conf.reloadConfiguration();
364    assertEquals("value1", conf.get("test.key1"));
365    // overlayed property overrides.
366    assertEquals("value4", conf.get("test.key3"));
367    assertEquals(null, conf.get("test.key2"));
368    assertEquals("value5", conf.get("test.key4"));
369  }
370
371  public void testSize() throws IOException {
372    Configuration conf = new Configuration(false);
373    conf.set("a", "A");
374    conf.set("b", "B");
375    assertEquals(2, conf.size());
376  }
377
378  public void testClear() throws IOException {
379    Configuration conf = new Configuration(false);
380    conf.set("a", "A");
381    conf.set("b", "B");
382    conf.clear();
383    assertEquals(0, conf.size());
384    assertFalse(conf.iterator().hasNext());
385  }
386
387  public static void main(String[] argv) throws Exception {
388    junit.textui.TestRunner.main(new String[]{
389      TestConfiguration.class.getName()
390    });
391  }
392}
Note: See TracBrowser for help on using the repository browser.