Test Driven Development — How to start with a test

Test driven development is mystical to anyone that hasn’t done it from the ground-up. It’s almost like riding a bike; you really don’t know how easy it is and how much fun you can have with it until you get up and going. When the training wheels are off, it then becomes a brand new world full of possibilities.

I have been writing unit tests for years. Having come to the Java game later than most of my colleagues, I really like to make sure that everything I commit to my various development communities are well-tested and as clean as I can make them. Unit testing has allowed me to verify this in two different ways. First, it guarantees that what I’ve writting works, and more importantly, it makes me keep my code simple. Frankly, if I start writing a test and it becomes a dependency-driven, closely-coupled to the implementation monster, I can pretty much guarantee that the code is going to be the same. I’ll try to refactor this and use my tests in this manner as a guage for it’s quality. Continue reading

A Simple “Restlet” Demo application

I’m assuming that if you’re here you already know the basics of REpresentational State Transfer.  I’m also going to assume that you’ve looked at all the goodies at the Restlet community site.  Further, I’m thinking that you want a quick start guide to show you how to implement it.  The following tutorial shows how I “found a home” with Restlet, what worked for me, and hopefully something that will work for you, too.

The Restlet API is a Java framework for the REST architectural style. The Noelios Restlet Engine (NRE) is available as the reference implementation, as well as several extensions to the API and to NRE. Continue reading

A Simple "Restlet" Demo application

I’m assuming that if you’re here you already know the basics of REpresentational State Transfer.  I’m also going to assume that you’ve looked at all the goodies at the Restlet community site.  Further, I’m thinking that you want a quick start guide to show you how to implement it.  The following tutorial shows how I “found a home” with Restlet, what worked for me, and hopefully something that will work for you, too.

The Restlet API is a Java framework for the REST architectural style. The Noelios Restlet Engine (NRE) is available as the reference implementation, as well as several extensions to the API and to NRE. Continue reading

Using Object-Oriented JavaScript to Create Multi-Use Tabs

Taking JavaScript More Seriously

As I migrated from basic HTML to JSPs and finally complex Server-Side Java, I became less and less respectful of client-side languages, and most of my colleagues were pretty much in lockstep with me. While working in the view layer, adding JavaScript was a tedious necessity, and most programmers I worked with built well-made, but structural, scripts to handle the needs of the page.  They knew what they wanted it to accomplish and they just wrote it to “do that thing”.  It “wasn’t worth” digging deep into it, because it is just “throw away” code that the view used; not really worthy of Server-Side Programmers’ attention. Continue reading

Using a prototype to extend your JavaScript methods.

The Problem: Calling a function many times throughout a particular JavaScript file or block.

JavaScript is often used to manipulate a variety of HTML objects on a page, i.e. to “show” or “hide” them, or perform some type of toggled functionality on a variety of buttons, divs, etc. It is not uncommon to see code like this littered in a variety of places throughout a typical large Script block:
if(foo == bar){
document.getElementById("blah").style.visibility="block";
document.getElementById("blue).style.visibility="none";
document.getElementById("red").style.visibility="block";
document.getElementById("black").style.visibility="none";
}
Implementing the above in numerous places throughout said block of Script with a couple of hundred lines can eat up a lot of space, be harder than all get out to maintain, and if the guy that wrote it gets hit by a bus, you can often watch the next engineer in line cry for days in front of their screen just trying to figure out what the original coder was thinking when they wrote it.

Continue reading

Ruby on Rails: Create a Select Drop Down from an Array

Problem: You wish to populate a “collection_select” box with an Array.

I recently had an issue where I wanted to create a select box populated from an array of continuous years. I wanted to use thecollection_select method so it would have a minimal “code footprint” and play nice with the form that I had built. The application that I’m working on is in Rails2 and I didn’t want to divert heavily or make to many changes, and after some fiddling I came up with a nice solution: In the environment.rb file I created an Array constant to contain the year range that I wanted to use:

#Constant Values
YEARS_ARRAY = Array.new(89) {|i| 1920+i}

This code is a shortcut to create an array of values between 1920 and 2008 (88 years). It creates a new array with a size of 88, and then uses the i value to populate each year incrementally. I was pleased to find a method like this!

The form declaration contained the object that I wanted to populate with the form values using the Rails2 syntax:

<table class=”new”>
<% form_for(@movie_poster) do |f| %>
 <tr>
<th>Title:</th>
  <td><%= f.text_field :title %></td>
 </tr>
…..
<% end %>
</table>

*note the newer Rails2 syntax for the form…

I will populate my select box with an Array of Objects. I need to create my Object first to populate my Labels and Values, and I used an “old trick” from Struts by creating a Ruby LabelValue object that mimics Struts LabelValueBean utility POJO. The object has a label and value accessor, which works extremely well for any set of arrays since any kind of mapping scheme would consume duplicate keys and often select boxes, radio buttons, etc may need some duplication. Here is my label_value.rb class, located in my model directory:

# Creates a Label and value for select boxes and
# forms without clearly delinieated# objects, such as Arrays.
class LabelValue
# name the accessors. Label and Value
attr_accessor :label, :value
end

Now that I have a model to work with and an Array Constant to use, I created the “helper” to use with the form tag in the application_helper.rb file, so I might use it throughout the application’s forms. There are two methods, one public, and one private. Note the “Select year” prompt:

# selection for a year value
# ‘f’ represents the passed in form value
def year_select(f)
f.collection_select(:year,year_lookup,:label,:value,{:prompt=>”Select year”})
end
# ———— PRIVATE METHODS —————–

private

def year_lookup
#create an emptycollection to hold the LabelValue Objects
years = Array.new()#populate the ArrayYEARS_ARRAY.each do |yr| y = LabelValue.new
y.label = yr
y.value = yr
years.push(y)
end
years
end

Finally, we need to call the _helper method from the form:

<tr>
<th>Grade:</th>
<td><%= grade_select(f) %></td>
</tr>

The resulting code renders a select box:

<select id=”movie_poster_year” name=”movie_poster[year]”>
<option value=””>Select year</option>
<option value=”1920″>1920</option>
<option value=”1921″>1921</option>
<option value=”1922″>1922</option>
<option value=”1923″>1923</option>
<option value=”1924″>1924</option>

# continues…

<option value=”2005″>2005</option>
<option value=”2006″>2006</option>
<option value=”2007″>2007</option>
<option value=”2007″>2007</option>
</select>

The nice thing is reusability of the LabelValue class, the YEAR Constant and the collection_select itelf. Everything can be accessed as it is needed, whether you want to use the Constant Array for something else, the LabelValue object for a different array or tuple, or the actual rendered select box in different views.

UPDATE, 1/15:

See the comments below for a nice alternative and discussion!

Creating a Java TesterTimer for Performance Testing.

I often find myself writing lightweight performance tests as part of a suite of the unit tests, to make sure that my code hasn’t been written in a way to destroy the performance of an application. I also create performance tests as a standalone Application to test (for instance) how many HttpServletRequest/Response calls can be made over a certain period of time.

My requirements are normally simple, and rarely have a strong need to be COMPLETELY perfect; i.e. the actual calls to and from the timer can cause a slight time loss in the actual application, but frankly I’m willing to put up with this in most circumstances. The “TesterTimer” class featured below is pretty damned efficient, especially if you never call the “elapsed(id)” method, since the start and stop happen pretty much outside the application being tested. Here is the fully commented code with a demonstration “main” class:

Continue reading

Adding a DRL FileType with Syntax Highlighting to IntelliJ

IntelliJ’s support of JBoss-rules in no way compares to Eclipse, but even that isn’t enough to get me off my butt and move from my fave. That said, I created a semi-literate FileType for the drl files so that IntelliJ will recognize them and you will get a decent amount of syntax highlighting. Is it perfect?

No.

But it will probably get you home. Here’s what you do. Create a new filetype and make sure that it includes ?*.drl files. Once that is created, it makes a filetype with the name you gave it in the

%User%/Library/Preferences/IntellijIDEA%your-release-version-here%/filetypes/%name_of_file.xml%

directory (at least that’s what it looks like on my Mac – I haven’t touched a windows box in years, but it should have some parallels.

Open the XML file and replace anything between the <filetype> nodes with:

Continue reading

Passion towards an IDE?

I have to admit that I don’t write as much Java as I used to. I spend a ton of time in design, using UML tools (Visual Paradigm is my favorite). Lately, I’ve spent some time digging into the JBoss-rules framework – really getting to understand it and draw comparisons between it and JESS, CLIPS and other rule engines. So I’ve had the joy of coming back to my IDE of choice for the last 6 years – IntelliJ – now about to release its 7th incarnation.

I have to admit a serious bias, but I don’t get all hung up on the “my tool is better than yours” debate. I’ve used the others, and I have a lot of smart friends that love Eclipse and NetBeans as well. I even have some very smart friends that prefer tabs to spaces, but frankly, why go down the path of flames? It’s important to talk about what works for me, and if you’ve never used IntelliJ, maybe it’s worth the effort. Yeah, it costs money, but frankly if you’re in the business of making money with your code, it’s trivial, especially when you start looking at the features that come “free” with it, and start looking at the extra cost of plugins for some of the competitors.

Why do I like it? Probably familiarity, definitely because it allows me to effortlessly get what’s in my head onto the screen in a seamless manner. I also credit this IDE for making me a better code-writer and giving me the confidence in my abilities to make the next steps in my career, challenge myself with new libraries and frameworks, and make my code professional and complete with documentation and unit tests.

Is it for everyone? Frankly, I don’t care if it is or isn’t. If you’re new to Java, or have no particular fondness for your IDE, I would heartily recommend IntelliJ immediately, and encourage complete understanding of its capabilities. If you are fine with your IDE but don’t particularly like Java, I would also recommend IntelliJ, as I found a fondness for the language and what I can accomplish with it all over again. Also if I quality, ease of use and completeness of the product are key considerations, you get what you pay for with IntelliJ and their plugins. Eclipse, in my opinion, tends to have a little “anarchy” associated with it and it’s overall “finish”.

If you love your IDE, well, tools that feel comfortable in your hand make you productive, so have fun with that.

I like the fact that there is Eclipse, NetBeans and IntelliJ to play back and forth with. They end up making each other better, and since there is enough of a user-base for them to stay around for awhile, this will continue.

BTW, if you are a Ruby on Rails supporter, the latest versions of IntelliJ support it out of the box, and they do it VERY WELL. I just wished that they had a JBoss-rules plugin like Eclipse does, because it has chapped me to no end…

As a Post-Script, Bob Congdon writes back in 2004: “Personally I think it’s healthy that there are a number of Java IDE alternatives. But you have to wonder how long IDE vendors will be able to survive against Eclipse.” Asked and answered?