Search in sources :

Example 6 with BufferedWriter

use of java.io.BufferedWriter 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 7 with BufferedWriter

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

the class TestConfiguration method testInputStreamResource.

public void testInputStreamResource() throws Exception {
    StringWriter writer = new StringWriter();
    out = new BufferedWriter(writer);
    startConfig();
    declareProperty("prop", "A", "A");
    endConfig();
    InputStream in1 = new ByteArrayInputStream(writer.toString().getBytes());
    Configuration conf = new Configuration(false);
    conf.addResource(in1);
    assertEquals("A", conf.get("prop"));
    InputStream in2 = new ByteArrayInputStream(writer.toString().getBytes());
    conf.addResource(in2);
    assertEquals("A", conf.get("prop"));
}
Also used : StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BufferedWriter(java.io.BufferedWriter)

Example 8 with BufferedWriter

use of java.io.BufferedWriter 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 9 with BufferedWriter

use of java.io.BufferedWriter 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 10 with BufferedWriter

use of java.io.BufferedWriter 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)

Aggregations

BufferedWriter (java.io.BufferedWriter)1557 IOException (java.io.IOException)726 FileWriter (java.io.FileWriter)706 File (java.io.File)629 OutputStreamWriter (java.io.OutputStreamWriter)603 FileOutputStream (java.io.FileOutputStream)291 BufferedReader (java.io.BufferedReader)206 Writer (java.io.Writer)138 InputStreamReader (java.io.InputStreamReader)124 PrintWriter (java.io.PrintWriter)117 ArrayList (java.util.ArrayList)102 FileNotFoundException (java.io.FileNotFoundException)101 Test (org.junit.Test)99 Path (org.apache.hadoop.fs.Path)80 FileReader (java.io.FileReader)78 OutputStream (java.io.OutputStream)65 StringWriter (java.io.StringWriter)58 Path (java.nio.file.Path)58 Date (java.util.Date)57 FileInputStream (java.io.FileInputStream)54