Search in sources :

Example 6 with LineNumberReader

use of java.io.LineNumberReader in project elasticsearch by elastic.

the class Definition method addElements.

/** adds class methods/fields/ctors */
private void addElements() {
    for (String file : DEFINITION_FILES) {
        int currentLine = -1;
        try {
            try (InputStream stream = Definition.class.getResourceAsStream(file);
                LineNumberReader reader = new LineNumberReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
                String line = null;
                String currentClass = null;
                while ((line = reader.readLine()) != null) {
                    currentLine = reader.getLineNumber();
                    line = line.trim();
                    if (line.length() == 0 || line.charAt(0) == '#') {
                        continue;
                    } else if (line.startsWith("class ")) {
                        assert currentClass == null;
                        currentClass = line.split(" ")[1];
                    } else if (line.equals("}")) {
                        assert currentClass != null;
                        currentClass = null;
                    } else {
                        assert currentClass != null;
                        addSignature(currentClass, line);
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("syntax error in " + file + ", line: " + currentLine, e);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) LineNumberReader(java.io.LineNumberReader)

Example 7 with LineNumberReader

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

the class TestJobMonitorAndPrint method testJobMonitorAndPrint.

@Test
public void testJobMonitorAndPrint() throws Exception {
    JobStatus jobStatus_1 = new JobStatus(new JobID("job_000", 1), 1f, 0.1f, 0.1f, 0f, State.RUNNING, JobPriority.HIGH, "tmp-user", "tmp-jobname", "tmp-queue", "tmp-jobfile", "tmp-url", true);
    JobStatus jobStatus_2 = new JobStatus(new JobID("job_000", 1), 1f, 1f, 1f, 1f, State.SUCCEEDED, JobPriority.HIGH, "tmp-user", "tmp-jobname", "tmp-queue", "tmp-jobfile", "tmp-url", true);
    doAnswer(new Answer<TaskCompletionEvent[]>() {

        @Override
        public TaskCompletionEvent[] answer(InvocationOnMock invocation) throws Throwable {
            return new TaskCompletionEvent[0];
        }
    }).when(job).getTaskCompletionEvents(anyInt(), anyInt());
    doReturn(new TaskReport[5]).when(job).getTaskReports(isA(TaskType.class));
    when(clientProtocol.getJobStatus(any(JobID.class))).thenReturn(jobStatus_1, jobStatus_2);
    // setup the logger to capture all logs
    Layout layout = Logger.getRootLogger().getAppender("stdout").getLayout();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    WriterAppender appender = new WriterAppender(layout, os);
    appender.setThreshold(Level.ALL);
    Logger qlogger = Logger.getLogger(Job.class);
    qlogger.addAppender(appender);
    job.monitorAndPrintJob();
    qlogger.removeAppender(appender);
    LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
    String line;
    boolean foundHundred = false;
    boolean foundComplete = false;
    boolean foundUber = false;
    String uberModeMatch = "uber mode : true";
    String progressMatch = "map 100% reduce 100%";
    String completionMatch = "completed successfully";
    while ((line = r.readLine()) != null) {
        if (line.contains(uberModeMatch)) {
            foundUber = true;
        }
        foundHundred = line.contains(progressMatch);
        if (foundHundred)
            break;
    }
    line = r.readLine();
    foundComplete = line.contains(completionMatch);
    assertTrue(foundUber);
    assertTrue(foundHundred);
    assertTrue(foundComplete);
    System.out.println("The output of job.toString() is : \n" + job.toString());
    assertTrue(job.toString().contains("Number of maps: 5\n"));
    assertTrue(job.toString().contains("Number of reduces: 5\n"));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) WriterAppender(org.apache.log4j.WriterAppender) Logger(org.apache.log4j.Logger) LineNumberReader(java.io.LineNumberReader) Layout(org.apache.log4j.Layout) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 8 with LineNumberReader

use of java.io.LineNumberReader in project checkstyle by checkstyle.

the class BaseCheckTestSupport method verify.

/**
     *  We keep two verify methods with separate logic only for convenience of debuging
     *  We have minimum amount of multi-file test cases
     */
protected void verify(Checker checker, File[] processedFiles, String messageFileName, String... expected) throws Exception {
    stream.flush();
    final List<File> theFiles = new ArrayList<>();
    Collections.addAll(theFiles, processedFiles);
    final int errs = checker.process(theFiles);
    // process each of the lines
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
    try (LineNumberReader lnr = new LineNumberReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
        final List<String> actuals = lnr.lines().limit(expected.length).sorted().collect(Collectors.toList());
        Arrays.sort(expected);
        for (int i = 0; i < expected.length; i++) {
            final String expectedResult = messageFileName + ":" + expected[i];
            assertEquals("error message " + i, expectedResult, actuals.get(i));
        }
        assertEquals("unexpected output: " + lnr.readLine(), expected.length, errs);
    }
    checker.destroy();
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) File(java.io.File) LineNumberReader(java.io.LineNumberReader)

Example 9 with LineNumberReader

use of java.io.LineNumberReader in project OpenAM by OpenRock.

the class FileUtils method findPropertyIndex.

/**
     * Method declaration
     * 
     * 
     * @param fileName
     * @param property
     * 
     * @return
     * 
     * @see
     */
private static int findPropertyIndex(String fileName, String property) {
    int matchIndex = -1;
    try {
        File file = new File(fileName);
        if (file.exists() && file.canRead()) {
            LineNumberReader lineRead = new LineNumberReader(new FileReader(file));
            String prevLine = null;
            String presentLine = lineRead.readLine();
            int prevLineNum = -1;
            int presentLineNo = 0;
            String pattern = property;
            while (presentLine != null) {
                prevLine = presentLine;
                prevLineNum = presentLineNo;
                presentLineNo = lineRead.getLineNumber();
                presentLine = lineRead.readLine();
                if ((presentLine != null) && (presentLine.indexOf(pattern) >= 0) && (!presentLine.startsWith(HASH))) {
                    matchIndex = presentLineNo;
                    break;
                }
            }
            if (lineRead != null) {
                lineRead.close();
            }
        }
    } catch (Exception ex) {
        Debug.log("FileUtils.findPropertyIndex() threw exception : ", ex);
    }
    return matchIndex;
}
Also used : FileReader(java.io.FileReader) File(java.io.File) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Example 10 with LineNumberReader

use of java.io.LineNumberReader in project OpenAM by OpenRock.

the class FileUtils method getLineWithPattern.

/**
     * Method getLineWithPattern
     * 
     * 
     * @param filePath
     * @param pattern
     * @param matchBegin
     * @param matchEnd
     * @param ignoreCase
     * @param beginAtLine
     * 
     * @return
     * 
     */
public static String getLineWithPattern(String filePath, String pattern, boolean matchBegin, boolean matchEnd, boolean ignoreCase, int beginAtLine) {
    String line = null;
    String prevLine = null;
    boolean match = false;
    String retLine = null;
    try {
        if (pattern != null) {
            LineNumberReader reader = getLineNumReader(filePath);
            if (reader != null) {
                line = reader.readLine();
                if (beginAtLine > 0) {
                    int lineCount = 0;
                    while ((line != null) && (lineCount < beginAtLine)) {
                        line = reader.readLine();
                        lineCount++;
                    }
                }
                while ((line != null) && !match) {
                    match = matchPattern(line, pattern, matchBegin, matchEnd, ignoreCase);
                    prevLine = new String(line);
                    line = reader.readLine();
                }
                if (match) {
                    Debug.log("FileUtils.getLineWithPattern : Match [ " + pattern + " ] => " + prevLine);
                    retLine = prevLine;
                }
                reader.close();
            }
        }
    } catch (Exception ex) {
        Debug.log("FileUtils.getLineWithPattern() threw exception : ", ex);
    }
    return retLine;
}
Also used : IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Aggregations

LineNumberReader (java.io.LineNumberReader)401 IOException (java.io.IOException)203 InputStreamReader (java.io.InputStreamReader)167 FileReader (java.io.FileReader)102 File (java.io.File)79 InputStream (java.io.InputStream)64 StringReader (java.io.StringReader)64 ArrayList (java.util.ArrayList)54 FileInputStream (java.io.FileInputStream)33 BufferedReader (java.io.BufferedReader)27 PrintWriter (java.io.PrintWriter)25 HashMap (java.util.HashMap)22 FileNotFoundException (java.io.FileNotFoundException)20 BufferedWriter (java.io.BufferedWriter)16 FileWriter (java.io.FileWriter)16 Pattern (java.util.regex.Pattern)16 Test (org.junit.jupiter.api.Test)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 Reader (java.io.Reader)14