Monday, February 11, 2013

Great Blogs to Follow

I follow these blogs for testing ideas and information:

http://www.gcreddy.net/

http://qualilogy.com/en/author/jpfayolle/

http://www.utest.com/

Thursday, September 13, 2012

Tuesday, August 7, 2012

Images of Mt Everest

Here are some cool pics of Mt Everest:



Saturday, July 28, 2012

Simple Parsing Scripts

-------------------------------------------------------------------
1) Simple perl script to extract ip address from ipconfig command:


#!/usr/bin/perl

use File::Find;
use File::Copy;
use File::Path;

system ("ipconfig > test.txt");

$filename = "test.txt";
$strToFind = "IPv4";

open(INFILE, $filename) || die "Cannot open file";

$matchFound = 0;

while ($line = )
{
   if ($line =~ /$strToFind/)
    {
      print "$line";
      $matchFound = 1;
    }
}

close(INFILE);

print "\n";

if ($matchFound eq 1)
{
    print "$strToFind was found: $matchFound \n";
}
else
{
    print "$strToFind not found in $filename \n";
}


-----> Simple Ruby Script to parse IP address from ipconfig command <------------


--------------------------------------------------------------------------------------
2) Simple VB Script to parse MAC address and ip address from ipconfig command:

' VBScript Source File 
'
' NAME: myIPfunc.vbs
'
' AUTHOR: Swati Kher
'
' COMMENT: How to parse IPConfig command with vbscript 
'======================================================

Option Explicit


wscript.echo "Mac Address = ["& GetMac() & "]"

wscript.quit(0)

' ********************************************************************************************
' ******** GetMac ********
' ********************************************************************************************

Function GetMac()

   Dim WshShell, oExec , physaddr, macaddr, ipaddr
   Dim iLocation, name, strInputFile, iLoc2, name2

   Dim FSO, objFile
   Dim strLine

   Set WshShell = CreateObject("WScript.Shell") 

   set oExec = WshShell.Exec("cmd.exe /c ipconfig /all")

   physaddr = oExec.StdOut.ReadAll()
   name    = "Physical Address"
   name2   = "IPv4 Address"
   macaddr = "tbd"
  

   iLocation = InStr (physaddr, name)
   iLoc2 = InStr(physaddr, name2)
   
   
   WScript.Echo iLocation
   
   if (iLocation) then

       macaddr = Right(physaddr, Len(physaddr)  - iLocation)
       ipaddr = Right(physaddr, Len(physaddr) - iLoc2)
       
       'WScript.Echo "First Pass: " & macaddr
       iLocation = InStr (1, macaddr, ":")
       iLoc2 = InStr(1, ipaddr, ":")
       
       macaddr = Right(macaddr, Len(macaddr) - iLocation)
       ipaddr = Right(ipaddr, Len(ipaddr) - iLoc2)
       
       Trim(macaddr)
       Trim(ipaddr)
       
       'WScript.Echo "Second : " & macaddr
       'macaddr = Right(macaddr, Len(macaddr) - 17)
       iLocation = InStr (1, macaddr, vbNewLine)
       iLoc2 = InStr(1, ipaddr, vbNewLine)
       
       macaddr = Left(macaddr, iLocation)
       ipaddr = Left(ipaddr, iLoc2)
       
       macaddr = replace(macaddr, "-", "")
       GetMac = macaddr
       'WScript.Echo "Mac address " & macaddr
       WScript.Echo "IP ADDR: " & ipaddr

   end If

   GetMac = macaddr
   set WshShell = Nothing
   Set oExec = Nothing
End Function

------------------------------------------------------
3)  C++ functions to check palindromes and matching strings:


bool isItPali(char *a)
{
int length, mid, j;
    bool match = true;


if (a==NULL)
{
cout<<"Got a NULL String!!"<
return false;
}

length = strlen(a)-1;
mid = length/2;
j = 0;

if (strlen(a)==1)
{
cout<<"Only 1 char!!"<
return true;
}

if (length<=0)
{
cout<<"String was blank!!"<
return false;
}


    while(j<= mid)
{
if (*(a+j) != *(a+length-j))
match = false;
j++;
}


  return match;
}


bool ExpressionMatch(char *a, char *b)
{
int lenA, lenB, i, j, indWld;
bool match = false, done = false;

lenA = strlen(a);
lenB = strlen(b);

i = j = indWld = 0;

    if ((lenA == 0) || (lenB == 0))
return false;


while (!done)
{
if (*(b + j) == '*')
{
j++;
indWld = j;
}
else if (*(b + j) == *(a+i))
{
match = true;
j++;
i++;
}
else if(*(b+j) != *(a+i))
{
i++;
match = false;
if (indWld == 0)
i = lenA;
else if (i< lenA)
j= indWld;
}
if (indWld == lenB)
{
done = true;
if (indWld == 1)
match = true;
}
if (i == lenA)
done = true;
}

return match;
}
-----------------------------------------------------------
4) Java code to compute string LCM

import java.util.*;
import java.util.regex.*;

public class LCMstr {

/**
* @param args
*/
public static String lcm(String a, String b) {
int c = a.length();
int d = b.length();
if (a.charAt(c-1)==b.charAt(0)) {
return(a.concat(b.substring(1))); 
}
if (c >= d) {
    if (a.contains(b)) return a;
}
if (d > c) {
if (b.contains(a)) return b;
}
return a.concat(b);
//System.out.println(a.charAt(0));
//System.out.println("len" + c + a.charAt(c-1));
   //return "err";
}
public static void main(String[] args) {

LCMstr l = new LCMstr();
        System.out.println( l.lcm("ab", "bacd"));
        System.out.println( l.lcm("abcc", "cd"));
        System.out.println( l.lcm("ab", "bad"));
        System.out.println( l.lcm("aba", "abacd"));
        System.out.println(l.lcm("x1", "1x"));
        System.out.println(l.lcm("swati", "1swati"));
        System.out.println(l.lcm("swati1", "swati"));
        System.out.println(l.lcm("x1", "x2"));
        System.out.println(l.lcm("ab", "ab"));
}

}
--------------------------------------------------------
5) java code to compute LCM of two numbers:

public class myLCM {
private void calculateLCM(int x, int y) {

        int lcm = x*y/getGCD(x, y);

        System.out.println(lcm);

 }


private int getGCD(int x, int y) {

       // Make sure, n1 is the bigger number

       int n1 = x > y ? x : y;

       int n2 = x > y ? y : x;

       while(n1%n2 != 0) {

              int remainder = n1%n2;

              n1 = n2;

              n2 = remainder;
       }

       return n2;

}


    public static void main(String[] argv) {

        myLCM x = new myLCM();
        
        x.calculateLCM(2, 3);
        x.calculateLCM(8, 16);
        x.calculateLCM(3,15);
        //x.calculateLCM(10,0);
        x.calculateLCM(11,12);
        x.calculateLCM(15, 20);
        x.calculateLCM(17, 51);

 }
}
---------------------------------------------




Friday, July 27, 2012

Example of Simple HTTPClient in Java


/* compile the following in eclipse : run as java application with the simpleHTTPServer listening on port 5000 (from previous example) */

/* to compile the following external jars are required: commons-codec-1.5.jar, commons-httpclient-3.0.1.jar
              commons-logging-1.1.1.jar, commons-logging-1.1.1-adapters.jar, commons-logging-1.1.1-api.jar
             commons-logging-1.1.1-tests.jar, jackson-core-asl-1.9.2.jar, json-20080701.jar, log4j-1.2.15.jar
              and log4j-1.2.16-sources.jar */

import org.apache.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.*;
import org.json.*;
import org.xml.sax.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.ws.http.HTTPException;
import org.apache.log4j.*;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class myClient {

public myClient() {
System.out.println("myclient");
Logger log = Logger.getLogger("wbs");
}

public Document getTemplateDocument(String fName) {
Document doc = null;
try {
DocumentBuilderFactory docFac = DocumentBuilderFactory.newInstance();

DocumentBuilder docBld = docFac.newDocumentBuilder();

if (!fName.equals(null)) {
File file = new File(fName);
doc = docBld.parse(file);
}
else {
System.out.println("null file");
}
}catch (ParserConfigurationException pce){
pce.printStackTrace();
}catch (IOException ioe) {
ioe.printStackTrace();
}catch (SAXException sxe) {
sxe.printStackTrace();
}
return doc;
}

public String convertDocToString(Document doc){
String xmls = null;

try {
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult res = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
trans.transform(source, res);
xmls = res.getWriter().toString();
}catch(TransformerConfigurationException tce){
tce.printStackTrace();
}catch(TransformerException te) {
te.printStackTrace();
}
return xmls;
}

public static String execReq(String xmls, String url) {
HttpClientParams cparam = new HttpClientParams();
DefaultHttpMethodRetryHandler retry_H = new DefaultHttpMethodRetryHandler();
cparam.setParameter(HttpClientParams.RETRY_HANDLER, retry_H);
cparam.setParameter(HttpClientParams.HEAD_BODY_CHECK_TIMEOUT, 600000);
cparam.setSoTimeout(600000);
cparam.setConnectionManagerTimeout(600000);
String strXMLFile = xmls;
File input = new File(xmls);
System.out.println("File length: " + input.length());
PostMethod post = null;
try {
post = new PostMethod(url);
HttpClient hc = new HttpClient();
hc.setConnectionTimeout(600000);
post.setRequestBody(new FileInputStream(input));
if (input.lastModified() < Integer.MAX_VALUE){
post.setRequestContentLength((int)input.length());
}
else {
post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
int stc = hc.executeMethod(post);
System.out.println(post.getResponseBodyAsString());
post.releaseConnection();
System.out.println("statusline: " + post.getStatusLine());
try {
Thread.sleep(100);
}catch(InterruptedException ie) {
System.out.println(ie.getMessage());
}
if(stc == HttpStatus.SC_OK) {
xmls = post.getResponseBodyAsString();
}else {
System.out.println(post.getStatusCode());
}
}
}catch(FileNotFoundException ffe) {
ffe.printStackTrace();
}catch(HTTPException he) {
he.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}finally {
post.releaseConnection();
}
return xmls;
}



public void sendtest() {
Document doc = getTemplateDocument("\\test.xml");
//String request = convertDocToString(doc);
//String response = execReq(request, "http://localhost:5000");

}




public static void main(String[] args) {
// TODO Auto-generated method stub

myClient mc = new myClient();
Document d = mc.getTemplateDocument("\\test.xml");
String request = mc.convertDocToString(d);
System.out.println(request);

        //File inp = new File(request);
     

PostMethod pos = null;
pos = new PostMethod("http://localhost:5000");
HttpClient hc = new HttpClient();
hc.setConnectionTimeout(600000);

try{
//pos.setRequestBody(new FileInputStream(inp));
   //pos.setRequestContentLength((int)inp.length());
 
int stc = hc.executeMethod(pos);
System.out.println(pos.getResponseBodyAsString());
pos.releaseConnection();
System.out.println("statusline: " + pos.getStatusLine());
}catch(HTTPException he) {
he.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}finally {
pos.releaseConnection();
}
}

}

/* sample test message test.xml file  :

*/