Reading And Writing File in JAVA- Usage of File Classes FileReader,BufferedReader,FileWriter,BufferedWriter

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReadWriteFileEx {

/**
* @param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub

File f = new File("myFile.txt");
System.out.println(f.getAbsolutePath());
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("myFile1.txt");
BufferedWriter bw = new BufferedWriter(fw);

String str;
int count=0;
while((str=br.readLine())!=null)
{
System.out.println(str);
bw.write(str);
bw.write("\n");
if(str.contains("Selenium"))
{
count++;
}
}
System.out.println("No of times found String Selenium is "+ count);
  //Closing the Resources
bw.close();
fw.close();
fr.close();
br.close();
f=null;
}

}

Generate XSLT Report+ Ant build.xml+ Resolving Unknown file:23:146: Fatal Error! Could not find function: if Issue +No suites, classes, methods or jar file was specified






We usually depend on Testng for generating the default reports. Apart from that we use XSLT reports as well to generate PIE Charts based on Failed,Passed,Skipped Testcases.
During this process we face lot of issues. Few of the most commonly observed issues are as follows:
[xslt] Unknown file:23:146: Fatal Error! Could not find function: if
[xslt] : Fatal Error! Fatal error during transformation Cause: Fatal error during transformation
In order to resolve this we have to follow the below steps. At every step we will check and see whether issues are resolved or not.
Step 1 :
Make sure the build.xml file contains 'SaxonLiaison' processor added.
Example:<xslt in="${outputdir}/test-output/testng-results.xml" style="${basedir}/testng-results.xsl" out="${outputdir}/testng-xslt/index.html"  processor="SaxonLiaison">

Re-run the build.xml and verify the issue is resolved. If the issue is not resolved goto Step2.

Step 2 :
Make sure the following jars added to the lib folder and Project build path as well.
saxon-8.7.jar -> Link to download the saxon jar-> Click here
SaxonLiaison.jar ->Link to download the SaxonLiaison jar->Click here
guice-3.0.jar->Link to download guice-3.0.jar->Click here
Re-run the build.xml and verify the issue is resolved. If the issue is not resolved goto Step3.
Step 3:
Re-verify the build.xml available in the project directory by downloading from this link Click here .Try to do necessary changes.
Re-run the build.xml and verify the issue is resolved. If the issue is not resolved goto Step3.
Step 4:
Re-verify the testng-results.xsl available in the project directory by downloading from the link Click here.Try to do necessary changes.
Re-run the build.xml and verify all the issues are resolved.
If even after the issues are not resolved, please refer to the directory structure mentioned in the below screenshot.
Resolving "No suites, classes, methods or jar file was specified" in Ant TestNG- Make sure the testng.xml is availble in the src folder as mentioned in the image below. Refer to the point1.











Implementation of Testng @DataProvider using EXCEL (Data Driven Framework) & JXL to Read XLS

@DataProvider
In order to store the multiple set of input data we use @DataProvider.
We are going to read the xls file which contains input data and store in @DataProvider.
Steps to Achieve :
  a. Store the input data in an xls file.
  b. Read the data from the xls using an third party jar jxl2.6.jar
   Link for Downloading JXL Jar ->http://www.findjar.com/index.x?query=jxl
   jxl stands for -> java excel library
 c. Add the jar to the build path.
 d. Store the data in @Dataprovider
 e. Use the @Dataprovider in the @Test methods.
JExcel API ->http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/
  WorkBook contains sheets
  Sheets contains rows and column
  Each and every values stored in the rows and columns in a cell.
  Retrieve the contents of the cell using row index and column index and store in an array
Program:
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ReadExcelJXL {
@Test(dataProvider="DP")
public void sales_coupon_count(String username,String password)
{

System.out.println("The value of username" + username);
System.out.println("The value of password" + password);

}
@DataProvider(name="DP")
public String[][] readExcel(  )
{
File file = new File("inputData.xls");
String inputData[][] = null;
Workbook w;

try {
w = Workbook.getWorkbook(file);

// Get the first sheet
Sheet sheet = w.getSheet(0);

int colcount=sheet.getColumns();

int rowcount=sheet.getRows();

inputData= new String[rowcount][colcount];

for ( int i=0;i<sheet.getRows();i++)
{
for (int j=0;j<sheet.getColumns();j++)
{
Cell cell=sheet.getCell(j,i);
inputData[i][j]=cell.getContents();
}
}
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inputData;
 }
}
Output
The value of username-->xyz
The value of password-->abc
The value of username-->abc
The value of password-->welcome
The value of username-->ab1123
The value of password-->welcome123
PASSED: sales_coupon_count("xyz", "abc")
PASSED: sales_coupon_count("abc ", "welcome ")
PASSED: sales_coupon_count("ab1123 ", "welcome123")

===============================================
    ReadExcelJXL
    Tests run: 3, Failures: 0, Skips: 0
===============================================


===============================================
DataDriven
Total tests run: 3, Failures: 0, Skips: 0
===============================================
inputData.xls