BeanShellPlugin.java :  » Test-Coverage » salome-tmf » salomeTMF_plug » beanshell » Java Open Source

Java Open Source » Test Coverage » salome tmf 
salome tmf » salomeTMF_plug » beanshell » BeanShellPlugin.java
/*
 * SalomeTMF is a Test Management Framework
 * Copyright (C) 2005 France Telecom R&D
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * @author Fayal SOUGRATI, Vincent Pautret, Marche Mikael
 *
 * Contact: mikael.marche@rd.francetelecom.com
 */


package salomeTMF_plug.beanshell;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;

import org.java.plugin.Plugin;
import org.java.plugin.PluginDescriptor;
import org.java.plugin.PluginManager;
import org.objectweb.salome_tmf.api.Util;
import org.objectweb.salome_tmf.data.Script;
import org.objectweb.salome_tmf.ihm.main.SalomeTMFContext;
import org.objectweb.salome_tmf.plugins.PluginClassLoader;
import org.objectweb.salome_tmf.plugins.core.ScriptEngine;
import org.objectweb.salome_tmf.plugins.core.TestDriver;

import bsh.NameSpace;
/**
 *
 * @author  marchemi
 */
public class BeanShellPlugin extends Plugin implements  TestDriver, ScriptEngine{
  
  bsh.Interpreter interp = null;
  bsh.Interpreter edit_inter = null;
  //ParentClassLoader pParentClassLoader = null;
  PluginClassLoader pParentClassLoader = null;
  String tmpLog = "";
  String testDriverArg = "";
  org.objectweb.salome_tmf.data.AutomaticTest pTest = null;
  NameSpace pGlobalNameSpace;
  NameSpace pTestNameSpace;
  NameSpace pEditNameSpace = null;
  
  boolean runENV_SCRIPT = false;
  
  String url_txt = null;
  
  /********************* extends Plugin**********************************/
  
  /** Creates a new instance of BeanShellPlugin
   * @param manager
   * @param descr
   */
  public BeanShellPlugin(PluginManager manager, PluginDescriptor descr) {
    super(manager, descr);
  }
  
  /**
   * @see org.java.plugin.Plugin()
   */
  protected void doStart() throws Exception {
    // no-op
    String _urlSalome = SalomeTMFContext.getInstance().getUrlBase().toString();
    url_txt = _urlSalome.substring(0,_urlSalome.lastIndexOf("/"));
    
    Util.log("[BeanShellPlugin->doStart] : interp = new bsh.Interpreter(); ]");
    interp = new bsh.Interpreter();
    
    Util.log("[BeanShellPlugin->doStart] : interp.setExitOnEOF(false);]");
    interp.setExitOnEOF(true);
    
    Util.log("[BeanShellPlugin->doStart] : set bsh.system.shutdownOnExit = false]");
    interp.set("bsh.system.shutdownOnExit", false);
    
    pGlobalNameSpace = interp.getNameSpace();
    Util.log("[BeanShellPlugin->doStart] : load salome_testcmd.bsh in " + interp);
    loadCommand("salome_testcmd.bsh", interp);
    Util.log("[BeanShellPlugin->doStart] : Create parent Class Loader ");
    //pParentClassLoader = new ParentClassLoader(this.getClass().getClassLoader());
    pParentClassLoader = SalomeTMFContext.getInstance().getSalomeClassLoader();
    Util.log("[BeanShellPlugin->doStart] : set parent Class Loader "+ pParentClassLoader+" to " + interp);
    interp.set("salome_classloder", pParentClassLoader);
    
    
  }
  
  /**
   * @see org.java.plugin.Plugin()
   */
  protected void doStop() throws Exception {
    // no-op
  }
  
  /*********************** Interface TestDriver ****************************/
  
  /**
   * @see salome.plugins.core.TestDriver
   */
  public void initTestDriver(URL urlSalome){
    Util.log("Init test driver");
    String _urlSalome = urlSalome.toString();
    url_txt = _urlSalome.substring(0,_urlSalome.lastIndexOf("/"));
  }
  
  
  
  /**
   * @see salome.plugins.core.TestDriver
   */  
  public void getHelp(){
    Util.log("BeanShell TestDriver");
  }
  
  /**
   * @see salome.plugins.core.TestDriver
   */  
  public String getDefaultTestDiverAgument(){
    return testDriverArg;
  }
  /**
   * @see salome.plugins.core.TestDriver
   */  
  public String modifyTestDiverAgument(String oldArg){
    return testDriverArg;
  }
  
  /**
   * @see salome.plugins.core.TestDriver
   */
  public int runTest(String testScript, org.objectweb.salome_tmf.data.AutomaticTest pTest, Hashtable argTest, String plugArg)  throws Exception{
    if (argTest != null) {
      interp.set("testargs", argTest);
      Enumeration e = argTest.keys();
      while (e.hasMoreElements()){
        Object o = e.nextElement();
        String key = o.toString();
        
        //String key = (String) e.nextElement();
        Object value = argTest.get(o);
        if (value instanceof java.lang.Boolean){
          interp.set(key, ((java.lang.Boolean)value).booleanValue());
        }else {
          interp.set(key, value);
        }
        Util.log("[BeanShellPlugin->runTest] : set arg : " + key + " = " + value + " ]");
      }
    }
    //interp.eval(testScript);
    //interp.set("test", pTest);    
    pTestNameSpace = new NameSpace(interp.getNameSpace(), "testNS");
    Object res = interp.eval("source(\""+testScript+"\")", pTestNameSpace);
    Util.log("[BeanShellPlugin->runTest] : res eval =  : " +  res +  " ]");
    tmpLog = (String)interp.get("testLog");
    String verdict = (String)interp.get("Verdict");
    verdict = verdict.toLowerCase();
    verdict.trim();
    Util.log("[BeanShellPlugin->runTest] verdict get : " +  verdict);
    pTestNameSpace.clear();
    if (verdict.equals("")){
      Util.log("[BeanShellPlugin->runTest] return pass");
      return 0;
    } else if (verdict.equals("pass")){
      Util.log("[BeanShellPlugin->runTest] return pass");
      return 0;
    } else if (verdict.equals("fail")){
      Util.log("[BeanShellPlugin->runTest] return fail");
      return 1;
    } else if (verdict.equals("unconclusif")){
      Util.log("[BeanShellPlugin->runTest] return unconclusif");
      return 2;
    }        
    Util.log("[BeanShellPlugin->runTest] default return unconclusif for "+  verdict);
    return 2; 
  }
  
  /**
   * @see salome.plugins.core.TestDriver
   */
  public String getTestLog(){
    Util.log("[BeanShellPlugin->getTestLog] : return =  : " +  tmpLog +  " ]");
    return tmpLog;
  }
  
  /**
   * @see salome.plugins.core.TestDriver
   */
  public void stopTest()  throws Exception{
    pTestNameSpace.clear();
    interp.eval("exit();");
  }
  
  /**
   * Odre d'dition du test
   */
  public void editTest(String testScript, org.objectweb.salome_tmf.data.AutomaticTest pTest, Hashtable arg, String plugParam) throws Exception{
    setCommands();
    /*
     if (edit_inter == null){
     edit_inter =  new bsh.Interpreter();
     URL pURL;
     //pURL = BeanShellPlugin.class.getResource("salome/plugins/beanshell/desktop.bsh");  
      edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_workspaceEditor.bsh\");");
      edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_desktop.bsh\");");
      edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_makeWorkspace.bsh\");");
      }*/
    if (arg != null) {
      Enumeration e = arg.keys();
      interp.set("testargs", arg);
      while (e.hasMoreElements()){
        Object o = e.nextElement();
        String key = o.toString();
        //String key = (String) e.nextElement();
        Object value = arg.get(o);
        if (value instanceof java.lang.Boolean){
          edit_inter.set(key, ((java.lang.Boolean)value).booleanValue());
        }else {
          edit_inter.set(key, value);
        }
        Util.log("[BeanShellPlugin->editTest] : set arg : " + key + " = " + value + " ]");
      }
    }
    edit_inter.set("salom_TestObject", pTest);   
    pEditNameSpace = new NameSpace(edit_inter.getNameSpace(), "editNS");
    
    if (pEditNameSpace == null){
      //pEditNameSpace = new NameSpace(pGlobalNameSpace, "editNS");
      pEditNameSpace = new NameSpace(edit_inter.getNameSpace(), "editNS");
    } else {
      pEditNameSpace.clear();
      //edit_inter.set("salome_ns", edit_inter.getNameSpace());
      //pEditNameSpace = new NameSpace(edit_inter.getNameSpace(), "editNS");
    }
    //edit_inter.set("salome_ns", edit_inter.getNameSpace());
    
    
    Util.log("[BeanShellPlugin->editTest] : call salome_desktop for : " + testScript  + " ]");
    edit_inter.eval("salome_desktop(\""+ testScript+ "\");", pEditNameSpace);
    Util.log("[BeanShellPlugin->editTest] : return ]");
    
    //pEditNameSpace.clear();
    
  }
  
  /**
   * Choix d'un test
   * @return le test choisi
   */
  public java.io.File choiceTest(org.objectweb.salome_tmf.data.AutomaticTest pTest){
    GuiScriptChoice bshChoice = new GuiScriptChoice( SalomeTMFContext.getInstance().getSalomeFrame() );
    return bshChoice.getSelectedFile();
    /*java.io.File file = null;
    javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
    fileChooser.setApproveButtonText("Valider");
    fileChooser.addChoosableFileFilter(new BeanShellFileFilter("BeanShell",".bsh"));
    int returnVal = fileChooser.showOpenDialog(null);
    if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
      file = fileChooser.getSelectedFile();
    }
    return file;*/
  }
  
  public void onDeleteTestScript(org.objectweb.salome_tmf.data.AutomaticTest pTest){
    
  }
  
  public void updateTestScriptFromImport(String testScript, org.objectweb.salome_tmf.data.AutomaticTest pTest){
    
  }
  /************************** Interface ScriptEngine **********************************/
  
  
  
  /**
   * @see salome.plugins.core.ScriptEngine
   */
  public void initScriptEngine(URL urlSalome){
    Util.log("Init test driver");
    String _urlSalome = urlSalome.toString();
    url_txt = _urlSalome.substring(0,_urlSalome.lastIndexOf("/"));
  }
  
  /**
   * Lancemement du test.
   * @param type de script ENV_SCRIPT, PRE_SCRIPT, POST_SCRIPT
   * @return Valeur de terminaison du script (0 : OK, <>0 code d'erreur)
   */
  public int runScript(int type,  String urlScriptFile, Script pScript, Hashtable arg, String plugParam)  throws Exception{
    if (type == ENV_SCRIPT) {
      Util.log("[BeanShellPlugin->runScript] : clearNameSpace in ENV_SCRIPT ]");
      interp.getNameSpace().clear();
      loadCommand("salome_testcmd.bsh", interp);
      //loadCommand("addJar.bsh", interp);
      interp.set("salome_classloder", pParentClassLoader);
      runENV_SCRIPT = true;
    } else if (type == PRE_SCRIPT && runENV_SCRIPT == false) {
      Util.log("[BeanShellPlugin->runScript] : clearNameSpace in PRE_SCRIPT ]");
      interp.getNameSpace().clear();
      loadCommand("salome_testcmd.bsh", interp);
      //loadCommand("addJar.bsh", interp);
      interp.set("salome_classloder", pParentClassLoader);
    }
    try{
      if (arg != null) {
        Enumeration e = arg.keys();
        while (e.hasMoreElements()){
          Object o = e.nextElement();
          Util.log("key elements is : " + o);
          String key = o.toString();
          Object value = arg.get(o);
          if (value instanceof java.lang.Boolean){
            interp.set(key, ((java.lang.Boolean)value).booleanValue());
          }else {
            interp.set(key, value);
          }
          Util.log("[BeanShellPlugin->runScript] : set arg : " + key + " = " + value + " ]");
        }
      }
    }catch(Exception e){
      e.printStackTrace();
      throw e;
    }
    interp.set("script", pScript);
    Object res = interp.eval("source(\""+urlScriptFile+"\")");
    Util.log("[BeanShellPlugin->runScript] : res eval =  : " +  res +  " ]");
    
    if (type == POST_SCRIPT) {
      Util.log("[BeanShellPlugin->runScript] : clearNameSpace in POST_SCRIPT ]");
      interp.getNameSpace().clear();
      loadCommand("salome_testcmd.bsh", interp);
      //loadCommand("addJar.bsh", interp);
      interp.set("salome_classloder", pParentClassLoader);
      runENV_SCRIPT = false;
    }
    return  0;
  }
  
  /**
   * Odre d'dition du script
   * @param type de script ENV_SCRIPT, PRE_SCRIPT, POST_SCRIPT
   */
  public void editScript(String urlScriptFile, int type,  Script pScript, Hashtable arg, String plugParam) throws Exception {
    setCommands();
    /*if (edit_inter == null){
     edit_inter =  new bsh.Interpreter();
     URL pURL;
     //pURL = BeanShellPlugin.class.getResource("salome/plugins/beanshell/desktop.bsh");  
      edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_workspaceEditor.bsh\");");
      edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_desktop.bsh\");");
      edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_makeWorkspace.bsh\");");
      }*/
    if (arg != null) {
      Enumeration e = arg.keys();
      while (e.hasMoreElements()){
        Object o = e.nextElement();
        String key = o.toString();
        
        //String key = (String) e.nextElement();
        Object value = arg.get(o);
        if (value instanceof java.lang.Boolean){
          edit_inter.set(key, ((java.lang.Boolean)value).booleanValue());
        }else {
          edit_inter.set(key, value);
        }
        Util.log("[BeanShellPlugin->editScript] : set arg : " + key + " = " + value + " ]");
      }
    }
    if (pEditNameSpace == null){
      pEditNameSpace = new NameSpace(edit_inter.getNameSpace(), "editNS");
    } else {
      pEditNameSpace.clear();
    }
    edit_inter.set("script", pScript); 
    Util.log("[BeanShellPlugin->editScript] : call salome_desktop for : " + urlScriptFile  + " ]");
    edit_inter.eval("salome_desktop(\""+ urlScriptFile+ "\");", pEditNameSpace);
    Util.log("[BeanShellPlugin->editScript] : return ]");
  }
  
  /**
   * This method is called when user want to modify/set free arg of plugin script
   * @return a string represented arg for test the testDriver 
   */
  public String modifyEngineAgument(Script pScript){
    return "";
  }
  
  public void stopScript()  throws Exception {
    interp.eval("exit()");
  }
  
  public String getScriptLog(){
    return "";
  }
  //************************ Interface Common ***************************************/
  
  private void setCommands() throws Exception{
    if (edit_inter == null){
      edit_inter =  new bsh.Interpreter();
      loadCommand("salome_desktop.bsh", edit_inter);
      loadCommand("salome_makeWorkspace.bsh", edit_inter);
      loadCommand("salome_workspaceEditor.bsh", edit_inter);
      loadCommand("salome_testcmd.bsh", edit_inter);
      edit_inter.set("salome_ns", edit_inter.getNameSpace());
      //loadCommand("addJar.bsh", edit_inter);
      edit_inter.set("salome_classloder", pParentClassLoader);
    }
    /*edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_workspaceEditor.bsh\");");
     edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_desktop.bsh\");");
     edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_makeWorkspace.bsh\");");
     */
  }
  
  private void loadCommand(String cmd, bsh.Interpreter pInterpreter) throws Exception{
    InputStream in = null;
    
    try {
      URL url = new URL(url_txt + "/plugins/beanshell/commands/" + cmd);
      Util.log("[BeanShellPlugin->loadCommand] : command url is  : " + url);
      in = url.openStream();
      
    } catch(Exception e) {
      e.printStackTrace();
      Util.log("[BeanShellPlugin->loadCommand] Error when loading URL try to load jar commands" );
      try {
        in = BeanShellPlugin.class.getResourceAsStream("/plugins/beanshell/commands/" + cmd);
      } catch(Exception e2) {
        Util.log("[BeanShellPlugin->loadCommand] Error when loading ResourceAsStream" );
        e2.printStackTrace();
        throw e2;
      }
    }
    InputStreamReader reader = new InputStreamReader(in);
    pInterpreter.eval(reader);
    reader.close();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.