Home » » How to read html content from any website in java program & JUnitTest

How to read html content from any website in java program & JUnitTest

Here the full code for the given program. First of all understand the problem thats more important. Now its time to write code for this. I think anyone can easily understand this program.

This program will show you html code of any website & simulateously store contents into file located in your E:\Sex.txt. You can change its location or its name.






package test;

import java.util.*;
import java.net.*;
import java.io.*;

  public class JavaSourceViewer{
 
  
  public static void main(String args[])throws IOException{
  System.out.print("Enter Your URL(use http before url) : ");
  Scanner in=new Scanner(System.in);

 String str =in.nextLine();

 try{
  StringBuffer sb=new StringBuffer();
  actionPerform(str,sb);
  }catch(MalformedURLException e){
  System.out.println(str+" is not valid");
  }
 }

  public static void actionPerform(String u,StringBuffer br)throws MalformedURLException{
  
  try{
   File out1=new File("E://Sex.txt");
   FileWriter f=new FileWriter(out1,false);
   URL u1=new URL(u);
  HttpURLConnection hp=(HttpURLConnection)u1.openConnection();
  int code =hp.getResponseCode();
  String str1=hp.getResponseMessage();
  System.out.println("HTTP/1.x "+code+" "+str1);
  Scanner out=new Scanner(hp.getInputStream());
 

  String str2;
 
  while(out.hasNext())
  {
   
          str2=out.nextLine();
          br.append(str2);
          f.write(str2);
          System.out.println(str2);
  }
  }catch(Exception e){}
  }}

Now its time for creating JUnit Test Cases for the given program. Always remember few steps before going to test. JUnitTest is an api defined in  import static org.* package. It means you have to import this library to use JUnitTest.


How to create JUnitTestCase in Eclipse ?

Please do the same thing otherwise it would difficult for novice users. There r some following steps for creating JUnitTest.


Step 1 :






Step 2 :

Name your Class and then browse class under test (class which we wants to test)







Step 3 :

 browse class under test



And click on Ok then click on next.



Step 4 :

Select class methods which you want to test. You can select any number of methods.





Now work is half completed, its time to write testClass.





This is simple JUnitTest program easy to understand i will make you understand later.


package test;
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileReader;

import org.junit.Test;
import java.io.*;


public class JavaSource {

 @Test
 public void testActionPerform() throws Exception
 {
  StringBuffer str=new StringBuffer();
  JavaSourceViewer.actionPerform("http://www.skillrefine.com",str);
   assertEquals("op",-1,str.indexOf("hhihiuhihui"));
  //fail("Not yet implemented");
  }
 }


assertEquals("Your Message",Object 1,Object 2); // it will show error if two objects r unequal.
assertTrue(boolean); // it will show error if expression inside this function return false.
assertFalse(boolean); // it will show error if expression inside this function return true.




 
Share this article :

0 comments:

Post a Comment

 
My Other Websites : Ad Networks | Games Copyright © 2014. Study Depth - All Rights Reserved