How to create java Arrays in TestComplete?
I am trying to use this java class http://wush.net/svn/mindprod/com/mindprod/csv/CSVSort.java in my test automation to sort a csv file.
I am using JScript as my scripting lang.
Here is the info about the parameters of the constructor of this class
* Param fileBeingProcessed CSV file to be packed to remove excess space and quotes.
* Param sortCols array of 0-based cols to sort on.
* Param sortTypes array of chars with letters s i n x to tell how to sort each column.
* Param isAscendings array of sort direction, true=ascending, false = descending.
* Param separatorChar field separator character, usually ',' in North America,
* ';' in Europe and sometimes '\t' for
* tab.
* Param quoteChar char to use to enclose fields containing a separator, usually '\"'. Use (char)0 if
* you don't want a quote character.
* Param commentChar char to use to introduce comments. Use (char) 0 if none. Only one character allowed.
* Param encoding encoding for input and output.
*
* @throws java.io.IOException if problems reading/writing file
*/
/*
CSVSort( final File fileBeingProcessed,
final int[] sortCols,
final char[] sortTypes,
final boolean[] isAscendings,
final char separatorChar,
final char quoteChar,
final char commentChar,
final Charset encoding )
*/
here is how I am calling it
function sortCSV(){
csvPath = Project.path + "\\test data\\SendToSAP\\csvFile.csv"
// this line gives me the java worker process
var jwp = getJwpProc();
// create a Java File
var file = jwp.JavaRuntime().JavaClasses.java_io.File.newInstance(csvPath);
//create a charset
var charset = jwp.JavaRuntime().JavaClasses.java_nio_charset.Charset.forName("UTF-8");
var arrCols = new Array(31,35);
var arrChars = new Array('i','i');
var arrBool = new Array(aqConvert.VarToBool("true"),aqConvert.VarToBool("true"));
CSVSort = JavaClasses.com_mindprod_csv.CSVSort.newInstance(file,arrCols,arrChars,arrBool,',','\"','#', charset);
Log.Message("done");
}
when I run it, it gives me error with msg "invalid procedure or argument"
I think I need to pass Java Arrays to the function instead of Jscript array, not sure what the syntax would be for that. any help is greatly appreciated.
amitbansal wrote:when I run it, it gives me error with msg "invalid procedure or argument"
I think I need to pass Java Arrays to the function instead of Jscript array, not sure what the syntax would be for that. any help is greatly appreciated.
You can create Java arrays using the java.lang.reflect.Array class. You need to add the following classes to project properties > Java Bridge > Java Classes:
java.lang.reflect.Array
java.lang.Class
Here's how you can create an integer array with 2 elements:
var intType = JavaClasses.java_lang.Class.forName("java.lang.Integer"); var arrCols = JavaClasses.java_lang_reflect.Array.newInstance_2(intType, 2); arrCols.Items(0) = 31; arrCols.Items(1) = 35;
Char and bool arrays can be created similarly - as arrays of types java.lang.Character and java.lang.Boolean.