tisdag 27 augusti 2013

Jasmine test a require.js + backbone + marionette application

We are building an application using Backbone. And since just about everyone talks about using Require.js, this was an obvious thing to do. The application gets really neat, but it turns out that testing the application is a bit harder...

Just in case someone stumbles upon this, here is a small gist on how I did it:
https://gist.github.com/me97esn/6353749

måndag 29 april 2013

Porting a Java Spring Hibernate Maven application over to Grails - Part 1

If you want to learn to build an application from scratch, there are plenty of tutorials available online. But what if you are stuck with an existing Java application, how easy is it to port it over to Grails? If the existing application is using spring, maven and hibernate or JPA, it is actually quite easy. But before we start the porting, lets look at the Grails architecture compared to a "standard Java architecture":
As you can see, the architecture of Grails is quite similar to the one on the left (which is a Java + Spring MVC application I pulled from GitHub).
There are some small differences:
- Grails has no DAO-layer (What! No more DAO? That's the most fun of all the Java programming I do!)
- The Java application on the left hand side uses Maven to handle dependencies and building, Grails on the right handles dependencies and building on it's own (If you want, you can choose to continue to build with Maven, more on that later in this article).

The similarities makes the porting over to Grails pretty painless, hopefully we only have to place the files in the correct place according to the Grails conventions, and we're good to go.

Next up, part2

fredag 26 april 2013

Porting a Java Spring Hibernate Maven application over to Grails - Slides

Yesterday I gave a presentation at https://sites.google.com/site/stockholmgtug/Home/april-mte-2013 about how to port an existing java application over to Grails. This is the slides from that presentation. http://www.youtube.com/watch?v=msr3LcDjmIc&feature=youtu.be

onsdag 21 mars 2012

Testing a JSON-RPC service with Firefox

Let's say you've created a json-rpc service like suggested here and you want to verify that it works. How can you do that?
The easiest way I've found is with the RESTClient plugin to firefox.
I have created a 'hello' method in my service, without any parameters, and am calling it like this:

torsdag 17 mars 2011

Grails script for running different environments

I have seen questions for how to do this, but it was really hard finding an answer, so I thought I might as well document it here.

As always, it´s really easy once you know how:


includeTargets << grailsScript("Init")

import org.codehaus.groovy.grails.cli.GrailsScriptRunner

target(main: "The description of the script goes here!") {
System.setProperty("grails.env", "wadl")
GrailsScriptRunner.main("test-app")
}

setDefaultTarget(main)

torsdag 24 februari 2011

Building a DSL with groovy

I am currently working an a small 2-d game, where there're supposed to be some organisms spread out over the "world". I soon realized that I will have to test different setups of the world with organisms spread out.
I wrote some tests for different locations of organisms, and added some comments to explain it visually.

The original test contained something like this:

/**
* This creates a world with two organisms, located:
* ---------------
* |______________|
* |_____1________|
* |__________2___|
* |______________|
*
*/
def world = new World(organisms: [new Organism(id:1, x:10,y:10), new Organism(id:2, x:20, y:20)])

This was an obvious candidate for a DSL; I beleive that any time you feel the need to explain your code (visually) with a comment, you should consider building a DSL instead.


This is my first draft:

class Organism {
def id, x, y, radius
}
class World {
def width, height
def organisms = []
}
class WorldBuilder {
def yIndex = 0
def organisms = []
def radius = 1
def xIndex = 0
def getProperty(String name) {
def metaProperty = metaClass.getMetaProperty(name)
if (!metaProperty) {
if(name.length() > xIndex){
xIndex = name.length()
}
name.eachWithIndex {character, index ->
if (character != '_') {
println "create an organism with: x:$index, y:${yIndex}, radius: 1"
organisms << new Organism(id:character, x:index, y:yIndex, radius: radius )
}
}
yIndex++
println "get the property with name: $name"
return this
}else{
return metaProperty.getProperty(this)
}
}

def build() {
new World(organisms: organisms, width: xIndex, height: yIndex)
}
}

class WorldBuilderTests extends GroovyTestCase {
void test_build() {
World world = new WorldBuilder().with {
_________1_____2
__3__4_____a___6
________________
}.build()
assertEquals(6, world.organisms.size())

def org1 = world?.organisms.find { it.id == "1" }
assertEquals(1, org1.radius)
assertEquals(9, org1.x)
assertEquals(0, org1.y)

assertEquals(16, world.width)
assertEquals(3, world.height)

}
}

onsdag 9 februari 2011

GroovyWS: setting endpoint and soap headers

I've seen questions on various forums regarding how to change the endpoint and add soap headers with GroovyWS.
Since GroovyWS is using CXF under its cover it is possible (albeit not intuitive) to do:


@Grab(
group='org.codehaus.groovy.modules',
module='groovyws',
version='0.5.2')
import groovyx.net.ws.WSClient
import org.apache.cxf.headers.Header
import javax.xml.soap.SOAPFactory
import javax.xml.namespace.QName
import org.apache.cxf.binding.soap.SoapHeader
import javax.xml.ws.BindingProvider
import org.apache.cxf.interceptor.LoggingOutInterceptor

proxy = new WSClient(
"http://www.w3schools.com/webservices/tempconvert.asmx?WSDL", this.class.classLoader)
proxy.initialize()

def cxfClient = proxy.client

def headers = []

// Add header
SOAPFactory sf = SOAPFactory.newInstance()
def authElement = sf.createElement(
new QName("a_namespace", "a_name"))

def productToken =
authElement.addChildElement("productToken")

def tokenHeader = new SoapHeader(
new QName("a_namespace", "a_name"), authElement);
headers.add(tokenHeader);

cxfClient.getRequestContext()
.put(Header.HEADER_LIST, headers)

// Set endpoint
cxfClient.getRequestContext()
.put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "another_url")

// Add a logging interceptor
cxfClient.outInterceptors.add(
new LoggingOutInterceptor())

result = proxy.CelsiusToFahrenheit(0)
println """You are probably freezing at
${result} degrees Farhenheit"""