Thursday

Create a jar file




The basic format of the command for creating a JAR file is:
jar cf jar-file input-file(s)

  • The c option indicates that you want to create a JAR file.
  • The f option indicates that you want the output to go to a file rather than to stdout.
  • jar-file is the name that you want the resulting JAR file to have. You can use any filename for a JAR file. By convention, JAR filenames are given a.jar extension, though this is not required.
  • The input-file(s) argument is a space-separated list of one or more files that you want to include in your JAR file. The input-file(s) argument can contain the wildcard * symbol. If any of the "input-files" are directories, the contents of those directories are added to the JAR archive recursively.
jar cvf my.jar mytest.class

http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

Basic Assert in Junit - Beginner


  • Assertions

assertEquals()
assertEquals("something", result);

assertTrue()/ assertFalse()
assertTrue (oMyClass.myMethod());
assertFalse(oMyClass.myMethod());


assertNull() / assertNotNull()
assertNull(oMyClass.myMethod());
assertNotNull(oMyClass.myMethod());

assertSame()/assertNotSame()
assertNotSame(oMyClass1.myMethod(),
oMyClass2.myMethod());


assertArrayEquals()

String[] expectedArray = {"one", "two", "three"};
String[] actualArray =  oMyClass.myMethod();
assertArrayEquals(expectedArray,actualArray );


Junit and Ant for Beginner - jump start



1. Install ant  {something like C:\Junit\Ant\apache-ant-1.8.4-bin\apache-ant-1.8.4 say it ANT_HOME}
2, Install Junit {remember to set classpath example C:\Junit\junit4.8.1\junit4.8.1 in classpath}
3. copy junit.x.x.jar in ANT_HOME/lib
4.  Now you working folder ex C:\Junit\Ant_project
5. Create test and src folder in it and put .java file in src folder

6.  example file 1:

public class Subscription {

   private int price ; // subscription total price in euro-cent
   private int length ; // length of subscription in months

   // constructor :
   public Subscription(int p, int n) {
     price = p ;
     length = n ;
   }

  /**
   * Calculate the monthly subscription price in euro,
   * rounded up to the nearest cent.
   */
   public double pricePerMonth() {
     double r = (double) price / (double) length ;
      return r ;
   }

  /**
   * Call this to cancel/nulify this subscription.
   */
   public void cancel() { length = 0 ; }

}


File 2:


import org.junit.* ;
import static org.junit.Assert.* ;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
 import java.io.IOException;
 import java.util.logging.Level;
 import java.util.logging.Logger;
public class SubscriptionTest {

   @Test
   public void test_returnEuro() {
      System.out.println("Test if pricePerMonth returns Euro...") ;
  Write_To_File(12,"hi","bye");
      Subscription S = new Subscription(200,2) ;
      assertTrue(S.pricePerMonth() == 1.0) ;


  Write_To_File(11,"hi","bye");
   }

   @Test
   public void test_roundUp() {
      System.out.println("Test if pricePerMonth rounds up correctly...") ;
  Write_To_File(10,"hi","bye");
      Subscription S = new Subscription(200,3) ;
      assertTrue(S.pricePerMonth() == 0.67) ;

   }


   @Test
   public void test_absolute() {
      System.out.println("shyam") ;
  Write_To_File(10,"hi","bye");
      Subscription S = new Subscription(200,4) ;
      assertTrue(S.pricePerMonth() == 50.0) ;
   }


   public void Write_To_File(int var, String str, String str1) {
   try {
FileWriter fstream = new FileWriter("C:\\Junit\\Result\\out.txt",true);

System.out.println("jhansi");
            BufferedWriter fbw = new BufferedWriter(fstream);
            fbw.write("Our append txt...");
            fbw.newLine();
            fbw.close();
}
catch (Exception ex) {
System.out.println("Exception Occured:: ");
ex.printStackTrace();
}
}
 
 
   }



7. Now craete Build.xml in C:\Junit\Ant_project

8. copy paste following xml in build.xml  (Please replace <   and  >  sing in the following xml)




lessthanSign ?xml version="1.0"?>

lessthanSign project name="tutorial" default="build" basedir=".">
  lessthanSign property name="srcdir" value="./src" />
   lessthanSign property name="testdir" value="./test" />
lessthanSign property name="lib" value="./lib" />
lessthanSign property name="classes" value="./classes" />


 lessthanSign target name="build">
        lessthanSign javac srcdir="." includeantruntime="false" includes="**/*.java"/>
    lessthanSign /target>

lessthanSign target name="ensure-test-name" unless="test">
    lessthanSign fail message="You must run this target with -Dtest=TestName"/>
lessthanSign /target>


 lessthanSign path id="classpath.base"/>
 
  lessthanSign path id="classpath.test">
  lessthanSign pathelement location="C:\Junit\junit4.8.1\junit4.8.1\junit-4.8.1.jar" />
    lessthanSign pathelement location="${testdir}" />
  lessthanSign pathelement location="${srcdir}" />
  lessthanSign path refid="classpath.base" />
  lessthanSign /path>
 
    lessthanSign target name="clean" >
  lessthanSign delete verbose="${full-compile}">
  lessthanSign fileset dir="${testdir}" includes="**/*.class" />
  lessthanSign /delete>
  lessthanSign /target>
 
    lessthanSign target name="compile" depends="clean">
  lessthanSign javac srcdir="${testdir}" destdir="${testdir}" verbose="${full-compile}" >
  lessthanSign classpath refid="classpath.test"/>
 
  lessthanSign /javac>
  lessthanSign /target>
 
  lessthanSign target name="test" depends="compile">
  lessthanSign junit>
  lessthanSign classpath refid="classpath.test" />
  lessthanSign formatter type="brief" usefile="false" />
  lessthanSign test name="test.SubscriptionTest" haltonfailure="no" outfile="result1"/>
  lessthanSign /junit>
  lessthanSign /target>



9. open command prompt and do the following

C:\Junit\Ant_project>ant test

10. you should get the test running