Can you guess the output? junit and initialization

Can you guess the output without running the code?

The relation to Eclipse is simply the fact that big part of eclipse.org is a great example of good test coverage built on top of jUnit. Plus, it was literally pulling my hair out in one of the eclipse-related testing-suites.

I stepped into this a few years ago while bug-fighting a test class that had unexpected initialization.

Pretty sure that many of you know the answer but definitely fun outcome!

As everyone can simply run this snippet yourself I won’t delay approving comments, I’ll just accept them whenever I get a free moment. This comment system here has captcha but additionally all comments must be approved manually.

import junit.framework.TestCase;
public class MyTest extends TestCase {
private static int count = 0;

{ count++; }

public MyTest() {count++;}

public void test1() { System.out.print(count); }
public void test2() { System.out.print(count); }
public void test3() { System.out.print(count); }

}

I’m sure some of us don’t always take this behavior into account :)

After figuring this out, SPECIAL fun is a bit modified case:

import junit.framework.TestCase;
public class MyTest extends TestCase {
private static int count = 0;

{ count=count*2; }

public MyTest() { count++; }

public void test1() { System.out.print(count); }
public void test2() { System.out.print(count); }
public void test3() { System.out.print(count); }
}

For this last snippet I think without running it you won’t figure it out :) At least I didn’t..

UPDATE: Decent in-depth hi-tech doc about java init http://www.artima.com/designtechniques/initialization7.html

UPDATE2: Correct answers were 666 and 777.

Ericsson comes out with the Android phone – mid-2009

According to http://www.ericsson.com/ericsson/press/releases/20081209-1276213.shtml Ericsson joined Open Handset Alliance. Yei!

This means Ericsson ongoing support for open source and more importantly Google-backed Android platform.

Ericsson will be offering Android-based phones mid-2009. Excellent news! I’m glad to see strong phone manufacturer joining the Alliance. As I understand, currently only Nokia and RIM (blackberrry) from the “big players list” are missing from the alliance.

To come up with some speculations — Ericsson might try to base new phone on X1/Experia that runs with Windows Mobile and first reviews claim it to have slow user interface.

Java Quiz of The Day – same private field instance for two classes

What is the smallest change to main method that makes it sysout “true”?

You are allowed to change ONLY the main method! Of course changing sysout line is out of question.

You can leave your answer in comments and I’ll publish/approve them together with the solution.

[java]
public class InstanceDemo {

public static void main(String[] args){
MyClass cl1 = new MyClass();
MyClass cl2 = new MyClass();
System.out.println(cl1.ocl==cl2.ocl && cl1!=cl2);
}

private static class MyClass extends java.util.ArrayList {
private final Object ocl = new Object();
}

}
[/java]

I’ll post the correct answer in 24h :)

UPDATE: Was not that hard afterall! :) ~45 correct answers.

Yes, clone() was the answer I was looking for:
[java]MyClass cl2 = (MyClass) cl1.clone();[/java]

Ed Merks surprised with a nice solution:
[java]
//Add this line as the first line of main.
class MyClass extends InstanceDemo.MyClass {
Object ocl = null;
}
[/java]

And a lot of people took the hard-core way:
[java]
Field field = cl1.getClass().getDeclaredField(“ocl”);
field.setAccessible(true);
field.set(cl2,cl1.ocl);
[/java] :)

P2 fun: Failed to prepare partial IU

I got this while trying to install new version of subclipse on top of eclipse rcp edition of ganymede release. Running eclipse with -clean didn’t help.

[code]
!ENTRY org.eclipse.equinox.p2.engine 4 4 2008-11-18 13:17:31.218
!MESSAGE An error occurred during provisioning.
!SUBENTRY 1 org.eclipse.equinox.p2.touchpoint.eclipse 4 0 2008-11-18 13:17:31.218
!MESSAGE Failed to prepare partial IU: [R]org.tigris.subversion.clientadapter.javahl 1.5.4.
[/code]

Then I thought – what the heck, not going to reinstall – and tried to update whole eclipse with P2. Worked nicely, all updated.

After that also installing full subclipse worked again, at least without errors.

BUT just installation. It didn’t actually work — it shows up as installed plugin in P2 manager — but not visible in Help->About->Plugin Details. Workspace .log is clean.

So, new eclipse re-install, here I come :(

This all probably started because I first tried to upgrade from older subclipse to 1.4.6 using p2 update manager. Usually I’ve been just reinstalling manually.

And I’m not even sure if it’s a P2 or subclipse update site issue. Some P2 internal cache trees maybe got screwed that -clean couldn’t reach.

SimpleDateFormat is not thread-safe

How many of you use java.text.SimpleDateFormat as static field?

I have seen this to be pretty standard practice (and have done this myself too).

Be aware that SimpleDateFormat#format(..) is not thread-safe and thereby for most of the cases you should not use this as a static field in server, Eclipse RPC or any other multi-threaded environment.

SimpleDateFormat is internally using field “calendar” that is set with each #format(…) method call so that pretty much breaks the thread-safety.

Google mentor summit is on!

Greetings from Mountain View, Google HQ in California!

Summit is on and sessions scheduled. There is a lot of fun going on and I’ll try to get some of it here, stay tuned!

I’m here representing Eclipse Foundation as one of the organizations participating in Google Summer of Code.
Eclipse had a lot of strong summer of code projects this year and 20 out of 22 projects succeeded, have a look at http://wiki.eclipse.org/Google_Summer_of_Code_2008 for more information.

In addition to me there are Markus Alexander Kuppe and Steffen Pingel from Eclipse and there has already been quite a lot of blending with orgs like Blender, K3D, opensuse etc :)

best regards,
Ahti

Turning off win xp master volume beep sound

Aren’t you annoyed by the beep that is so loud?

I got pretty frustrated after trying to look for disablement under volume control preferences. Nothing there. Also turning off all sound notifications at control panel (including Exclamation sound) didn’t help.

Still nothing, just one aloud beep.

Windows Master Volume Control

Windows XP Volume Control

 

Here’s the quickest way I found to disable it:

  1. Go to My Computer
  2. rightclick for Properties
  3. tab Hardware
  4. button Device Manager
  5. menu View
  6. set checkbox Show Hidden Devices
  7. navigate to Non-Plug and Play drivers
  8. open Beep
  9. tab Drivers
  10. button Stop
  11. combo Disabled
  12. button OK
  13. Congratulations!

 

Windows XP Volume Control

Disabling beep driver forever

 

If you find a better way, please let me know :)

Usability of the week: MS Project 2007 search dialog

How to search for all tasks in MS Project file?

You can’t. By default MS Project is searching for only expanded tasks. So it is obvious to expect search dialog to have checkbox for all-nodes search.

But looks like there is no such checkbox so you always have to expand.

Instead of the checkbox the search dialog has pretty much only two combos: “Test:” and “Field:” (“Search:” has just “Up” and “Down”) :

MS Project search dialog

Maybe it’s just me but they look funny and probably used only for about 0.1% of the searches people do? :)

PS! Should we be worried when project file size suddenly drops from 1MB to 400KB but all information is still there?

Refreshing eclipse workspace with ant script

Did you knew that if you have ant build.xml in eclipse Ant view then you can refresh your eclipse workspace simply using:

<eclipse.convertPath filesystempath=”${basedir}” property=”resource.basedir”/>
<eclipse.refreshLocal resource=”${resource.basedir}” depth=”one”/>

?

Very handy for build scripts to make sure eclipse resources are in sync after the build.

Thanks to Roland for the tip!

Getting unsigned bytes in java

Welcome back after long overwork and vacation period :) Let’s start small, it’s still almost summer.

Bytes in java are always signed and are quite horrible to use because of the casting and weird side-effects (or I’m just stupid enough to call it weird).

But we need unsigned bytes quite a lot. Most of the time for reading various streams (networks, 8bit strings etc) some text stream you still end up with byte streams.

There is a nice trick converting them from signed to unsigned in case you want to get normal 0..255 range for the chars.

byte mybyte = -104;
long mynewint = mybyte & 0xFF;

Hope it helps!

I had quite a nice puzzling while trying to find some easier way. Is there?