Performance and Test
This guide will explain how to use the pre-installed software on the machines in the lab as well as describe how to install on your own computer. Please note that the pre-installed Eclipse version is slightly older than the one we recommend installing, and that the profiler (TPTP) doesnt work in the pre-installed version.
First specify Java 1.6(aka Mustang/6.0) as default, do this by choosing
Window->Preferences->Java->Compiler->Compiler Compliance Level: 6.0.
This setting is global for all Java projects.
Choose File->New->Project. Highlight Java Project and click next.
On the next screen name your project and check that it’s using Java 1.6. Click next and finish.
To create a new class, right click the project in the package explorer and choose New->Class.
Indicate the class name and package and click Finish.
Unless told to do otherwise Eclipse automatically compiles the project when changes are saved to files. It will also dynamically indicate problems with the code.
To set up a launch profile for the project, click the downward arrow next to the green ‘play’ icon and choose ‘Run…’. Double-click ‘Java Application’ to create a new launch profile. Eclipse should automatically have added your class if it has a main method. The Main Class option specifies which class’ Main method to launch. The Arguments tab allows you to provide command line arguments for your own program as well as the Java Virtual Machine.
Click run to test the launch profile. When you need to run again, you can click the ‘play’ icon to launch the last used profile, or click the downwards arrow to choose among profiles.
Right click your project in the package explorer and choose Properties->Java Build Path->Add Library, click JUnit and choose next. In the next screen you can choose the JUnit version. JUnit 4.x is backwards compatible so you might as well choose 4.x.
Another way to add JUnit is to let eclipse add it automatically. To do this right click your project select new -> JUnit Test Case. Then click "Click here to add JUnit 4 to the build path ..."
To create a TestCase class, right click the project and choose New-> JUnit Test Case. In the form presented choose to create a 3.8 JUnit test case. Fill out the rest of the form and click next to choose the methods to test or click finish if you want to do it manually. 3.8 JUnit test methods need to have a name that starts with ‘test’.
To run the unit test, proceed as to make a new launch profile, but this time choose JUnit instead of Java Application.
To create a TestCase class, right click the project and choose New-> JUnit Test Case. In the form presented choose to create a JUnit 4 test case. Fill out the rest of the form and click finish.
Remember to add an import statement for org.junit.* in addition to import static org.junit.Assert.*; The methods containing the tests must be marked with the annotation @Test to indicate they are JUnit test methods. Furthermore remember that @Before is used to annotate a method that should be run before every test, and @After to annotate a method that should be run after every test.
To run the unit test, proceed as to make a new launch profile, but this time choose JUnit instead of Java Application. Make sure JUnit 4 is selected and click run. Alternatively right click the test file in the project hierarchy and select Run As -> JUnit Test
To use jMock in a project download jmock-2.2.0-jars.zip and unpack to a place on your harddrive. Then add jmock-2.2.0.jar, hamcrest-core-1.1.jar and hamcrest-library-1.1.jar to the build path of your project. To do this right click your project and choose Properties->Java Build Path. Click add external Jar and locate the jars.
You can read here how to use jMock to create mock objects corresponding to an interface:
http://www.jmock.org/getting-started.html
If you need to mock a concrete class you need to add jmock-legacy-2.2.0.jar, cglib-nodep-2.1_3.jar, objenesis-1.0.jar to the build path as well.
Further instructions can be found here:
http://www.jmock.org/mocking-classes.html
When performing unit tests it is essential to know how much of the
code base is covered in the tests. Instructions can be found
at:
http://www.eclemma.org/userdoc/index.html
Log into a Unix command line at ITU (either directly from a Linux machine or by connecting to ssh.itu.dk using an SSH client and your Unix username and password).
Let <path> be the FULL path of your desired CVS repository (for example /import/home/username/cvs).
Execute the following from the command line:
cvs –d <path> init
For this guide we will assume a CVS repository located at ITU in the directory /import/home/username/cvs
Click Window->Show Perspective->Other..
Choose CVS Repository exploring.
Right click in the CVS repositories view and choose ‘New’. Enter the following:
Hostname: ssh.itu.dk
Repository path: /import/home/username/cvs
User: your linux username
Password: Your linux password
Connection: extssh
Note that these setting will also allow you to access the CVS repository from home.
If you wish to checkout a project from CVS, you can expand the repository you created, find the version you want, and right click it. If you just want the files directly, click checkout. If you want to create a project based on the content click ‘Check out as’.
To import a project into CVS, right click the project and choose Team->Share Project, you can then use an existing repository location or create a new one.
In general for performing CVS operations, right click the shared project and choose for example Commit or Update.
Feel free to use SVN in place of CVS. In order to use SVN from Eclipse you will need the Subclipse plugin. Follow this guide to install the plugin: http://subclipse.tigris.org/install.html.
You will also need to create a SVN repository. In order to do this, proceed according to 'Creating a CVS repository', and simply invoke the command 'svnadmin create <PATH>'. The setup in Eclipse for the repository is very similar to CVS, but in this case the URL for repository is 'svn+ssh://<username>@ssh.itu.dk/<PATH>'. If you created your repository in your home directory in a folder called svn, this translates into : 'svn+ssh://<username>@ssh.itu.dk/import/home/<username>/svn/' . NOTE: If you encounter problems with SVN please contact sysadm.
Finally, go to Window -> Preferences -> Team -> SVN, and make sure that 'SVN Interface' is set to 'SVNKit (Pure Java)'.
In your Java project, create a new text file and save it as build.xml. This will be your build file. Now click the down arrow next to the play icon with the red suitcase(henceforth Run External button) and click External Tools. Double-click Ant-Build to create new build launcher. Note the different available settings in the tabs, and then choose build.xml as your buildfile and choose an appropriate base directory (your project directory for example). When your buildfile has some targets defined, you can indicate which ones to run here. Now click close. You can setup multiple build launchers for different targets, just as you can create multiple launch profiles for a project.
NOTE: Some information on the profiler might be slightly out of date.
To use the Profiler, add a new profile by clicking the downright arrow next to the profile icon(a play icon with a clock by it). As when creating a run profile, double click Java-Application(or whatever you want to profile), notice the new tab called 'Monitor' and click it.
Now highlight Java Profiling and click edit options.
This will bring you to the Filter Set options. A profiler collects information about the memory and time consumed by objects and methods in the Java runtime. The filter allows you to decide which objects and methods you want information to be collected for. If you add an empty Filter Set, everything will be profiled and you will get profile information on parts of the Java runtime that you are probably not interested in such as Java security checks for loading class files etc. On the other hand, the default filter will for example exclude the instance count and memory usage of String objects. For now select the default filter set.
You can choose between different types of profiling, for example 'Memory Analysis' and 'Execution Time Analysis'. It would be a good idea to check the available options for each of these.
Try to run the profiler now in Execution Time analysis mode. You will be asked to change view, accept.
The ‘Execution Time Analysis’ view will for each profiled class list the time spent in that methods classes. The term 'Base time' is the time spent by the method EXCLUDING the time for any method calls it makes, while the term 'Cumulative time' refers to the total time spent by the method. Therefore the main method of a program will typically have a large cumulative time but near zero base time because it immediately calls other methods. The time analysis also gives the number of calls, which is helpful for analysing algorithms.
The profiler is a more complicated component than the ones previously described so you are encouraged to play with it a little bit to feel comfortable with it.
You might want to try the following.
1. Create a small program to performance testDownload Eclipse IDE for Java Developers from this page: http://www.eclipse.org/downloads/
Unzip it to a location of your choice.
Launch eclipse.exe. When prompted give a workspace location in your home directory.
Please follow the guide at : http://www.eclemma.org/installation.html
Auto-Import
If you use a class which you haven’t imported you can mark its name in the editor and hit Ctrl-Shift-M to let Eclipse perform the import automatically.
Line numbers
The default editor doesn’t show line numbers, you can change this in Window->Preferences->General->Appearance->Editors->Text Editors: Show Line numbers.
Re-factoring
Eclipse supports re-factoring of Java code. If you have a class, method, field or similar which you need to rename you simply highlight its definition/name, right-click and choose Refactor. This brings up for example rename. You can use this to rename the selected item, and all references to it in the code will be updated as well. If you forget to add getters and setters for a field, and later find that this was a bad idea, you can use the Encapsulate field to automatically generate getters and setters and redirect all use of the field to these.
Eclipse as CVS client
Eclipse can be used as CVS client for any kind of project, not only Java stuff. By using the 'Simple' project type you can easily use Eclipse for CVS and take advantage of its nice conflict resolution editor.
Links to guides etc
http://help.eclipse.org/help32/index.jsp
http://open.ncsu.edu/se/tutorials/
http://www.cs.umd.edu/class/fall2004/cmsc131/EclipseTutorial/