Package org.jostraca.util

Examples of org.jostraca.util.RecordingUserMessageHandler


  }


  public void testExceptions() {
    RecordingUserMessageHandler umh = new RecordingUserMessageHandler();
    umh.setThreshold( UserMessageHandler.INFO );
    umh.error( new StandardException( StandardException.CODE_user, "se01") );

    System.out.println( umh.toString() );

    StandardException se02s = new StandardException( StandardException.CODE_user, "se02s" );
    StandardException se02  = new StandardException( StandardException.CODE_user, "se02", se02s );
    umh.error( se02 );

    System.out.println( umh.toString() );

  }
View Full Code Here



  /* Public Methods << */

  public void testOutput() throws Exception {
    RecordingUserMessageHandler rumh = new RecordingUserMessageHandler();
    MessageHandlerOutputStream  out  = new MessageHandlerOutputStream( UserMessageHandler.INFO,  rumh );
    MessageHandlerOutputStream  err  = new MessageHandlerOutputStream( UserMessageHandler.ERROR, rumh );
    PrintStream                 sysOut = System.out;
    PrintStream                 sysErr = System.err;
   
    System.setOut( new PrintStream( out ) );
    System.setErr( new PrintStream( err ) );

    System.out.println( "out01" );
    System.err.println( "err01" );
    System.out.println( "out02" );
    System.out.println( "out03" );
    System.err.println( "err02" );
    System.err.println( "err03" );

    System.setOut( sysOut );
    System.setErr( sysErr );

    assertEquals( "INFO: out01\n"+
                  "INFO: out02\n"+
                  "INFO: out03\n"+
                  "ERROR: err01\n"+
                  "ERROR: err02\n"+
                  "ERROR: err03", rumh.toString().trim() );
  }
View Full Code Here

  public void testExecute() throws Exception {

    String args = "/c echo %JAVA_HOME%";
    ExecutableCommand ec = new ExecutableCommand( "badcmd", args );
    final RecordingUserMessageHandler rumh = new RecordingUserMessageHandler();

    ec.setUserMessageHandler( rumh );
    ec.setActivityDescription("Activity:");
    boolean outcome = ec.execute( new ExecutableCommand.AlternativeExecutableCommands
      (
       new ExecutableCommand( "badcmd2", args ),
       new ExecutableCommand( "", args ) {
           public boolean execute( ExecutableCommand pParent ) {
             iCmdPrefix = "badcmd3";
             return super.execute();
           }
         },
       new ExecutableCommand() {
           public boolean execute( ExecutableCommand pParent ) {
             ExecutableCommand sec = new ExecutableCommand( "subcmd1", "r", "s", this );
             sec.setUserMessageHandler( rumh );
             return sec.execute( new ExecutableCommand.AlternativeExecutableCommands
               (
                new ExecutableCommand( "subcmd2", sec.getCmdRoot(), sec.getCmdSuffix() ),
                new ExecutableCommand( "subcmd3", sec.getCmdRoot(), sec.getCmdSuffix() ),
                new ExecutableCommand( "cmd",     pParent.getCmdRoot() )
                ) );
           }
         }

       ) );


    assertEquals( ("DEBUG: Unable to execute commmand: badcmd /c echo %JAVA_HOME% \n"+
                   "\n"+
                   "DEBUG: Trying alternative commands...\n"+
                   "DEBUG: Unable to execute commmand: badcmd2 /c echo %JAVA_HOME% \n"+
                   "\n"+
                   "DEBUG: Unable to execute commmand: badcmd3 /c echo %JAVA_HOME% \n"+
                   "\n"+
                   "DEBUG: Unable to execute commmand: subcmd1 r s\n"+
                   "\n"+
                   "DEBUG: Trying alternative commands...\n"+
                   "DEBUG: Unable to execute commmand: subcmd2 r s\n"+
                   "\n"+
                   "DEBUG: Unable to execute commmand: subcmd3 r s\n"+
                   "\n"+
                   "DEBUG: Activity:         cmd /c echo %JAVA_HOME%\n"
                   ),
                  rumh.toString( UserMessageHandler.DEBUG ) );
    assertTrue( outcome );
  }
View Full Code Here

    }
    catch( StandardException se ) { throw se; }
    catch( Exception e ) {

      // REVIEW: this is a hack - need to move formatting out of RecordingUserMessageHandler
      RecordingUserMessageHandler rumh = new RecordingUserMessageHandler();
      rumh.error( e );
      throw new JostracaException( rumh.toString() );
    }
  }
View Full Code Here

    }
    catch( StandardException se ) { throw se; }
    catch( Exception e ) {

      // REVIEW: this is a hack - need to move formatting out of RecordingUserMessageHandler
      RecordingUserMessageHandler rumh = new RecordingUserMessageHandler();
      rumh.error( e );
      throw new JostracaException( rumh.toString() );
    }
  }
View Full Code Here

    PropertySet tmps       = template.getMergedPropertySet();

    boolean     successful = false;
    PrintStream sysOut     = null;
    PrintStream sysErr     = null;
    RecordingUserMessageHandler rumh = new RecordingUserMessageHandler();

    try {
      String   cw   = tmps.get( Property.main_CodeWriter );
      String   cwo  = makeCodeWriterOptions( tmps, template );
      String[] args = ArgUtil.splitQuoted( cwo );
      String   cwp  = getCodeWriterPath( tmps );

      Properties jythonProps = new Properties();
      jythonProps.put( "python.home", tmps.get("jython.home") );
      PythonInterpreter.initialize( System.getProperties(), jythonProps, args );

      PyStringMap       dict  = new PyStringMap();
      PySystemState     pysys = new PySystemState();
      PythonInterpreter pi    = new PythonInterpreter( dict, pysys );


      // DO NOT use CommandLineUserMessageHandler here - causes infinite recursion
      MessageHandlerOutputStream  out  = new MessageHandlerOutputStream( UserMessageHandler.INFO,  rumh );
      MessageHandlerOutputStream  err  = new MessageHandlerOutputStream( UserMessageHandler.ERROR, rumh );

      // space out dots to avoid accidental replace on System DOT out search
      sysOut = System . out;
      sysErr = System . err;
   
      // set context if not null
      Object context = template.getContext();
      if( null != context ) {
        pi.set( "context", context );
      }

      System.setOut( new PrintStream( out ) );
      System.setErr( new PrintStream( err ) );

      pi.execfile( cwp );
     
      String jythonexec
        = "cw = "+cw+"()\n"
        + (null!=context?"cw._setContext( context )\n":"")
        + "cw.main( sys.argv )\n"
        + "result = cw._getResult()\n"
        ;
      pi.exec( jythonexec );
      WayPointRecorder.add( BasicWayPoint.ExecutingCodeWriter.make( template.getCodeWriterPath().getAbsolutePath() ) );

      Object result = pi.get("result");

      if( result instanceof Integer ) {
        successful = ( 0 == ((Integer)result).intValue() );
      }
      else {
        successful = true;
      }

      template.setResult( result );
    }

    // FIX: need a util to handle general exceptions and just rethrow StandardExceptions
    catch( StandardException se ) {
      throw se;
    }
    catch( Exception e ) {
      throw new ProcessException( e );
    }
    finally {
      if( null != sysOut ) {
        System.setOut( sysOut );
      }
      if( null != sysErr ) {
        System.setErr( sysErr );
      }

      String out = rumh.toString( UserMessageHandler.INFO, false );
      if( !TextUtil.isEmptyOrNull( out ) ) {
        // REVIEW: need to do this a better way so that it is automatic
        parseOutResult( out, template );
        mUserMessageHandler.info( out );
      }

      String err = rumh.toString( UserMessageHandler.ERROR, false );
      if( !TextUtil.isEmptyOrNull( err ) ) {
        mUserMessageHandler.info( err );
      }

    }
View Full Code Here

    ps.set( Property.main_CompileCodeWriter, Standard.YES );
    ps.set( Property.main_ExecuteCodeWriter, Standard.YES );
    g.setCmdPropertySet( ps );
    Template tm = g.generate( FileUtil.findFile( "org/jostraca/test/simple.jtm" ) );

    RecordingUserMessageHandler rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    assertEquals( 7, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    simple_txt = new File( "simple.txt" );
    System.out.println(simple_txt.getAbsolutePath());
    assertTrue( simple_txt.exists() );
View Full Code Here

    ps.set( Property.main_CompileCodeWriter, Standard.YES );
    ps.set( Property.main_ExecuteCodeWriter, Standard.YES );
    g.setCmdPropertySet( ps );
    g.generate( new File( "src/org/jostraca/test/parse.jtm" ) );

    RecordingUserMessageHandler rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    //System.out.println( rumh );
    assertEquals( 7, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    parse_txt = new File( "parse.txt" );
    assertTrue( parse_txt.exists() );

    assertEquals( "parse: parse", FileUtil.readFile( parse_txt ).trim() );
View Full Code Here

    if( internal_txt.exists() ) {
      internal_txt.delete();
    }

    // just compile first
    RecordingUserMessageHandler rumh = null;
    Generator   g  = new Generator();
    PropertySet ps = new PropertySet();
    ps.set( Property.main_MakeBackup,         Standard.NO  );
    ps.set( Property.main_CompileCodeWriter,  Standard.YES );
    ps.set( Property.main_ExecuteCodeWriter,  Standard.NO  );
    g.setCmdPropertySet( ps );

    g.generate( new File( "src/org/jostraca/test/internal.jtm" ) );
    rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    //System.out.println( rumh );
    assertEquals( 6, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 1, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    internal_txt = new File( "internal.txt" );
    assertTrue( !internal_txt.exists() );

    // and now execute
    ps.set( Property.main_CompileCodeWriter,    Standard.EMPTY );
    ps.set( Property.main_ExecuteCodeWriter,    Standard.YES  );

    g.setCmdPropertySet( ps );
    g.generate( new File( "src/org/jostraca/test/internal.jtm" ), new RecordingUserMessageHandler() );

    rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    //System.out.println( rumh );
    assertEquals( 6, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    assertTrue( internal_txt.exists() );
    assertEquals( "internal: internal", FileUtil.readFile( internal_txt ).trim() );
  }
View Full Code Here

    if( context_txt.exists() ) {
      context_txt.delete();
    }

    // just compile first
    RecordingUserMessageHandler rumh = null;
    Generator   g  = new Generator();
    PropertySet ps = new PropertySet();
    ps.set( Property.main_MakeBackup,         Standard.NO  );
    ps.set( Property.main_CompileCodeWriter,  Standard.YES );
    ps.set( Property.main_ExecuteCodeWriter,  Standard.NO  );
    g.setCmdPropertySet( ps );
   
    String c = "context";
    g.setContext( c );

    g.generate( new File( "src/org/jostraca/test/context.jtm" ) );
    rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    //System.out.println( rumh );
    assertEquals( 6, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 1, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    context_txt = new File( "context.txt" );
    assertTrue( !context_txt.exists() );

    // and now execute
    ps.set( Property.main_CompileCodeWriter,    Standard.EMPTY );
    ps.set( Property.main_ExecuteCodeWriter,    Standard.YES  );

    g.setCmdPropertySet( ps );
    g.generate( new File( "src/org/jostraca/test/context.jtm" ), new RecordingUserMessageHandler() );

    rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    //System.out.println( rumh );
    assertEquals( 6, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    assertTrue( context_txt.exists() );
    assertEquals( "context: context", FileUtil.readFile( context_txt ).trim() );
  }
View Full Code Here

TOP

Related Classes of org.jostraca.util.RecordingUserMessageHandler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.