Notice

╠ This is my personal blog and my posts here have nothing to do with my employers or any other association I may have. It is my personal blog for my personal experience, ideas and notes. ╣

Monday, December 25, 2017

Spring Dependency Injection

Overview 
As a developer, we trend to create modular and loosely coupled applications. To accomplish modularity developer divided the application into multiple classes and each class depends on one or more other classes to accomplish desired functionality in an application. Spring is the most popular dependency injection framework for Java. 

Let take an example to understand the dependency. A person has a name. It also means 'Person' has a dependency on 'Name'. 

public class Person { 
       private Name personName; 

To accomplish independence from other dependent classes Spring framework provides Inversion of Control (IoC) which is basically inverting the dependency injection flow. In IoC, Spring container inject those dependencies when it creates the beans. We will now see how to define this bean and inject the dependent object in it. 


Type of dependency injection 
Primarily there are two types of dependency injection 
1.     Constructor-based dependency injection 
2.     Setter-based dependency injection 

Constructor-based dependency injection 
Constructor-based dependency injection is accomplished by constructor arguments where each argument represents the dependency. 

package example; 

public class Person { 
   private Name personName; 

// Dependency injected via argument by Spring 
   public Person(Name personName){ 
     this.personName = personName; 
   } 
Spring Bean Definition 
<bean id="personName" class="example.Name" /> 

<bean id="constructorExample" class="example.Person"> 
        <constructor-arg type="example.Name" ref="personName" /> 
</bean> 

Constructor based injection using static factory method 
package example;  

public class PersonFactory { 
public static Person createPerson(Name personName){ 
return new Person(personName); 

Spring Bean Definition 
<bean id="personName" class="example.Name" /> 

<bean id="person" class="example.PersonFactory"  
         factory-method="createPerson"> 
    <constructor-arg ref="personName" /> 
</bean> 
 


Setter-Based Dependency injection 
Setter-based dependency injection is accomplished by calling the Java bean write method (i.e. setter) on object which is created using no-argument constructor or no-argument static factory method.

public class Person { 
private Name personName; 
// Setter method so that Spring can inject the dependency  
public void setPersonName(Name personName){ 
this.personName = personName; 
Spring Bean Definition 
<bean id="personName" class="example.Name" /> 

<bean id="setterExample" class="example.Person"> 
        <property name="personName" ref="personName" /> 
</bean> 

Setter based injection using static factory method 
package example;  

public class Person {  
private Name personName;  

private Person(){ 


// Factory method 
public static Person createPerson(){ 
return new Person(); 

private void setPerson(Name personName){  
this.personName = personName;  
}  
Spring Bean Definition 
<bean id="personName" class="example.Name" />  

<bean id="person" class="example.Person"  
        factory-method="createPerson">  
<property name="personName" ref="personName" /> 
</bean> 




So, where to use constructor-based and when to use setter-based ?
Constructor based injection is recommended when we want dependencies to be injected during creation of object. In other words if dependency is absolutely mandatory then we will use constructor, and if dependency is option then we could use setter based injection. 
Now what if during first phase of development, dependency was optional so we used setter based injection but now due to change in requirement dependency is mandatory. To deal with this kind of scenario Spring provide an annotation @Required which actually enforce that setter based dependency must be populated during bean creation otherwise it will raise BeanInitializationException exception. 

public class Person {  
private Name personName;  
// Setter method so that Spring can inject the dependency  
 @Required 
 public void setPersonName(Name personName){  
    this.personName = personName;  
 }  


Spring Bean Definition 
<bean id="personName" class="example.Name" /> 

<bean id="setterExample" class="example.Person"> 
        <!-- Not injecting setter based dependency -->  
        <!--property name="personName" ref="personName" / --> 
</bean> 

Exception message will be printed as "Property 'personName' is required for bean 'setterExample'". 
So technically developers need to take decision based on their business requirement which type need to use. 
 

Sunday, February 5, 2017

Spying on JavaScript Method using Jasmine [Part - 2]

Basic Introduction of JavaScript Testing Framework Jasmine

The basic of Jasmine is already discussed in my earlier blog. To read it please click here.


Spying with Jasmine

One of the basic reason of spying is to do unit testing of method in isolation. So we mock the method to bypass other dependencies.  

Let's see what Jasmine offer us.

Spy on a method 

spyOn(object, methodName) it can only exist inside the describe or it block. In another words it can only be define inside spec or suit, and it will be removed once spec get existed.  

spyOn(obj , 'setName');

When you want to invoke the actual method but call will be tracked.

spyOn(obj , 'setName').and.callThrough();

When you call and.callThrough, the spy acts as a proxy, calling the real function, but passing through a spy object allowing you to add tests like expectation.

When you want to remove the effect of spyOn(obj , 'setName').and.callThrough()

spyOn(obj , 'setName').and.stub() removes the effect of
spyOn(obj , 'setName').and.callThrough() on a spy.

When you want to return a specific value on all calls.

spyOn(obj, "getName").and.returnValue('Andy');

When you want to invoke a custom function.

spyOn(obj, "getName").and.callFake(function(arguments, here) { return 'Andy'; });

When you want to throw specific error.

spyOn(obj, "setName").and.throwError("myError");




Every call to a spy is tracked and exposed on the calls property.


Calls Purpose
any() returns false if the spy has not been called at all, and then true once at least one call happens.
expect(obj.setName.calls.any()).toEqual('Andy');
count() returns the number of times the spy was called.
expect(obj.setName.calls.count()).toEqual(2);
argsFor(index) returns the arguments passed to call number index.
obj.setName('Andy'); expect(obj.setName.calls.argsFor(0)).toEqual(["Andy"]);
allArgs() returns the arguments to all calls.
obj.setName('Andy'); 
obj.setName('Anindya','Banerjee'); 
expect(obj.setName.calls.allArgs()).toEqual([["Andy"],["Anindya","Banerjee"]]);
all() returns the context (the this) and arguments passed all calls.
obj.setName('Andy'); 
expect(obj.setName.calls.all()).toEqual({object: obj, args: ["Andy"]});
mostRecent() returns the context (the this) and arguments for the most recent call.
obj.setName('Andy'); 
obj.setName('Anindya','Banerjee'); 
expect(obj.setName.calls.mostRecent()).toEqual({object: obj, args:"Anindya","Banerjee"});
first() returns the context (the this) and arguments for the first call.
obj.setName('Andy'); 
obj.setName('Anindya','Banerjee');
expect(obj.setName.calls.first()).toEqual({object: obj, args:"Andy"});

reset()
clears all tracking for a spy.
obj.setName.calls.reset();

There is a property named object which actually return the this object when the spy was call. This can be used with all/mostReset/first.
Please note here these methods are return an array of json object and one of key is object. Please refer to the above table.

Here is an example.

obj.setName('Andy');
obj.setName('Anindya','Banerjee');
expect(obj.setName.calls.all().object).toEqual(obj);
expect(obj.setName.calls.mostRecent().object).toEqual(obj);
expect(obj.setName.calls.first().object).toEqual(obj);



There are three matchers toHaveBeenCalledtoHaveBeenCalledWith and toHaveBeenCalledTimes.

toHaveBeenCalled return true if the spy was called.

expect(obj.setName).toHaveBeenCalled();


toHaveBeenCalledWith return true if the argument list matches any of the recorded calls to the spy.


expect(obj.setName).toHaveBeenCalledWith('Andy');

toHaveBeenCalledTimes return true if the spy was called the specified number of times.

expect(obj.setName).toHaveBeenCalledTimes(2);


What we will do if we didn't have a function to spy on ?

Jasmine provide functions to deal with this kind of situations. 

Function Purpose
jasmine.createSpy create a spy function which doesn't exist.
var dummyFunction = jasmine.createSpy('dummy function'); $('#mybutton').click(dummyFunction);
jasmine.createSpyObj create multiple spy function which doesn't exist.
// Suppose you got a Json object from back-end // Person is a spy object with getName and getAge methods and an id property var person = jasmine.createSpyObj("person", ["getName", "getAge"]); person.id = 1234;
jasmine.any returns true if the constructor matches the constructor of the actual value.
expect({}).toEqual(jasmine.any(Object)); expect(12).toEqual(jasmine.any(Number));
jasmine.anything returns true if the actual value is not null or undefined.
expect({}).toEqual(jasmine.anything());
jasmine.objectContaining when an expectation only cares about certain key/value pairs in the actual.
var person = { firstName : "Anindya", lastName : "Banerjee", aliasName : "Andy" }; expect(person).toEqual(jasmine.objectContaining({ aliasName: "Andy" }));
jasmine.arrayContaining when an expectation only cares about some of the values in an array.
var num = [1, 2, 3, 4]; expect(num).toEqual(jasmine.arrayContaining([3, 1])); expect(num).not.toEqual(jasmine.arrayContaining([6]));
jasmine.stringMatching When match a portion of a string in a spy expectation.
expect({names: 'AndyAnindya'}).toEqual({names: jasmine.stringMatching('Andy')});

Basic introduction of JavaScript testing framework Jasmine [Part - 1]

While working on user interface I found that I need to write complex logic in JavaScript
to validate the form data. I started writing validation logic then deploy it in web container and when I find any bug I fix it and redeploy the JavaScript in web container.

Development Cycle



At one point I find that I am simply struggling with complex logic and I realize due to this process I am losing time.

Then I started exploring JavaScript testing framework and asked few of user interface experts they told me that they are using JSUnit for testing but one of them recommend me explore Jasmine JavaScript testing framework. So here I'm to share my experience and a brief tutorial / cheat sheet for JUnit users. 

Jasmine 

Jasmine is a behavior-driven development framework for testing JavaScript code. It does not rely on browsers, DOM, or any JavaScript framework. 

You can download it from here. Here we will be using Jasmine version 2.5.2.

Once you extract the zip we will see three folder, one html file and one license file.
Inside Jasmine zip

  1. /src folder: contains the JavaScript source files that you want to test
  2. /lib folder: contains the framework files
  3. /spec folder: contains the JavaScript testing files
  4. SpecRunner.html : is the test case runner HTML file

Please note that actual JavaScript file need to be included before spec/test JavaScript file.

SpecRunner.html

Learn the Syntax of Jasmine

Suite

A Jasmine suite is a collection of sub-suites and/or test cases to test the behaviour of JavaScript function or JavaScript Object. This suite is define by describe keyword. describe take two parameters, first parameter is string and second parameter is JavaScript function.

describe("Test Suite", function(){
      // test case here
});


An inner test suite example

describe("Test Suite", function(){
   

   describe("1st inner Suite" , function() {

   });

   describe("2nd inner Suite" , function() {

   });
});

To disable a suit just simply put a 'x' before keyword describe.

describe("Test Suite", function(){

   xdescribe("Disable Suite" , function() {


   });
   describe("2nd inner Suite" , function() {





   });
});



Spec

A Jasmine spec represents a test case inside the test suite. This spec is defined by it keyword. it also have two parameters,  first parameter is string and second parameter is JavaScript function.

describe("Test Suite", function(){
      it("Spec", function() {
            // test matchers here
      });
});

To disable a spec just simply put a 'x' before keyword it.

describe("Test Suite", function(){
      xit("Disable Spec", function() {
            // test matchers here
      });
});


Setup and Tear down

For setup and teardown Jasmine provide four global beforeAll, afterAll, beforeEach and afterEach functions.

As these names implied, the beforeAll is invoked once before all the spec in describe are run, and the afterAll function is called after all spec finish and the beforeEach is invoked once before each the spec in describe are run, and the afterEach function is called after each spec finish.

describe("Test Suite", function(){

    beforeAll("Before all spec inside this suite", function() {
         // Suite level setup code here
     });

    beforeEach("Before each spec inside this suite", function() {
         // Spec level setup code here
     });
     
      it("Spec1", function() {
            // test matchers here
      });


      it("Spec2", function() {
            // test matchers here
      }); 

     afterEach("After each spec inside this suite", function() {
          // Spec level tear down code here
     });

     afterAll("After all Spec inside this suite", function() {
          // Suite level tear down code here
     });

});

The this keyword

Another way to share variables between a beforeEach, it, and afterEach is through the this keyword. Each spec's beforeEach/it/afterEach has the this as the same empty object that is set back to empty for the next spec's beforeEach/it/afterEach.


Available Matchers in Jasmine

Matcher Purpose
toBe() passed if the actual value is of the same type and value as that of the expected value. It compares with === operator
toEqual() works for simple literals and variables;
should work for objects too
toMatch() to check whether a value matches a string or a regular expression
toBeCloseTo() check if a number is close to another number, given a certain amount of decimal precision as the second parameter.
expect(12.34).toBeCloseTo(12.3, 1); // success
expect(12.34).toBeCloseTo(12.3, 2); // failure
toBeDefined() to ensure that a property or a value is defined
toBeUndefined() to ensure that a property or a value is undefined
toBeNull() to ensure that a property or a value is null.
toBeNaN() this is different from JavaScript's build-in isNaN function. The build-in will return true for non-numeric type, objects, and arrays. Jasmine's toBeNaN will be positive only if it's the NaN value.
expect(parseInt("hello")).toBeNaN(); // success
toBeTruthy() to ensure that a property or a value is true
toBeFalsy() to ensure that a property or a value is false
toContain() to check whether a string or array contains a substring or an item.
toBeLessThan() for mathematical comparisons of less than
toBeLessThanOrEqual() for mathematical comparisons of less than or equal
toBeGreaterThan() for mathematical comparisons of greater than
toBeGreaterThanOrEqual() for mathematical comparisons of greater than or equal
toBeCloseTo() for precision math comparison
toThrow() for testing if a function throws an exception
toThrowError() for testing aspecificthrown exception
toHaveBeenCalled() return true if the spy was called
toHaveBeenCalledWith() return true if the argument list matches any of the recorded calls to the spy
toHaveBeenCalledTimes() return true if the spy was called the specified number of times



Write and Execute a test

Lets begin with Jasmine , I will be using Eclipse as my intregated development enviroment (IDE) tool. You can use any IDE or notepad editors of your choice.

Step 1

I create a JavaScript project named 'JasminTutorial' and under that project I create a folder named js.
In js folder I copied Jasmine's lib folder and SpecRunner.html file only.

Step 2

Write a JavaScript file which I will be using to test.

HelloWorld.js
  1. HelloWorld = function() {};
  2. HelloWorld.prototype.helloWorld = function(){
  3. return "Hello World!";
  4. }
  5. HelloWorld.prototype.sayHi = function(name) {
  6. return "Hi " + name;
  7. }

Step 3

Now I create a spec folder under js folder to sperate this test file. I create another JavaScript file named HelloWorldSpec.js which is to test HelloWorld.js.


Step 4

Now write the spec.


HelloWorldSpec.js
  1. describe("test HelloWorld", function(){
  2. var helloworld;
  3. beforeAll(function(){
  4. helloworld = new HelloWorld();
  5. });
  6. // 1st Suite for helloWorld
  7. describe("test helloWorld function ", function(){
  8. it("test helloWorld function return 'Hello World!' string", function(){
  9. expect(helloworld.helloWorld()).toEqual("Hello World!");
  10. });
  11. it("do negative test with helloWorld function", function(){
  12. expect(helloworld.helloWorld()).not.toEqual("Hi Andy");
  13. });
  14. });
  15. // 2nd Suite for sayHi
  16. describe("test sayHi function ", function(){
  17. it("test sayHi function return 'Hi Andy' string", function(){
  18. expect(helloworld.sayHi('Andy')).toEqual("Hi Andy");
  19. });
  20. it("do negative test with helloWorld function", function(){
  21. expect(helloworld.sayHi('Andy')).toContain("Andy");
  22. });
  23. });
  24. });

Step 4

Now change in SpecRunner.html , add JavaScript file (HelloWorld.js) and corresponding Spec file (spec/HelloWorldSpec.js).

SpecRunner.html


Now lets see the result.



Now I have changed my development cycle. As a result I'm saving time because I need not have to do redeployment.




Sunday, March 20, 2016

How a Http Server works and develop a simple web server for yourself


I worked with HTTP Servers for a long time but I never tried to explore how an HTTP server works, so let do it together.

What is an HTTP server actually? 

A server which runs in a computer to serve HTTP request from remote browsers.

How it works?

As a user when we type an URL (Unified Resource Locator) in our browser it sends an HTTP request to the internet then it forwarded to the concerned server, the server process the request and send back HTTP response to the client browser.

Okay, it sounds simple to make HTTP server work. 

Let's get into it.
A Server class which creates a server program, do initial setup for web server operation like port number, file directory etc then it run in an infinite loop and wait for requests.


In request object we are going to process the HTTP request coming from client browsers. In Request.java class we are going to process the request and see the URLs browsers are requesting are valid or not.
In request class we are read the request from socket request stream and striping up the requested page name.



Now let work on HTTP response. In this class we create a response and send it back to requesting browser.
So what we are doing here is we are looking at a specific location where all the web files should be present and if the requested page found then simply write it to socket output stream.



Source Code of SimpleHttpWebServer

Welcome to Anindya's Simple Web Server


Thursday, March 17, 2016

Immutable Object


What is Immutable Object?
The immutable object is the object whose state cannot be changed, but immutable object values often required modification and we are not here to declare constants so whenever immutable object values get modified it create a new object with new value.

We need immutable object as a cache key. As in no circumstances we are not allowed to change the cache key.

Here we will see how to create an immutable object and what are the advantages and disadvantages of it.

A class needs to meet certain conditions to become immutable object when we create an object of it. We will also see why this condition needs to be meet in order to get the immutable object.

  1. The class must be declared final. Why? So that no one can extend it. 
  2. All its fields must be final and private. Why? So that no can access from outside of this class and once the value is set then no one can change it. 
  3. Don't allow "setter" methods in order to protect any change. Why? No one can change the value from outside.
  4. Any field which is a type of mutable (value can be changed) object should not directly return the reference. Otherwise, caller method can modify the value of it. 
  5. Any method which modifies any field which is a type of mutable object should be private
  6. The class may have factory method instead of the constructor as it will provide more sophistication. 
  7. In some of the cases where outside code needs to send mutable object to immutable object here things become more tricky. In this case, we need to create a defensive copy of that mutable parameter object and so that immutable class can work on a separate copy.  

ImmutableClass.java
  1. import java.util.*;
  2. import java.util.concurrent.*;
  3. public class ImmutableClass {
  4. private final Date callDate;
  5. private final String friendName;
  6. private final List<String> address = new CopyOnWriteArrayList<String>();
  7. public ImmutableClass(Date date, String name, List<String> modifyData){
  8. callDate = new Date(date.getTime());
  9. friendName = name;
  10. Collections.copy(address,modifyData);
  11. if(callDate.compareTo(new Date()) == 1) {
  12. throw new IllegalArgumentException("Date cannot be greater than today's date. ");
  13. }
  14. modifyAddress();
  15. }
  16. public Date modifyDate(Date date){
  17. return new Date(date.getTime());
  18. }
  19. private void modifyAddress(){
  20. address.add(this.friendName);
  21. }
  22. public List<String> getAddress() {
  23. return Collections.unmodifiableList(address);
  24. }
  25. }

Monday, March 7, 2016

Yet another date time in Java 8

In Java 8 there is new package introduced related to Date and Time java.time package.  
This improvement is done against JSR - 310 by threeten.org  


Why we need another Date/Time class in Java 8 as there is already lots of Date/Time class available in previous Java versions ?

In previous versions of JDK we didn't find good support for date/time operation. Everything thing jumbled up together in Date and Calendar class as a result it is not convenient to use for developer.
Date and Time is wrapped in Date class. So whenever we need to do operations related to date only then we need to remove the time component from Date object. To do simple things developers need to write lots of extra code just to do formatting, getting date/time and day light saving. Using Date class we cannot go below milliseconds. 
More than that Date class have lots of bugs in API and equals method equality breaks when using date object case to case basis. 

In case of Calendar class it better than Date but it still have some problems like date formatting support is not available. We need to covert Calendar object to Date object to do formatting. 


So, what we are going to get in JDK 8  Date and Time ? 

  1. Clear bug free API for date, time and date-time. 
  2. All these objects are thread safe as these objects are immutable. 
  3. Package are organised well and classes are design to cater API extensible.  
Detail about new Java 8 Date-Time 



Examples can be downloaded from https://github.com/andysundar/JDK8