Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, August 9, 2015

Java: Compare two JSON strings


Following is the code to parse two json strings and compare them
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class TestJson3{
 
 private static String srcJsonStr = "SRC JSON String here";
 private static String destJsonStr = "Dest JSON String here";

 public static void main(String[] args){
  try{
   JSONObject srcJson = new JSONObject(srcJsonStr);
   JSONObject destJson = new JSONObject(destJsonStr);
   
   compareJson(srcJson, destJson);
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 
 public static void compareJson(JSONObject srcJson, JSONObject destJson){
  StringBuilder diffLog = new StringBuilder();
  
  try{
   Map srcJsonMap = new LinkedHashMap();
   Map destJsonMap = new LinkedHashMap();
   
   srcJsonMap = parseJson("", srcJson, srcJsonMap);
   destJsonMap = parseJson("", destJson, destJsonMap);
   
   //union
   Set union = new HashSet(srcJsonMap.keySet());
   union.addAll(destJsonMap.keySet());
   
   //h1-h2
   Set diff1 = new HashSet(srcJsonMap.keySet());
   diff1.removeAll(destJsonMap.keySet());
   
   //h1-h2
   Set diff2 = new HashSet(destJsonMap.keySet());
   diff2.removeAll(srcJsonMap.keySet());
   
   //intersection
   Set intersect = new HashSet(srcJsonMap.keySet());
   intersect.retainAll(destJsonMap.keySet());
   
   if(intersect.size() > 0){
    for(String key: intersect){
     Object srcData = srcJsonMap.get(key);
     Object destData = destJsonMap.get(key);
     
     if( ! srcData.toString().equals(destData.toString())){
      diffLog.append("KEY :: " + key + "\n");
      diffLog.append("SRC DATA :: " + srcData + "\n");
      diffLog.append("DEST DATA :: " + destData + "\n");
     }
    }
   }
   
   if(diff1.size() > 0){
    diffLog.append("\n");
    diffLog.append("###########################");
    diffLog.append("Only in SRC JSON");
    diffLog.append("###########################");
    diffLog.append("\n");
    for(String key: diff1){
     diffLog.append("KEY :: " + key + "\n");
     diffLog.append("DATA :: " + srcJsonMap.get(key) + "\n");
     diffLog.append("\n");
    }
   }
   
   if(diff2.size() > 0){
    diffLog.append("\n");
    diffLog.append("###########################");
    diffLog.append("Only in DEST JSON");
    diffLog.append("###########################");
    diffLog.append("\n");
    for(String key: diff2){
     diffLog.append("KEY :: " + key + "\n");
     diffLog.append("DATA :: " + destJsonMap.get(key) + "\n");
     diffLog.append("\n");
    }
   }
  }
  catch(Exception e){
   e.printStackTrace();
  }
  System.out.println(diffLog.toString());
 }
 
 public static Map parseJson(String key, JSONObject jsonObject, Map map){
  try{
   Iterator iterator = jsonObject.keys();
   
   while(iterator.hasNext()){
    String obj = iterator.next().toString();
    String keyVal = (key.equals("")) ? obj : key + "." + obj;
    
    if(jsonObject.get(obj) instanceof JSONArray){
     JSONArray jsonArr = (JSONArray) jsonObject.get(obj);
     
     for(int k=0; k < jsonArr.length; k++){
      if(jsonArr.get(k) instanceof JSONObject){
       parseJson(keyVal + "[" + k + "]", (JSONObject) jsonArr.get(k), map);
      }
      else{
       map.put(keyVal, jsonArr.get(k));
      }
     }
    }
    else{
     if(jsonObject.get(obj) instanceof JSONObject){
      parseJson(keyVal, (JSONObject) jsonObject.get(obj), map);
     }
     else{
      map.put(keyVal, jsonObject.get(obj));
     }
    }
   }
  }
  catch(Exception e){
   e.printStackTrace();
  }
  return map;
 }
}

Java: Store Json String into a Java Map


Following is the code to parse a json string and store the structure in Map
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class TestJson2{
 
 private static String jsonStr = "JSON String here";

 public static void main(String[] args){
  try{
   JSONObject obj = new JSONObject(jsonStr);
   parseJson(obj);
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 
 public static void parseJson(JSONObject jsonObject){
  Map map = new LinkedHashMap();
  
  try{
   parseJson("", jsonObject, map);
   
   for(String key: map.keySet()){
    System.out.println(key + "::" + map.get(key));
   }
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 
 public static Map parseJson(String key, JSONObject jsonObject, Map map){
  try{
   Iterator iterator = jsonObject.keys();
   
   while(iterator.hasNext()){
    String obj = iterator.next().toString();
    String keyVal = (key.equals("")) ? obj : key + "." + obj;
    
    if(jsonObject.get(obj) instanceof JSONArray){
     JSONArray jsonArr = (JSONArray) jsonObject.get(obj);
     
     for(int k=0; k < jsonArr.length; k++){
      if(jsonArr.get(k) instanceof JSONObject){
       parseJson(keyVal + "[" + k + "]", (JSONObject) jsonArr.get(k), map);
      }
      else{
       map.put(keyVal, jsonArr.get(k));
      }
     }
    }
    else{
     if(jsonObject.get(obj) instanceof JSONObject){
      parseJson(keyVal, (JSONObject) jsonObject.get(obj), map);
     }
     else{
      map.put(keyVal, jsonObject.get(obj));
     }
    }
   }
  }
  catch(Exception e){
   e.printStackTrace();
  }
  return map;
 }
}

Java: parse a JSON string


Following is the code to parse a json string
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class TestJson1{
 
 private static String jsonStr = "";

 public static void main(String[] args){
  try{
   JSONObject obj = new JSONObject(jsonStr);
   parseJson("", obj);
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 
 public static void parseJson(String key, JSONObject jsonObject){
  try{
   Iterator iterator = jsonObject.keys();
   
   while(iterator.hasNext()){
    String obj = iterator.next().toString();
    String keyVal = (key.equals("")) ? obj : key + "." + obj;
    
    if(jsonObject.get(obj) instanceof JSONArray){
     JSONArray jsonArr = (JSONArray) jsonObject.get(obj);
     
     for(int k=0; k < jsonArr.length; k++){
      if(jsonArr.get(k) instanceof JSONObject){
       parseJson(keyVal + "[" + k + "]", (JSONObject) jsonArr.get(k));
      }
      else{
       System.out.println(keyVal + " :: " + jsonArr.get(k));
      }
     }
    }
    else{
     if(jsonObject.get(obj) instanceof JSONObject){
      parseJson(keyVal, (JSONObject) jsonObject.get(obj));
     }
     else{
      System.out.println(keyVal + " :: " + jsonObject.get(obj));
     }
    }
   }
   
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
}

Wednesday, November 28, 2012

Java : Send Email


Following is the code to send email using java. Download mail.jar and activation.jar to run the program.
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailProcessor {
 
 private String mailHost;
 private String from;
 private String to;
 private String subject;
 private String text;
 
  public static void main(String[] args)
 {
  EmailProcessor emp = new EmailProcessor("mailhost", "from@test.com", "to1@test.com,to2@test.com", "Subject", "Email Text");
  emp.send();
 }
 
 public EmailProcessor(String mailhost, String from, String to, String subject, String text){
  this.from = from;
  this.to = to;
  this.subject = subject;
  this.text = text;
  this.mailHost = mailhost;
 }
 
 public void send(){
 
  Properties props = new Properties();
  
  props.put("mail.smtp.host", mailHost);
 
  Session mailSession = Session.getDefaultInstance(props);
  Message simpleMessage = new MimeMessage(mailSession);
 
  InternetAddress fromAddress = null;
  InternetAddress[] toAddress = null;
  try {
   fromAddress = new InternetAddress(from);
   
   String[] toEmails = to.split(",");
            toAddress = new InternetAddress[toEmails.length];
            for (int i = 0; i < toEmails.length; i++)
            {
             toAddress[i] = new InternetAddress(toEmails[i].trim());
            }
  } catch (AddressException e) {
   Utility.logError(e.getMessage());
   e.printStackTrace();
  }
 
  try {
   simpleMessage.setFrom(fromAddress);
            simpleMessage.setRecipients(RecipientType.TO, toAddress);
   simpleMessage.setSubject(subject);
   simpleMessage.setText(text);
 
   Transport.send(simpleMessage);
  } catch (MessagingException e) {
   Utility.logError(e.getMessage());
   e.printStackTrace();
  }
 }
}

Thursday, April 19, 2012

Selenium: Check if Selenium Server is running

If you want to check whether selenium server is running or not, hit the url "http://host:port/selenium-server/driver/?cmd=testComplete", If response is returned that means, server is running on the given host:port. Here is the Java code for this:


import java.net.HttpURLConnection;
import java.net.URL;

public static boolean isSeleniumServerRunning(String host, int port) 
  {
   try {
    String baseUrl = "http://" + host + ":" + port;
    System.out.println("Checking selenium server status [" + baseUrl + "]");
    URL url = new URL(baseUrl + "/selenium-server/driver/?cmd=testComplete");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
    if(connection.getResponseCode() == HttpURLConnection.HTTP_OK)
     return true;
  } catch (Exception e) {
   System.err.println("Could not check selenium server status: " + e.getMessage());
   e.printStackTrace();
  }   
  return false;
  }

Java: Copy Directory structure

Here is the code for copying a directory structure
 public static void copyFolder(String srcFile, String destFile) throws IOException
 {
  File src = new File(srcFile);
  File dest = new File(destFile);
  
  if(!src.exists())
  {
   System.out.println("Source folder do not exists [" + srcFile + "]");
   return;
  }
  
  if(src.isDirectory())
  {
    //if directory not exists, create it
    if(!dest.exists())
    {
       dest.mkdir();
    }

    //list all the directory contents
    String files[] = src.list();

    for (String file : files) 
    {
     
     //construct the src and dest file structure
     String sourceFile = src.getAbsolutePath() + File.separator + file;
     String destinationFile = dest.getAbsolutePath() + File.separator + file;
     
     //recursive copy
     copyFolder(sourceFile, destinationFile);    }
  }
  else
  {
    //if file, then copy it
    //Use bytes stream to support all file types
    InputStream in = new FileInputStream(src);
          OutputStream out = new FileOutputStream(dest); 

          byte[] buffer = new byte[1024];

          int length;
          //copy the file content in bytes 
          while ((length = in.read(buffer)) > 0){
          out.write(buffer, 0, length);
          }

          in.close();
          out.close();
  }
 }

Java: Comparing Arrays

Want to compare arrays of which item may not be in same order. Here is the solution:

public static boolean compareArrays(String[] actual, String[] expected)
    {
        if(actual == null || expected == null)
            return false;
       
        Arrays.sort(actual);
        Arrays.sort(expected);
       
        if(Arrays.equals(actual, expected))
            return true;
        else
            return false;
    }

Sunday, January 8, 2012

Write data into Excel file using Apache POI

IN my previous post, I posted code for reading excel file using Apache POI library. In this post I'll cover, how can we write data into excel. Here is the code:
package com.qa.test;

import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class SpreadSheetWriter {
	
	 /** This method writes data to new excel file **/
    public static void writeDataToExcelFile(List> data, String fileName) 
    {
    	try{
	    	HSSFWorkbook myWorkBook = new HSSFWorkbook();
	        HSSFSheet mySheet = myWorkBook.createSheet();
	        HSSFRow myRow = null;
	        HSSFCell myCell = null;
	        
	        //Create header row
	        createHeaderRow(mySheet);
	        
	        int rowNum = 1;
	        Iterator> iter = data.iterator();
	        while(iter.hasNext())
	        {
	        	myRow = mySheet.createRow(rowNum++);
	        	int cellNum = 0;
	        	
				List key = iter.next();
				for(String values: key)
				{
					myCell = myRow.createCell(cellNum++);
					myCell.setCellValue(values);
				}
			}
	
	        FileOutputStream out = new FileOutputStream(fileName);
            myWorkBook.write(out);
            out.close();
        }catch(Exception e){ 
        	e.printStackTrace();
        }         
   }
    
 }

Read Excel file using apache POI library

If you want to read Excel file in Java, Apache POI is the library which supports to read/write microsoft documents. Here is the code to read an Excel file.
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

public class ReadExcel {
 
 public static void main(String[] args){
  
  try{
   InputStream myxls = new FileInputStream("C:\\exceltest.xls");
   HSSFWorkbook wb     = new HSSFWorkbook(myxls);
   
   HSSFSheet sheet = wb.getSheetAt(0); 
   
   Iterator rows = sheet.rowIterator();
   
   System.out.println("Total Rows :: " + sheet.getLastRowNum());
   
   //Iterate over each row
   while (rows.hasNext()) 
   {
    HSSFRow row = (HSSFRow) rows.next();
    if(checkIfRowEmpty(row)) 
     continue;
    
    int minColIndex = row.getFirstCellNum();
    int maxColIndex = row.getLastCellNum();
    
    for(int colIndex = minColIndex; colIndex <= maxColIndex; colIndex++) 
    {
     HSSFCell cell = row.getCell(colIndex);
     
     // POI can't recognize empty cells of Excel, so we should create new cells with a blank value
     if(cell == null) 
     {
        cell = row.createCell(colIndex);
           cell.setCellValue(" ");
     }    
     String value = "";
     
     switch(cell.getCellType())
     {
      case HSSFCell.CELL_TYPE_NUMERIC:
       value = Double.toString(cell.getNumericCellValue());
       break;
      case HSSFCell.CELL_TYPE_BOOLEAN:
       value = Boolean.toString(cell.getBooleanCellValue());
       break;
      case HSSFCell.CELL_TYPE_BLANK:
       value = "";
       break;
      case HSSFCell.CELL_TYPE_ERROR:
       value = "";
       break;
      case HSSFCell.CELL_TYPE_FORMULA:
       value = "";
       break;
      default:
       value = cell.getStringCellValue();
       break;
     }
     System.out.print(value + "|");
    }
    System.out.println();
   }
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 
 public static boolean checkIfRowEmpty(HSSFRow row) 
 {
  int minColumIndex = row.getFirstCellNum();
  int maxColumnIndex = row.getLastCellNum();
  
  boolean isRowEmpty = true;
  for(int columIndex = minColumIndex; columIndex <= maxColumnIndex; columIndex++) 
  {
   HSSFCell cell = row.getCell(columIndex);
   if(cell == null || cell.toString().trim().isEmpty() || cell.toString().length() == 0){
    
   }
   else{
    isRowEmpty = false;
    return isRowEmpty;
   }
  }
  return isRowEmpty;
 }
}

Saturday, October 3, 2009

https redirection handling in httpunit

In httpunit when you are requesting a http page and if the page internally redirects to a https page, httpunit throws following error and response is not received:

sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed

To resolve this error, add following code in your httpunit program.

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.*;

try{
SSLContext context = SSLContext.getInstance("SSLv3");
TrustManager[] trustManagerArray = { new NullX509TrustManager() };
context.init(null, trustManagerArray, null);
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new NullHostnameVerifier());
}catch(Exception e) {
e.printStackTrace();
}


class NullX509TrustManager implements X509TrustManager {
/* Implements all methods, keeping them empty or returning null */
public X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(X509Certificate[] chain, String authType) {

}

public void checkServerTrusted(X509Certificate[] chain, String authType) {

}
}

class NullHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}



Getting thread dump for java application

When an java application server freezes or becomes non-responsive, it is recommended to generate a thread dump for the application. A thread dump is a user-friendly snapshot of the threads and monitors in a Java Virtual Machine (JVM). A thread dump can range from fifty lines to thousands of lines of diagnostics depending on how complex your application is.

On UNIX platforms you can send a signal to a program by using the kill command. This is the quit signal, which is handled by the JVM. On Solaris you can use the command kill -QUIT process_id, where process_id is the process id of your Java program.

run the command kill -QUIT <pid> or kill -3 <pid> on shell which will cause the thread dump to be displayed your application console. Alternatively you can enter the key sequence <CTRL> \ in the window where the Java program was started. This is why it's critical to redirect standard output & standard error to a log file so it is captured when you need a thread dump.

On Windows platform, enter the key sequence <CTRL> <break> for the thread dump of your application.

Thread States :: The key used for the thread states in Thread dump is:
R ==> Running or runnable thread
S ==> Suspended thread
CW ==>Thread waiting on a condition variable
MW ==> Thread waiting on a monitor lock
MS ==> Thread suspended waiting on a monitor lock

Thursday, August 14, 2008

Execute a perl script from java Program

Interested to execute a perl script from a java program.... uhmmmmm... see below java program (Sample.java)

import java.io.*;

public class Sample{

public static void main(String args[]) {

Process process;

try
{
process = Runtime.getRuntime().exec("perl testscript.pl");
process.waitFor();
if(process.exitValue() == 0)
{
System.out.println("Command Successful");
}
else
{
System.out.println("Command Failure");
}
}
catch(Exception e)
{
System.out.println("Exception: "+ e.toString());
}
}
}

javac Sample.java
java -cp . Sample

Monday, December 24, 2007

Kill java processes from perl script

Requirement:- If multiple java processes are running on your system and you want to kill only few specific from them.
If we do it manually then we have to perform below two steps:

1. jps (it will return all the java processes running on the system) in below format:
<pid>     <process name>
<pid>     <process name>
<pid>     <process name>
<pid>     <process name>
...

in my case i got the below output from above command:

1234 Server1
1334 Server2
1454 Server3
1264 Server4
1238 Server5
1244 Server6

2. now i have the process id of all the java processes so i can kill them using kill command, (let say if i have to kill only Server1, Server5 , then)

kill -9 1234
kill -9 1238

By using perl script, i can write below simple script which will do my above task:

foreach $_ (`jps`) {
my($pid,$pname) = /(\S+)\s+(.*)/;
if($pname eq "Server1" | $pname eq "Server5" ) {
print("Killing Process : $pname ($pid) \n");
system("kill -9 $pid");
}
}