Wednesday, November 26, 2008

Instant custom-formatted feeds with ROME, Velocity

It's nice when technologies blend well. ROME is a free, open-source, Java-based feed (Atom/RSS) parser and publishing library from Sun. And it works oh so well with Velocity templates.

Java/Servlet code:

import java.net.*;
import com.sun.syndication.feed.synd.*;
import com.sun.syndication.io.*;

...

URL feedUrl = new
URL("http://codewandering.blogspot.com/feeds/posts/default");
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));

List entries = feed.getEntries();

context.put("entries",entries);

StringWriter body = new StringWriter();
Velocity.mergeTemplate("Feed.vtl",context,body);
out.print(body.toString());


Velocity code:

#foreach($entry in $entries)
<a href="$entry.link">$entry.title</a><br/>
#{end}

Sample output:

Bruce Schneier as national CTO?

Regression testing for any web application with TeamCity, Selenium, and JUnit

The future of portable gaming

Summary

Because Velocity accesses beans and nested beans via a simple property interface, and ROME parses feed XML into bean structures, you can easily extract exactly the parts of the feeds that you want. The above just does titles and links, but a full list of properties supported by ROME (as getter methods) is given here. Now you have the view flexibility of Velocity matched with feed data ideally structured for it.

And since you're doing the parsing on the server side, you won't risk that nasty "Access to restricted URI denied" code: "1012" error or related security risk.

Tuesday, November 25, 2008

Bruce Schneier as national CTO?

It would be very refreshing to have a computer security expert such as Bruce Schneier be appointed to the position of CTO (Chief Technology Officer) in the new U.S. administration.

Saturday, November 8, 2008

Regression testing for any web application with TeamCity, Selenium, and JUnit

Maintaining a web application generally involves regression testing, and why not automate what you can? This post describes how to setup regression testing using TeamCity, Selenium, and JUnit. The steps are high-level, and detailed instructions are linked. The approach described here is independent of your web application technology/platform, so it can work with Java, .NET, PHP, etc. You will need to write *some* Java to make it work well, however.

Tools

TeamCity is a continuous integration system that is well suited for managing and running tests and tracking results over time. It has a free license for the Professional version. Selenium is an automated web testing framework that is compatible with all major browsers. Like all OpenQA projects, it has an Apache 2 license. Why not use OpenQA's other product, Bromine, instead of TeamCity? I have experimented with Bromine, but for continuous integration, it doesn't match TeamCity. TeamCity handles source control, builds, triggering, schedules, and unit tests. Bromine does not.

Continuous Integration Server

Choose a machine to run your continuous integration. This should ideally be a server-class machine because your build and test process, and perhaps deployment, will depend on it. Linux is a good choice.

Install TeamCity.

Get the server and a least one agent running.

If your application requires a build step and you want TeamCity to do that for you, setup a TeamCity build configuration. Otherwise this part is optional. Most likely you will have the build configuration check out your application from Subversion or other source control system. TeamCity can run your build for you via Ant, Maven2, and other Java and .NET runners.

You might want to setup a separate build configuration for regression testing, or you can have it run as part of your main build configuration, depending on your needs. If using Ant, you could have a target for regression testing. Be sure to check this Ant script or whatever build script you are using into your source control system.

The regression testing build configuration will need to be able to run JUnit. Ant is a very simple way to do that, because it has JUnit tasks available, or you can easily write your own. Again, this is independent of your web application technology.

Why JUnit? Because that is how we will hook in Selenium. More on that later. For now you can start with an empty JUnit task.

Regression Testing Server

Choose another machine (could be the same one) for automated web regression testing. This one needs to have browsers installed, which Selenium will control. Windows may be your best choice here; it depends on your user base, of course. If you need several platforms, you can setup multiple machines. Virtual machines may also be a consideration for you.

Install Selenium RC (remote control).

Start the Selenium RC server.

There are some good tips for setting up Selenium RC here.

Writing Tests

The easiest way to write a Selenium tests is to record it using Selenium IDE. One limitation is that it only runs in Firefox. If you are not targeting Firefox, I still encourage you to give Selenium IDE a try, because recording scripts is a lot faster than writing every line of code yourself, and it requires less time up front learning the Selenium scripting grammar. Of course, you will still need to tweak tests by adding your own assert statements (tests such as checking for entered text, user task completion indicators, expected or unexpected error conditions, etc.).

Whether you write or record your tests first, get a version of the tests in Java. Again, Selenium IDE makes this easy, because you can switch the test format from HTML to Java by clicking a menu item and copy or save the new format. (I recommend keeping a copy of the default HTML format file around too.) The Java tests should extend the class SeleneseTestCase, which in turn extends the JUnit class TestCase.

You may want to hand-edit your Java test further, to make sure it includes a good Java class name and any additional parameters you might want. I recommend parameterizing your selenium server, port, browser, and URL. This makes it much easier to add or adjust locations for your Selenium RC server(s), target browser(s), and application URL(s). You can do this by extending SeleneseTestCase with your own class MySeleneseTestCase like this and having your test extend MySeleneseTestCase:

package my.seleniumtests;

import com.thoughtworks.selenium.*;

public abstract class MySeleneseTestCase extends SeleneseTestCase {

private static final String DEFAULT_SERVER = "mydefaultserver";
private static final int DEFAULT_PORT = 4444;
private static final String DEFAULT_BROWSER = "*firefox";
private static final String DEFAULT_URL = "http://mydefaulturl";

private String server = System.getProperty("mySeleniumServer");
private String port = System.getProperty("mySeleniumPort");
private String browser = System.getProperty("mySeleniumBrowser");
private String url = System.getProperty("mySeleniumUrl");

public void setUp() throws Exception {
 selenium = new DefaultSelenium(
  server == null ? DEFAULT_SERVER : server,
  port == null ? DEFAULT_PORT : Integer.parseInt(port),
  browser == null ? DEFAULT_BROWSER : browser,
  url == null ? DEFAULT_URL : url);
 selenium.start();
}

}


Adding Tests to Automated System

Collect all your Java tests into a known location, and ideally check them into your source control system so that TeamCity can check them out and build them.

Make sure your regression testing build configuration in TeamCity has the ability to run JUnit. Pass the JUnit runner the names of your tests and any appropriate system properties.

If using Ant and tests that extend MySeleneseTestCase as described above, you can include lines like these in your Ant target that runs the tests:

<junit showoutput="true">
 <sysproperty key="mySeleniumServer" value="${mySeleniumServer}" />
 <sysproperty key="mySeleniumPort" value="${mySeleniumPort}" />
 <sysproperty key="mySeleniumBrowser" value="${mySeleniumBrowser}" />
 <sysproperty key="mySeleniumUrl" value="${mySeleniumUrl}" />
 <classpath>
  <pathelement path="${project.class.path}" />
  <fileset dir="${dist.dir}">
   <include name="**/*.jar" />
  </fileset>
 </classpath>

 <test name="my.seleniumtests.MyFirstTest" />
</junit>


The above would let you set the mySelenium* Ant properties in your build configuration parameters using the -DmySeleniumProperty=mySeleniumValue syntax. Point these properties (or if you hard-coded locations in your tests, then set those) to your Selenium RC server, Selenium RC port (4444 is default), test browser, and application URL.

Review

When everything is setup, you should have:

Source Control
  • Your application
  • Build script for application (optional) and regression testing (can be same script)
  • Selenium tests in JUnit/Java format
TeamCity Server
  • (Optional) Build configuration for application
    • Connected to source control system containing application code (application and build script)
    • Hooks into build script for application
  • (Possibly separate) Build configuration for regression testing
    • Connected to source control system containing testing code (tests and build script)
    • Hooks into build script for regression testing
  • Server running
  • At least one agent running
Selenium RC Server
  • Browser(s) for testing installed on same machine
  • Server running
Test

Do a trial run with at least one Selenium test. The easiest way is to click the Run button for your TeamCity regression testing build configuration. (You can also setup automatic build triggers based on source code changes, timing, etc.) It should check out the source code from the source control system you indicated, perform a build of the Selenium tests if necessary, run the Selenium tests via JUnit, and report back the test results to TeamCity.

Thursday, October 30, 2008

The future of portable gaming

With the arrival of some pretty polished games for the iPhone (heck, you can play Super Monkey Ball), and more for Google's Android platform surely on the way, will smart phones take over the portable gaming market? Or are phones destined to remain second-class gaming systems as many of these comments suggest? The old Java Mobile Edition was definitely more limited and thus probably helped keep expectations low for a while. But with version 3 on the way, even Java ME may see better times ahead. And how will Microsoft fare in all this?

Going the other direction -- from game system to multifunction device -- has been a challenge. Nintendo's DS Browser product was pulled from the shelves due to lackluster performance and thus sales. I think the smart money is on the phones moving further faster.

One concern for those of us who are developers is which platform to choose. I hope we see good tools and techniques for porting applications. You can always cover iPhone with Flash, but that of course misses the point. At least Android is based on the Java language. I just hope Google and Sun play nice.

Sunday, October 26, 2008

Dell low-cost Linux laptops

I'm glad Dell finally came out with a low-cost Linux laptop, the Inspirion Mni 9. I've been anticipating such a move for a long time. Now the OLPC XO and Eee PC (which is getting better, by the way) finally have some more real competition. I think this space is one to watch very closely, especially with the floundering economy, increasing globalization, and increasing technology adoption across borders and socioeconomic groups. Is ubiquitous computing really almost here? Are other variations in the works? Will we see more cross-breeding between these low-cost laptops, portable game devices, and smart phones?

Super-quick Linux performance monitoring using top command

Need to have super quick system performance monitoring in Unix and don't have time to install even Nagios?

A one-line top command might do the trick:
top -b -n 672 -d 900 > top.log&
The above command will run top in the background (due to the ampersand, "&") 672 times (in this case 7 days) with a measurement interval of 900 seconds (15 minutes) and write the results to the file "top.log". On a system with low-to-moderate usage, this could produce a log file as small as under 7 MB. Adjust the inputs to your needs as necessary. If you want to predict the size of a log file in advance, you could just run a test like this beforehand:
top -b -n 1 > top.test.log
This syntax was tested in bash on openSUSE but should work in other contexts. The "-b" option runs the command in a batch mode.

The top command only tracks very basic system stats like CPU, memory usage, and swapped size, of course. If or when you have more time for deeper and better monitoring, you might try Nagios or even Hyperic.

Eee PC Keyboard Size

Just how large is the Eee PC keyboard? Specifically, is it large enough for frequent typing? I asked myself that question and unfortunately could not visualize the answer at a store, because the Eee PC is only available online as far as I can tell. No matter. Best Buy lists the dimensions here (most importantly, the 8.9" width) and includes a picture. I adjusted the original picture a bit in MS Paint to get it to the right size. Here is a practical adjusted image in black and white (view image to get full size):









If you open this in MS Paint (be sure to print as Landscape in Page Setup) and print to 8 1/2" by 11" paper, you will get a life-size picture of the keyboard. Upon examining the printout, I found the row of letters from A to L, for example, to be approximately one key width narrower than the A to L on a standard keyboard (in this case from a Dell desktop). In conclusion, it appears slightly smaller than normal but perhaps large enough for many people

Installing JBoss Tools with Seam

This is to document my experience trying to install JBoss Tools with Seam. Eventually I got all the versions right, but it took a few tries. This looks like the best approach as of 7/15/2008.

  1. Download/install JBoss AS 4.2.2.GA
  2. Download/install Seam 2.0.2.SP1.
  3. Follow the directions here to try tunning the Seam examples. Note: It looks like the seam-issues example is not included on the current GA release of Seam (so it is probably either too old or too new).
  4. Download/install Eclipse 3.3.2. As of 7/15/2008 this is the only version that works with JBoss Tools and Seam correctly.
  5. Download/install the data tools plugins (and their dependencies -- note the checkbox on the right part of the update dialog box that lets you search for dependencies) directly via the Eclipse update feature; you'll find them in one of the automatically included Eclipse update sites; while you're at it, you might also install the web tools (may not be necessary)
  6. Download/install JBossTools 2.1.2.GA from the Eclipse update site directly via the Eclipse update feature
  7. Try creating a Seam project (from the Eclipse new project menu) using HSQLDB (there's a driver in the Seam installation); you'll need to point it to your installed JBoss and Seam instances.
This was tested on Windows XP Professional.
Copyright 2011 by William Cain