Sunday, March 11, 2012

CodeRunner and ANT

I like CodeRunner, an IDE for any language.

The templates in CodeRunner inspired me to automate the source folder creation step generally performed when creating an ANT project. Instead of creating the source folders manually, I use the task that I created in the ANT template I stored in CodeRunner. Now the steps I perform to create an ANT project are
1. use mkdir to create a project folder
2. open CodeRunner and use the JUnitProject template which creates a ANT build file
3. run the command in red, below including the path I want for the source code

[batchbrowser] ant create -Dsrcfolder=xxx/yyy/zzz
Buildfile: /Users/javapro/dev/src/java/batchbrowser/build.xml create:
[mkdir] Created dir: /Users/javapro/dev/src/java/batchbrowser/bin
[mkdir] Created dir: /Users/javapro/dev/src/java/batchbrowser/lib
[mkdir] Created dir: /Users/javapro/dev/src/java/batchbrowser/src/main/java/com/bank/batchbrowser [mkdir] Created dir: /Users/javapro/dev/src/java/batchbrowser/src/test/java/com/bank/batchbrowser BUILD SUCCESSFUL
Total time: 0 seconds
[batchbrowser]

 The JUnitProject template I stored in CodeRunner is a generic ANT build file that accepts the command line property srcfolder and uses this property in the create step. and here's the code I need tomorrow at work . . .
package com.bank.batchbrowser;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.net.URLEncoder;

public class BatchBrowser {

 public static boolean verifyResponseContains(String url, String content)  throws Exception {
  try {
   HttpGet httpget = new HttpGet(url);
   HttpClient httpclient = new DefaultHttpClient();
   HttpResponse response = httpclient.execute(httpget);
   HttpEntity entity = response.getEntity();
   if (entity != null) {
    InputStream instream = entity.getContent();
    String data = PageReader.readStream(instream);
    return data.indexOf(content) > -1;
   }
  } catch (Exception e) {
   System.out.println(e.getMessage());
  }
  return false;
 }
}
package com.bank.batchbrowser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

public class PageReader {

 public static String readStream(InputStream is) throws IOException {
  int n = is.available();
  char[] buffer = new char[n];
  Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  Writer writer = new StringWriter();
  while ((n = reader.read(buffer, 0, n)) != -1) {
   writer.write(buffer, 0, n);
  }
  return writer.toString();
 }
}
package com.bank.batchbrowser;

import org.junit.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.net.URLEncoder; 

public class BatchBrowserTest {
 String url = "http://localhost:8080/empower/atm/prefs?customerID=123456789&atm=FG3781";

 @Test
 public void urlTest()  throws Exception {
  try {
   HttpGet httpget = new HttpGet(url);
   HttpClient httpclient = new DefaultHttpClient();
   HttpResponse response = httpclient.execute(httpget);
   HttpEntity entity = response.getEntity();
   if (entity != null) {
    InputStream instream = entity.getContent();
    String data = PageReader.readStream(instream);
    assertTrue(true);
   }
  } catch (Exception e) {
   fail(e.getMessage());
  }
 }
 
 @Test
 public void testUrlValidation()  throws Exception {
  try {
   assertTrue(BatchBrowser.verifyResponseContains(url, "FG3781"));
  } catch (Exception e) {
   fail(e.getMessage());
  }
 }
}