Search in sources :

Example 11 with FileReader

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

the class TestFsck method verifyAuditLogs.

private void verifyAuditLogs() throws IOException {
    // Turn off the logs
    Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
    logger.setLevel(Level.OFF);
    BufferedReader reader = null;
    try {
        // Audit log should contain one getfileinfo and one fsck
        reader = new BufferedReader(new FileReader(AUDITLOG_FILE));
        String line;
        //
        for (int i = 0; i < 2; i++) {
            line = reader.readLine();
            assertNotNull(line);
            assertTrue("Expected getfileinfo event not found in audit log", GET_FILE_INFO_PATTERN.matcher(line).matches());
        }
        line = reader.readLine();
        assertNotNull(line);
        assertTrue("Expected fsck event not found in audit log", FSCK_PATTERN.matcher(line).matches());
        assertNull("Unexpected event in audit log", reader.readLine());
    } finally {
        // handle after verifying the content of the file.
        if (reader != null) {
            reader.close();
        }
        if (logger != null) {
            logger.removeAllAppenders();
        }
    }
}
Also used : Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Matchers.anyString(org.mockito.Matchers.anyString) Logger(org.apache.log4j.Logger) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger)

Example 12 with FileReader

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

the class TestNativeIO method testSetFilePointer.

@Test(timeout = 30000)
public void testSetFilePointer() throws Exception {
    assumeWindows();
    LOG.info("Set a file pointer on Windows");
    try {
        File testfile = new File(TEST_DIR, "testSetFilePointer");
        assertTrue("Create test subject", testfile.exists() || testfile.createNewFile());
        FileWriter writer = new FileWriter(testfile);
        try {
            for (int i = 0; i < 200; i++) if (i < 100)
                writer.write('a');
            else
                writer.write('b');
            writer.flush();
        } catch (Exception writerException) {
            fail("Got unexpected exception: " + writerException.getMessage());
        } finally {
            writer.close();
        }
        FileDescriptor fd = NativeIO.Windows.createFile(testfile.getCanonicalPath(), NativeIO.Windows.GENERIC_READ, NativeIO.Windows.FILE_SHARE_READ | NativeIO.Windows.FILE_SHARE_WRITE | NativeIO.Windows.FILE_SHARE_DELETE, NativeIO.Windows.OPEN_EXISTING);
        NativeIO.Windows.setFilePointer(fd, 120, NativeIO.Windows.FILE_BEGIN);
        FileReader reader = new FileReader(fd);
        try {
            int c = reader.read();
            assertTrue("Unexpected character: " + c, c == 'b');
        } catch (Exception readerException) {
            fail("Got unexpected exception: " + readerException.getMessage());
        } finally {
            reader.close();
        }
    } catch (Exception e) {
        fail("Got unexpected exception: " + e.getMessage());
    }
}
Also used : FileWriter(java.io.FileWriter) FileReader(java.io.FileReader) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor) Test(org.junit.Test)

Example 13 with FileReader

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

the class TestAuditLogs method verifyAuditLogsRepeat.

// Ensure audit log has exactly N entries
private void verifyAuditLogsRepeat(boolean expectSuccess, int ndupe) throws IOException {
    // Turn off the logs
    Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
    logger.setLevel(Level.OFF);
    // Close the appenders and force all logs to be flushed
    Enumeration<?> appenders = logger.getAllAppenders();
    while (appenders.hasMoreElements()) {
        Appender appender = (Appender) appenders.nextElement();
        appender.close();
    }
    BufferedReader reader = new BufferedReader(new FileReader(auditLogFile));
    String line = null;
    boolean ret = true;
    try {
        for (int i = 0; i < ndupe; i++) {
            line = reader.readLine();
            assertNotNull(line);
            assertTrue("Expected audit event not found in audit log", auditPattern.matcher(line).matches());
            ret &= successPattern.matcher(line).matches();
        }
        assertNull("Unexpected event in audit log", reader.readLine());
        assertTrue("Expected success=" + expectSuccess, ret == expectSuccess);
    } finally {
        reader.close();
    }
}
Also used : Appender(org.apache.log4j.Appender) AsyncAppender(org.apache.log4j.AsyncAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) Logger(org.apache.log4j.Logger)

Example 14 with FileReader

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

the class TestAuditLogs method verifyAuditLogsCheckPattern.

// Ensure audit log has exactly N entries
private void verifyAuditLogsCheckPattern(boolean expectSuccess, int ndupe, Pattern pattern) throws IOException {
    // Turn off the logs
    Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
    logger.setLevel(Level.OFF);
    // Close the appenders and force all logs to be flushed
    Enumeration<?> appenders = logger.getAllAppenders();
    while (appenders.hasMoreElements()) {
        Appender appender = (Appender) appenders.nextElement();
        appender.close();
    }
    BufferedReader reader = new BufferedReader(new FileReader(auditLogFile));
    String line = null;
    boolean ret = true;
    boolean patternMatches = false;
    try {
        for (int i = 0; i < ndupe; i++) {
            line = reader.readLine();
            assertNotNull(line);
            patternMatches |= pattern.matcher(line).matches();
            ret &= successPattern.matcher(line).matches();
        }
        assertNull("Unexpected event in audit log", reader.readLine());
        assertTrue("Expected audit event not found in audit log", patternMatches);
        assertTrue("Expected success=" + expectSuccess, ret == expectSuccess);
    } finally {
        reader.close();
    }
}
Also used : Appender(org.apache.log4j.Appender) AsyncAppender(org.apache.log4j.AsyncAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) Logger(org.apache.log4j.Logger)

Example 15 with FileReader

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

the class SnapshotTestHelper method printFile.

static void printFile(File f) throws IOException {
    System.out.println();
    System.out.println("File: " + f);
    BufferedReader in = new BufferedReader(new FileReader(f));
    try {
        for (String line; (line = in.readLine()) != null; ) {
            System.out.println(line);
        }
    } finally {
        in.close();
    }
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Aggregations

FileReader (java.io.FileReader)1602 BufferedReader (java.io.BufferedReader)1114 IOException (java.io.IOException)762 File (java.io.File)701 FileNotFoundException (java.io.FileNotFoundException)264 ArrayList (java.util.ArrayList)250 Test (org.junit.Test)154 FileWriter (java.io.FileWriter)121 HashMap (java.util.HashMap)103 Reader (java.io.Reader)86 BufferedWriter (java.io.BufferedWriter)78 Properties (java.util.Properties)57 InputStreamReader (java.io.InputStreamReader)54 Map (java.util.Map)54 List (java.util.List)51 Matcher (java.util.regex.Matcher)49 LineNumberReader (java.io.LineNumberReader)46 HashSet (java.util.HashSet)43 FileInputStream (java.io.FileInputStream)41 PrintWriter (java.io.PrintWriter)40