[code-snippet] Fast integration tests with Spring context

This snippet shows how complex integration test involving Spring contexts should be written. If you have numerous unit tests similar to the one below, the Spring context (given as the parameter of @ContextConfiguration annotation) will be read only once and re-used. This really improves the speed of running mvn test command.

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "classpath:spring-test-context.xml") 
public class FooImplTest {
@Autowired
 private FooImpl fooImpl;
@Test
 public void someWeirdTest {
}
}

As you can notice @Autowired annotation is used for automatic loading of a bean. The context in this example should contain a bean of type FooImpl (in this case the wiring is made according to the bean type).

Do it online

Online programming editors/debuggers are getting popular. Here are the couple of them which I use from time to time:

  • IDEOne – a broad choice of programming languages (there is even BrainF**k, however, there is no XSLT)
  • jsFiddle – a nice IDE for JavaScript.
  • jsBin – another one for JavaScript
  • XSLTCake – useful IDE for XSLT. It is still ‘beta’, but it offers three types of XSLT processing (JavaScript, web service or .NET 4.0)
  • XML Playground – another cool IDE for XSL templates. Unfortunately it doesn’t support XSLT 2.0
  • Rubular – IDE for checking regular expression written in Ruby
  • XSLTTest – Small and neat application for XSLT. Supports XSLT 2.0.

Faster than light…

What I did:
1) Ran Eclipse and wrote some code.
2) Ran JMeter and did some tests.
3) Ran SOATest and did some tests.
4) Closed all the above.
5) After 5 minutes all the javaw.exe processes are still running:

Useful XSLT snippet with translate()

Let’s say there is a following function written in XSLT 2.0:

    <xsl:function name="str:convertBits">
        <xsl:param name="cpnNumber" />

        <xsl:choose>
            <xsl:when test="$cpnNumber = 1">
                <xsl:value-of>8</xsl:value-of>
            </xsl:when>
            <xsl:when test="$cpnNumber = 2">
                <xsl:value-of>4</xsl:value-of>
            </xsl:when>
            <xsl:when test="$cpnNumber = 3">
                <xsl:value-of>2</xsl:value-of>
            </xsl:when>
            <xsl:when test="$cpnNumber = 4">
               <xsl:value-of>1</xsl:value-of>
            </xsl:when>
            <xsl:otherwise>
               <xsl:value-of>0</xsl:value-of>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:function>

Quite long, isn’t it?

This is how it can be shortened:

number(translate(xs:string($cpnNumbers[1]), '1234567890', '8421000000'))

How to change XML to lower case using XSLT multi-pass?

Today I’ve resolved another issue using XSLT multi-pass technique presented in my previous post. I think this be might useful in future, so I post it here. The XSLT translates the input XML to lower case (it changes only node names, it doesn’t affect data inside elements or attributes). After that, it applies the actual logic.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="firstPassResult">
<xsl:apply-templates select="/" mode="firstPass"/>
</xsl:variable>
<xsl:template match="@*|node()" mode="firstPass">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="firstPass">
<xsl:element name="{translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz')}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="$firstPassResult" mode="secondPass"/>
</xsl:template>
<xsl:template match="//boss" mode="secondPass">
<xsl:value-of select="text()"/>
</xsl:template>
</xsl:stylesheet>

The sample XML file (only to prove that this technique works):

<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Boss>John</Boss>
<BoSs>Henry</BoSs>
<BOSS>Nathan</BOSS>
<boss>igor</boss>
</Root>

It worked under Altova XMLSpy and produced the following output:

<?xml version="1.0" encoding="UTF-8"?>JohnHenryNathanigor

How to get rid of empty attributes in XSLT output?

Some time ago, I faced an issue related to XSLT stylesheets. It was while we were developing quite complex XSLT template whose purpose was to convert one  XML  into completely different another one. During the implementation, it turned out that the output XML contains a lot unnecessary empty attributes, e.g.:

<field attr="" />

It was actually possible to get rid of them with <xsl:if> or <xsl:choose> instructions. However, it would require one conditional instruction per one <xsl:attribute…/> instruction. This would make the XSLT file too complex. So I needed other approach, that would act after the basic transform. And this is achievable by usage of

multi-pass XSLT transforms.

Let’s say that we have the following input XML:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<database>
<record firstName=\"john\" lastName=\"smith\">writer</record>
</database>

We would like to have the above XML converted by this XSLT transform:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output indent="yes" exclude-result-prefixes="xs xsl"/>

    <xsl:template match="/database/record">
        <output>
            <xsl:attribute name="firstName">
                <xsl:value-of select="@firstName"/>
            </xsl:attribute>
            <xsl:attribute name="lastName">
                <xsl:value-of select="@firstName"/>
            </xsl:attribute>
            <xsl:attribute name="middleName">
                <xsl:value-of select="@middleName"/>
            </xsl:attribute>
            <xsl:attribute name="dob">
                <xsl:value-of select="@dob"/>
            </xsl:attribute>
            <xsl:text>recordFound</xsl:text>
        </output>
    </xsl:template>
</xsl:stylesheet>

The output looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<output firstName="john" lastName="john" middleName="" dob="">recordFound</output>

As you can see there are two empty attributes: middlename and dob. And this is where we can think about multi-pass transforms. We would like to have another transform applied on the results of the this XSLT.

Now, let’s consider what should be done to remove empty attributes. This is very to accomplish using identity transforms. This is simple variant of identity transform that removes empty attributes:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:template match="node()|@*" >
        <xsl:copy>
             <xsl:apply-templates select="@*[.!='']" />
             <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

What is left is merging our both  XSLTs. The following code is the final XSLT transformation. We are using modes that are available XSLT 2.0. We store the result of our business operation in the variable $firstPassResult and we perform the removal of empty attributes on it.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">    
    <xsl:output indent="yes" exclude-result-prefixes="xs xsl"/>
    <xsl:variable name="firstPassResult">
        <xsl:apply-templates select="/" mode="firstPass" />
    </xsl:variable>

    <xsl:template match="/database/record" mode="firstPass">
        <output>
            <xsl:attribute name="firstName">
                <xsl:value-of select="@firstName"/>
            </xsl:attribute>
            <xsl:attribute name="lastName">
                <xsl:value-of select="@firstName"/>
            </xsl:attribute>
            <xsl:attribute name="middleName">
                <xsl:value-of select="@middleName"/>
            </xsl:attribute>
            <xsl:attribute name="dob">
                <xsl:value-of select="@dob"/>
            </xsl:attribute>
            <xsl:text>recordFound</xsl:text>
        </output>
    </xsl:template>

    <xsl:template match="/">
        <xsl:apply-templates select="$firstPassResult"
            mode="secondPass" />
    </xsl:template>

    <xsl:template match="node()|@*" mode="secondPass">
        <xsl:copy>
             <xsl:apply-templates select="@*[.!='']" mode="secondPass"/>
             <xsl:apply-templates select="node()" mode="secondPass"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

“Real programmers do not use Pascal”

I’ll bet that most of the readers of this blog have never read that article. To be honest I read it in 1993, when a Polish magazine about 8-bit Atari computers published its translation.
I must admit that I didn’t realize how ironic it was when I read it for the first time. I was convinced that the guy seriously tried to define and describe the Real Programmer. But it’s not important right now. Don’t you get an impression such attitude appears sometimes when you’re dealing with IT engineers, software developer or simply programmers? “Because… real programmers DO this and DO NOT DO this…” But if you could read their thoughts you’d know that the only thing that really matters is to express the superiority of MY EGO…

To be continued…

A piece of very old assembly code

A couple of weeks ago I’ve been looking through some old backup CDs. To my surprise I have found some old source codes written more than 10 years ago. Of course, most of them were unfinished and ugly. Some of them turned out to be totally useless rubbish which made me feel embarrassed. But I have also found a couple of interesting things. For a moment I was amazed… I still cannot believe that I used to have so much patience to write code in pure assembler.
Here is a sample source code. It was supposed to be a 4KB intro.
The intro was written in 1999 (a twilight of MS-DOS era) in pure 80×86 assembly language. It should run on 32-bit Windows. The executable itself is 3529 bytes long and it is .COM executable file. Its name was supposed to be “Loser”, but I really do not remember why such a title was chosen. There were some nasty tricks used to make the code as short as possible… but who would care about them today? It doesn’t generate any sound effects or music, but trust me it was also possible in assembler! The visual effects were of course supposed to make an impression of a damaged TV-set (so please do not adjust your monitor:).
I have recorded and uploaded this animation on YouTube:

If you would like to compile source code of this “demo” you should use Borland’s Turbo Assembler and Turbo Linker.

Problem: MS Office Communicator 2005 fails to install

In my current company, I’m forced to use IM which is MS Office Communicator 2005. Accidentally (or perhaps my tortured subconsciousness told me do so;), I uninstalled this thing. Unfortunately, re-installation of this application wasn’t so easy as it seemed to be. When I ran installer it exited saying that Communicator could not be installed. That was really strange. After a couple of hours, I’ve finally realized that configuration of my workstation doesn’t really differ from the configuration of a typical WinXP machine, so the problem may be in some service pack or some security update. And that was it. A little creature named “Security Update KB974571″ was the reason causing the failure (it was enough to uninstall it and reboot the computer). I wasted about 4 hours of my life which could be spent on coding.