Wednesday, September 12, 2012

NetBeans 7.2, workaround for the predefined "Idea" keymap issue

   I use the NetBeans IDE for long years and I was very waiting for he new 7,2 version, unfortunately I have found a terrible critical issue for me in the IDE - a NullPointerException is being thrown when I am trying to change the keyboard map to the IDEA in the "Options->Keymap".
   It is very critical for me and I am sure that the feature is the most important for any IDE. The 7.2 version has been published already more than 2 months and I read that the bug has been fixed but the corrections will be published only in the future version. I tried to use the development version where the bug has been fixed, but the developer version contains a lot of other bugs and can't be used for development (for instance there are a lot of exceptions when you are trying to work in CSS ans JS editors).
  Fortunately I found some workaround for the keymap issue in NB 7.2:
1. Open Tools->Options->Keymap
2. Press "Manage profiles"
3. Choose the "Idea" in the list and press "Duplicate"
4. Select the new duplicated "Idea" profile
5. Restart the IDE
After the restart I have the IDEA key-map working on my machine without any exception.

Sunday, July 22, 2012

Visual testing of a triangle filling graphic method

Some time ago I was invited by a big company for a technical interview where I was asked by a test engineer - "how to write test for a method filling a triangle area?".. I didn't find the answer for the provided time (some inappropriate ideas like to use a neural network chased each other in my brain), but the problem was very interesting for me and an idea dawned me on the next day..
Idea: A Triangle is a very easy shape but it is very hard to distinguish the shape in an image by a computer, but a rectangle is much easier to be processed by a computer, thus we should just make a rectangle from two triangles and then we will check that the rectangle area is presented and artifacts-free, also we can check that there are not any points outside of the area..
I have written some Java code to check the idea. There is not a method in the java.awt.Graphics object to fill a triangle thus I have written such one:
public class TriangleFiller {
     * The method fills a triangle area and we check that the method fills a triangle
     * @param graphics the graphics context
     * @param xcoords an array contains the x coordinates for vertices (must have 3 positions)
     * @param ycoords an array contains the y coordinates for vertices (must have 3 positions)
    public static void fillTriangle(final Graphics graphics, int[] xcoords, int[] ycoords) {
        if (xcoords.length != 3 || ycoords.length != 3) {
            throw new IllegalArgumentException("Triangle must have 3 points");
        final Polygon polygon = new Polygon(xcoords, ycoords, 3);
        // we need use draw+fill because the fill operation fills the inside area

Then I wrote a unit test to make the "visual check" of the method just on an image and it works well:
package com.igormaznitsa.testtriangle;

import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import static org.junit.Assert.*;
import org.junit.Test;

public class TriangleFillerTest {
    private boolean checkTriangleCornerPoints(final BufferedImage baseRGBImage, final int pointColor, final int[] xVerticies, final int[] yVerticies) {
        for (int pointIndex = 0; pointIndex < 3; pointIndex++) {
            final int x = xVerticies[pointIndex];
            final int y = yVerticies[pointIndex];

            if ((baseRGBImage.getRGB(x, y) & 0xFFFFFF) != pointColor) {
                return false;
        return true;

    private boolean checkRectangleAreaHasBeenFilledOnly(final BufferedImage baseRGBImage, final int fillColor, final int backgroundColor, final int areaLeftX, final int areaTopY, final int areaWidth, final int areaHeight) {
        final int areaRightX = areaLeftX + areaWidth;
        final int areaBottomY = areaTopY + areaHeight;

        final int imagewidth = baseRGBImage.getWidth();
        final int imageheight = baseRGBImage.getHeight();

        for (int y = 0; y < imageheight; y++) {
            for (int x = 0; x < imagewidth; x++) {
                final int rgb = baseRGBImage.getRGB(x, y) & 0xFFFFFF;
                final boolean notInArea = x < areaLeftX || x > areaRightX || y < areaTopY || y > areaBottomY;
                if (notInArea) {
                    assertEquals("Must be background color "+backgroundColor, backgroundColor, rgb);
                } else {
                    assertEquals("Must be fill color " + fillColor, fillColor, rgb);
        return true;
    public void testFillTriangle_VisualTest() throws Exception {
        // the graphic log image file
        final File gfxLogFile = new File("./testimage.png");

        // create a RGB memory rendered image which will be our base for the test
        final BufferedImage baseTestImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
        final Graphics2D gfx = (Graphics2D) baseTestImage.getGraphics();

        // we must disable antialasing for the graphics to avoid artefacts
        ((Graphics2D) gfx).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

        final int backColor = 0x000000;
        final int shapeColor = 0xFFFFFF;

        // fill the image by our background color
        gfx.setColor(new Color(backColor));
        gfx.clearRect(0, 0, 200, 200);

        // set the shape color
        gfx.setColor(new Color(shapeColor));

        // our triangles, we use non-equaterial triangles, to make a rectangle, not a square
        final int[] firstTriangleX = new int[]{10, 10, 50};
        final int[] firstTriangleY = new int[]{10, 150, 150};
        final int[] secondTriangleX = new int[]{10, 50, 50};
        final int[] secondTriangleY = new int[]{10, 10, 150};

        // draw the first triangle
        TriangleFiller.fillTriangle(gfx, firstTriangleX, firstTriangleY);

        // save the current graphic state as an image (like log)
        ImageIO.write(baseTestImage, "png", gfxLogFile);

        assertTrue("The image must have set the points of vertices", checkTriangleCornerPoints(baseTestImage, shapeColor, firstTriangleX, firstTriangleY));
        // draw the second triangle, to get a filled rectangular area on the image
        TriangleFiller.fillTriangle(gfx, secondTriangleX, secondTriangleY);


        // save the current graphic state as an image (like log)
        ImageIO.write(baseTestImage, "png", gfxLogFile);

        assertTrue("Only the rectangle area must be filled, without artefacts",checkRectangleAreaHasBeenFilledOnly(baseTestImage, shapeColor, backColor, 10, 10, 40, 140));

Sunday, June 17, 2012

Java Performance mind map

On the last week I had visited our local Oracle branch (where smart guys develop very important parts of JVM) because there was very interesting presentation about Java Performance. I made some mind map based on the presentation and verbal information from speakers and translated it into English (because the original presentation was in Russian). You can see the Mind Map image through the link

I have used below information sources to make the mind map:
 - the JEEconf 2012 presentation of Aleksander Shipilev and Sergey Kuksenko
 - the Russian presentation  of Aleksander Shipilev and Sergey Kuksenko on JUG.RU (14 june 2012)
 - the high level of their mind map included into  the presentation (they used the map as the speech plan)   

also there are a lot of interesting stuff on the home page of Aleksander Shipilev ( but mainly that information in Russian.

Tuesday, January 24, 2012

A Mockup hard case

   Imagine that you need to test some piece of code which needs a 3th part developed class that is placed into a non-controllable library and you need to get some mockup of the class, of course you will use a mockup framework like Mockito and will not get any problem if the class is a usual "right" class but imagine that the class is not so usual one but it is a final one and furthermore it has a private constructor (because you have to create it through a special class factory method). In the case the Mockito framework will tell you "sorry, it is a final class, I can't make any mockup". 
   Fortunately there is very strong mockup framework called PowerMock and it allows make very interesting and helpful things in such hard cases. As instance, I needed to make a org.apache.bcel.generic.BranchHandle object for some test case but the class is both a final one and it has the private constructor.

public final class BranchHandle extends InstructionHandle {

private BranchInstruction bi;
private static BranchHandle bh_list;

private BranchHandle(BranchInstruction i) {
//compiled code
throw new RuntimeException("Compiled Code");

The class has the factory method
static final BranchHandle getBranchHandle(BranchInstruction i) {
//compiled code
throw new RuntimeException("Compiled Code");
but the factory method is not accessible for me because it has the package view modifier.
Fortunately there is a very useful class org.powermock.reflect.Whitebox within the PowerMock framework that allows me to solve the problem
final BranchInstruction instructionInstance = instruction.getConstructor(InstructionHandle.class).newInstance(mockTarget);
final BranchHandle branchHandle = Whitebox.invokeMethod(BranchHandle.class, "getBranchHandle", instructionInstance);
As you can see in the code snippet above, the Whitebox utility class allows you to call a non-visible method of a class. Also the utility class allows you to create instance of the class without any call the private constructor through its newInstance() static method

Sunday, January 8, 2012

Translation JVM bytecodes into Z80 machine codes

I had some free time during these holidays and have made some "spike" to check possibility to translate JVM byte-codes into Z80 commands and it works. You can see screenshot of a Java program and its translated version execution under the Fuse ZX-Spectrum emulator. The length of translated binary block is 376 bytes but mainly (80%) it contains some garbage imported from the class constant pool.