Sunday, December 29, 2013

Eclipse – Jenkins

Eclipse – Jenkins


Of course my assumption is that you are practicing continuous integration. If not you should begin with it right away. For more information see http://en.wikipedia.org/wiki/Continuous_integration.
One of the problems with Jenkins is that you need to always open the browser to check what the status of the build is. One of the rules for CI is never commit on a broken build (some even say on a running build), so you need to know the status of the build in a simple an easy way.
Another issue, is once the build is broken you need to click multiple times until you can get the information that you need to fix the build.
Well all this changes with the plugin for eclipse. You can download the plugin from http://marketplace.eclipse.org/content/hudsonjenkins-mylyn-builds-connector#.UsAyivQW1VA, or just search the market place for Jenkins.
This plugin will display the builds that you choose within your eclipse:


The interval for the status checking should be changed since the default is 15 minutes.
A double click on the build will display the tests run, where you can choose to see the console output



On the junit view, you can click on any test, and it will open the code for that test. You can filter the view to see only failed tests or all of them.
A right click on the build will bring you the option to view old build or even to run a new build.
Eclipse even support instant notifications that pop up when a build finished.
Once you install this plugin you will never need to open the Jenkins in your web browser again

Wednesday, December 18, 2013

XML Subsets

XML Subsets

Most applications today use XML for some form of data saving. In testing we also need to validate XML files, find entries in the XML via XPath and search for differences in the XML.
A lot of these abilities can be found in the lib of XMLUnit (http://xmlunit.sourceforge.net/)
This lib knows how to compare XML files, and other actions like:
·         The differences between two pieces of XML
·         The outcome of transforming a piece of XML using XSLT
·         The evaluation of an XPath expression on a piece of XML
·         The validity of a piece of XML
·         Individual nodes in a piece of XML that are exposed by DOM Traversal
One feature that I needed that it did not support is to detect if one XML is a subset of another. For example
I would like to check the following XML is a subset of the next one.
<a>
    <b>
        test b
    </b>
</a>
<a>
    <b>
        test a
    </b>
    <b>
        test b
    </b>
</a>
As you can guess a simple compare will fail since it will compare the first node of b in both XML’s and since the elements are not same it fails. What we need to do is to recursively search all nodes that have the same name and check if they are the same and not to stop on the first. This problem of course is not just on nodes but on attributes as well. If you need this functionally then you can grab the code below:

public class XmlSubsetHelper {
       static private boolean equalNotNullPointers(Object a, Object b) {
              if ((a == null && b != null) || (a != null && b == null)) {
                     return false;
              }
              return true;
       }

       static public boolean isSubset(NamedNodeMap namedNodeMapA,
                     NamedNodeMap namedNodeMapB) {
              if (!equalNotNullPointers(namedNodeMapA, namedNodeMapB)) {
                     return false;
              }
              if ((namedNodeMapA == null && namedNodeMapB == null)) {
                     return true;
              }
              for (int i = 0; i < namedNodeMapA.getLength(); i++) {
                     Node itemA = namedNodeMapA.item(i);
                     Node itemB = namedNodeMapB.getNamedItem(itemA.getNodeName());
                     if (itemB == null) {
                           return false;
                     }
                     if (!itemA.getNodeValue().equals(itemB.getNodeValue())) {
                           return false;
                     }
              }
              return true;

       }

       static public boolean isSubset(NodeList childNodesA, NodeList childNodesB) {
              if (!equalNotNullPointers(childNodesA, childNodesB)) {
                     return false;
              }
              if ((childNodesA == null && childNodesB == null)) {
                     return true;
              }
              for (int a = 0; a < childNodesA.getLength(); a++) {
                     boolean foundMatch = false;
                     Node itemA = childNodesA.item(a);
                     for (int b = 0; b < childNodesB.getLength(); b++) {
                           Node itemB = childNodesB.item(b);
                           if (isSubset(itemA, itemB)) {
                                  foundMatch = true;
                                  break;
                           }
                     }
                     if (!foundMatch) {
                           return false;
                     }
              }
              return true;
       }

       static public boolean isSubset(Node nodeA, Node nodeB) {
              if (!equalNotNullPointers(nodeA, nodeB)) {
                     return false;
              }
              if ((nodeA == null && nodeB == null)) {
                     return true;
              }
              if (!nodeA.getNodeName().equals(nodeB.getNodeName())) {
                     return false;
              }
              if (!isSubset(nodeA.getChildNodes(), nodeB.getChildNodes())) {
                     return false;
              }
              if (!isSubset(nodeA.getAttributes(), nodeB.getAttributes())) {
                     return false;
              }

              return true;
       }

       static public boolean isSubset(Document documentA, Document documentB) {
              if (!documentA.getDocumentElement().getNodeName()
                           .equals(documentB.getDocumentElement().getNodeName())) {
                     return false;
              }
              if (!isSubset(documentA.getDocumentElement(),
                           documentB.getDocumentElement())) {
                     return false;
              }
              return true;
       }

       static public Document parseXML(String xmlSource) {
              try {
                     DocumentBuilderFactory factory = DocumentBuilderFactory
                                  .newInstance();
                     DocumentBuilder builder = factory.newDocumentBuilder();
                     return builder.parse(new InputSource(new StringReader(xmlSource)));
              } catch (Exception e) {
                     throw new RuntimeException(e);
              }
       }

       static public boolean isSubset(String xmlA, String xmlB) {
              try {
                     return isSubset(parseXML(xmlA), parseXML(xmlB));
              } catch (Exception e) {
                     throw new RuntimeException(e);
              }

       }

}

Tuesday, December 3, 2013

Hebrew on the Pebble

Hebrew on the Pebble

There are many new smart watches out in the market. My personal one is the pebble. In my opinion a watch is an extension of the phone, and not a replacement,   and the pebble does just that.
The main disadvantage of the pebble is the SDK environment. There is no simple way to create a pebble application in windows. The workaround for this is a cloud environment that is very nice, you can find it at: https://cloudpebble.net/ide/. In the cloud IDE you upload your resources and write your code. From the cloud you can also compile the application and download it for installation from your phone.
This article will not go into the details of the pebble development. In a broad overview, the pebble GUI is made out of multiple layers that you can create. Each layer can have its own callback so that you can update each layer on a timer that suits you, and only update that layer. There are many API’s for writing text or graphics on each layer so that you can display almost any idea you have.

Language

The main feature that is lacking in my option is the support for multiple languages. Pebble supports only English, and does not support other languages. I am not sure for the reason behind this, it might be since the main market is in the US or the limited memory that the pebble has, or both.
For a review in Hebrew about the watch see: http://www.geektime.co.il/pebble-smartwatch-review/.
The pebble supports two types of applications. Once is a watch face and the other is a watch app. The watch face displays the time in different types of formats and visualizations (a list of watches can be found at http://www.mypebblefaces.com). The watch app is a richer application that usually also interacts with your phone.
If you want to create your own watch face but do not know or want to program, there is a great site that lets you customize your own watch app: http://www.watchface-generator.de/.

Fonts

This blog will deal only with the watch face, and in a future post we will write about watch apps.
What the pebble does support is the usage of True Type Fonts (TTF - http://en.wikipedia.org/wiki/TrueType). The pebble itself comes with some basic fonts and sizes built into the rom (https://developer.getpebble.com/blog/2013/07/24/Using-Pebble-System-Fonts/). You also have the option to add your own custom fonts at the cost of rom location in your app (http://developer.getpebble.com/sdkref/group___fonts.html).

Solution

Once I found this, the rest is as they say history. All I had to do is to use a Hebrew Font, say David, and decide on the font size that I want. I could then display any graphics that I wanted to, and when adding text I needed to use the TTF that I added to my watch. The only disadvantage is that since the pebble does not support RTL you need to write works backwards for it to display correctly.
The pebble has computing capabilities as well, so with a build in math lib, you can easily create a conversion from Gregorian dates to Jewish dates.  Do in addition to a Hebrew clock, you also get the current Hebrew date, and day of the week.

A copy of the watch face I created can be found at:



For the more adventurist people that want to see the code for the pebble have a look at:

More Hebrew watches

Another nice watch that displays in Hebrew words the time can be found at:




A watch that knows how to display the moon phase can be found at:






Tuesday, November 12, 2013

WSDL 2 Java

WSDL 2 Java
So you decided to do wsdl 2 java design. This is currently the more “proper” way to go about it. For more information about this debate see: http://docs.spring.io/spring-ws/site/reference/html/tutorial.html. You have the wsdl but how do you generate the code?

Maven Plugin

If all you need is to convert one wsdl, you can do a quite simple solution with maven using the ant plugin:

<plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-antrun-plugin</artifactId>
       <executions>
              <execution>
                     <phase>process-classes</phase>
                     <configuration>
                           <target>
                                         <java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true">
                                         <arg value="-impl" />
                                         <arg value="-b" />
                                         <arg value="bindings.xjb" />
                                         <arg value="-server" />
                                         <arg value="-d" />
                                         <arg value="gen" />
                                         <arg value="-wsdlLocation" />
                                         <arg value="/WEB-INF/wsdls/my.wsdl" />
                                         <arg value="my.wsdl" />
                                         <classpath>
                                                <path refid="maven.compile.classpath" />
                                         </classpath>
                                  </java>
                           </target>
                     </configuration>
                     <goals>
                           <goal>run</goal>
                     </goals>
              </execution>
       </executions>
</plugin>

This plugin will generate using the cxf engine a java file with all the schema files needed for the code.
This is a good solution if you just need one file or want to just look things over.
If you have multiple wsdl’s and you want to intervene during the generating process you need to do it by code. Also in the case that you do not know in advance what the wsdls are but only know the directory of the wsdls, then you need to generate them via code.

Java Code

To run the cxf engine via the java code you will need to run the same command line as above just via java code.
So why do we want to use java code and not just a simple maven plugin. First reason is, if you want to add a binding file. For more about binding please see: http://www.oracle.com/technetwork/articles/entarch/jax-ws-jaxb-customization-082750.html, https://blogs.oracle.com/sdimilla/entry/implementing_handlers_using_jaxws_2.
Binding files allows you to change the way the java code is generated. You can add annotations, or define the package name and many more things.
Binding files, recommend that you add the wsdl location in the binding file, like:
<jaxws:bindings     
  wsdlLocation="xxx.wsdl"
The disadvantage of this, is that you need to create a template binding file so that you can change the name for each wsdl file. The other option is to just omit this section in the binding file.

The main options are:  where to generate the code, whether or not to compile it, and the wsdl location. For all options see http://cxf.apache.org/docs/wsdl-to-java.html.
Another issue that needs to be handled is the location of the command path relevant to the bind file. You can put the bind file where ever you want, but when passing the location to the api, the relative path must be relative to the working directory of the command execution.  For an easy way of calculating relative paths between directories please see:

Code generation intervene

One of the features that are greatly missing is the ability to intervene in the code generation of the wsdl service file. If you want to intervene in the generation of the xsd files, you can do this with jaxb plugins.

For a nice example that adds support for XmlElementWrapper see (https://github.com/dmak/jaxb-xew-plugin). For more information on plugins and jaxb extensions see http://www.jroller.com/gmazza/entry/enhancing_jaxb_artifacts.
The problem is that for the wsdl generation cxf did not use the jaxb plugins scheme and there is no official way to intervene in the code generation.
I “hack” can be done. Cxf uses a velocity template to generate the file. The velocity template name is iml.vm. So if you write your own template (and place it in the same package as cxf template), and add it to a jar that is in the classpath before the cxf jar, your template will be used by cxf and not the internal one.

Other issues for code generation

If you are generating all the web service stubs, you will most probably also want your code to add to your cxf xml file all the proper endpoints as well.

Generation of Code

Since we will be generating the service classes from the wsdl’s you need to think if this process is a onetime process or an ongoing one. If it is part of your build system, then you cannot edit the generated files (by default cxf generates the files with a TEMP suffix). There are a few options to solve this issue. First you need to make sure that all generated files are not in the source area but in the generated (/target/ generated-sources).

You now have a few options of how to add your specific code, while still continue to regenerate the service classes. The simple one is to just copy the service class into you source code. This sort of defeats the idea of having new services added dynamically. Second solution is if you edited the  template generation you can have all services delegate the call to a central spring bean, and then use the proxy pattern to handle all calls. Third option is to add an AOP layer to catch all calls to the service and write a generic code for all of them without having to do any wiring or changing of cxf templates.

Wednesday, October 23, 2013

Java Decompiling and Eclipse


Java Decompiling and Eclipse


One of the more frustrating issues are when you debug code in eclipse and want to debug a third party jar but do not have the source.


If the jar is open source and you are using maven you can try to use
mvn install –DdownloadSources=true.
This does not always work, since not all jar’s supply the source files.
In addition there are third part jar’s or sun jar’s that do not have source files. For this we have decompile utilities. The most famous one is jad (http://en.wikipedia.org/wiki/JAD_(JAva_Decompiler)).
So just go to your eclipse market and write jad. Once you have installed the plugin, next time you need to look at the code you will see: