public void testAppenders() throws Exception { DOMConfigurator.configure(getClass().getResource("log4j.xml")); AsyncCoalescingStatisticsAppender appender = (AsyncCoalescingStatisticsAppender) Logger.getLogger(StopWatch.DEFAULT_LOGGER_NAME).getAppender("coalescingStatistics"); // log from a bunch of threads TestLoggingThread[] testThreads = new TestLoggingThread[10]; for (int i = 0; i < testThreads.length; i++) { testThreads[i] = new TestLoggingThread(); testThreads[i].start(); } for (TestLoggingThread testThread : testThreads) { testThread.join(); } // close the output appender, which prevents us from returning until this method completes. appender.close(); // simple verification ensures that the total number of logged messages is correct. // tagName avg min max std dev count, which is group 1 String regex = "tag\\d\\s*\\d+\\.\\d\\s*\\d+\\s*\\d+\\s*\\d+\\.\\d\\s*(\\d+)"; Pattern statLinePattern = Pattern.compile(regex); Scanner scanner = new Scanner(new File("target/statisticsLog.log")); int totalCount = 0; while (scanner.findWithinHorizon(statLinePattern, 0) != null) { totalCount += Integer.parseInt(scanner.match().group(1)); } assertEquals(testThreads.length * TestLoggingThread.STOP_WATCH_COUNT, totalCount); }
// http://jira.codehaus.org/browse/PERFFORJ-21 public void testAppendersTimesliceOver() throws Exception { // need to do immediateflush on the fileappender since close will not be called DOMConfigurator.configure(getClass().getResource("log4j-timeslicebug.xml")); AsyncCoalescingStatisticsAppender appender = (AsyncCoalescingStatisticsAppender) Logger.getLogger(StopWatch.DEFAULT_LOGGER_NAME).getAppender("coalescingStatistics"); // log from a bunch of threads TestLoggingThread[] testThreads = new TestLoggingThread[10]; for (int i = 0; i < testThreads.length; i++) { testThreads[i] = new TestLoggingThread(); testThreads[i].start(); } for (TestLoggingThread testThread : testThreads) { testThread.join(); } // we should see all the logging after waiting this long Thread.sleep(2 * appender.getTimeSlice()); // simple verification ensures that the total number of logged messages is correct. // tagName avg min max std dev count, which is group 1 String regex = "tag\\d+\\s*\\d+\\.\\d\\s*\\d+\\s*\\d+\\s*\\d+\\.\\d\\s*(\\d+)"; Pattern statLinePattern = Pattern.compile(regex); Scanner scanner = new Scanner(new File("target/statisticsLog-timeslicebug.log")); int totalCount = 0; while (scanner.findWithinHorizon(statLinePattern, 0) != null) { totalCount += Integer.parseInt(scanner.match().group(1)); } assertEquals(testThreads.length * TestLoggingThread.STOP_WATCH_COUNT, totalCount); }
public static void tokenize(String regexStr, String source, String delimiters) { // (1) System.out.print("Index: "); for (int i = 0; i < source.length(); i++) { System.out.print(i % 10); } System.out.println(); System.out.println("Target: " + source); System.out.println("Delimit: " + delimiters); System.out.println("Pattern: " + regexStr); System.out.print("Match: "); Pattern pattern = Pattern.compile(regexStr); // (2) Scanner lexer = new Scanner(source); // (3) if (!delimiters.equalsIgnoreCase("default")) lexer.useDelimiter(delimiters); // (5) while (lexer.hasNext()) { // (4) if (lexer.hasNext(pattern)) { // (5) String matchedStr = lexer.next(pattern); // (5) MatchResult matchResult = lexer.match(); // (6) int startCharIndex = matchResult.start(); int lastPlus1Index = matchResult.end(); int lastCharIndex = startCharIndex == lastPlus1Index ? lastPlus1Index : lastPlus1Index - 1; out.print("(" + startCharIndex + "," + lastCharIndex + ":" + matchedStr + ")"); } else { lexer.next(); // (7) } } System.out.println(); }
public static List<String[]> readCSVTable(Readable source, int cellNum) throws IOException { Scanner in = new Scanner(source); List<String[]> vals = new ArrayList<String[]>(); String exp = "^"; String separator = ""; for (int i = 0; i < cellNum; i++) { exp += separator + "(.*)"; separator = ","; } // exp += "$"; Pattern pat = Pattern.compile(exp, Pattern.MULTILINE); while (in.hasNextLine()) { String line = in.findInLine(pat); if (line != null) { String[] cells = new String[cellNum]; MatchResult match = in.match(); for (int i = 0; i < cellNum; i++) { cells[i] = match.group(i + 1); if (cells[i].contains(",")) { throw new IOException("input format error"); } } vals.add(cells); in.nextLine(); } else { if (!"".equals(in.nextLine())) throw new IOException("input format error"); } } IOException ex = in.ioException(); if (ex != null) throw ex; return vals; }
void parseHeaderLine(String line, KatResult kr) { Scanner lineScanner = new Scanner(line); lineScanner.findInLine(":Skein-(\\d+):\\s*(\\d+)-.*=\\s*(\\d+) bits(.*)"); MatchResult result = null; try { result = lineScanner.match(); } catch (Exception e) { System.out.println("Header line: " + line); e.printStackTrace(); System.exit(1); } kr.stateSize = Integer.parseInt(result.group(1)); kr.hashBitLength = Integer.parseInt(result.group(2)); kr.msgLength = Integer.parseInt(result.group(3)); kr.restOfLine = result.group(4); if ((kr.msgLength == 0) || (kr.msgLength % 8) != 0) kr.msg = new byte[(kr.msgLength >> 3) + 1]; else kr.msg = new byte[kr.msgLength >> 3]; if ((kr.hashBitLength % 8) != 0) kr.result = new byte[(kr.hashBitLength >> 3) + 1]; else kr.result = new byte[kr.hashBitLength >> 3]; kr.msgFill = 0; kr.resultFill = 0; kr.macKeyFill = 0; }
private List<GherkinLineSpan> getSpans(String delimiter) { List<GherkinLineSpan> lineSpans = new ArrayList<GherkinLineSpan>(); Scanner scanner = new Scanner(trimmedLineText).useDelimiter(delimiter); while (scanner.hasNext()) { String cell = scanner.next(); int column = scanner.match().start() + indent() + 1; lineSpans.add(new GherkinLineSpan(column, cell)); } return lineSpans; }
private long stringTimeToLong(String time) { Scanner scanner = new Scanner(time); scanner.findInLine("(\\d+):(\\d+):(\\d+),(\\d+)"); MatchResult result = scanner.match(); int h = Integer.parseInt(result.group(0)); int m = Integer.parseInt(result.group(1)); int s = Integer.parseInt(result.group(2)); int ms = Integer.parseInt(result.group(3)); scanner.close(); return getTimeAsLong(h, m, s, ms); }
private static boolean matchesWinners(File f1, File f2) { boolean res = true; try { Scanner in1 = new Scanner(f1); Scanner in2 = new Scanner(f2); in1.findInLine("This election has (\\d+) Nash equilibria!"); MatchResult result = in1.match(); for (int i = 1; i <= result.groupCount(); i++) { in1.findWithinHorizon("The winner is candidate\\(s\\) (\\w*)", 0); MatchResult winnerResult1 = in1.match(); in2.findWithinHorizon("The winner is candidate\\(s\\) (\\w*)", 0); MatchResult winnerResult2 = in2.match(); res = res && (winnerResult1.group(1).equals(winnerResult2.group(1))); } in1.close(); in2.close(); } catch (IOException e) { e.printStackTrace(); res = false; } return res; }
void parseMacKeyHeaderLine(String line, KatResult kr) { Scanner ls = new Scanner(line); ls.findInLine(".*=\\s*(\\d+) .*"); MatchResult result = null; try { result = ls.match(); } catch (Exception e) { System.out.println("Mac header: " + line); e.printStackTrace(); System.exit(1); } kr.macKeyLen = Integer.parseInt(result.group(1)); kr.macKey = new byte[kr.macKeyLen]; state = MacKey; }
public List<TestConfig> parse() throws FileNotFoundException, IOException { log( "Parser debug output is truncated to a max of " + MAX_LOG_LINE_SIZE + " characters per line."); List<TestConfig> result = new ArrayList<TestConfig>(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); try { String line = reader.readLine(); String functionName = null; int length = -1; while (line != null) { if (isComment(line)) { // Do nothing log("comment: " + line); } else if (isConfigLine(line)) { Scanner scanner = new Scanner(line); try { log("config line: " + line); scanner.findInLine(CONF_PATTERN); MatchResult match = scanner.match(); functionName = match.group(1); log("parsed config functionName: " + functionName); length = Integer.parseInt(match.group(2)); log("parsed config length: " + length); } finally { scanner.close(); } } else { String input = parseInputLine(line, length); log("parsed input line: " + input); TestConfig conf = new TestConfig(functionName, input, length); log("adding " + conf); result.add(conf); } line = reader.readLine(); } } finally { reader.close(); } return result; }
protected int computeAttributes(Rule rule) { int result; String text; Pattern nodePattern; Scanner scanner; MatchResult match; Set<String> attributeBag; System.out.println("COMPUTING ATTRIBUTES"); text = rule.toString(); nodePattern = Pattern.compile("(node[0-9]*_[a-z0-9_]+)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); attributeBag = new HashSet<String>(); scanner = new Scanner(text); while (scanner.findWithinHorizon(nodePattern, text.length()) != null) { match = scanner.match(); attributeBag.add(match.group()); } System.out.println(attributeBag); result = attributeBag.size(); return result; }
protected int computecontextsx(Rule rule) { int result; String text; Pattern nodePattern; Scanner scanner; MatchResult match; Set<String> nodeBag; System.out.println("COMPUTING contextSX"); text = rule.getClassifier().toString(); nodePattern = Pattern.compile("(node[0-9]*)_[a-z0-9_]+", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); nodeBag = new HashSet<String>(); scanner = new Scanner(text); while (scanner.findWithinHorizon(nodePattern, text.length()) != null) { match = scanner.match(); nodeBag.add(match.group(1)); } System.out.println(nodeBag); result = nodeBag.size(); return result; }
public static SimpleDTMC loadFrom(Reader in) throws ParseException { Scanner s = new Scanner(in); String str; Pattern ptnBracket = Pattern.compile("\\[.*"), ptnParamValue = Pattern.compile("\\s*(.*)"), ptnInt = Pattern.compile("\\d+"); str = s.next(); if (!"[Model]".equals(str)) throw new ParseException("Missing Section: Model", 0); int size = -1, init = -1, absorbing = -1; while (s.hasNext() && !s.hasNext(ptnBracket)) { String param = s.next(); str = s.next(); if (!"=".equals(str)) throw new ParseException("Bad assigment: " + param, 0); s.findInLine(ptnParamValue); MatchResult r = s.match(); if (param.equals("size")) size = Integer.parseInt(r.group(1)); else if (param.equals("init")) init = Integer.parseInt(r.group(1)); else if (param.equals("absorbing")) absorbing = Integer.parseInt(r.group(1)); else throw new ParseException("Unknown parameter: " + param, 0); } if (size < 0) throw new ParseException("Missing parameter: size", 0); if (init < 0) throw new ParseException("Missing parameter: init", 0); if (absorbing < 0) absorbing = 0; str = s.next(); if (!"[Transition]".equals(str)) throw new ParseException("Missing Section: Transition", 0); double[][] trans = new double[size][size]; SparseMatrix<String> sm = new SparseMatrix<String>(); s.useDelimiter("[\\s&&[^ ]]+"); for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { if (s.hasNextDouble()) trans[i][j] = s.nextDouble(); else { if (!s.hasNext()) throw new ParseException("Missing Transition Entry" + (i * size + j), 0); trans[i][j] = -1; sm.put(i, j, s.next()); } } } s.useDelimiter("\\s+"); if (s.hasNext() && !s.hasNext(ptnBracket)) throw new ParseException("Extra Transition Entry: " + s.next(), 0); str = s.next(); if (!"[AP]".equals(str)) throw new ParseException("Missing Section: AP", 0); HashMap<String, BitSet> ap = new HashMap<String, BitSet>(); while (s.hasNext() && !s.hasNext(ptnBracket)) { String param = s.next(); str = s.next(); if (!"=".equals(str)) throw new ParseException("Bad assigment: " + param, 0); BitSet bs = new BitSet(size); while (true) { str = s.findInLine(ptnInt); if (str == null) break; bs.set(Integer.parseInt(str)); } ap.put(param, bs); } s.close(); return new SimpleDTMC(trans, sm, size - absorbing, init, ap); }