Thursday, September 3, 2015

Program to sort array into single loop

Program to sort array into single loop

class SortTest {

 public void sort1loop() {
  int arr[] = { 5, 1, 7, 3, 9 };
  for (int i = 1; i < arr.length; i++) {
   if (arr[i] < arr[i - 1]) {
    arr[i] = arr[i] + arr[i - 1];
    arr[i - 1] = arr[i] - arr[i - 1];
    arr[i] = arr[i] - arr[i - 1];
    i = 0;
   }
   printArr(arr);

  }
  System.out.print("Sorted Array : ");
  System.out.println();
  for (int i = 0; i < arr.length; i++) {
   System.out.print(arr[i] + " ");
  }
 }


Monday, August 24, 2015


JUnit - Tutorial


This tutorial explains unit testing with JUnit 4.x. It explains the creation of JUnit tests and how to run them in Eclipse or via own code.


Table of Contents
This tutorial is part of this Kindle book:
1. Introduction to unit testing
1.1. Unit testing
A unit test is a piece of code written by a developer that executes a specific functionality in the code under test. Unit tests ensure that code is working as intended and validate that this is still the case after code changes.
Typically unit tests are created in their own project or their own source folder to avoid that the normal code and the test code is mixed.
1.2. Unit testing with JUnit
JUnit 4.x is a test framework which uses annotations to identify methods that are test methods. The following code shows a JUnit test method.
@Test
public void testMultiply() {

   // MyClass is tested
   MyClass tester = new MyClass();
  
   // Check if multiply(10,5) returns 50
   assertEquals("Result", 50, tester.multiply(10, 5));
 }
JUnit assumes that all test methods can be executed in an arbitrary order. Therefore tests should not depend on other tests.
To write a test with JUnit you annotate a method with the @org.junit.Test annotation and use a method provided by JUnit to check the expected result of the code execution versus the actual result.
You can use the Eclipse user interface or the org.junit.runner.JUnitCore class to run the test.
1.3. Annotations
The following table gives an overview of the available annotations in JUnit 4.x.
Table 1. Annotations
Annotation
Description
@Test public void method()
The annotation @Test identifies that a method is a test method.
@Before public void method()
Will execute the method before each test. This method can prepare the test environment (e.g. read input data, initialize the class).
@After public void method()
Will execute the method after each test. This method can cleanup the test environment (e.g. delete temporary data, restore defaults).
@BeforeClass public void method()
Will execute the method once, before the start of all tests. This can be used to perform time intensive activities, for example to connect to a database. Methods annotated with this annotation need a static modifier to work with JUnit.
@AfterClass public void method()
Will execute the method once, after all tests have finished. This can be used to perform clean-up activities, for example to disconnect from a database. Methods annotated with @AfterClass need a static modifier to work with JUnit.
@Ignore
Will ignore the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included.
@Test (expected = Exception.class)
Fails, if the method does not throw the named exception.
@Test(timeout=100)
Fails, if the method takes longer than 100 milliseconds.

1.4. Assert statements
JUnit provides methods to test for certain conditions, these methods typically start with asserts and allow to specify the error message, the expected and the actual result. The following table gives an overview of the available methods to check certain conditions.
Table 2. Test methods
Statement
Description
fail(String)
Let the method fail. Might be used to check that a certain part of the code is not reached. Or to have failing test before the test code is implemented.
assertTrue(true) / assertTrue(false)
Will always be true / false. Can be used to predefine a test result, if the test is not yet implemented.
assertTrue([message], boolean condition)
Checks that the boolean condition is true.
assertsEquals([String message], expected, actual)
Tests that two values are the same. Note: for arrays the reference is checked not the content of the arrays.
assertsEquals([String message], expected, actual, tolerance)
Test that float or double values match. The tolerance is the number of decimals which must be the same.
assertNull([message], object)
Checks that the object is null.
assertNotNull([message], object)
Checks that the object is not null.
assertSame([String], expected, actual)
Checks that both variables refer to the same object.
assertNotSame([String], expected, actual)
Checks that both variables refer to different objects.

2. Installation of JUnit
If you use Eclipse no additional setup is required. Please skip this section.
Eclipse allows you to use a version of JUnit which is integrated in Eclipse.
If you want to control the used JUnit library explicitly, download JUnit4.x.jar from the following JUnit website. The download contains the junit-4.*.jar which is the JUnit library. Add this library to your Java project and add it to the classpath.
http://www.junit.org/
3. Eclipse
3.1. Eclipse support for JUnit
Eclipse supports the creation and execution of JUnit tests of of the box. It provides wizards to create unit tests.
Eclipse also provides the Alt+Shift+X,+T shortcut to run the selected JUnit test.
To see the result of a JUnit test, Eclipse uses the JUnit View which show the results of the tests. By default this view will always be shown if you run unit test, but you can also configure it, that it is only shown if you have a failing test.
3.2. JUnits static imports
JUnit uses a lot of static methods and Eclipse cannot always correctly automatically import static imports.
You can make the JUnit test methods available via the content assists.
Open the Preferences via Window → Preferences and select Java → Editor → Content Assist → Favorites.
Use the new New Type button to add the org.junit.Assert type. This makes for example the assertTrue,assertFalse and assertEquals methods directly available in the content assists.
You can now use Content Assist (Ctrl+Space) to add the method and the import.
4. Exercise: Using JUnit
4.1. Project preparation
Create a new project called de.vogella.junit.first.
Create a new source folder test. For this right-clicking on your project, select Properties and choose the Java Build Path . Select the Source tab.
Press the Add folder button, afterwards press the Create new folder button. Create the test folder.
Alternatively you can add a new source folder by right-clicking on a project and selecting New → Source Folder.
4.2. Create a Java class
In the src folder, create the de.vogella.junit.first package and the following class.
package de.vogella.junit.first;

public class MyClass {
  public int multiply(int x, int y) {
    return x / y;
  }
}
4.3. Create a JUnit test
Right click on your new class in the Package Explorer View and select New → JUnit Test Case.
In the following wizard select New JUnit 4 test and set the source folder to test, so that your test class gets created in this folder.
Press the Next button and select the methods which you want to test.
If the JUnit library in not part of the classpath of your project, Eclipse will prompt you to add it. Use this to add JUnit to your project.
Create a test with the following code.
package de.vogella.junit.first;

import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class MyClassTest {

  @BeforeClass
  public static void testSetup(){
    // Preparation of the unit tests
  }
 
  @AfterClass
  public static void testCleanup(){
    // Teardown for data used by the unit tests
  }
 
  @Test(expected = IllegalArgumentException.class)
  public void testMultiply() {
    MyClass tester = new MyClass();
    assertEquals("Result", 50, tester.multiply(10, 5));
  }

}
4.4. Run your test via Eclipse
Right click on your new test class and select Run-As → JUnit Test.
The result of the tests will be displayed in the JUnit View.
The test should be failing (indicated via a red bar).
This is because our multiplier class is currently not working correctly (it does a division instead of multiplication). Fix the bug and re-run test to get a green bar.
4.5. JUnit Testsuite
If you have several tests you can combine them into a test suite. Running a test suite will execute all tests in that suite.
To create a test suite, select your test classes → right click on it → New → Other → JUnit → Test Suite.
Select the Next button and select the methods for which you want to create a test.
Change the code to the following to make your test suite run your test. If you develop another test later you can add it to@Suite.SuiteClasses.
package mypackage;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ MyClassTest.class })
public class AllTests {
}
4.6. Run your test via code
You can also run your tests from via your own code. The org.junit.runner.JUnitCore class provides therunClasses() method which allows you to run one or several tests classes. As a return parameter you receive an object of the type org.junit.runner.Result. This object can be used to retrieve information about the tests.
In your test folder create a new class MyTestRunner with the following code. This class will execute your test class and write potential failures to the console.
package de.vogella.junit.first;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class MyTestRunner {
  public static void main(String[] args) {
    Result result = JUnitCore.runClasses(MyClassTest.class);
    for (Failure failure : result.getFailures()) {
      System.out.println(failure.toString());
    }
  }
}
5. Mocking with EasyMock
Unit testing uses also mocking of objects. In this case the real object is replaced by a replacement which has a predefined behavior the test. There are several frameworks available for mocking. To learn more about mock frameworks please see EasyMock Tutorial
6. Thank you


Testing with EasyMock
This tutorial explains testing with the EasyMock framework within Eclipse. It is based on the EasyMock 3.1 release.


1. Prerequisites

The following tutorial is based on an understanding of unit testing with the JUnit framework.
In case your are not familiar with JUnit please check the following JUnit Tutorial.

2. EasyMock


EasyMock is a mock framework which can be easily used in conjunction with JUnit. The following description demonstrates the usage of EasyMock.
EasyMock instantiates an object based on an interface or class.
import static org.easymock.EasyMock.createNiceMock;
....

// ICalcMethod is the object which is mocked
ICalcMethod calcMethod = createNiceMock(ICalcMethod.class); 
The createNiceMock() method creates a mock which returns default values for methods which are not overiden. A mock created with the Mock() method will fails in such a case.
EasyMock has several methods which are used to configure the Mock object. The expect() method tells EasyMock to simulate a method with certain arguments. The andReturn() method defines the return value of this method for the specified method parameters. The times() method defines how often the Mock object will be called.
The replay() method is called to make the Mock object available.
// setup the mock object
expect(calcMethod.calc(Position.BOSS)).andReturn(70000.0).times(2);
expect(calcMethod.calc(Position.PROGRAMMER)).andReturn(50000.0);
// Setup is finished need to activate the mock
replay(calcMethod); 

3. Download Easy Mock

Download EasyMock from the EasyMock Homepage and add the easymock.jar library to your classpath.
http://easymock.org/ 
You also need to download the Objenesis and Cglib libraries and add these jars to your classpath.

4. Tutorial: Using Easy Mock and JUnit

4.1. Create project and classes

Create a new Java Project called com.vogella.testing.easymock.first. Create the following classes.
package com.vogella.testing.easymock.first;

public enum Position {
    BOSS, PROGRAMMER, SURFER
} 
package com.vogella.testing.easymock.first;

public interface ICalcMethod {
    double calc(Position position);
} 
package com.vogella.testing.easymock.first;

public class IncomeCalculator {

    private ICalcMethod calcMethod;
    private Position position;

    public void setCalcMethod(ICalcMethod calcMethod) {
        this.calcMethod = calcMethod;
    }

    public void setPosition(Position position) {
        this.position = position;
    }

    public double calc() {
        if (calcMethod == null) {
            throw new RuntimeException("CalcMethod not yet maintained");
        }
        if (position == null) {
            throw new RuntimeException("Position not yet maintained");
        }
        return calcMethod.calc(position);
    }
} 
The IncomeCalculator class should be tested. The class has the purpose to calculate the salary of a person based on the provided method and position. Obviously the test depends on the provided methods.

4.2. Create tests

Create a new test source folder in your project.
Create a new test for IncomeCalculator and place the new test class in this folder.
package com.vogella.testing.easymock.first.test;

// use static imports to 
// have direct access to these methods
import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Test;

import com.vogella.testing.easymock.first.ICalcMethod;
import com.vogella.testing.easymock.first.IncomeCalculator;
import com.vogella.testing.easymock.first.Position;

public class IncomeCalculatorTest {

  private ICalcMethod calcMethod;
  private IncomeCalculator calc;

  @Before
  public void setUp() throws Exception {
    // NiceMocks return default values for
    // unimplemented methods
    calcMethod = createNiceMock(ICalcMethod.class);
    calc = new IncomeCalculator();
  }

  @Test
  public void testCalc1() {
    // Setting up the expected value of the method call calc
    expect(calcMethod.calc(Position.BOSS)).andReturn(70000.0).times(2);
    expect(calcMethod.calc(Position.PROGRAMMER)).andReturn(50000.0);
    // Setup is finished need to activate the mock
    replay(calcMethod);

    calc.setCalcMethod(calcMethod);
    try {
      calc.calc();
      fail("Exception did not occur");
    } catch (RuntimeException e) {

    }
    calc.setPosition(Position.BOSS);
    assertEquals(70000.0, calc.calc(), 0);
    assertEquals(70000.0, calc.calc(), 0);
    calc.setPosition(Position.PROGRAMMER);
    assertEquals(50000.0, calc.calc(), 0);
    calc.setPosition(Position.SURFER);
    verify(calcMethod);
  }

  @Test(expected = RuntimeException.class)
  public void testNoCalc() {
    calc.setPosition(Position.SURFER);
    calc.calc();
  }

  @Test(expected = RuntimeException.class)
  public void testNoPosition() {
    expect(calcMethod.calc(Position.BOSS)).andReturn(70000.0);
    replay(calcMethod);
    calc.setCalcMethod(calcMethod);
    calc.calc();
  }

  @Test(expected = RuntimeException.class)
  public void testCalc2() {
    // Setting up the expected value of the method call calc
    expect(calcMethod.calc(Position.SURFER)).andThrow(new RuntimeException("Don't know this guy")).times(1);

    // Setup is finished need to activate the mock
    replay(calcMethod);
    calc.setPosition(Position.SURFER);
    calc.setCalcMethod(calcMethod);
    calc.calc();
  }

} 
After execution of the test you can call the verify method to check if the mock object was called as defined.