Thursday May 25, 2006

NetBeans in full screen mode

Yesterday I was speaking with Stanislav Albrecht. He is working on fullscreen mode feature in NetBeans. He showed me it on Windows L&F. The feature has not finished yet. He need to implement full sreen mode feature also for others Look&Feels. But I was impressed. I use fullscreen mode in other application on my notebook. It saves workspace on my display. I can use it now also in NetBeans.

UI spec with flash demo for this feature is available on this page. Stanislav gave me a build with this feature for testing. I will show few screenshots. On the first screenshot is NetBeans in normal mode.

Two actions are highlighted in the screenshot. The first is in main menu. It shows NetBeans in full screen mode. The second is represented by icon in the right corner of window. It maximizes the window in NetBeans layout. The other windows are slided off - minimized by window sliding feature. I clicked on both actions. The result is on the screenshot below.

The main menu will be hidden in final version. This feature will be also available for application based on RCP. Developers don't need to change their applications. They only need to rebuild their applications with new NetBeans platform. The full screen action and maximize window button will be added automatically.

Tuesday May 23, 2006

Test size of your data structures in Unit Test

Petr Nejedly invented interesting tool for measuring memory usage of data structures on java heap. Name of the tool is Insane. Pages of the tools are here. It uses Java introspection to traverse all fields and references in graph of data structure. It allows to exclude traversing for specified references. Insane was integrated with NBJunit library. NBJunit is extension of Junit 3.x. It was designed in order to test NetBeans IDE. In this article I try show power of Insane library inside NBJunit.

NBTestCase class is supper class of for NetBeans test. It extends from Junit TestCase. The NBTestCase contains assertSize methods for testing size of java structures. For example we want to measure size of NumList class:

    public static class NumList {
        public int num;
        public NumList next;

A instance of the class takes 16 bytes on the heap on 32bit virtual machine. A simple test for this structure can look like this:

  public void testA() {
        NumList item = new NumList();
        // 1. test one instance   
        assertSize("NumList a",16,item);
        // 2. test size of instances in collection 
        assertSize("NumList a",Collections.singleton(item),16);
        NumList item2 = new NumList(); = item2; 
        // 3. test linked structure 
        assertSize("NumList a",Collections.singleton(item),32);
        // 4. ignore the item2 references
        assertSize("NumList a",Collections.singleton(item),16,new Object[]{item2});

The test passed when the size of the structure doesn't exceed the defined limit (16 or 32 bytes). Interesting is use case on line number four

assertSize("NumList a",Collections.singleton(item),16,new Object[]{item2});

The last parameter tells that the measurement won't traverse through item2 reference. It is very useful when we measure only subset of complicated structure.

The test passed. Why the instance of NumList takes 16 bytes in memory of 32bit JVM?

  • 8 bytes are for object header
  • 4 bytes are allocated for num variable
  • 4 bytes are allocated for reference to next NumList
We can start to experiment with our structure. We add one field to NumList class:

    public static class NumList {
        public int num;
        public int num2;
        public NumList next;

We run the test again. The test fails with message:

Testcase: testA(MemoryTest):	FAILED
NumList a: leak 8 bytes  over limit of 16 bytes
  MemoryTest$NumList: 1, 24B

junit.framework.AssertionFailedError: NumList a: leak 8 bytes  over limit of 16 bytes
  MemoryTest$NumList: 1, 24B

        at org.netbeans.junit.NbTestCase.assertSize(
        at org.netbeans.junit.NbTestCase.assertSize(
        at org.netbeans.junit.NbTestCase.assertSize(
        at org.netbeans.junit.NbTestCase.assertSize(
        at MemoryTest.testA(

Instance takes 24 bytes in memory now. How is it possible if the int type tales 4 bytes in memory? Yes it takes four bytes. The instance takes 24 bytes on heap because the size of java object is aligned to 8 bytes. So we will fix the test for new NumList version.

  public void testA() {
        NumList item = new NumList();  
        assertSize("NumList a",24,item);
        assertSize("NumList a",Collections.singleton(item),24);
        NumList item2 = new NumList(); = item2; 
        assertSize("NumList a",Collections.singleton(item),48);
        assertSize("NumList a",Collections.singleton(item),24,new Object[]{item2});

The test passed now. The instance is aligned to 8 bytes as I wrote. Why to no try add one more varible to TestNum structure? The new version is below.

    public static class NumList {
        public int num;
        public int num2;
        public int num3;
        public NumList next;

This version does't need more memory than the previous. We can check it by running the test. Yes, the test passed now. Isn't combination of Insane ane NbJUnit great idea?

Monday May 15, 2006

TestNG over beats JUnit ?

GlassFish project quality team chose TestNG framework for testing. Why did they make the decision? They compared the both tools in this document.

It looks like war Eclipse versus NetBeans. NetBeans dominate five years ago in Java IDE's. NetBeans had few dessign problems and almost no innovation. Therefore Eclipse eclipsed NetBeans. But the situation is quite different now. There was many new features in NetBeans 5.0. Everything is in one pack (J2SE, J2EE, J2ME, profiler). The NetBeans team is focused on usability. Many 3rd party plugins are available on NetBeans 5.5 beta with EJB support has been released. Completely new java editor will be in NetBeans 6.0.

. And eclipse? Yes there exists many plugins for eclipse. But with different UI and the plugins are not compatible. No it is no end of Eclipse. It's challenge for Eclipse team. They also need to innovate. So who wins the war between two java opensource IDE's? I think the user. He will have two powerful java IDE's. Isn't great to have two different opensourced java IDE's in the world?

Back to subject TestNG versus JUnit 4. What is idea of TestNG? I think:

  • Support features which are missing in JUnit
  • Simple usage. Only one jar without dependencies on 3rd party libraries and simple API.
So Junit needs to innovate. To copy features from TestNG is not enough.

Saturday May 13, 2006

Mocking final classes with BCEL, java.lang.instrument and EasyMock

I've introduced EasyMock library in previous entry of testing category. I read in documentation of EasyMock that final methods cannot be mocked. I've found solution of this problem for testing with Jmock library. Bytecode of the class is changed before the class is loaded. The solution also works for EasyMock. I'll describe how to do it in next paragraphs.

The Byte Code Engineering Library (BCEL) library provide simple way how to navigate and change bytecode of java classes. I used this library in order to remove final flag from classes. It is only few lines by using this library.

Java.lang.instrument provides services that allow Java programming language agents to instrument programs running on the JVM. Nice article about it is here. I used java.lang.instrumnet for changing bytecode during classloading.

Example with removing final flag is below. Transf class is implementation of java.lang.instrument service. It is not difficult to add removing of final flag for methods of class to code. I created this example to demonstrate how powerful is java.lang.instrument service.

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import org.apache.bcel.Constants;
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;

public class Transf implements ClassFileTransformer {
	public byte[] transform(ClassLoader loader, 
                                String className, 
                                Class redefiningClass, 
                                ProtectionDomain domain, 
                                byte[] bytes) throws IllegalClassFormatException {
                try {
                    ClassParser parser = new ClassParser(new ByteArrayInputStream(bytes),className);
                    JavaClass clazz = parser.parse();
                    clazz.setAccessFlags(clazz.getAccessFlags() & (~Constants.ACC_FINAL ));
                    bytes = clazz.getBytes();
                    System.out.println("Transformed Class: " + className);
                } catch (ClassFormatError ex) {
                } catch (IOException ex) {
            return bytes;

To initialize the class patching agent it is necessary to create premain class:

public class SimpleMain {
	public static void premain(String agentArguments, Instrumentation instrumentation) {	
		instrumentation.addTransformer(new Transf());
The referece of the premain class is specified in of build jar with java agent.
Manifest-Version: 1.0 
Premain-Class: mypackage.SimpleMain
When we run tests we need to say java where the java agent service is located by using -javaagent parameter.
java -javaagent:classpatching.jar ...

Friday May 12, 2006

SourceForge - CVS server down for a week and up with different host name!

CVS server is many times down. The last was on the end of last week. The server is up again today. But with a small difference. The host name of my cvs server has been chaged. What's mean for me? I was developing for a week without commit. I will have to checkout the cvs repository to other folder on my local disk. Copy changed files from old cvs checkout to new folder. SourceForge have many other problems. I started lookong for new opensource provider. When I find them I'll move the nbxdoclet project.

Hibernate plugin for NetBeans updated

Today was updated Hibernate plugin for NetBeans 5.x. It's is available on its update center . Few bugfixes and new features were integrated:

Customizing velocity templates of wizards. All templates are written in Velocity language (I would like to switch to Freemaker engine in future. FreeMaker is more powerful than Velocity.). The Templates browser is available from main menu ( Window|Show Velocity Teplates). The templates can be changed in editor. Opened editor with hibernate11relation is below on the screenshot. If someone know how to improve the code of templates he can send me his new version.

HQL wizard for EJB3 query language. UI is the same like for HQL wizard. Only the wizard is available in code completion of facade for EJB3 POJO.

User hibernate properties for hibernate project extension. The Hibernate project extension generated default hibernate properties file. It was difficult to change the properties manually. So I decided to merge the generated properties with new user properties. The user properties are available from popup of Hibernate project extension.

Creating Hibernate util when the facade is created. There was a usability problem. When user wanted to create new hibernate facade the HibernateUtil class had to already created. The HibernateUtil wizard is now shown when the HibernateUtil is not presented in project on creating facade from code completion. The facade class is automatically opened in editor after creating. It's expected behaviour, I hope.

Thursday May 11, 2006

Test Patterns In Java™ Technology Development on Java One

Are you going on Java One? Don't forget on Test Patterns In Java™ Technology Development session.

  • Tuesday , 05/16/2006, 09:30 PM - 10:20 PM BOF-0220

Three architects of NetBeans (Jesse Glick, Jaroslav Tulach, Milos Kleint) will be talking about their experiences with test driven development. To tell you truth, these guys save me a lot of time in my work. Yes, they are careful and write tests whether it is possible. Almost no regression is in their code. I than can cover more features as QA representative.

I tell you a success story with test driven development. Jaroslav Tulach is one of the NetBeans fathers. I remember times when he started to write tests. He wrote API of NetBeans. Many developers dependent on his code. Before four years every one in NetBeans were afraid of his commits. A small change in code on which depends many developers caused the break of NetBeans build. But now he write also a lot of code. But the code is already tested with unit tests. And the code is really stable. Every one writes code with mistake. With test the code contains a little count of bugs and we are able to catch regressions earlier with daily tests runs.

I learn from Jaroslav's and Jesse's code a lot of useful testing pattern. Therefore I recommend the session.

Session Abstract

Testing is an important part of software development. Effective testing is a key factor in reducing total cost of maintenance of any application over its lifetime. It reduces the cost and time of development and can increase savings on quality assurance. Knowing when to invest in better design, in postdevelopment quality assurance, in manual tests, or in automatic testing forms a basic difference between successful and unsuccessful software projects in these tough and competitive days.

This presentation starts with general motivation and presents automatic tests as a form of functional specification. It then quickly delves deep down into hard-core JUnit test examples showing various forms of regression tests verifying algorithm complexity, memory management, data structure sizes, deadlocks, race condition behavior and tests randomly generating new test cases, simulating user clicks in the UI, API signature tests, and so on.

Attendees should know what tests are and that tests are an important and useful part of software development. The presentation demonstrates practical usage of a test framework. You learn techniques, tips, and tricks for testing various aspects of real-world Java™ Platform, Standard Edition (Java SE) applications. The session also gives examples of the savings and improvements that were achieved by increased usage of extensive automatic testing in the development of the NetBeans™ IDE and platform.

Monday May 01, 2006

First Experience with EasyMock

I like test driven development. But I don't write tests for all features of my code because it is difficult to setup testing environment. For example it is difficult to set up unit test environment for NetBeans Java Model (API). There are too many dependencies. It is possible to run tests in running NetBeans (IDE mode). But I don't prefer this variant because it takes a lot of execution time. Therefore I didn't write tests for pieces code which uses JMI API.

I found regression in my code which work quite long time. So I designed to create Mock objects for few JMI interfaces. Mock objects is implementation of classes for testing purpose. Simply you write tests against API without its implementation, It's mean that you define a scenario of use case of API's pieces in your test. For example you want to to develop tests for application which uses jdbc connection to access data. To setup database is quite difficult. So you write tests without database. You need to simulate jdbc. In this case you can use already implemented mock objects for jdbc api. Developing JDBC applications test-first is nice article about mocking jdbc. Writing of test is simple when jmock library already exists for the API. But in many cases there is no jmock implementation for API which you are using and the api is not well designed for testing. But you want to write tests at least for the critical code. You will have to write jmock objects yourself. Fortunately you can use library for writing jmock object. I know two libraries for Java languages for this purpose:

I tried to use JMock library in the past. You can mock interface and classes. I don't like two things on JMock. JMock uses strings for mocked method names. I have bad memory and I am alredy got a customed to use code completion. The jmock was dessigned for junit 3.8. The recomended way how to use jmock is to use jmock's class like supperclass for yor test case. You don't need to extends your testcase but the code is not then so nice. Today I used Easy Mock it the first time. It is dessigned for TestNG and JUnit 4. It supports jdk 1.5. You can use static import if you don't want to write the classname in everytime. You can use code completion when you define the behavior of mocked code.

I describe my first unittest for code with JMI usage. Look at the code. The comments is bellow.

import java.util.\*;
import junit.framework.\*;
import org.netbeans.jmi.javamodel.Method;
import org.netbeans.junit.\*;
import static org.easymock.EasyMock.\*;

public class ElementTest extends NbTestCase {
 public void testParseUnknownTag() {
        Method m = createMock(Method.class);


        Element el = Element.findElement(m);
        Tag tag = el.getTag("myTag");
        assertTrue(m instanceof Method);

In the code is tested parsing javadoc tags from Method element. The test is separed into three parts. In the first part is created jmock object for method element. The method contains javadoc with value "/\*\*@myTag\*/" and no annotations. No more methods is used in tested code so it is not necessary to have more mocking methods. The second part activates the mock object by invoking replay(m) method. The code is tested in the last part.

By EasyMock you can also check count of calls. Let's show example. A method's call is expensive. To be sure that the method is called only one time you can write mock object for the class of method. In the test will be tested if the method is called only one time.

Friday Apr 28, 2006

Hibernate plugin in NetBeans 5.5

I've started to migrate features from hibernate plugin based on xdoclet to javax.persistence (EJB30) annotations. Working with annotations is simpler that working with xdoclet tags. It is necessary to run post processing tool for sources with xdoclet tags and to generate xml descriptors. The annotations are stored do classes. Therefore there is no need to generate xml meta data. The EJB30 is standard. It is another reason why to use them because you are not depenedend on only one implementation. You can simply move to other vendors.

I added templates for POJO with @Entity annotations. Screenshot with code completion is shown bellow.

The ui of customizer is the same like for xdoclet customizer. Only different code is generated. For example wizard for creating 1-N relation is shown bellow.

I plan also migrate the HQL wizard to EJB30 stuff. I may be add another hibernate features to NetBeans 5.5 ( Hibernate Validator and Hibernate Lucene Integration. I looked also on JBoss Seam. It is interesting technology. Has anyone experience with it?

Thursday Apr 27, 2006

Spellchecker for Javadoc plugin

Spellchecker module was added on update center this week. Author is Jan Lahoda. This module provides Spellchecker API. It underlines misspelled words in the editor, provides hints with possible fixes. Currently is implemented spellchecker for javadoc.

It will be useful if someone also implements spellchecker for HTML editor. Is there any volunteer?

Friday Apr 21, 2006

NBXdoclet met annotations

Nbxdoclet allows to change property for many xdoclet tags at once. It is described at this page. I've implemented this feature for annotations. The feature can be for example used in order to change groups value of TestNG Test annotation property. Look at the screenshot.

Three testcases (methods) are selected in the explorer. The @Test tag is shown in the XDoclet Tags window. Properties of @Test annotations are shown in the property sheet. The <Different Values> is shown for timeOut property because the property is defined only in method2. Change of any annotation's property is applied to all selected methods.

Friday Apr 14, 2006

The best documentation of API - unit test

Developers don't like writing of documentation. Many tutorials and documentations are obsolete. I saw wrong javadoc comments sometimes. I claim that the best documentation of API are the unit tests.

  • The unit test cannot be obsolete if is run daily.
  • The unit test is tutorial of the tested code.
  • It contains recommended design patterns.
  • The code will be more reliable if already tested patterns are used.

For example I was reading javadoc of Java Language Model API (JMI). From the javadoc I didn't get knowledge how to read, create and modify annotations. Thanks to Google I found some unit tests of JMI module which uses annotations. I can use a pattern shown bellow in my code and I'm sure that it is good usage.

 public void testAddAnnotation() throws, FileStateInvalidException {
        boolean fail = true;
        try {
            Method method = (Method) clazz.getContents().get(0);
            // log the annotation information
            getLog().println("Method '" + method.getName() + "' has following annotations:");
            for (Iterator it = method.getAnnotations().iterator(); it.hasNext(); ) {
                Annotation annotation = (Annotation);
                getLog().println("\\tAnnotation named '" + annotation.getType().getName() + "'.");
                for (Iterator it2 = annotation.getAttributeValues().iterator(); it2.hasNext(); ) {
                    AttributeValue attribute = (AttributeValue);
                    getLog().println("\\t\\tAttribute value '" + attribute.getName() + "', '" + ((MetadataElement) attribute.getValue()).getSourceText() + "'.");
            Annotation ann = createSimpleAnn("\\"2004-06-21\\"", "\\"Lojza\\"", "\\"Unknown\\"", "46");
            method = (Method) clazz.getContents().get(1);
            fail = false;
        finally {
        assertFile("File is not correctly generated.",
            Utility.getFile(getDataDir(), "org/netbeans/test/codegen/"),

I got a idea. Why not to add link to unit tests to javadoc html. TestNG uses annotations to specify tests groups. Why to not add annotations which package, class or method is tested. The link to test sources should be added to javadoc documentation. The test can be also indexed. Usage statistics can shown in javadoc. After that many developers, who don't write tests, will have to write tests I hope. Because the API users (developers) rather choose already tested API.

Thursday Apr 13, 2006

NBXdoclet cooperates with Hibernate Query Tool for NetBeans

Hibernate Query Tool (hts) is amazing tool with these feature:

  • Session factory management
  • Powerful HQL editor with syntax coloring, code completion, Java to HQL (and reverse) formatting features
  • Powerful query result browse
  • HQL 2 SQL translation
  • EXPLAIN PLAN for the Oracle9 dialect

Leon Chiver is author of hts.

It so cool tool that I had to integrate it with my hibernate plugin for NetBeans 5.0. My plugin provide creating POJO object, facades and helps to setup hibernate dialects. Leon's plugin helps to create and trace queries.

I added a action to Hibernate project extension.

The action opens hts session for active project with hibernate support.

The code completion is really great. This feature will be available on my update center next week.

Tuesday Apr 11, 2006

Project Extensions (natures) Wizard for NetBeans 5.0

Project Extensions plugin is implementation of project natures for NetBeans 5.0. Project natures extends functionality of NetBeans projects. Currently it consists of these features:

  • Adding new ant tasks to project's build script
  • Specification of ant tasks dependencies in project's build script, for example the ant task is executed before -post-compile task.
  • Extending the classpatpath of project.
  • Allow to either download libraries from remote library repository (for example maven1) or specify root of repository on local filesystem (for example hibernate installation).
  • A project extension is represented with a node in Project Extensions window.
  • Customizer for Project extension
I've already blogged about few Project Extensions ( Test NG, Java HelpSet validation, Emma code coverage). All these extensions were created with Project Extension Wizard.

To create new project extension is very simple with Project Extension Wizard. It will be demonstrated on creating RMI project extension for J2SE project. The Java RMI (Remote Method Invocation) system allows an object running in one Java Virtual Machine (VM) to invoke methods on an object running in another Java VM. It is necessary to generate stub and skeleton classes using rmic compiler to establish connection between client and server parts. The rmic compiler must to be run on compiled class of implementation of RMI object.

At first we create simple J2SE project with modified build script. We add two new ant's tasks to main build script (build.xml in root folder of project) :

  <target name  = "-post-compile" depends  = "rmi-post-compile"/> 
     <target name  = "rmi-post-compile" depends  = "-do-compile"> 
         <property name  = "remote.impl.filter" value  = "\*\*/\*RemoteImpl.class"/> 
         <rmic base  = "${build.classes.dir}" includes  = "${remote.impl.filter}"/> 

The -post-compile ant task is empty because the project extension wizard doesn't copy the standard project ant tasks to the project extension template. We can to create implementation of a RMI service and test it now. We expect that everything is alright.

Now it's time start to create a project extension using project extension wizard:

In the second Panel select the project with modified build script and clicked to Next button. After clicking to Next button Customize Project Extension Panel is shown.

The wizard automatically parses the modified build script and shows all new targets. The -post-compile is not added to table with available ant tasks because more project extensions cannot override the same target. Only dependencies between targets are stored to dependencies table.

If we assign to ant target display name the target will be represented by action in popup of project extension node in Project Extensions window. There is not necessary to change dependencies between targets. It will be usefull for example when dependencies between different Project extensions are defined. Click to next button you can specify classpath for the project extension. The libraries are got from Library manager.

RMI doesn't need extra library on the classpath. Click to Next button to show the last panel of wizard. There can be specified the name, display name, icon, post create action and customizer. The post create action is invoked after adding project extension to project. It is useful for example when it is necessary to initialize some properties. The customizer panel is shown in JDialog when is performed Properties action from project extension node.

After clicking to "Finish" button the Project extension template will be generated.

rmiext.xml - descriptor

     <featuredef  name =  "RMICompiler"customizerClassRef = "rmiext-RmiextCustomizer.instance"> 
                 <title>RMI Compiler</title> 
         <velocityTemplate vmFile = "Rmiext.vm"/> 

rmiext.vm - velocity template
<target name="rmi-post-compile">
  <property name="remote.impl.filter" value="\*\*/\*RemoteImpl.class"/>
  <rmic base="${build.classes.dir}" includes="${remote.impl.filter}"/>
And references are added to layer.xml
     <folder name = "velocity"> 
         <folder name = "build"> 
             <file name = "Rmiext.xml"url = "Rmiext.xml"/> 
         </folder > 
         <folder name = "customizers"> 
             <file name = "rmiext-RmiextCustomizer.instance"/> 
         </folder > 
         <folder name = "templates"> 
             <file name = "Rmiext.vm" url = "Rmiext.vm"/> 

The project extension was created. No line of of code was written. So we can try to run the module. And Test the nature. The Nature can be added to project from popup in Project Extensions window.

The project extension node is shown bellow.

The RMIC compile is not run either on project building or from RMI Project extension popup.

This wizard is available in NBXdoclet Modules Development plugin. The plugin available on my update center. The NBXdoclet Modules Development plugin depends on NetBeans Module Development Environment U1.

Monday Apr 10, 2006

NetBeans JavaHelp Set Wizard with validation

In NetBeans Develoment Support Update 1 was introduced JavaHelp Set Wizard with validation.

It generates java help and save a lot of time. To tell you truth I started to write help for my pluging with the JavaHelp Set wizard. I was too lazy to set up the java help files manually.

Modules in NetBeans CVS uses different harness than the harness from NetBeans 5.0. It contains ant target for validation help IDs. But what to do in not netbeans CVS modules? I am too lazy to check the IDs manually that I copied the ant tasks. I've created Java Help Set Project extension.

The NetBeans Harness Extension plugin is available on my update center .




« April 2014