Search in sources :

Example 31 with PrintStream

use of java.io.PrintStream in project spring-loaded by spring-projects.

the class SpringLoadedTests method runUnguarded.

public Result runUnguarded(Class<?> clazz, String methodname, Object... params) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    PrintStream oldo = System.out;
    PrintStream olde = System.err;
    Object result = null;
    ByteArrayOutputStream oso = new ByteArrayOutputStream();
    ByteArrayOutputStream ose = new ByteArrayOutputStream();
    try {
        if (capture) {
            System.setOut(new PrintStream(oso));
            System.setErr(new PrintStream(ose));
        }
        Object o = clazz.newInstance();
        Method m = null;
        Method[] ms = clazz.getMethods();
        for (Method mm : ms) {
            if (mm.getName().equals(methodname)) {
                m = mm;
                break;
            }
        }
        if (m == null) {
            Assert.fail("Invocation failure: could not find method '" + methodname + "' on type '" + clazz.getName());
        }
        m.setAccessible(true);
        result = m.invoke(o, params);
    } finally {
        if (capture) {
            System.setOut(oldo);
            System.setErr(olde);
        }
    }
    return new Result(result, oso.toString().replace("\r", ""), ose.toString().replace("\r", ""));
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Method(java.lang.reflect.Method) Result(org.springsource.loaded.test.infra.Result)

Example 32 with PrintStream

use of java.io.PrintStream in project spring-loaded by spring-projects.

the class SpringLoadedTests method captureOn.

/**
	 * Start intercepting the System.out/System.err streams
	 */
protected void captureOn() {
    oldo = System.out;
    olde = System.err;
    oso = new ByteArrayOutputStream();
    ose = new ByteArrayOutputStream();
    System.setOut(new PrintStream(oso));
    System.setErr(new PrintStream(ose));
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 33 with PrintStream

use of java.io.PrintStream in project platform_frameworks_base by android.

the class SettingsStateTest method testUpgrade.

/**
     * In version 120, value "null" meant {code NULL}.
     */
public void testUpgrade() throws Exception {
    final File file = new File(getContext().getCacheDir(), "setting.xml");
    file.delete();
    final Object lock = new Object();
    final PrintStream os = new PrintStream(new FileOutputStream(file));
    os.print("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>" + "<settings version=\"120\">" + "  <setting id=\"0\" name=\"k0\" value=\"null\" package=\"null\" />" + "  <setting id=\"1\" name=\"k1\" value=\"\" package=\"\" />" + "  <setting id=\"2\" name=\"k2\" value=\"v2\" package=\"p2\" />" + "</settings>");
    os.close();
    final SettingsState ss = new SettingsState(lock, file, 1, SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED, Looper.getMainLooper());
    synchronized (lock) {
        SettingsState.Setting s;
        s = ss.getSettingLocked("k0");
        assertEquals(null, s.getValue());
        assertEquals("null", s.getPackageName());
        s = ss.getSettingLocked("k1");
        assertEquals("", s.getValue());
        assertEquals("", s.getPackageName());
        s = ss.getSettingLocked("k2");
        assertEquals("v2", s.getValue());
        assertEquals("p2", s.getPackageName());
    }
}
Also used : PrintStream(java.io.PrintStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 34 with PrintStream

use of java.io.PrintStream in project groovy-core by groovy.

the class Java2GroovyProcessor method nodePrinter.

public static String nodePrinter(String input) throws Exception {
    JavaRecognizer parser = getJavaParser(input);
    String[] tokenNames = parser.getTokenNames();
    parser.compilationUnit();
    AST ast = parser.getAST();
    // modify the Java AST into a Groovy AST
    modifyJavaASTintoGroovyAST(tokenNames, ast);
    String[] groovyTokenNames = getGroovyTokenNames(input);
    // groovify the fat Java-Like Groovy AST
    groovifyFatJavaLikeGroovyAST(ast, groovyTokenNames);
    // now output
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Visitor visitor = new NodePrinter(new PrintStream(baos), groovyTokenNames);
    AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
    traverser.process(ast);
    return new String(baos.toByteArray());
}
Also used : PrintStream(java.io.PrintStream) AST(antlr.collections.AST) Visitor(org.codehaus.groovy.antlr.treewalker.Visitor) NodePrinter(org.codehaus.groovy.antlr.treewalker.NodePrinter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AntlrASTProcessor(org.codehaus.groovy.antlr.AntlrASTProcessor) SourceCodeTraversal(org.codehaus.groovy.antlr.treewalker.SourceCodeTraversal)

Example 35 with PrintStream

use of java.io.PrintStream in project groovy-core by groovy.

the class Java2GroovyProcessor method convert.

public static String convert(String filename, String input, boolean withHeader, boolean withNewLines) throws Exception {
    JavaRecognizer parser = getJavaParser(input);
    String[] tokenNames = parser.getTokenNames();
    parser.compilationUnit();
    AST ast = parser.getAST();
    // which is a really nice way of seeing the AST, folding nodes etc
    if ("mindmap".equals(System.getProperty("ANTLR.AST".toLowerCase()))) {
        // uppercase to hide from jarjar
        try {
            PrintStream out = new PrintStream(new FileOutputStream(filename + ".mm"));
            Visitor visitor = new MindMapPrinter(out, tokenNames);
            AntlrASTProcessor treewalker = new PreOrderTraversal(visitor);
            treewalker.process(ast);
        } catch (FileNotFoundException e) {
            System.out.println("Cannot create " + filename + ".mm");
        }
    }
    // modify the Java AST into a Groovy AST
    modifyJavaASTintoGroovyAST(tokenNames, ast);
    String[] groovyTokenNames = getGroovyTokenNames(input);
    // groovify the fat Java-Like Groovy AST
    groovifyFatJavaLikeGroovyAST(ast, groovyTokenNames);
    // now output
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Visitor visitor = new SourcePrinter(new PrintStream(baos), groovyTokenNames, withNewLines);
    AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
    traverser.process(ast);
    String header = "";
    if (withHeader) {
        header = "/*\n" + "  Automatically Converted from Java Source \n" + "  \n" + "  by java2groovy v0.0.1   Copyright Jeremy Rayner 2007\n" + "  \n" + "  !! NOT FIT FOR ANY PURPOSE !! \n" + "  'java2groovy' cannot be used to convert one working program into another" + "  */\n\n";
    }
    return header + new String(baos.toByteArray());
}
Also used : SourcePrinter(org.codehaus.groovy.antlr.treewalker.SourcePrinter) PrintStream(java.io.PrintStream) AST(antlr.collections.AST) PreOrderTraversal(org.codehaus.groovy.antlr.treewalker.PreOrderTraversal) Visitor(org.codehaus.groovy.antlr.treewalker.Visitor) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MindMapPrinter(org.codehaus.groovy.antlr.treewalker.MindMapPrinter) AntlrASTProcessor(org.codehaus.groovy.antlr.AntlrASTProcessor) FileOutputStream(java.io.FileOutputStream) SourceCodeTraversal(org.codehaus.groovy.antlr.treewalker.SourceCodeTraversal)

Aggregations

PrintStream (java.io.PrintStream)1829 ByteArrayOutputStream (java.io.ByteArrayOutputStream)805 Test (org.junit.Test)583 File (java.io.File)331 IOException (java.io.IOException)295 FileOutputStream (java.io.FileOutputStream)207 ArrayList (java.util.ArrayList)89 FileNotFoundException (java.io.FileNotFoundException)84 OutputStream (java.io.OutputStream)81 Before (org.junit.Before)66 BufferedReader (java.io.BufferedReader)53 BufferedOutputStream (java.io.BufferedOutputStream)49 Map (java.util.Map)49 Date (java.util.Date)46 Path (org.apache.hadoop.fs.Path)42 UnsupportedEncodingException (java.io.UnsupportedEncodingException)41 InputStreamReader (java.io.InputStreamReader)38 Matchers.anyString (org.mockito.Matchers.anyString)38 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)36 List (java.util.List)35