TestNGclass Test2 having two test cases which has to run with the same set of the data So I am passing the same dataprovider to both cases which is define in another TestNg class Test1.
It is running fine when i am not passing the data from excel
public class Test1 {
static String filepath = new File("").getAbsolutePath();
#DataProvider(name = "sample")
public static Object[][] readdata(Method m) throws Exception {
String name = m.getName();
System.out.println("NAME===>" + name);
String[][] locationdata = null;
String[][] finallocation = null;
if ("runlocation".equals(name)) {
locationdata = XLSXReader.getData(filepath + "//src//test//java//com//Myname//dataset//Sample.xlsx", "Sheet1");
finallocation = new String[locationdata.length - 1][2];
for (int i = 1; i < locationdata.length; i++) {
String[] s = locationdata[i];
for (int j = 0; j < s.length; j++) {
finallocation[i - 1][j] = s[j];
// System.out.println(finallocation[i-1][j]);
}
}
return finallocation;
} else if ("slocation".equals(name)) {
String[][] secondlocationdata = XLSXReader.getData(filepath + "//src//test//java//com//Myname//dataset//Sample.xlsx", "Sheet2");
String[][] location = new String[secondlocationdata.length - 1][2];
for (int i = 1; i < secondlocationdata.length; i++) {
String[] s = secondlocationdata[i];
for (int j = 0; j < s.length; j++) {
location[i - 1][j] = s[j];
// System.out.println(finallocation[i-1][j]);
}
}
return location;
}
return finallocation;
}
}
public class Test2 {
static String filepath = new File("").getAbsolutePath();
#Test(dataProvider = "sample", dataProviderClass = Test1.class)
public void runlocation(String from, String to) {
System.out.println("From-->" + from + ", TO-->" + to);
}
#Test(dataProvider = "sample", dataProviderClass = Test1.class)
public void slocation(String from, String to) {
System.out.println("#####From-->" + from + ", ######TO-->" + to);
}
}
I'm writing a Java LWJGL 3D game-engine. I decided to rewrite my mesh class and the .obj loader. The mesh class works fine when putting in data manually, but wehn loading from an .obj-file it gives some strange results: (it's supposed to be a dragon but lokks like a 2D ... something)
public static Mesh loadMesh(String fileName) throws IOException
{
String splitArray[] = fileName.split("\\.");
String ext = splitArray[splitArray.length-1];
if(!ext.equals("obj"))
System.err.println("Error: Engine can only load .obj files, try converting the file: " + fileName);
ArrayList<Vector3f> vertices = new ArrayList<Vector3f>();
ArrayList<Integer> vindices = new ArrayList<Integer>();
ArrayList<Integer> tindices = new ArrayList<Integer>();
ArrayList<Integer> nindices = new ArrayList<Integer>();
ArrayList<Vector3f> normals = new ArrayList<Vector3f>();
ArrayList<Vector2f> texCoords = new ArrayList<Vector2f>();
BufferedReader reader = new BufferedReader(new FileReader("./res/models/"+fileName));
String line = "";
while((line=reader.readLine())!=null)
{
String[] p = line.split(" ");
if(line.startsWith("v"))
{
vertices.add(new Vector3f(Float.valueOf(p[1]),
Float.valueOf(p[2]),
Float.valueOf(p[3])));
}
if(line.startsWith("vn"))
{
normals.add(new Vector3f(Float.valueOf(p[1]),
Float.valueOf(p[2]),
Float.valueOf(p[3])));
}
if(line.startsWith("vt"))
{
texCoords.add(new Vector2f(Float.valueOf(p[1]),
Float.valueOf(p[2])));
}
if(line.startsWith("f"))
{
String[] arg1 = p[1].split("/");
String[] arg2 = p[2].split("/");
String[] arg3 = p[3].split("/");
vindices.add(Integer.parseInt(arg1[0]));
if(arg1.length>1)
tindices.add(Integer.parseInt(arg1[1]));
if(arg1.length>2)
nindices.add(Integer.parseInt(arg1[3]));
vindices.add(Integer.parseInt(arg2[0]));
if(arg1.length>1)
tindices.add(Integer.parseInt(arg2[1]));
if(arg2.length>2)
nindices.add(Integer.parseInt(arg2[3]));
vindices.add(Integer.parseInt(arg3[0]));
if(arg1.length>1)
tindices.add(Integer.parseInt(arg3[1]));
if(arg3.length>2)
nindices.add(Integer.parseInt(arg3[3]));
}
}
float[] vdata = new float[vertices.size() * 3];
float[] tdata = new float[texCoords.size() * 2];
float[] ndata = new float[normals.size() * 3];
for(int i = 0; i < vdata.length; i++)
{
vdata[i] = vertices.get(Integer.valueOf(vindices.get(i))).getX();
vdata[i++] = vertices.get(Integer.valueOf(vindices.get(i))).getY();
vdata[i++] = vertices.get(Integer.valueOf(vindices.get(i))).getZ();
}
for(int i = 0; i < ndata.length; i++)
{
ndata[i] = normals.get(Integer.valueOf(nindices.get(i))).getX();
ndata[i++] = normals.get(Integer.valueOf(nindices.get(i))).getY();
ndata[i++] = normals.get(Integer.valueOf(nindices.get(i))).getZ();
}
for(int i = 0; i < tdata.length; i++)
{
tdata[i] = texCoords.get(Integer.valueOf(tindices.get(i))).getX();
tdata[i++] = texCoords.get(Integer.valueOf(tindices.get(i))).getY();
}
return new Mesh(vdata, tdata, ndata);
}
thats my .obj-file loader. Can't see what is wrong...
Upon closer inspection there are some nice bugs:
vindices.add(Integer.parseInt(arg1[0]));
if(arg1.length>1)
tindices.add(Integer.parseInt(arg1[1]));
if(arg1.length>2)
nindices.add(Integer.parseInt(arg1[3])); // this should be 2 for the normals
and
for(int i = 0; i < vdata.length; i++)
{
vdata[i] = vertices.get(Integer.valueOf(vindices.get(i))).getX(); //i=0
vdata[i++] = vertices.get(Integer.valueOf(vindices.get(i))).getY(); //i=0 and counted up afterwards
vdata[i++] = vertices.get(Integer.valueOf(vindices.get(i))).getZ();//i=1 and counted up afterwards
}
This is what makes the mesh two-dimensional.
I would suggest using eigther ++i or
for(int i = 0; i < vdata.length; i+=3)
{
vdata[i] = vertices.get(Integer.valueOf(vindices.get(i))).getX();
vdata[i+1] = vertices.get(Integer.valueOf(vindices.get(i))).getY();
vdata[i+2] = vertices.get(Integer.valueOf(vindices.get(i))).getZ();
}
EDIT: I have posted it earlier for java. But it working in Java but not in android. So I am posting the android code now.
I am trying to use a custom class into a HashMap in my android application. But it is not giving my desired output. Please help.
I want to do something like this...
//Code for android application:
//Point class
class Point{
private int x;
private int y;
public Point(){
}
public Point(int _x, int _y){
this.x = _x;
this.y = _y;
}
}
DataManager.java
public class DataManager {
private Vector<Integer> RSSI = null;
private int numOfRSSI;
private Map<Vector<Integer>, Point> SingleData = null;
private Vector<Map<Vector<Integer>, Point>> Data = null;
private Context context;
public DataManager(Context _context) {
this.context = _context;
Data = new Vector<Map<Vector<Integer>, Point>>();
}
public void loadData(String filename) {
if (Data == null) {
System.out.println("***ERROR: DataSet not initalized!!!\n");
}
readFile(filename);
}
public void printData(){
Vector<Integer> rssi = null;
Map<Vector<Integer>, Point> single = null;
Point point = null;
for(int i=0;i<Data.size();i++){
single = new HashMap<Vector<Integer>, Point>() ;
single = Data.get(0);
for (Map.Entry<Vector<Integer>, Point> entry :single.entrySet()) {
rssi = new Vector<Integer>();
point = new Point();
rssi = entry.getKey();
point = entry.getValue();
System.out.print("("+point.x+" "+point.y+") ");
for(int j = 0;j<rssi.size(); j++){
System.out.print(rssi.get(j)+" ");
}
System.out.println("");
}
}
}
private void readFile(String filename) {
InputStream is = null;
try {
is = context.getResources().getAssets().open("datasets.txt");
} catch (IOException e) {
e.printStackTrace();
System.out.println("error file reading");
}
if (is != null) {
StringBuilder text = new StringBuilder();
int flag = 0;
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
if(flag==0){
flag=1;
this.numOfRSSI = Integer.parseInt(line);
System.out.println("number of RSSI: "+numOfRSSI);
}
else if(flag==1){
parseLine(line);
}
}
} catch (IOException e) {
// Error reading file
}
finally {
// myHelper.print(text.toString());
}
}
}
private void parseLine(String line) {
RSSI = new Vector<Integer>();
SingleData = new HashMap<Vector<Integer>, Point>();
StringTokenizer st = new StringTokenizer(line, ",");
int co = 0;
int x=0,y=0;
while (st.hasMoreTokens()) {
if(co < this.numOfRSSI){
RSSI.add(Integer.parseInt(st.nextToken()));
co++;
}
else{
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
}
}
Point point = new Point(x,y);
SingleData.put(RSSI, point);
Data.add(SingleData);
}
}
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
DataManager dataManager = new DataManager(MainActivity.this);
dataManager.loadData("datasets.txt");
dataManager.printData();
}
}
datasets.txt
5
-61,-51,-46,-41,-28,1,0
-60,-50,-51,-47,-34,2,0
-72,-52,-53,-55,-37,3,0
-60,-44,-58,-53,-40,3,1
-68,-55,-46,-47,-45,2,1
-66,-60,-48,-43,-37,1,1
-62,-57,-49,-45,-34,0,2
Output is showing.....
1 0 -61 -51 -46 -41-28
1 0 -60 -50 -51 -47 -34
1 0 -72 -52 -53 -55 -37
etc.....
But it should be...
1 0 -61 -51 -46 -41-28
2 0 -60 -50 -51 -47 -34
etc...
So this is my problem.
Below is working fine on my machine :-
import java.util.Map;
import java.util.Vector;
public class Demo {
public static void main(String[] args) {
Map<Vector<Integer>, Point> vectorPointMap = new HashMap<Vector<Integer>, Point>();
Vector<Integer> vector1 = new Vector<Integer>();
vector1.add(1);
vector1.add(2);
vector1.add(3);
vectorPointMap.put(vector1, new Point(10, 10));
Vector<Integer> vector2 = new Vector<Integer>();
vector2.add(4);
vector2.add(5);
vector2.add(6);
vectorPointMap.put(vector2, new Point(20, 20));
// Print data
for (Map.Entry<Vector<Integer>, Point> entry : vectorPointMap.entrySet()) {
Vector<Integer> keyVector = entry.getKey();
Point valuePoint = entry.getValue();
System.out.print("(" + valuePoint.x + " " + valuePoint.y + ") ");
for (int j = 0; j < keyVector.size(); j++) {
System.out.print(keyVector.get(j) + " ");
}
System.out.println("");
}
}
}
You should change this from
for(int i=0;i<Data.size();i++){
single = new HashMap<Vector<Integer>, Point>() ;
single = Data.get(0);
}
to
for(int i=0;i<Data.size();i++){
single = new HashMap<Vector<Integer>, Point>() ;
single = Data.get(i);
}
try changing:
single = Data.get(0);
to
single = Data.get(i);
What does it mean, "it does not give right"?
You can put your own classes to Map, there is no problem. But remember, when you put items to HashMap in some order, there is no guarantee, it will come out in the same order. If you need to have same order on output, as on input, instead HashMap use LinkedHashMap. You can use it in the same way as HashMap, but it preserves order of inserted items.
Here I read the dataset and extracted the data lines(not the attributes) and print it.Next I need to sort the dataset.Now this is stored in an ArrayList.How to sort it?
public static void main(String args[]) throws Exception
{
String filen, jsnfl;
Customiseddata data = new Customiseddata();
data.setAlgorithm("C4.5");
data.setUserName("Dahlia");
System.out.println("Enter the file name");
sc = new Scanner(System.in);
filen = sc.nextLine();
data.setFileName("input_files/" + filen);
Mainclass main = new Mainclass();
main.build(data);
}
public void build(Customiseddata data) throws Exception
{
int extension;
String filename;
filename = data.getFileName();
extension = filename.lastIndexOf('.');
String extensionType = filename.substring(extension + 1,
filename.length());
if (extensionType.equalsIgnoreCase("csv"))
{
readcsv(filename);
}
else if (extensionType.equalsIgnoreCase("arff"))
{
readarff(filename);
}
}
public void readarff(String filename) throws Exception
{
#SuppressWarnings("unused")
int filesize, attributesize, c = 0, i;
#SuppressWarnings("unused")
float v = 0;
String s, line1;
ArrayList<String> filelines;
ArrayList<String> attributes;
Customiseddata data = new Customiseddata();
Arfffilereader arfffile = new Arfffilereader();
Extractdata exdata = new Extractdata();
exdata = arfffile.extractInputArff(filename);
filelines = exdata.getFileLines();
attributes = exdata.getAttributes();
filesize = filelines.size();
attributesize = attributes.size();
data.setFilesize(filesize);
System.out.println("Print the attributes");
System.out.println("--------------------");
for (i = 0; i < attributesize; i++)
{
System.out.println(attributes.get(i));
}
System.out.println("\t");
System.out.println("Print the filelines");
System.out.println("--------------------");
for (int j = 0; j < filesize; j++)
{
System.out.println(filelines.get(j));
}
}
But after this I need to sort the dataset.
Since the elements of the list are Strings and since String implements Comparable, sorting a list is as simple as:
Collections.sort(theList);
Note however that it will sort the list in place. If you don't want that, make a copy of the list and sort that copy.
I use Selenium Web Driver in Eclipse with IUnit. I have code which read data from excel file. Every column is presented as array. This is code:
class ReadExcel {
ArrayList path_name = new ArrayList();
ArrayList field_key = new ArrayList();
ArrayList field_name = new ArrayList();
ArrayList window_new = new ArrayList();
ArrayList link = new ArrayList();
lov_name = new ArrayList();
public void mai() {
int i = 0;
String path_namel = "", field_keyl = "", field_namel = "", window_newl = "", linkl = "", lov_namel = "";
String filename = "E:/data.xls";
if (filename != null && !filename.equals("")) {
FileInputStream fs = new FileInputStream(filename);
HSSFWorkbook wb = new HSSFWorkbook(fs);
for (int k = 0; k < wb.getNumberOfSheets(); k++) {
int j = i + 1;
HSSFSheet sheet = wb.getSheetAt(k);
int rows = sheet.getPhysicalNumberOfRows();
for (int r = 1; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
int cells = row.getPhysicalNumberOfCells();
HSSFCell cell1 = row.getCell(0);
path_namel = cell1.getStringCellValue();
HSSFCell cell2 = row.getCell(1);
field_keyl = cell2.getStringCellValue();
HSSFCell cell3 = row.getCell(2);
field_namel = cell3.getStringCellValue();
HSSFCell cell4 = row.getCell(3);
window_newl = cell4.getStringCellValue();
HSSFCell cell5 = row.getCell(4);
linkl = cell5.getStringCellValue();
HSSFCell cell6 = row.getCell(5);
lov_namel = cell6.getStringCellValue();
path_name.add(path_namel);
field_key.add(field_keyl);
field_name.add(linkl);
window_new.add(window_newl);
link.add(linkl);
lov_name.add(lov_namel);
}
i++;
}
}
}
}
In my selenium test I have such cycle:
for (int i=0; i<path_name.length; i++){
driver.findElement(By.xpath(path_name[i])).click();
}
Here I use variable path_name which is array and must be equal path_name from class ReadExcel. Actually I want use this values from excel as array. How should I call variable from ReadExcel?
Edit
I try use getter and setter methods.
int q;
String g;
public String getG() {
return g;}
public void setG(String g) {
this.g = g;}
public int getQ() {
return q;}
public void setQ(int q) {
this.q = q;}
q=path_name.size();
g=path_name.get(i).toString();
I my test I call variables in such way
ReadExcel h = new ReadExcel();
String k= h.getG();
ReadExcel p = new ReadExcel();
int n= p.getQ();
for (int j=0; j<n; j++){
driver.findElement(By.xpath(k)).click();}
Have no errors in editor, but cycle do not work. It should click on links (k), but have no effect.
Also I try this (was suggested in first answer)
ReadExcel readExcel = new ReadExcel();
ArrayList<String> path_name = readExcel.getPath_name();
for(String pathName: path_name){
driver.findElement(By.xpath(pathName)).click();
}
The same effect. It does not click on links
Can you convert your variable into field of that class? And then add a method there to return value of that field to anyone interested.
One way would be to implement setter() and getter() methods in your ReadExcel class for the variables that you want to access. And they would obviously be public methods.
Edit:
Guessing from what you've tried to update and my understanding, you're doing a lot of things wrong. Assuming that you're calling your last piece of code from another class, here's what you really should have done:
Also, I'm assuming that you've modified your ReadExcel class to look something like this
public class ReadExcel {
ArrayList<String> pathName = new ArrayList<String>();
ArrayList<String> fieldKey = new ArrayList<String>();
ArrayList<String> fieldName = new ArrayList<String>();
ArrayList<String> windowNew = new ArrayList<String>();
ArrayList<String> link = new ArrayList<String>();
public ReadExcel() {
pathName = new ArrayList<String>();
fieldKey = new ArrayList<String>();
fieldName = new ArrayList<String>();
windowNew = new ArrayList<String>();
link = new ArrayList<String>();
}
/**
* Not so sure of this method name. But make sure that this method is called before
* you try to call getXX() methods
*/
public void mai() {
String filename = "E:/data.xls";
if(fileName == null || "".equals(fileName))
return;
HSSFWorkbook workBook = null;
FileInputStream fileInputStream = null;
HSSFSheet sheet;
HSSFRow row;
int rows;
try{
fileInputStream = new FileInputStream(new File(fileName));
workBook = new HSSFWorkbook(fileInputStream);
for(int sheetIndex = 0; sheetIndex < workBook.getNumberOfSheets(); sheetIndex++){
sheet = workBook.getSheetAt(sheetIndex);
rows = sheet.getPhysicalNumberOfRows();
for(int rowIndex = 0; rowIndex < rows; rowIndex++){
/**
* Update with your own logic for retrieval
*/
row = sheet.getRow(rowIndex);
if(row.getPhysicalNumberOfCells() < 6)
continue;
pathName.add(row.getCell(0).getStringCellValue());
fieldKey.add(row.getCell(0).getStringCellValue());
fieldName.add(row.getCell(0).getStringCellValue());
windowNew.add(row.getCell(0).getStringCellValue());
link.add(row.getCell(0).getStringCellValue());
}
}
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}finally{
if(fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
workBook = null;
}
}
}
/**
* The getter/setter methods for the variables
*/
public ArrayList<String> getPathName() {
return pathName;
}
public void setPathName(ArrayList<String> pathName) {
this.pathName = pathName;
}
public ArrayList<String> getFieldKey() {
return fieldKey;
}
public void setFieldKey(ArrayList<String> fieldKey) {
this.fieldKey = fieldKey;
}
public ArrayList<String> getFieldName() {
return fieldName;
}
public void setFieldName(ArrayList<String> fieldName) {
this.fieldName = fieldName;
}
public ArrayList<String> getWindowNew() {
return windowNew;
}
public void setWindowNew(ArrayList<String> windowNew) {
this.windowNew = windowNew;
}
public ArrayList<String> getLink() {
return link;
}
public void setLink(ArrayList<String> link) {
this.link = link;
}
}
And I hope that you're somewhere calling your mai() method (this name sounds really weird though) to retrieve the data from the excel and store them in the ArrayList before you're trying to call the following piece of code:
ReadExcel readExcel = new ReadExcel();
ArrayList<String> path_name = readExcel.getPath_name();
for(String pathName: path_name){
driver.findElement(By.xpath(pathName).click();
}
Some pointers to your code:
Use generics. Instead of defiing ArrayList pathNameList consider, using ArrayList<String> pathNameList
It looks good if you use Java Naming Convention when you code. One of them is to compose method names using mixed case letters, beginning with a lower case letter and starting each subsequent word with an upper case letter. So instead of having a getPath_name(), consider having something like getPathName() or even getPath_Name() (although most of us would prefer the 1st one). Here's a link that can help you with that.