Monday, January 26, 2015

Missing passphrase in JSch

I have a key pair being setup with passphrase, when I use this key to establish a SFTP connection through following code snippet, I still got an authentication error.
  ...

  JSch jsch = new JSch();
  jsch.addIdentity(privateKey);

  ...
I was wondering why this could happened? Am I making any mistake when generating the key pair? Even though stacktrace below showing me some clue but I still couldn't find out the real root cause.
com.jcraft.jsch.JSchException: USERAUTH fail
   at com.jcraft.jsch.UserAuthPublicKey.start(UserAuthPublicKey.java:118)
   at com.jcraft.jsch.Session.connect(Session.java:463)
   at com.jcraft.jsch.Session.connect(Session.java:183)
Or maybe I have configure the passphrase wrongly? This took me sometimes reading on the Java Secure Channel documentation. Found out that I have overlook on the addIdentity() which could actually take the passphrase as 2nd parameter. Without further ado, the code snippet above have been updated as shown below and test run it:
  ...

  JSch jsch = new JSch();
  jsch.addIdentity(privateKey, "Passphrase");

  ...
Tada! I have the SFTP connection established successfully.

Monday, January 12, 2015

Adding Tomcat plugin into existing Maven configuration

I have an existing POM which have been configure to use WAS Liberty Profile as my development, and now I'm going to add new configuration that could support Tomcat as well. I'm wondering whether this will harm my project? I'm also worry there will be lot of work need to be done.

Basically I was wrong, and I was so wrong. I did the experiment, it will not harm my existing configuration. All I need to do is to add following pieces into existing POM with the goal clean install tomcat7:run and the web app will fly.
    <build>
        ...

        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.0</version>
            </plugin>

            ...
            ...
        </plugins>
    </build>

Sunday, January 11, 2015

Reading properties file with Spring framework

Good day, I'd learned a new way of reading properties file with Spring. Usually when setting up a project with Spring framework, one common thing should have in Spring is following code:
   <bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:config/application.properties</value>
                <value>classpath:config/database/database.properties</value>
            </list>
        </property>
   </bean>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="properties" ref="applicationProperties" />

    </bean>

I guess that was the classic design of a Spring project. Let's assume the application.properties file containing following content:

input.path=./
output.path=./

In order to read that properties, this is what I did in a my class:
public class MyClass
   protected Logger log = Logger.getLogger(this.getClass().getName());

   @Value("#{applicationProperties['input.path']}")
   private String filePath;
   ...
   ...
}

How could I unit test inner class?

Given the source code below, how could I effectively unit test the Message class, the inner class of MainMsg? The challenge for this use case is Message class doesn't have any public interface that could access from outside world, the only entry is from MainMsg's addMessage(). How could I return a fake data when getMsgList() is called? Or I shouldn't mock Message class actually, just instantiate a new value is more than enough.
public class MySystem {
 public static void getMessage(MainMsg mm) {
  if( mm.getMsgList().size() > 0 ) {
   … 
   … 
  }
 }
}

public class MainMsg {
 private List<Message> msgList = new ArrayList<Message>();
 public class Message {
  private String str;
  Message(String str) {
   this.str = str;
  }
 }
 public void addMessage(String content) {
  msgList.add(new Message(content));
 }
 public List<Message> getMsgList() {
  return msgList;
 }
}
According to the advice from expert, I shouldn’t mock the inner class, whereas I should create a real value of it. But how? After a long R&D, I found EasyMock could handle this very well. In my unit test class, this is how I did it:
@RunWith(PowerMockRunner.class)
@PrepareForTest({MySystem.class})
public class MySystemTest {
 private MainMsg mm;
 @Test
 public void testSendMessage () throws IOException {
  mm = PowerMock.createPartialMock(MainMsg.class, "getMsgList");
  EasyMock.expect(mm.getMsgList()).andStubAnswer(new IAnswer<List<Message>>() {
   @Override
   public List<Message> answer() throws Throwable {
    Whitebox.setInternalState(mm, new ArrayList<Message>());
    mm.addMessage("message_A");
    List<Message> fakeMsgList = Whitebox.getInternalState(mm, "msgList");
    return fakeMsgList;
   }
  });

  MySystem.getMessage(mm);
 }
}

Sunday, January 4, 2015

Steps to create new cocos2d-x project in Netbeans

Many times when I wanted to create a new Cocos2d-x project in Netbeans, I always forgot the procedure. Thus I decide to write it down for future reference. As of this writing, I'm using Cocos2d-x 3.1.1 and Netbeans 8.

Steps:
  1. Create new project using cocos command.
  2. In Netbeans, goto File > New Project > C/C++ Project with Existing Sources, then click Next button.
  3. Under the Specify the folder that contains existing sources field, navigate to the game project's root path created with cocos command.
  4. Let it build.
A short note, when trying to run the project, there will be a dialog prompt to select an executable. Pick the default selection will do.