Java EE 6 vs. Spring Framework: A technology decision making process
I came across this article which I just want to share with others – I found this a good read.
Java EE 6 vs. Spring Framework: A technology decision making process.
I came across this article which I just want to share with others – I found this a good read.
Java EE 6 vs. Spring Framework: A technology decision making process.
Last week I have made released a framework iFramework with a coupe of features. Here is another release that adds a few more features to the it. You can find the details
This simple yet powerful implementation provides some of the most desirable features in any Exception Handling framework:
This package provides factory to fetch various types of Message Readers. The current implementation only supports Resource Bundle based message stores, but it can be easily extended to other data stores.
This package provides base classes that allow you to run your functions in a producer and consumer fashion. By using this framework you have to worry about writing your business logic for a Producer and a Consumer and not about multi-threading and how these two entities interact. This framework currently has only one implementation of a Broker i.e AsyncQueueBroker but more will be added in coming months.
In enterprise world, where performance holds the key to everything; the Concurrency patterns bring to table a very interesting and effective solution. One specific pattern Producer and Consumer allow us to write programs with high throughput and get the job done much quickly. This pattern provides us a solution for a common problem where we have to migrate data form System 1 to System 2 and in the process we need to do three tasks: Load data from Database based on groups, Process and Update the records back
You can read the complete post here on Scratchpad101.com (http://scratchpad101.com/2011/08/22/concurrency-pattern-producer-consumer/)
For many years now, I have always found myself going back to TestNG whenever it comes to doing Unit Testing with Java Code. Everytime, I picked up TestNG, people have asked me why do I go over to TestNG especially with JUnit is provided by the default development environment like Eclipse or Maven. Continuing the same battle, yesterday I started to look into Spring’s testing support. It is also built on top of JUnit. However, in a few minutes of using the same, I was searching for a feature in JUnit that I have always found missing. TestNG provides Parameterized Testing using DataProviders. Given that I was once again asking myself a familiar question – TestNG or JUnit, I decided to document this so that next time I am sure which one and why.
If you are just going to do some basic Unit Testing, both the frameworks are basically the same. Both the frameworks allow you to test the code in a quick and effective manner. They have had tool support in Eclipse and other IDE. They have also had support in the build frameworks like Ant and Maven. For starters JUnit has always been the choice because it was the first framework for Unit Testing and has always been available. Many people I talk about have not heard about TestNG till we talk about it.
Let us look at a very simple test case for each of the two.
package com.kapil.itrader;
import java.util.Arrays;
import java.util.List;
import junit.framework.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class FibonacciTest
{
private Integer input;
private Integer expected;
@BeforeClass
public static void beforeClass()
{
// do some initialization
}
@Test
public void FibonacciTest()
{
System.out.println("Input: " + input + ". Expected: " + expected);
Assert.assertEquals(expected, Fibonacci.compute(input));
assertEquals(expected, Fibonacci.compute(input));
}
}
Well, this is example showcases I am using a version 4.x+ and am making use of annotations. Priori to release 4.0; JUnit did not support annotations and that was a major advantage that TestNG had over its competitor; but JUnit had quickly adapted. You can notice that JUnit also supports static imports and we can do away with more cumbersome code as in previous versions.
package com.kapil.framework.core;
import junit.framework.Assert;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class BaseTestCase
{
protected static final ClassPathXmlApplicationContext context;
static
{
context = new ClassPathXmlApplicationContext("rootTestContext.xml");
context.registerShutdownHook();
}
@BeforeSuite
private void beforeSetup()
{
// Do initialization
}
@Test
public void testTrue()
{
Assert.assertTrue(false);
}
}
A first look at the two code, would infer that both are pretty much the same. However, for those who have done enough unit testing, will agree with me that TestNG allows for more flexibility. JUnit requires me to declare my initialization method as static; and consequently anything that I will write in that method has to be static too. JUnit also requires me to have my initialization method as public; but TestNG does not. I can use best practices from OOP in my testing classes as well. TestNG also allows me to declare Test Suite, Groups, Methods and use annotations like @BeforeSuite, @BeforeMethod, @BeforeGroups in addition to @BeforeClass. This is very helpful when it comes to writing any level of integration testing or unit test cases that need to access common data sets.
Junit is very effective when it comes to testing in isolation. It essentially means that there is you can not control the order of execution of tests. And, hence if you have two tests that you want to run in a specific order because of any kind of dependency, you can not do that using JUnit. However, TestNG allows you to do this very effectively. In Junit you can make workaround this problem, but it is not neat and that easy.
A very powerful feature that TestNG offers is “Parameterized Testing”. JUnit has added some support for this in 4.5+ versions, but it is not as effective as TestNG. You may have worked with FIT you would know what I am talking about. However, the support added in JUnit is very basic and not that effective. I have modified my previous test case to include parameterized testing.
package com.kapil.itrader;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.List;
import junit.framework.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class FibonacciTest
{
private Integer input;
private Integer expected;
@Parameters
public static List data()
{
return Arrays.asList(new Integer[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
}
@BeforeClass
public static void beforeClass()
{
System.out.println("Before");
}
public FibonacciTest(Integer input, Integer expected)
{
this.input = input;
this.expected = expected;
}
@Test
public void FibonacciTest()
{
System.out.println("Input: " + input + ". Expected: " + expected);
Assert.assertEquals(expected, Fibonacci.compute(input));
assertEquals(expected, Fibonacci.compute(input));
}
}
You will notice that I have used @RunWith annotation to allow my test case to be parameterized. In this case, the inline method – data() which has been annotated with @Parameters will be used to provide data to the class. However, the biggest issue is that the data is passed to class constructor. This allows me to code only logically bound test cases in this class. And, I will end up having multiple test cases for one service because all the various methods in the Service wil require different data sets. The good thing is that there are various open source frameworks which have extended this approach and added their own “RunWith” implementations to allow integration with external entities like CSV, HTML or Excel files.
TestNG provides this support out of the box. Not support for reading from CSV or external files, but from Data Providers.
package com.kapil.itrader.core.managers.admin;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.uhc.simple.common.BaseTestCase;
import com.uhc.simple.core.admin.manager.ILookupManager;
import com.uhc.simple.core.admin.service.ILookupService;
import com.uhc.simple.dataprovider.admin.LookupValueDataProvider;
import com.uhc.simple.dto.admin.LookupValueRequest;
import com.uhc.simple.dto.admin.LookupValueResponse;
/**
* Test cases to test {@link ILookupService}.
*/
public class LookupServiceTests extends BaseTestCase
{
@Test(dataProvider = "LookupValueProvider", dataProviderClass = LookupValueDataProvider.class)
public void testGetAllLookupValues(String row, LookupValueRequest request, LookupValueResponse expectedResponse)
{
ILookupManager manager = super.getLookupManager();
LookupValueResponse actualResponse = manager.getLookupValues(request);
Assert.assertEquals(actualResponse.getStatus(), expectedResponse.getStatus());
}
}
The code snippet above showcases that I have used dataProvider as a value to the annotations and then I have provided a class which is responsible for creating the data that is supplied to the method at the time of invocation. Using this mechanism, I can easily write test cases and its data providers in a de-coupled fashion and use it very effectively.
For me the Parameterized Testing is the biggest reason why I choose TestNG over Junit. However, everything that I have listed above is the reason why I always want to spend a few minutes in setting up TestNG in a new Eclipse setup or maven project. TestNG is very useful when it comes to running big test suites. For a small project or a training exercise JUnit is fine; because anyone can start with it very quickly; but not for projects where we need 1000s of test cases and in most of those test cases you will have various scenarios to cover.
http://kapilvirenahuja.com/tech/2011/08/07/testng-or-junit/
In one of my previous lives, I used to work in Microsoft and there this word – NTLM (NT Lan Manager) was something that came to us whenever we used to work on applications. Microsoft OS have always provided us with an inbuilt security systems that can be effectively used to offer authentication (and even authorization to web applications).
Many years back, I moved over into Java world and when I was asked to carry out my very first security implementation, I realized that there was no easy way to do this and many clients would actually want us to use LDAP for authentication and authorization. For many years, I continued to use that. And, then one day in a discussion with a client, we were asked to offer SSO implementation and client did not have an existing setup like SiteMinder. I started to think about if we can go about using NTLM based authentication. The reason that was possible was because the application we were asked to build was to be used within the organization itself and all the people were required to login into a domain.
After some research, I was able to find out a way we could do this. We did a POC and showed it to the client and they were happy about it. What we did has been explained below:
Basically, we bypassed the “Login Screen” where the user was entering the password and used Domain information. Please note that it was possible for us because the Client guaranteed that there was this domain always and all users had unique NTIDs. Also, that it was their responsibility to shield the application from any external entry points where someone may impersonate the Domain/ID.
If you are interested, you can refer to the code below:
<%@ page import="sun.misc.BASE64Encoder" %>
<%
String auth = request.getHeader("Authorization");
String s = "";
//no auth, request NTLM
if (auth == null)
{
response.setStatus(response.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "NTLM");
return;
}
//check what client sent
if (auth.startsWith("NTLM "))
{
byte[] msg =
new sun.misc.BASE64Decoder().decodeBuffer(auth.substring(5));
int off = 0, length, offset;
if (msg[8] == 1) {
off = 18;
byte z = 0;
byte[] msg1 =
{(byte)'N', (byte)'T', (byte)'L', (byte)'M', (byte)'S',(byte)'S', (byte)'P',
z,(byte)2, z, z, z, z, z, z, z,
(byte)40, z, z, z, (byte)1, (byte)130, z, z,
z, (byte)2, (byte)2, (byte)2, z, z, z, z, //
z, z, z, z, z, z, z, z};
// send ntlm type2 msg
response.setStatus(response.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "NTLM "
+ new sun.misc.BASE64Encoder().encodeBuffer(msg1).trim());
return;
}
else if (msg[8] == 3) {
off = 30;
length = msg[off+17]*256 + msg[off+16];
offset = msg[off+19]*256 + msg[off+8];
s = new String(msg, offset, length);
// print computer name // out.println(s + " ");
}
else
return;
length = msg[off+1]*256 + msg[off];
offset = msg[off+3]*256 + msg[off+2];
s = new String(msg, offset, length);
//domain//out.println(s + " ");
length = msg[off+9]*256 + msg[off+8];
offset = msg[off+11]*256 + msg[off+10];
s = new String(msg, offset, length);
out.println("Hello "); out.println(s + "");
}
%>
I came across this very interesting article and the author’s views where programming started way back. Read here – http://java.sys-con.com/node/1879986
Internet body approves corporate web suffixes – http://pulse.me/s/i87x
I came across this topic on “filter bubble” via a fellow blogger. Although the blogger talks about the filtering in context to Atheism, amongst other things, I find this discussion very relevant to how we are building our web today and also very relevant to my job today.
I have been a part of a project/product where we did have similar discussions and ensuring that people are getting information relevant to them. The filter bubble that Eli Pariser mentions in his talk is exactly what we did try to build in our product and are even trying to do so today. We pick up programs that people have selected and we then pass on content based on the selections.
We do not go to the extent of what has been mentioned in this post where a common search result gives back different results. Eli also accepts the fact that personalization is important and we would definitely want to have some results based on what we like; it is simply a way to provide links that we like. However, it is equally important for me to view what are other areas in this regard and especially the view point of other group; to bring in a little flavor to the topic. however, if Google and Facebook or Yahoo and other giants are not doing that for us, then we are doomed. They need to add capability to ask the user what they want – preferential results or just results. Users need to have options, to decide what they need to view. We do not want things to be edited out for us and us not knowing what has been edited out. That way we just don’t know what we are missing – thats not what internet is supposed to do. It is like going into Nike and not knowing that Reebok also exists. Not Fair.
I do not find that title implied to me; but see it almost everyday being implied to others.
This is in a continuation to a post a while back. In that post, I talked about a project team and their view point on quality. Here is an addition to the same set of people. The conversation goes like
Project Manager: I was just checking on the defect count and noticed we have 24 open P1 and P2 in system. How are we are going to go live tomorrow. What just happened?
QA Manager 1: Nothing, it is just that my team currently does not have any work for Release 2. They had some time on their hands and hence pro-actively they started to test in R1 and logged these defects.
Project Manager 1: so what are you going to do?
QA Manager 1: Nothing, these are not R1 defects. Testing is closed. We wrapped that last week and all these have tobe logged in R2. These are not R1 defects.
Everyone else in the room which included Analyst, and Project Managers laughed about it and joked that this is funny and defects have to go away.
This was the last point of discussion in the meeting and I was shell shocked to say anything. Not that it would have mattered to these guys. I am responsible for the delivery of a different project and not the modules they were talking about. So, amongst all politics and shock I did not say anything. But, I was thinking what happened to quality here.
One of them was the QA Manager, who is responsible for quality of the project and she said “Testing is closed”. How would we feel if Toyota or Honda or any car manufacturer would say that decide to fix problems in the next batch or version of the car.
And what amuses me is that these discussions reach people who are Director IT and Vice President IT and they are in agreement of the fact that “Testing is closed”
I chanced upon this yesterday (http://sourceforge.net/adobe/cairngorm/home/) and was impressed with what they are trying to do. Building upon Parsley looking to do a lot of injection and also support modules that they have been criticized for in the past.
Looks like Adobe Cairngorm team finally got it right.