Advertisements

General Musing

blaze your trail

Posts Tagged ‘java

Mocking up your objects with EasyMock #test #junit #xp #tdd

leave a comment »

I’ve recently been using EasyMock to create mock objects to pass in my JUnit tests, and I often find that I often mock up objects for private members which are auto-wired or injected by the platform, and in some cases are unrelated to the code I unit testing. Usually it is too expensive to run a full environment of middleware for testing, and it is also necessary to test the exception handling code which will most likely not be called unless it is specifically mocked.

So I wrote the following function to insert a mock object into a private member:

public <T> T insertPrivateField(Object instance, Class<?> targetClass,
        String targetFieldName,
        Class<T> mockedClass) {
    T mockedObject = null;
     try {
        Class<?> clazz = Class.forName(targetClass.getCanonicalName());
        Field field = clazz.getDeclaredField(targetFieldName);
        field.setAccessible(true);

        mockedObject = EasyMock.createMock(mockedClass);
        field.set(instance, mockedObject);
        } catch (Exception e) {
         log.error(e.getMessage(), e); // logs exception with log4j
        fail(e.getMessage()); // junit failure generated
    }
     return mockedObject;
}

Have fun with it.

    

Advertisements

Written by Daniël W. Crompton (webhat)

August 15, 2011 at 1:41 pm

Posted in programming

Tagged with , , , , , , ,

Proof of Concept: XML and SQL mapping with JiBX and Hibernate #orm #hierarchy

leave a comment »

I was reading ESB in Action* while preparing to implement Apache ServiceMix and was interested to read about JiBX, which is a tool for binding XML data to POJOs (Plain Old Java Objects). What it immediately reminded me of was Hibernate, which does ORM (Object Relational Mapping), and the possibilities there were for integration. This brought the thought to my head that together they could be used to create a WebService which could expose features of the database easily.

JIBX example

A search brought me the view of a Hibernate developer who pointed out in 2004 that there was a “impedance mismatches here: object/relational and object/hierarchical.”[1] And I believe that would be true if it wasn’t for the fact that much of the data in relational databases is mostly hierarchical in nature. Even the core pattern of the embedded indexing in Hibernate Search assumes that the data being indexed is a nested hierarchy or inclusion hierarchy. I won’t get started on the issues that Hibernate Search has due to this impedance mismatch, needless to say there are a number.

The JiXB[2] documentation is quite clear, and makes it easy to implement.

  1. XML Marshalling/Unmarshalling
  2. A basic binding

Image source:

Written by Daniël W. Crompton (webhat)

August 3, 2011 at 11:50 am

Posted in database, programming

Tagged with , , , ,

6 Months of Security Links #2011

leave a comment »

I’m a regular curator of daily links, and like to give overviews of my collection of curated links and posts. This is partly as there are some good sources and articles in here and as I am working on a research project which I started based on a number of books I read.

I’m sure you’ll find something interesting in the items below – there are some gems in the list – and I dare to hazard the guess you might learn something you wanted to know. 🙂

Read the rest of this entry »

Written by Daniël W. Crompton (webhat)

July 15, 2011 at 4:10 pm

Posted in tagging

Tagged with , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

J2EE MindMap #programming #java

with 4 comments

Java Logo

I recently made myself a map of terms I had found related to J2EE. Most of these items aren’t completely unknown to me, and this list is far from complete.

– Denotes the definition of the anagram abbreviation

Image source: Java Logo – Wikimedia; J2EE Mind Map – me

Written by Daniël W. Crompton (webhat)

March 15, 2011 at 2:49 pm

Posted in programming, technology

Tagged with , , ,

The Future Of Java: Java’s Greatest Stench? #strength #programming

leave a comment »

Java

John R. Rymer wrote a piece on Forrester about the The Future Of Java, it’s pretty complete with the exception of the greatest weakness being called the greatest strength.

“Fewer young developers will learn Java first. One of Java’s greatest strengths has been the number of young developers who learn it as a first language.”
John R. Rymer

It may have been an advantage for the adoption of Java as a programming language, but it is certainly not a strength of a programming language that students learn it as a first language.

Personally I don’t see it as an advantage for students to learn Java as a first language, it means that every language learned afterwards is seen from a perspective of a language which doesn’t teach the student simple rules like type safety or writing portable code. A programming language as teaching tool should allow a student to make all the mistakes which can be made, as a student learns more of their failures than from their successes. It should allow different programming modes, Object Oriented, Linear and a mixture.

I’ll stop here before I start talking about round pegs and square holes. 😉

Image source: Hillary Hartley

Written by Daniël W. Crompton (webhat)

January 25, 2011 at 8:06 pm

Posted in programming, school

Tagged with , ,

%d bloggers like this: