A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Sample XMLUnit test class

import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import java.util.HashMap;
import org.custommonkey.xmlunit.*;

public class MyTest extends XMLTestCase {
    private HttpClient client;
    private String pageTitle = "something";
  
    protected void setUp() {
         client = new HttpClient();        
        // declare XML namespaces
        HashMap m = new HashMap();
        m.put("agg", "http://www.bbc.co.uk/aggregation/1.0");
        m.put("atom", "http://www.w3.org/2005/Atom");
        NamespaceContext ctx = new SimpleNamespaceContext(m);
        XMLUnit.setXpathNamespaceContext(ctx);
    }

    public void testVersion() throws Exception {
        String pageXML = getXML("http://example.com/version");
        assertXpathEvaluatesTo("2010-07-29", "/version/release-date", pageXML);
    }
  
    public void testTitle() throws Exception {
        String pageXML = getXML("http://example.com/test");
        assertXpathEvaluatesTo("items about "+pageTitle, "/agg:page//atom:feed/atom:title[1]", pageXML);
    }
  
     private String getXML(String uri) throws IOException {
        GetMethod method = new GetMethod(uri);
        int statusCode = client.executeMethod(method);
        assertEquals("HTTP GET " + uri, HttpStatus.SC_OK, statusCode); // 200 OK
        return method.getResponseBodyAsString();
     }

     public void TearDown() {
     }

}

Testing XML with Java

Testing
  • XMLUnit
  • JUnit - most popular

XML

with Java
  • Selenium
  • Eclipse
  • Maven + Jetty

3rd party javadocs in Eclipse

  • Download and unzip the 3rd party package's source
  • Open the Package Explorer view in Eclipse
  • Navigate to [your project] | Referenced libraries | [3rd party library] | right-click | Properties
  • Enter the source directory in the Java Source Attachment | Location path
There is also a Javadoc Location field that accepts a URL (didn't work for XMLUnit or JUnit though)

How to run a Selenium/JUnit test

  • Install Eclipse 3.5
  • Download JUnit
  • Download Selenium RC
  • Run java -jar selenium-server-1.0.3/selenium-server.jar
  • Create a Java project in Eclipse
  • Right-click on the project name and go to Properties | Java Build Path | Add External JARs
    • add JUnit JAR
    • all the Selenium Server JARs
    • all the Selenium Java JARs
  • Run a program like this (This one doesn't actually work; the assertion fails)
    package com.example;
    
    import junit.framework.TestCase;
    import com.thoughtworks.selenium.DefaultSelenium;
    import com.thoughtworks.selenium.Selenium;
    
    public class GoogleTest extends TestCase {
     private static Selenium selenium;
     protected void setUp() {
      selenium = new DefaultSelenium("localhost" , 4444 , "*firefox", "http://www.google.com");
      selenium.start();
      selenium.setSpeed("2000");
     }
     public final void testTitle() {
      selenium.open("http://www.google.com");
      selenium.type("//input[@title='Google Search']", "I am using selenium");
      selenium.click("//input[@value = 'Google Search']");
      assertTrue(selenium.isElementPresent("/html/head/title"));
     }
     protected void tearDown() {
      selenium.stop();
     }
    } 

    oXygen Eclipse plugin

    oXygen XML/XSLT: http://www.oxygenxml.com/download_oxygenxml_editor.html#Eclipse
    • it's not all that, but step-through-debugging does work
    • have to manually set the file association defaults to Oxygen types
    • unreadable default colour scheme for Oxygen filetypes. It is difficult to import syntax-highlighting preferences in Eclipse. You're better off setting them yourself.
    • to use step-through debugging, you need to:
      • 'Configure Transformation Scenario' (under XML/XSL menu - it appears when you open an XML or XSL file)
      • open the 'Debug Scenario' perspective in the XML/XSL menu
    • setting the input parameters for debugging is fiddly
    • the XML file being used as input must be "part of the project"

    Nginx high-performance webserver

    Nginx is a high-performance HTTP server. http://wiki.nginx.org/Main

    Seems to be used by quite a lot of sites and a few people have noticed nginx 404 error pages by facebook and youtube indicating they may also be using it? http://wiki.nginx.org/NginxWhyUseIt

    Comes also with a memcached module to talk with memcached directly: http://www.igvita.com/2008/02/11/nginx-and-memcached-a-400-boost/

    And has embedded perl in it as standard: http://wiki.nginx.org/NginxEmbeddedPerlModule

    And call perl directly from SSI
    <!- # perl sub="module::function" arg="parameter1" arg="parameter2"... >

    For XSLT fans: http://wiki.nginx.org/NginxHttpXsltModule

    Eclipse notes

    • File associations are under Preferences | General | Editors | File Associations
    • Background colour is under: Preferences | General | Editors | Text editors 
      • and Preferences | oXygen | Editor
    • Syntax highlighting is under: Preferences | XML | XML Files | Editor | Syntax Coloring
      • and Preferences | oXygen | Editor | Syntax highlighting
    • Eclipse plugins site: http://eclipse-plugins.info/eclipse/
    • How to import/export syntax highlighting colours
    • No word wrap!

      Start Apache with a custom configuration

      /path/to/main/httpd -f /path/to/your/custom/httpd.conf -k start

      Uptime

      99.8% uptime allows for 17.52 hours downtime per year.
      That seems reasonable.

      For-each over static values in XSL

      When you want to do this:

      <xsl:for-each select="$some_value in ('a','b','c','d')"><!-- invalid? -->

      You have to do this:

      <xsl:variable name="items">
          <item>a</item>
          <item>b</item>
          <item>c</item>
          <item>d</item>
      </xsl:variable>

      <xsl:for-each select="$items/item">
          <xsl:choose>
          <xsl:when test="$some_value = .">

      Remote file browsing

      • Mount a remote filesystem via SFTP
        • (Mac): http://pqrs.org/macosx/sshfs/
        • (Ubuntu): Use software centre
        • GUI
          • Try http://sshfs-gui.darwinports.com/
          • or Mac ports: sudo port install sshfs-gui
        • To start: sshfs hostname: mountpoint
        • To stop: fusermount -u mountpoint
      • Target Management (Remote file browsing in Eclipse via SSH): http://www.eclipse.org/dsdp/tm/

      Eclipse plugins

      Eclipse 3.5
          • Target Management (Remote file browsing, via SSH): http://www.eclipse.org/dsdp/tm/
            • works great (but sshfs is simpler)
            • Instead of that, see also http://wills-tech-notes.blogspot.com/2010/07/remote-file-browsing.html
          • XSLT: http://www.eclipse.org/webtools/
            • can't download through update manager, it's too slow
            • supposed to offer XML/XSL features, but I can't see any
          • Oxygen XML/XSLT: http://www.oxygenxml.com/download_oxygenxml_editor.html#Eclipse
            • Works. To use step-through debugging,you need to open the 'Debug Scenario' in the XML or XSL menu (it appears when you open an XML or XSL file)
          • vim -- why not use vim editor inside Eclipse? No good solution, apparently (none of these were tested):
            • http://www.viplugin.com/viplugin/ (2009 - costs a few pounds)
            • http://eclim.org/vim/ (recent)
            • http://vrapper.sourceforge.net/ (recent)
            • http://sourceforge.net/projects/vimplugin/ (2007, but states it supports syntax files)
          • Full Screen: http://code.google.com/p/eclipse-fullscreen/ (just works) 
          • P4WSAD: Perforce plugin: http://www.perforce.com/perforce/products/p4wsad.html (had to download zip file from FTP site and install manually, as automatic install URL was too slow)
            • Plugin can be slow to use if you browse the repository directly, but is faster if you check out, then do right-click | Import as project, and use the standard Eclipse Navigator or Project tab to work with the files.

              Installing Catalyst

              http://wiki.catalystframework.org/wiki/installingcatalyst#Debian.2C_Ubuntu_.28.2A.deb.29

              apt-cache search catalyst

              How to set up apt-get on ubuntu

              NOTE: Yum is designed for use on redhat-like systems using RPMs.
              Ubuntu is based on debian, so uses apt-get/aptitude and .deb files.

              On a fresh linux install:
              • sudo apt-get update
              • "You will have to enable the component called 'universe'": Go to System > Administration > Software Sources. Under the Ubuntu Software tab, tick the box for Community maintained Open Source Software (universe).
              • sudo apt-get install XXX

              How to create a PDF

              • Save as PDF from OpenOffice
              • http://www.pdfonline.com/convert-pdf/

              To where has yum installed a package?


              Yum works with RPMs:

              rpm -ql packagename

              How to use Saxon XSL 2 processor

              java -jar saxon9he.jar -xsl:/path/to/file.xsl -s:/path/to/file.xml -o:/path/to/output.xml parameter1='value1' parameter2='value2'