Friday, May 24, 2013

Alternative solution on PreRenderView

Usually when I want to determine whether the page is first loaded, I will put the following code in Facelet template.

    

Unfortunately that solution does not work under JSF 2.0 and below. The alternative is to add beforePhase attribute name right inside the <f:view> tag like this:

On the action class, add following code to perform the necessary task need to be complete before a page is render:
    public void doPreRenderView(PhaseEvent event) {
      
      if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
        // do your stuff
      }
    }

Can not find Facelets taglib in JSP


This is an old project develop using JSP, my plan was to inject some new element into this project. I got the error Can not find the tag library descriptor for http://java.sun.com/jsf/facelets when the following line was included in this JSP page:

< %@ taglib uri="http://java.sun.com/jsf/facelets" prefix="ui"% >

This is impossible because Facelets is to replace JSP, it is a XML based view technology which is designed to be a successor of JSP. If I do this, this is definitely a brainless move. No choice, in order to use Facelets, I must have XHTML but not JSP.

Alias in Java

There is a very interesting question I got in the JAVA exam. Take a look at the following code, when does the object at line marked (0) will be the candidate for garbage collection?
String objA = “ABC”;  // (0)
String objB = a;

objA = “DEF”;  // (1)
objB = “GHI”;  // (2)
The answer is the line marked (2). Why not the line marked (1)? Because String objB is holding a reference of objA thus it will not get garbage collected. Until line marked (2), objB is assigned a new object, objA is no longer get reference by objB or no other objects are holding a reference of it, thus this is the great chance to dispose the object.

Saturday, May 18, 2013

Bootstrap.jar was not found in classpath?

I was working on Tomcat 7 and I suspecting there are some problem with my existing Tomcat installation, thus I decided to re-download again to make sure a clean copy was use in my development. This time I download it into another folder name to ensure the old Tomcat directory is no longer using. Here is the problem, when I start the server, I get the following error:

The archive: D:/tool/Tomcat7/bin/bootstrap.jar which is referenced by the classpath, does not exist.

What a joke? My existing project (in Eclipse) was not reflected by the new Tomcat location? No, not that way. I mean take a look at the server classpath, goto Run -> Debug/Run Configuration -> Classpath tab, notice that the new bootstrap.jar path was there but the old path were still remain. Thus I have to manually remove the one and put the new one in exactly the same order as the existing one.

Transition from Mockito to PowerMock

There was an error throwing out when I change the mocking framework from Mockito to PowerMock. See this error stack:

java.lang.ExceptionInInitializerError
 at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.(ConditionalStackTraceFilter.java:17)
 at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30)
 at org.mockito.exceptions.base.MockitoException.(MockitoException.java:19)
 at org.mockito.exceptions.misusing.MockitoConfigurationException.(MockitoConfigurationException.java:18)
 at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:145)
 at org.mockito.internal.configuration.ClassPathLoader.findPluginImplementation(ClassPathLoader.java:110)
 at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:106)
 at org.mockito.internal.configuration.ClassPathLoader.(ClassPathLoader.java:59)
 at org.mockito.internal.util.MockUtil.(MockUtil.java:21)
 at org.mockito.internal.MockitoCore.(MockitoCore.java:40)
 at org.mockito.Mockito.(Mockito.java:932)
 at org.huahsin.BuilderTest.(BuilderTest.java:41)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.createTestInstance(PowerMockJUnit44RunnerDelegateImpl.java:188)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.createTest(PowerMockJUnit44RunnerDelegateImpl.java:173)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:195)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
 at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
 at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
 at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)
 at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
 at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NullPointerException
 at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.(StackTraceFilter.java:21)
 ... 33 more

I don’t know where this stupid NullPointerException came from? The java.lang.ExceptionInInitializerError is real misleading, and I was being fool with this stupid error. If I am able to realize that MyTestClass has implement PowerMock framework already, then the mock implementation should also has to change accordingly. The actual problem on this error is that I was mess up the mock framework where I suppose to use the PowerMock’s API instead of Mockito’s API.

This is the code that causes the error shown above.

@RunWith(PowerMockRunner.class)
@PrepareForTest(CommonDao.class)
public class MyTestClass {

   private static TheMockClass theMockClass = Mockito.mock(TheMockClass.class);
}

To resolve this issue, use PowerMock.mock(TheMockClass.class); when initialize a mock object.

Sunday, May 12, 2013

Have some fun with encryption and decryption

I just had some fun with gpg command on Windows. Basically when I first download and install from the Gpg4win site, the first thing I am going to do is to encrypt a file, called secret_file.txt. No! Wait! Before this can be done, gpg need to be setup first.

Do this: gpg --gen-key

A series of interview question, just answer them accordingly. No worry, just a very simple question, you should able to answer. After this, get ready to encrypt the file.

Sending out this command: gpg --encrypt --recipient secret_file.txt

This will generate an output file, called secret_file.txt.gpg. To decrypt the encrypted file, send this command:

gpg --output secret_file.txt --decrypt secret_file.txt.gpg

Enjoy!

Wednesday, May 1, 2013

When there is a class inherited from parent class, how could I mock the parent class with Mockito?

Unit testing consisting of running a series of test case on the particular unit/module, in this case, the unit is referring to a JAVA class. This should not be a problem if it is a standalone class, but things started to get complicated when it comes to inheritance where the parent class is doing all the preliminary work for a child class.

When this is the case, it is impossible for me to execute the test on parent class since the scope of this test is only focus on the child class, which is mine module. It is worth the effort if I could mock the parent class regardless of whether the parent class is running correctly or not and I'll just concentrate on my module.

This is the code snippet I'm trying to do how it could be done.
public class ClassA {

 private int a = 1;
 
 public int getA() {
  return a;
 }
}

public class ClassB extends ClassA {

 private int b;
 
 public int getB() {
  return getA() + b;
 }
 
 public void setB(int b) {
  this.b = b;
 }
}

public class TestMainApp {

 ClassA a;
 ClassB b;
 
 @Before
 public void setup() {
  
  a = Mockito.mock(ClassA.class);
  Mockito.when(a.getA()).thenReturn(2);
 }
 
 @Test
 public void testFunctionA() {
  
  b = new ClassB();
  b.setB(a.getA());
  
  assertEquals("testFunctionA()", b.getB(), 3);
 }
}
In this sample, I am trying to mock the parent class, ClassA to return me a value 2 instead of 1, and pass it to ClassB will return me 3. Thus the output of this test is success.