Tuesday, April 23, 2013

Using proXPN on Tunnelblick

Just signed up for the free proXPN service that supports OpenVPN connectivity. Since I use Tunnelblick (OpenVPN client on Mac OS X), it'd be nice not having to install another VPN app.

Good thing it's fairly easy to configure.

1. Download and open proXPN for Mac but don't install. It's needed to copy the config files.
2. Add new VPN configuration in Tunnelblick

Choose "I have configuration files"

Choose "OpenVPN Configuration(s)"

Choose "Create Tunnelblick VPN Configuration"

Follow these steps.

That's about it! Here are the files to copy into the new Tunnelblick configuration folder. Open the package contents for the proXPN download. Look in /Contents/Resources and copy these files.


├── ca.crt
├── client.1.down.tunnelblick.sh
├── client.1.up.tunnelblick.sh
├── client.2.down.tunnelblick.sh
├── client.2.up.tunnelblick.sh
├── client.crt
├── client.down.osx.sh
├── client.down.tunnelblick.sh
├── client.key
├── client.nomonitor.down.osx.sh
├── client.nomonitor.up.osx.sh
├── client.up.osx.sh
├── client.up.tunnelblick.sh
└── proxpn.ovpn


The proxpn.ovpn file needs a little modification. Just uncomment this line:
#remote miami.proxpn.com 443

And remove the "ssl/" directory from the certs.

Change these:
ca ssl/ca.crt
cert ssl/client.crt
key ssl/client.key


To these:
ca ca.crt
cert client.crt
key client.key


Give it a test, and it connects after logging in.













The basic 300kbps free service will need some patience, but it's still faster than many free wifi services. ;-)

Saturday, June 2, 2012

Unit Testing Servlet with Mocks

While integration testing a servlet in a real servlet container such as tjws or jetty works well, in development, unit testing just the servlet handler methods is more efficient and provides the granularity in "unit testing". Unfortunately the HttpServletRequest and HttpServletResponse aren't so easy to work with in testing. Fortunately there are many mock frameworks that help. This example shows how mockito helps to test the behavior of the servlet under development.

HiNemoServlet.java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HiNemoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("plain/text");
        resp.getOutputStream().println("Hi Nem!");
    }
}

There are two behaviors to test in this servlet:
  • setting the content type to "plain/text"
  • outputting the response "Hi Nemo!"

HiNemoServletTest.java
import org.junit.Test;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class HiNemoServletTest {

    @Test
    public void testDoGet() throws Exception {
        HttpServletRequest mockRequest = mock(HttpServletRequest.class);
        HttpServletResponse mockResponse = mock(HttpServletResponse.class);
        ServletOutputStream mockOutput = mock(ServletOutputStream.class);

        when(mockResponse.getOutputStream()).thenReturn(mockOutput);

        new HiNemoServlet().doGet(mockRequest, mockResponse);

        verify(mockResponse).setContentType("plain/text");
        verify(mockOutput).println("Hi Nemo!");
    }
}

Three mock objects are used in this test. The mock request simply satisfies the method call argument requirement. Since the servlet does not use the request, the mock request is untouched. The mock response and the mock outputstream verify the desired behavior in the servlet.

First, the getOutputStream() call gets stubbed with returning the mock outputstream.
when(mockResponse.getOutputStream()).thenReturn(mockOutput);

Then the servlet's doGet() method is executed with the mocks.
new HiNemoServlet().doGet(mockRequest, mockResponse);

Finally, the two desired behaviors are verified.
verify(mockResponse).setContentType("plain/text");
verify(mockOutput).println("Hi Nemo!");

In this example, I have a typo in my servlet code. Instead of "Nemo", it's "Nem". So the unit test fails with this output:
Argument(s) are different! Wanted:
servletOutputStream.println("Hi Nemo");
-> at HiNemoServletTest.testDoGet(HiNemoServletTest.java:26)
Actual invocation has different arguments:
servletOutputStream.println("Hi Nem!");
-> at HiNemoServlet.doGet(HiNemoServlet.java:13)

Expected :servletOutputStream.println("Hi Nemo!");
Actual   :servletOutputStream.println("Hi Nem!");

    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 HiNemoServletTest.testDoGet(HiNemoServletTest.java:26)
    ...
    ...

Fix my typo in the servlet, run the test again, and it passes this time. Job done. Let's call it a day!
(This post is written for michja.)

Saturday, February 19, 2011

MySQL, Ruby on Rails, and Mac OS X

A year ago, it was a bit of a pain to setup Ruby on Rails & MySQL on Windows Vista for the Foundations class at Carnegie Mellon. Download & install Ruby, download & install Rails, download & install MySQL, and spend some time finding a mysql.dll override to get it all to work.

A year later, with my first MacBook Pro that ships with Ruby and Rails 2, I thought setting up RoR dev environment would be just installing MySQL.

Ha! I was wrong...

Installing MySQL 5.5 64-bit was a no brainer.

gem update --system; no problems there.

gem install mysql; ooops... this is where it fell apart.

Full mysql gem install: sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

It resulted in something like the following:
Building native extensions.  This could take a while...
ERROR: Error installing mysql:
ERROR: Failed to build gem native extension.

Some attempts later, I ended up with this:

uninitialized constant MysqlCompat::MysqlRes


Turns out rails & gem still don't work well with MySQL 5.5. (note: updating mysql2.bundle with the libmysqlclient library path might work...)

So time to uninstall MySQL 5.5. Hmm... how? Found this set of instructions.

  • Use mysqldump to backup your databases to text files!

  • Stop the database server

  • sudo rm /usr/local/mysql

  • sudo rm -rf /usr/local/mysql*

  • sudo rm -rf /Library/StartupItems/MySQLCOM

  • sudo rm -rf /Library/PreferencePanes/My*

  • edit /etc/hostconfig and remove the line MYSQLCOM=-YES-

  • rm -rf ~/Library/PreferencePanes/My*

  • sudo rm -rf /Library/Receipts/mysql*

  • sudo rm -rf /Library/Receipts/MySQL*


However, the MySQL 5.1 installer complained that a newer version of MySQL is already installed. One more piece of MySQL to clean up.


  • sudo rm -rf /var/db/receipts/com.mysql.*



That got MySQL 5.1 installed. MySQL gem installed and compiled (requires Xcode).

It worked! How about that?

Coming up next, Rails 3 on MySQL 5.5. Hopefully not another year later...

Wednesday, August 4, 2010

Measuring Architecture

A presentation from guest speaker Jeromy Carriere in Architecture class.

Wednesday, September 30, 2009

HP DV4 Laptop Unfreeze

The intermittent system freeze on this less-than-a-year-old laptop was really getting in the way of my grad school remote meetings.  Sometimes it'd work for most of the day and would freeze in the middle of a skype meeting.  If I reboot just before the meeting, it senses that and freezes up right at the moment I call up class mates on skype.

So, time to fix it or find some back up...

Googling found two potential causes.

  1. bloated registry - I hope not; been careful not to install or otherwise mess up this system

  2. bad driver - maybe


Luckily the event logs showed "unexpected reboots" starting way back in July 2009.  The Realtek RTL8102E/8103E Family PCI-E Fast Ethernet driver had just been updated from Vista updates a few days prior to that.  Sure looks suspicious...  Driver is provided by Microsoft.




Install and check driver in Device Manager, now provider is Realtek and version/date is much older.  Clicked on "Update Driver..." and somehow got it to download the latest from Realtek automatically.  OK, maybe it works since the provider is still Realtek.  And the DV4 laptop is still freezing.


Normally this is sufficient reason to buy a new laptop, but hey, grad school ain't cheap...  So, try HP's old Realtek driver again.



  • Driver Date: 11/10/2008

  • Driver Version: 6.213.1110.2008

  • Running for 3 days now.  Rebooted only to get the external monitor to resync or whatever it does to clear up the correct display resolution but just not showing anything on screen issue


Laptop:  HP Pavilion dv4t 1300  (I'll probably forget this so best to jot it down.)

Reminder #2:  Don't Update Driver in Device Manager (stick with driver provided by HP)

Monday, January 12, 2009

Notes on Design Patterns Group Study





  • suggestions, books, "practice"



http://www.dofactory.com/Patterns/Patterns.aspx


  • data & object factory

  • definitions, diagrams, code sample in C#






  • Form Your Own Design Pattern Study Group (article based on and written by coauthors of Head First Design Patterns )

  • To start a study group, you need the following ingredients:

    • a moderator

    • a small group of interested individuals

    • a meeting place and time

    • a schedule of reading assignments











Tuesday, May 13, 2008

Effective Java - Second Edition

Pre-order your copy of Effective Java (2nd Edition).

Joshua Bloch presented these items at JavaOne 2008 TS-6623, More Effective Java:

  • Generics (Item 28)

  • Enum types (Items 31–34, 77)

  • Lazy initialization (Item 71)