Posts

Accessing Resources in Java, use of getResource method of Class and ClassLoader, How to access test data in unit tests

Given that: TestABC.java is located at: test/x/y/z/TestABC.java testdata.txt is located at: resources/x/y/z/TestABC/testdata.txt classpath includes resources folder We are trying to read the testdata.txt from TestABC.java. We need a location independent way to point to these files. Therefore, we would like to use getResource() method of a Class or ClassLoader. Here are the correct ways: 1: this.getClass().getClassLoader().getResource("x/y/z/TestABC/testdata.txt"); 2: this.getClass().getResource("/x/y/z/TestABC/testdata.txt")); 3: this.getClass().getResource("TestABC/testdata.txt")); Explanation: 1: works because ClassLoader takes the path as it is (no manipulation) and search in the classpath folders. In this case, it will go to the resources folder and it will find the testdata.txt file in the given path. 2: works because Class.getResource takes the "absolute" path, removes its starting / and t

Affect of Implicity vs. Explicit Exception Handling to the Code Quality

In this post I will try to compare 3 different implementations of the same method which are equivalent with respect to the set of unit tests they are expected to pass. The comparison is mainly based on the way exceptions are handled in each and I am comparing them with respect to how they impact code quality. Here is the method we are expected to implement: /** * Should push given element to stack with given stack no. * Should throw a RuntimeException if the capacity is full. **/ public void push( int stackNo, T element ) { ... } A part of the unit test that is written for this method is as follows: // Available capacity at this point is 2: ms.push( 10, e1); ms.push( 10, e1); //becomes full at this point. try { ms.push( 10, e1); assertTrue( false ); } catch( RuntimeException e ) { assertTrue( true ); } Here are 3 alternative implementations that pass the tests: 1) public void push( int stackNo, T element ) { elements[num_elements] = (StackRecord) new StackRecord(); elements[num_ele

Eclipse shortcuts

http://rayfd.wordpress.com/2007/05/20/10-eclipse-navigation-shortcuts-every-java-programmer-should-know/

TCPView

TCPView (for Windows): a nice tool to show the tcp and udp endpoints in your system. http://technet.microsoft.com/en-us/sysinternals/bb897437.aspx

from actual parameter of type array to formal parameter of list in Ruby

I was curious what * (star) means in front of a method parameter. Here is the result: def xyz(*arr) arr.each do |a| puts a.class end end $xyz 1,2,3 => Fixnum, Fixnum, Fixnum $xyz [1,2,3] => Array $xyz *[1,2,3] => Fixnum, Fixnum, Fixnum Therefore, for example, attribute_names = 'name', 'age', 'phone_number' #creates an array attr_accessor *attribute_names is equivalent to attr_accessor 'name', 'age', 'phone_number'

print what you like

The following web site allows you to select part of a page, or edit a page to help you print in a way you like: http://www.printwhatyoulike.com

Comparison of equality operators in Ruby: == vs. equal? vs. eql? vs. ===

Here is a short explanation of when these predicates are true (From http://www.wellho.net/mouth/985_Equality-in-Ruby-eql-and-equal-.html) The == comparison checks whether two values are equal eql? checks if two values are equal and of the same type equal? checks if two things are one and the same object. How do I remember which is which ... The longer the operator, the more restrictive the test it performs Let's understand the differences with some examples. Each "abc" below is a different object. Therefore, equal? is false. irb(main):065:0> "abc".object_id => 24173330 irb(main):066:0> "abc".object_id => 24165760 irb(main):067:0> "abc" == "abc" => true irb(main):068:0> "abc".eql? "abc" => true irb(main):069:0> "abc".equal? "abc" => false As you might expect, there is a single instance of 1. Therefore, all comparisons are true. irb(main):094:0> 1.class =>