Service Virtualization

  • 1.  Observered static key word issue in javascript element in DeveTest9.0

    Posted Feb 14, 2017 11:47 PM

    Hi Team,

    I am using JavaScript component of DevTest and wirting java code to read one txt file but i am getting static keyword error
    Could anyone help me in resolving below static error:

     

    ============================================================================
    | Error in Script
    ============================================================================
    | Step: Parse To CurrentDate
    ----------------------------------------------------------------------------
    | Message: Sourced file: inline evaluation of: ``import java.io.*; import java.io.BufferedReader; import java.io.File; import . . . '' : Typed variable declaration : Can't declare static variable outside of class: filenames
    ----------------------------------------------------------------------------
    | Trapped Exception: Sourced file: inline evaluation of: ``import java.io.*; import java.io.BufferedReader; import java.io.File; import . . . '' : Typed variable declaration : Can't declare static variable outside of class: filenames
    | Trapped Message: Sourced file: inline evaluation of: ``import java.io.*; import java.io.BufferedReader; import java.io.File; import . . . '' : Typed variable declaration : Can't declare static variable outside of class: filenames : at Line: 9 : in file: inline evaluation of: ``import java.io.*; import java.io.BufferedReader; import java.io.File; import . . . '' : static File [ ] filenames = null

    ----------------------------------------------------------------------------

     

    if i copy the same java code in some other system which is LISA configured, then it works 

    Dont know whats the problem

     

    Please help



  • 2.  Re: Observered static key word issue in javascript element in DeveTest9.0

    Broadcom Employee
    Posted Feb 15, 2017 01:06 AM

    Can you please paste the code you are seeing the issue with?

     

    If you just need to read a text file - there are other mechanism in DevTest for the same.



  • 3.  Re: Observered static key word issue in javascript element in DeveTest9.0

    Posted Feb 15, 2017 03:41 AM

    Below is the code:

    Here i want to parse the date into current date so i am writing the code:

    if i remove static and then run through Lisa, then it is making all the file blank, but same code is working in eclipse IDE.


    import java.io.*;
    import java.util.Date;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    static File[] filenames = null;
    try
    {

    File file = new File("C:\\Lisa\\Projects\\Parse");
    filenames = file.listFiles();
    for(File file1 : filenames )
    {
    BufferedReader reader = new BufferedReader(new FileReader(file1));
    String line = "";
    String oldtext = "";
    while((line = reader.readLine()) != null)
    {
    oldtext += line + "\r\n";
    }
    reader.close();

    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    Calendar c=Calendar.getInstance();
    Date d=c.getTime();
    String Track_date=format.format(d);
    String newtext = oldtext.replaceAll("@@CURRENT_DATE@@", Track_date);
    System.out.println("CHkText"+newtext);

    FileWriter writer = new FileWriter(file1);
    writer.write(newtext);
    writer.close();

    }

    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }



  • 4.  Re: Observered static key word issue in javascript element in DeveTest9.0
    Best Answer

    Posted Feb 15, 2017 12:31 PM

    I moved the array inside the try / catch and used a JSR-223 (Beanshell) Step. I was able to iterate over a set of files and replace @@CURRENT_DATE@@ with today's date in one of my files. Is there a reason this will not work for you?  

     

    import java.io.*;
    import java.util.Date;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;

    try {
       File file = new File("C:\\Lisa\\Projects\\Parse");
       File[] filenames = file.listFiles();

       for(File file1 : filenames ) {
          BufferedReader reader = new BufferedReader(new FileReader(file1));
          String line = "";
          String oldtext = "";
          while((line = reader.readLine()) != null) {
             oldtext += line + "\r\n";
          }

          reader.close();

          SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
          Calendar c=Calendar.getInstance();
          Date d=c.getTime();
          String Track_date=format.format(d);

          String newtext = oldtext.replaceAll("@@CURRENT_DATE@@", Track_date);
          System.out.println("CHkText"+newtext);

          FileWriter writer = new FileWriter(file1);
          writer.write(newtext);
          writer.close();
       }
    } catch (Exception e) {
       e.printStackTrace();
    }
    return 0;



  • 5.  Re: Observered static key word issue in javascript element in DeveTest9.0

    Posted Feb 22, 2017 01:51 PM

    Sabir,

     

    Did Joel's recommendation work for you?

     

    Regards,

    Reid



  • 6.  Re: Observered static key word issue in javascript element in DeveTest9.0

    Posted Feb 22, 2017 11:07 PM

    Yes, its working for me for that specific requirement.

     

    But reading a file and parsing to current date is small scenario,

    As i too have large scenarios where i often use static keyword to maintain the scope of code.

     

    So that time it throws an error" Can't declare static variable outside of class: filenames".