oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Getting Fit for the Holidays
Pages: 1, 2, 3

Running the Fit Tests

This time the process is a little different. We want the client to be able to open up an HTML page that shows the acceptance tests and click on a link to fit-cgi . The CGI script should then process the acceptance tests and generate the output HTML page. Finally, the browser should redirect to this newly generated page.

In this example, I have a directory called dozen inside of the /Library/WebServer/Documents directory. Its structure looks like this:

Directory structure.

Create a file called CaseDiscountFitTest.html inside of /Library/WebServer/Documents/dozen/tests/acceptance/testsource . It should contain a hyperlink to http://localhost/cgi-bin/fit-cgi. It should also include the first table we looked at in this article. The corresponding fixture, CaseDiscountFixture will go in the fixtures directory inside of a register subdirectory. The compiled code should be inside of the classes directory. The file fit.jar should be copied to your CGI-Executables directory. Finally, make sure the permissions on the testresults directory have been set so that you can write into it.

With that setup, we can create the code for fit-cgi . Note that in the following listing, each of the indented lines is really a continuation of the previous lines. In your script they should be part of the previous line with the spaces deleted.

java -classpath fit.jar:../Documents/dozen/classes fit.FileRunner  
echo "Location: 

The first really long line is of the form

java -classpath (the classpath) (the input file) (the output file)

The first echo line is the redirect to the specified location. Direct your browser to http://localhost/dozen/tests/acceptance/testsource/CaseDiscountFitTest.html . Click on the link to fit-cgi . After a moment, you should be redirected to http://localhost/dozen/tests/acceptance/testresults/CaseDiscountFitTest.html , and the results of running the tests should be displayed. If there are problems, check your error log for more information.

There are lots of problems with that CGI script. The first problem is that all of the paths are hard-coded. This means that every HTML file will need its own CGI script. Also, if I decide to change the location of the dozen folder, I need to make a lot of changes to a lot of scripts.

Here's the plan for the new and improved script. Determine the classpath, location of the input file, and location of the output file from the address of the file that is calling fit-cgi . Process the input file based on this information. Redirect the browser to the output file location.

TEST_HTML=`echo "$HTTP_REFERER"|sed 's/testsource/testresults/'`
TEST_RESULTS=`echo "$DOCUMENT_ROOT/$TEST_HTML"|sed 's/http:\/\/localhost\///'`
TEST_INPUT=`echo "$TEST_RESULTS"|sed 's/testresults/testsource/'`
java -classpath $TEST_CLASSPATH fit.FileRunner $TEST_INPUT $TEST_RESULTS
echo "Location: $TEST_HTML"

Concentrate on the last three lines of this revised script. See how flexible and easy to read these lines are. The values that begin with "$" come from the variables that are set in the first four lines. Let's turn our attention to those. Check out the first line.

TEST_HTML=`echo "$HTTP_REFERER"|sed 's/testsource/testresults/'` 

This looks puzzling. First we set the variable TEST_HTML to the value $HTTP_REFERER . The rest of that line pipes the contents of $HTTP_REFERER to the sed application, which searches for the string testsource and replaces it with the string testresults . This is the location of the output file that will used later to redirect the browser. Let's move on to the next line.

TEST_RESULTS=`echo "$DOCUMENT_ROOT/$TEST_HTML"|sed 's/http:\/\/localhost\///'`

Here, we turn this URL into a path by stripping of the http://localhost from the beginning and adding the path to the document root to the beginning. In our case, this replaces http://localhost with /Library/WebServer/Documents . This result is stored in the TEST_RESULTS variable to be passed into the class fit.FileRunner as a parameter.

TEST_INPUT=`echo "$TEST_RESULTS"|sed 's/testresults/testsource/'`

Next, this TEST_RESULTS variable is processed to replace testresults with testsource . It may seem that we've taken an extra step here. The reason we worked in this order is that once the tests have been run, $HTTP_REFERER can be coming from testresults and not from testsource . By processing in this order we easily accommodate that possibility.

Finally, the classpath is set. The file fit.jar is inside of the same directory as fit-cgi , and we know the relative position from the TEST_INPUT to the class files. You may object that this means that the file structure of a project directory needs to be kept the same. On one hand, that is true. On the other hand, if I change that structure, I only need to make the corresponding change to the files that process the acceptance tests in this one location. This is the weakest line in the script. Feel free to suggest changes in the TalkBack.

Now you should be able to add links to fit-cgi from any of your acceptance tests inside of the testsource directory. On the Fit Wiki, Ward has published his run script. It creates a temporary local file with the contents of the page being processed. He processes that page and then displays the results. The advantage in his approach is that you don't have to have a fixed structure for your projects. Either script can serve as a starting place for your experiments.

Final Thoughts

In this article, I took a look at the newly released Fit framework and introduced you to some of the possibilities available in using CGI on your Mac. There are a lot more elegant ways to accomplish the same result. What if we wrote our scripts in Perl or Python or Ruby? What type of installation nightmares would we need to endure to use those languages? None. Perl, Python, Ruby, and even AppleScript would provide alternative ways to accomplish the same results and they're all built in to Mac OS X.

Seasons Greetings. Enjoy the developers' toy box that is Mac OS X.

Daniel H. Steinberg is the editor for the new series of Mac Developer titles for the Pragmatic Programmers. He writes feature articles for Apple's ADC web site and is a regular contributor to Mac Devcenter. He has presented at Apple's Worldwide Developer Conference, MacWorld, MacHack and other Mac developer conferences.

Read more Java Programming on the Mac columns.

Return to the Mac DevCenter.