Search in sources :

Example 16 with FileWriter

use of java.io.FileWriter in project hadoop by apache.

the class TestConfiguration method testPattern.

public void testPattern() throws IOException {
    out = new BufferedWriter(new FileWriter(CONFIG));
    startConfig();
    appendProperty("test.pattern1", "");
    appendProperty("test.pattern2", "(");
    appendProperty("test.pattern3", "a+b");
    endConfig();
    Path fileResource = new Path(CONFIG);
    conf.addResource(fileResource);
    Pattern defaultPattern = Pattern.compile("x+");
    // Return default if missing
    assertEquals(defaultPattern.pattern(), conf.getPattern("xxxxx", defaultPattern).pattern());
    // Return null if empty and default is null
    assertNull(conf.getPattern("test.pattern1", null));
    // Return default for empty
    assertEquals(defaultPattern.pattern(), conf.getPattern("test.pattern1", defaultPattern).pattern());
    // Return default for malformed
    assertEquals(defaultPattern.pattern(), conf.getPattern("test.pattern2", defaultPattern).pattern());
    // Works for correct patterns
    assertEquals("a+b", conf.getPattern("test.pattern3", defaultPattern).pattern());
}
Also used : Path(org.apache.hadoop.fs.Path) Pattern(java.util.regex.Pattern) FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter)

Example 17 with FileWriter

use of java.io.FileWriter in project hadoop by apache.

the class TestConfiguration method testDoubleValues.

public void testDoubleValues() throws IOException {
    out = new BufferedWriter(new FileWriter(CONFIG));
    startConfig();
    appendProperty("test.double1", "3.1415");
    appendProperty("test.double2", "003.1415");
    appendProperty("test.double3", "-3.1415");
    appendProperty("test.double4", " -3.1415 ");
    appendProperty("test.double5", "xyz-3.1415xyz");
    endConfig();
    Path fileResource = new Path(CONFIG);
    conf.addResource(fileResource);
    assertEquals(3.1415, conf.getDouble("test.double1", 0.0));
    assertEquals(3.1415, conf.getDouble("test.double2", 0.0));
    assertEquals(-3.1415, conf.getDouble("test.double3", 0.0));
    assertEquals(-3.1415, conf.getDouble("test.double4", 0.0));
    try {
        conf.getDouble("test.double5", 0.0);
        fail("Property had invalid double value, but was read successfully.");
    } catch (NumberFormatException e) {
    // pass
    }
}
Also used : Path(org.apache.hadoop.fs.Path) FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter)

Example 18 with FileWriter

use of java.io.FileWriter in project hadoop by apache.

the class TestConfiguration method testPropertySource.

public void testPropertySource() throws IOException {
    out = new BufferedWriter(new FileWriter(CONFIG));
    startConfig();
    appendProperty("test.foo", "bar");
    endConfig();
    Path fileResource = new Path(CONFIG);
    conf.addResource(fileResource);
    conf.set("fs.defaultFS", "value");
    String[] sources = conf.getPropertySources("test.foo");
    assertEquals(1, sources.length);
    assertEquals("Resource string returned for a file-loaded property" + " must be a proper absolute path", fileResource, new Path(sources[0]));
    assertArrayEquals("Resource string returned for a set() property must be " + "\"programmatically\"", new String[] { "programmatically" }, conf.getPropertySources("fs.defaultFS"));
    assertEquals("Resource string returned for an unset property must be null", null, conf.getPropertySources("fs.defaultFoo"));
}
Also used : Path(org.apache.hadoop.fs.Path) FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter)

Example 19 with FileWriter

use of java.io.FileWriter in project hadoop by apache.

the class TestConfiguration method testEscapedCharactersInValue.

public void testEscapedCharactersInValue() throws IOException {
    out = new BufferedWriter(new FileWriter(CONFIG));
    startConfig();
    appendProperty("my.comment", ESCAPED);
    endConfig();
    Path fileResource = new Path(CONFIG);
    conf.addResource(fileResource);
    //two spaces one after "this", one before "contains"
    assertEquals("''''", conf.get("my.comment"));
}
Also used : Path(org.apache.hadoop.fs.Path) FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter)

Example 20 with FileWriter

use of java.io.FileWriter in project hadoop by apache.

the class TestConfiguration method testMultiplePropertySource.

public void testMultiplePropertySource() throws IOException {
    out = new BufferedWriter(new FileWriter(CONFIG));
    startConfig();
    appendProperty("test.foo", "bar", false, "a", "b", "c");
    endConfig();
    Path fileResource = new Path(CONFIG);
    conf.addResource(fileResource);
    String[] sources = conf.getPropertySources("test.foo");
    assertEquals(4, sources.length);
    assertEquals("a", sources[0]);
    assertEquals("b", sources[1]);
    assertEquals("c", sources[2]);
    assertEquals("Resource string returned for a file-loaded property" + " must be a proper absolute path", fileResource, new Path(sources[3]));
}
Also used : Path(org.apache.hadoop.fs.Path) FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter)

Aggregations

FileWriter (java.io.FileWriter)1776 File (java.io.File)1056 IOException (java.io.IOException)741 BufferedWriter (java.io.BufferedWriter)705 PrintWriter (java.io.PrintWriter)285 Test (org.junit.Test)222 Writer (java.io.Writer)162 FileReader (java.io.FileReader)121 BufferedReader (java.io.BufferedReader)107 ArrayList (java.util.ArrayList)101 FileNotFoundException (java.io.FileNotFoundException)68 Date (java.util.Date)61 Properties (java.util.Properties)61 FileOutputStream (java.io.FileOutputStream)58 HashMap (java.util.HashMap)53 StringWriter (java.io.StringWriter)50 Path (org.apache.hadoop.fs.Path)49 FileInputStream (java.io.FileInputStream)47 Map (java.util.Map)33 OutputStreamWriter (java.io.OutputStreamWriter)31