Pages

Wednesday, October 24, 2012

Refactoring Eclipse refactoring programmatically

Stack Overflow question : Is there any eclipse refactoring API that I can call programmatically?

http://stackoverflow.com/questions/9129689/is-there-any-eclipse-refactoring-api-that-i-can-call-programmatically

How to execute inline refactoring programmatically using JDT/LTK?

http://stackoverflow.com/questions/12898718/how-to-execute-inline-refactoring-programmatically-using-jdt-ltk

From MacJournal

Map in python - and it's application to ini file reader

Mapping Types is a container that maps from an element to the other element. In python, map is implement in dict.

Making a map(dirctionary) in python

Dictionary is represented as follows:
e = {"one": 1, "two": 2}
The two items are mapped with ":" charactore, and each mapping is separated by a comman. Python builds dict object with '{' and '}' character.

You can use dict() method to build one.
class dict(**kwarg)
You may not familiar with ** notation. This is a simple example used in python method.
def hello(**x):
    print x

hello(x=1,y=2)
When you execute this code, you will get a dictionary object.
{'y': 2, 'x': 1}
When python method sees **something, all the assignments (x = 1, y = 2 in this example) given as a parameter is wrapped inside an automatically generated dictionary, and parameter variable x is pointing to the dictionary. In short, you can think of **something as collect (*) and make dictionary (*), and name it something.

As you see the meaning of **something as a parameter to the function. You'll see that this code will make the same dictionary as before.
a = dict(one=1, two=2)
You have more options to make the dictionary.
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
For the first case, when first item is a mapping (other dictionary), it will build a new dictionary with the contents of it together with the additional parameters.
b = dict({'one': 1, 'two': 2})
For the next case, the first parameter can be a list with lists that has two elements.
c = dict(zip(('one', 'two'), (1, 2)))
d = dict([['two', 2], ['one', 1]])
zip method has two parameters, and iterate over the elements in each of the two elements to zip a list of list that contains two elements from each parameters.

Of course, you can add additional assignments so that they are added to the newly generated dictionary.

Using dictionary

For getting all the items in a dictionary, you need to use items().
d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}
print d
print d.items()
{'orange': 2, 'pear': 1, 'banana': 3, 'apple': 4}
[('orange', 2), ('pear', 1), ('banana', 3), ('apple', 4)]

How to make cloud form of labels in blogpost

As the number of blogs grow, I need to organize and find the posting based on labels.

This post explains how to do that.

In short:
  1. click the Layout link
  2. Select where to put the label cloud and select label :
  3. Then select options :

Unit testing - Java JUnit 3

Download and build JUnit

Download the source from git, and execute ant.

The result jar file is compiled into junit4.11-SNAPSHOT directory.

Simple example

Class under test

public class Hello
{
    int add(int x, int y)
    {
        return (x + y);
    }
    
    int sub(int x, int y)
    {
        return (x - y);
    }
}

Test class

import junit.framework.*;
import static org.junit.Assert.assertEquals;

public class HelloTest extends TestCase {
  private Hello tester = null;
  protected void setUp() {
       tester = new Hello();
  }

  public void testAdd() {
    assertEquals("Result1", 15, tester.add(10, 5));
  }
}
TestCase comes from junit.framework.*, And you need to have '.' for classpath in order to find the class under the test. You need to copy the junit.jar in the classpath also.

Compile and execute

The junit.textui.TestRunner is called with the test class (HelloTest) parameter.
javac Hello.java
javac -cp .:junit-4.11-SNAPSHOT.jar HelloTest.java
java -cp .:junit-4.11-SNAPSHOT.jar junit.textui.TestRunner HelloTest
.
Time: 0.001

OK (1 test)

setUp() added

You can add setup() and tearDown() method.

import junit.framework.*;
import static org.junit.Assert.assertEquals;

public class Hello2Test extends TestCase {
  private Hello tester = null;
  protected void setUp() {
       super.setup();
       tester = new Hello();
  }

  public void testSub2() {
    assertEquals("Result1", 15, tester.sub(20, 5));
  }
  
  public void testSub() {
    assertEquals("Result1", 15, tester.sub(20, 5));
  }
}

Or, you can use @Override
  @Override
  protected void setUp() {
       tester = new Hello();
  }

Using suite()

You can make suite(), and add test methods to the suite.
import junit.framework.*;
import static org.junit.Assert.assertEquals;

public class Hello2Test extends TestCase {
  private Hello tester = null;
  
  public Hello2Test(String str)
  {
      super(str);
  }
  
  @Override
  protected void setUp() {
    tester = new Hello();
  }

  public void testAdd() {
    assertEquals("Result1", 15, tester.add(10, 5));
  }
  
  public void testAdd2() {
    assertEquals("Result2", 25, tester.add(10, 15));
  }
  
  public static Test suite() {
      TestSuite suite= new TestSuite();
      suite.addTest(
          new Hello2Test("testAdd") {
               protected void runTest() { testAdd(); }
          }
      );

      suite.addTest(
          new Hello2Test("testAdd2") {
               protected void runTest() { testAdd2(); }
          }
      );
      return suite;
  }
  
  public static void main(String[] args) {
      junit.textui.TestRunner.run(suite());
  }
}

You can use dynamic way to put all the tests in the suite using one line of code with Java Reflection working behind the scene.
  public static Test suite() {
      // method 1
      return new TestSuite(HelloTest.class);
  }
You can just execute the test like normal java code
javac Hello.java
javac -cp .:junit-4.11-SNAPSHOT.jar HelloTest.java
java -cp .:junit-4.11-SNAPSHOT.jar HelloTest

Merging tests with suite()

You can collect tests from TestCases to make test suite() to run.
import junit.framework.*;

public class HelloTestSuite
{
    public static Test suite() { 
        TestSuite suite= new TestSuite(); 
        suite.addTest(new HelloTest("testAdd")); 
        suite.addTest(new Hello2Test("testAdd")); 
        suite.addTest(new Hello2Test("testAdd2")); 
        return suite;
    }
    
    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }
}
One can even concatenate the test cases into suite.
public class HelloTestSuite
{
    public static Test suite() { 
        TestSuite suite= new TestSuite(); 
        suite.addTestSuite(HelloTest.class); 
        suite.addTestSuite(Hello2Test.class); 
        return suite;
    }
    
    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }
}

Reference

  1. vogella.com on JUnit
  2. JUnit Testing Utility Tutorial
  3. JUnit 4.0 Example

Using LaTeX in blogspot

First method

I googled this site helps to make LaTeX working in blogspot. I have prosseek/files, for file storing purposes (even though it allows me keep only 1GB). Just right click in google/sites, and copy the file using HREF allows me to download the file from the site like this.
The code is as follows:
$latex \displaystyle S(n)=\sum_{k=1}^{n}{\frac{1}{T_{k}}=\sum_{k=1}^{n}{\frac{6}{k(k+1)(k+2)}$
This is the result: $latex \displaystyle S(n)=\sum_{k=1}^{n}{\frac{1}{T_{k}}=\sum_{k=1}^{n}{\frac{6}{k(k+1)(k+2)}$
You can refer to this post for embedding HTML/javascript in your blog.

MathJax

I googled this post to use MathJax. You need to add the code in template, which add jQuery and MathJax.



The code is as follows:


And this is the result:

Warning

As is written in this post, there is a bug that the result is not shown in Chrome.

References

  1. Embedding math with replacemath
  2. How to show the string inside a tag verbatim?

Easier way to embed java script/HTML in blogpost

When using javascript in your blogspot, I just copied the javascript and HTML code in the editor, but using template, one can share the java script and HTML code for all of the blogs that I make. The first step is visit blogger.com, and choose your blogspot. Open layout.
Then, open "Add a Gadget", and select HTML/Javascript.
When you need to change some of the template, you also can edit the template HMTL code.
Then add your code in there. I have this for my code.

Showing LaTeX code in blogger

Refer to this post.


Syntax highlighter setup

Refer to this post.







pretty printer

Refer to Code highlighting on blogger by LUKA MARINKO. You also need to modify HTML template as explained before.


You can edit the code anytime you want.