macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

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

Running the Fit Framework Locally

There are four components to running acceptance tests using the Fit framework. First you need HTML files containing one or more tables like the ones you saw in the last section. Second, the classes in fit.jar are used to process the tables and to call the classes, methods, and variables referenced by them. Third, fixtures must be created by developers to implement the methods specified by the tables. Finally, you need the production code itself. Remember, the goal is to produce production code that meets the customer's needs. You should keep your fixtures thin. They should call into the application code that does the real work.



With fit.jar and your fixtures in the classpath you can process HTML files by running fit.FileRunner and passing in the path to the input and output files as arguments. OK, I love being able to say the following. On your Mac, open up a Terminal window, move to an appropriate directory, and use this command.

java -classpath fit.jar:classes fit.FileRunner inputPath outputPath 

If the necessary classes, methods, or variables aren't available, you'll get an error message that looks like this.

Error message.

If the fixture is in place, but some of the returned results don't match the expectation, you'll see something like this:

Results.

In this image you can see the green background for the first two tests indicating that the expected and actual results agree. When they don't agree you'll see a red background along with details. You can see the actual value and the expected value to see what is being returned by your application. I will provide a more detailed example of the Java code being called in a separate article on Fit. For now, let's move on to creating the CGI to process these files remotely.

Running Remotely Using CGI Scripts

If you haven't started up your Apache Web Server, you'll need to carefully follow these directions. (1) Open up your System Preferences, (2) Click on Sharing, and (3) Check the Personal Web Sharing checkbox. That's it. You're running the Apache Web Server. Open up a browser and go to , http://localhost and you'll see a welcome page from Apache.

At this point you should check out Kevin Hemenway's second article on Apache Web-Serving with Mac OS X. The biggest configuration issues will tend to be setting the permissions on the files your running and on the folders containing files you want to run and write to. Create the following script and save it as compile-cgi inside of the /Library/WebServer/CGI-Executables directory.

#!/bin/sh
echo "Contents: text/html"
echo
echo "<h2>hello</h2>"

This is a simple script that should produce HTML output with the single heading "hello." To run it, you point your browser at http://localhost/cgi-bin/compile-cgi . In this case you'll see a message that says you don't have permission to access the file. Open up your Terminal application and navigate inside of /Library/WebServer/CGI-Executables . Change the permissions on compile-cgi by typing chmod 755 compile-cgi . Now you should be able to access compile-cgi with your browser and should see the heading "hello".

In your default setting the cgi-bin directory is /Library/WebServer/CGI-EXECUTABLES , and the root directory for documents is /Library/WebServer/Documents . Now create a directory named Hello inside of /Library/WebServer/Documents . Inside of that, create a Java source file named Hi.java that looks like this:

 public class Hi {
  public static void main(String[] args){
    System.out.println("Hi");
  }
}

Yes, this is HelloWorld. You know from my ONJava article that I hate using HelloWorld as a first program in teaching Java -- but this isn't an article on teaching Java. Change the permissions of Hello.java as before.

Now let's do some work with compile-cgi . We'll change directories to Hello and print out the name of the directory to verify that we're there. We'll then print out the contents of the directory, compile the Java class file and again print out the contents of the directory to verify that the Hello.class file was generated. Here's the new compile-cgi .

#!/bin/sh
echo "Contents: text/html"
echo
cd ../Documents/Hello
pwd
echo      
ls   
echo      
javac Hi.java
echo      
ls

The output is not exactly what we expect. You should see something like this:

/Library/WebServer/Documents/Hello

Hi.java

Hi.java

The file Hi.class was never created. What went wrong? The easiest way to figure out what is going wrong is to look at the error log. You can do this from the Terminal with this command:

 tail -f /private/var/log/httpd/error_log 

You'll see that there was "an error while writing Hi:Hi.class (Permission denied)". This time you need to change the permissions in the Hello directory so that you can write to it. Change directories to /Library/WebServer/Documents, and give Hello all permissions with the command chmod 777 Hello . Now when you point your browser at http://localhost/cgi-bin/compile-cgi , the results will be this:

/Library/WebServer/Documents/Hello

Hi.java

Hi.class
Hi.java

Success. We have compiled a Java source file remotely, not that you'd want to. This example gives you a glimpse of the power of this mechanism. It should also sound warning bells of the possible dangers. Before you actually do this on a publicly accessible machine, you'll want to reconsider the permissions you are setting.

Pages: 1, 2, 3

Next Pagearrow