Tuesday, 13 December 2011

Write JUnit test for XSLT

Firstly, you just create a Util Class for xslt transformer:
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.InputStream;
import java.io.StringWriter;

public class XsltTransformer {
    public static String transformer(InputStream xml, InputStream xslt) throws TransformerException {
        Source xmlSource = new StreamSource(xml);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);

        TransformerFactory transFact = TransformerFactory.newInstance();
        Source xsltSource = new StreamSource(xslt);
        Transformer trans = transFact.newTransformer(xsltSource);
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");

        trans.transform(xmlSource, result);
        return stringWriter.getBuffer().toString();
    }
}

Secondly, combined with XMLUnit, Junit to verify
@Test
    public void testXSLT() {
        InputStream expectedOutput = ODSEventXsltTest.class.getResourceAsStream("ods-output.xml");
        InputStream xml = ODSEventXsltTest.class.getResourceAsStream("ods.xml");
        InputStream xslt = ODSEventXsltTest.class.getResourceAsStream("SSNOdsExportToCannonical.xsl");

        try {
            String actual = XsltTransformer.transformer(xml, xslt);
            String expected = IOUtils.toString(expectedOutput);
            XMLUnit.setIgnoreWhitespace(true);
            Diff diff = XMLUnit.compareXML(expected, actual);
            String errorMessage = "\nexpected:\n" + expected + "\n-------------------------------------\nbut actual: \n" + actual + "\n>>>>" + diff.toString();
            assertTrue(errorMessage, diff.similar());
        } catch (TransformerException e) {
            fail(e.getLocalizedMessage());
        } catch (SAXException e) {
            fail(e.getLocalizedMessage());
        } catch (IOException e) {
            fail(e.getLocalizedMessage());
        }
    }

How to write unit test for private method in java

/**
  * Method: parseDateTime(String text)
 */
 @Test
 public void testParseDateTime() throws Exception {
     try {
        CsvToXml csvToXml = new CsvToXml();
        Method method = CsvToXml.class.getDeclaredMethod("parseDateTime", String.class);
        method.setAccessible(true);
        String actual = (String) method.invoke(csvToXml, "2011-11-08T13:04:31.000+11:00");
        String expect = "2011-11-08T12:04:31Z";
        assertEquals(expect, actual);
     } catch (NoSuchMethodException e) {
        fail(e.getMessage());
     } catch (IllegalAccessException e) {
        fail();
     } catch (InvocationTargetException e) {
        fail();
     }
}

BTW. Another choice is PowerMock Framework

Wednesday, 7 December 2011

fibonacci number in javascript under cached way

function fibonacci(n) {
    if (!(n in fibonacci)) {
        if (n == 0) {
           fibonacci.counter++;

           fibonacci[n] = 0;
           return 0;
         }

         if (n == 1) {
            fibonacci.counter++;

            fibonacci[0] = 0;
            fibonacci[1] = 1;
            return 1;
          }
        
          fibonacci.counter++;

          fibonacci[n] = fibonacci(n -1) + fibonacci(n-2);
     }
  
     return fibonacci[n];
}
fibonacci.counter = 0; // for debug
var val1 = fibonacci(10);
var val1 = fibonacci(9);
console.log('counter: ' + fibonacci.counter); // >> counter: 10
var val1 = fibonacci(11);
console.log('counter: ' + fibonacci.counter); // >> counter: 11

Monday, 5 December 2011

static variable in javascript

Functions are not primitive values in JavaScript, but a specialized kind of object, which means that functions can have properties. When a function needs a “static” variable whose value persists across invocations, it is often convenient to use a property of the function, instead of cluttering up the namespace by defining a global variable. You could store this information in a global variable, but that is unnecessary, because the information is used only by the function itself. It is better to store the information in a property of the Function object. So there're two solutions to solve this.
1) using global variable
2) function properties
function uniqueSuffix() {
    uniqueSuffix.counter = uniqueSuffix.counter || 0;
    return uniqueSuffix.counter++;
}

uniqueSuffix();
uniqueSuffix();
console.log(uniqueSuffix.counter); // >>2