0% found this document useful (0 votes)
3 views10 pages

Java

Uploaded by

anilyadavnpl
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Java

Uploaded by

anilyadavnpl
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical Java Tips

Maximilian Berger
Copyright © 2009 Maximilian Berger, University of Innsbruck

This document or part of this document may only be re-published with the written consent of
the copyright holder. Source code examples may be re-used under the following conditions:

• The resulting code must be published under an OSI [[Link] approved


open-source license.

• The source code must refer to this document and contain my name (Max Berger).

• If the project contains a contributor or thanks list, it must mention my name (Max Berger)
in the same style as it would mention someone contributing a patch.

Table of Contents
Project Management ...................................................................................................... 1
Maven ................................................................................................................. 1
Regression Tests ........................................................................................................... 3
TestNG ............................................................................................................... 3
Other Test Frameworks .......................................................................................... 4
IDEs ........................................................................................................................... 4
Eclipse ................................................................................................................ 4
Netbeans ............................................................................................................. 5
IntelliJ ................................................................................................................. 5
Version Management ..................................................................................................... 5
Mercurial ............................................................................................................. 6
Style ........................................................................................................................... 7
Checkstyle ........................................................................................................... 7
PMD ................................................................................................................... 8
Findbugs ............................................................................................................. 9
NCSS ................................................................................................................. 9
Logging ..................................................................................................................... 10
log4j ................................................................................................................. 10
commons-logging ................................................................................................ 10
[Link] ........................................................................................................ 10

Project Management
Maven
• Download from [Link]

• Complete Project Management Tool

• Describe WHAT rather than HOW

• Convention over configuration

• Maven: The definite guide (English) [[Link]

• Maven: The definite guide (German) [[Link]


[Link]]

1
Practical Java Tips

Important Conventions

• Project is described in [Link]

• Java source is in src/main/java

• Java tests are in src/test/java

• Webpage is in src/site

• Artifacts go to target/

Example 1. Example [Link]


<?xml version="1.0"?>
<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>sample</artifactId>
<name>Sample Project</name>
<version>0.1.0-SNAPSHOT</version>
<inceptionYear>2009</inceptionYear>
<licenses>
<license>
<name>GNU General Public License, Version 3.0</name>
<url>[Link]
<distribution>manual</distribution>
</license>
</licenses>
<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<debug>false</debug>
<optimize>true</optimize>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>testng</artifactId>
<version>5.8</version>
<type>jar</type>
<classifier>jdk15</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>

2
Practical Java Tips

<version>1.1.1</version>
</dependency>
</dependencies>
</project>

Important Maven commands

mvn test Compile and run tests

mvn package Compile, run tests, and package

mvn install Compile, run tests, package, and install locally

mvn site Run reports and generate website

For a list of available plugins use [Link] .

Other items that can be configured are

• description

• developers

• contributors

• scm

Regression Tests
TestNG
• Successor of JUnit

• Same Idea

• Easier to use

• TestNG Homepage [[Link]

• Eclipse plugin available

What you need to do:

• Write a normal class

• Test functions throw Exception

• Test functions are annotated with @Test

• Assert class for basic tests

• [Link]()

Maven Use:

• Put class in src/test/java

• Load TestNG plugin in [Link] (see Example 1, “Example [Link]”)

Example 2. Example TestNG Tests


import [Link];
import [Link];

3
Practical Java Tips

/**
* Tests for {@link Blabla}.
*/
public class VirtualFileTest {

/**
* Tests the add function.
*
* @throws Exception
* if the test fails.
*/
@Test
public void addTest() throws Exception {
Blabla b = new Blabla(4);
[Link](5);
[Link]([Link](), 9);
}

@Test
public void divideTest() throws Exception {
Blabla b = new Blabla(4);
try {
[Link](0);
[Link]();
catch (IllegalArgumentException e) {
// Good!
}
}
}

Other Test Frameworks


The following framework may help in GUI testing:

• FEST Swing [[Link]

IDEs
VI(M) and Emacs (Kate / gedit ) are only good for viewing and small edits!

Use a modern, IDE and use its features!

Since we use Maven for project management we can switch IDEs at any time.

It is important to always use the IDE which is fit for a particular task!

Eclipse
• Get from Eclipse Homepage [[Link]

• Current: 3.5 SR 1

• Summer: 3.6 or 4.0

You can import my [Link] [[Link]


[Link]] file into the eclipse update sites to get access to some frequently used plugins
(Go to Help/Software Updates / Manage Sites / Import).

4
Practical Java Tips

Some important plugins:

• Eclipse Maven plugin [[Link]

Important settings:

• Auto Refresh

• Auto Update

• Font: Use a font that clearly distinguished Zero from Oh (0,O), one from el (1,l). Good example:
Consolas (Win Vista), Andale Mono (Win XP/2k), DejaVu Sans Mono (Unix).

Important Features:

• Code Cleanup

• Format

• Organize imports

All theses should be configured

• Per Project

• Automatically on save!

Additional plugins which may be of interest:

• Jigloo [[Link] GUI Builder

Netbeans
• Get from NetBeans homepage [[Link]

• Current: 6.7.1

• Summer: 6.8 or 7.0

Important Features

• GUI Builder

• Profiler

• Built-in Maven support (since 6.7)

IntelliJ
• Built-in support for Maven

• Released as open source 16. Oct!

• [Link]

• Very good code inspection and refactoring features

• No experience yet

Version Management
History:

5
Practical Java Tips

Shared File System RCS

Single Repository CVS, Subversion

Multiple Repositories Mercurial, Git, Bazaar

Actually in use:

Subversion Legacy projects

GIT Unix Projects

Mercurial Java / Python projects DV

Mercurial
Common to all DVCS:

• Distributed Version System

• Every client has a full copy of the repository

• Efficient Micro-Checkins

• Can push / pull from any other client copy

• Allow efficient branching

Important commands

hg init create repository

hg clone clone existing repository. Automatically makes this the default repository.

hg push push changes to default remote repository

hg pull pull changes from default remote repository (warning: does NOT update. Use hg pull
-u )

hg commit Check changes into local repository

hg update Update files in local repository

hg heads shows if there are different head revisions

hg merge merges multiple head revisions

Can be configured in .hg/hgrc

Supports Plugins (here: Windows / Unix LF Conversion, Keyword expansion)

Example 3. Example hgrc


[paths]

[extensions]
[Link]=
hgext.win32text=

[keyword]
**.java =

6
Practical Java Tips

[encode]
** = cleverencode:

Ignore files can be configured using .hgignore in your root directory.

• Files will not be added to the repository

• Will not show up as changed

• Normally: All generated directories

• Maven: target directory

Example 4. Example .hgignore


syntax: regexp

target/
^test
^.*patch$

Links:

• Mercurial Homepage [[Link]

• Distributed revision control with Mercurial [[Link] (online book)

Style
• Define Code Conventions (Naming, spaces vs. tabs, etc.)

• Re-use existing conventions!

• Follow them!

• Use tools to check them

Maven use:

• Put in <reporting> section

Checkstyle
• Checkstyle Homepage [[Link]

• Checkstyle Eclipse plugin [[Link]

• SQE NetBeans plugin [[Link] (Use version from Kenai for 6.7 Support!)

• Checkstyle Beans [[Link] (may work better than SQE)

• Also provides some Code quality checks

Example 5. Example Checkstyle Maven Configuration


<plugin>
<groupId>[Link]</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.3</version>
<configuration>

7
Practical Java Tips

<configLocation>[Link]</configLocation>
</configuration>
</plugin>

An example [Link] can be found at: [Link]


resources/wmsx/[Link]

Checkstyle 4 vs 5
• The checkstyle maven plugin until version 2.3 is based on checkstyle 4.

• Current eclipse checkstyle plugins is based on checkstyle 5

• both are incompatible (slight changes in [Link])

Fix: Use newer checkstyle plugin. Problem: Not officially available yet! Must set plugin location!

Example 6. Example Checkstyle 5 Maven Configuration


...reporting...plugins...
<plugin>
<groupId>[Link]</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.5-SNAPSHOT</version>
<configuration>
<configLocation>[Link]</configLocation>
</configuration>
</plugin>
...
<pluginRepositories>
<pluginRepository>
<id>apache-snapshots</id>
<name>Apache Snapshot repository</name>
<url>[Link]
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

PMD
Common Programming checks for code style.

Yes, all the standard rules make sense!

Plugins available for Eclipse, Netbeans, Maven

• PMD Homepage [[Link]

• PMD Eclipse plugin [[Link]

• SQE NetBeans plugin [[Link] (Use version from Kenai for 6.7 Support!)

Example 7. PMD Configuration for Maven


<plugin>
<groupId>[Link]</groupId>
<artifactId>maven-pmd-plugin</artifactId>

8
Practical Java Tips

<version>2.4</version>
<configuration>
<linkXref>true</linkXref>
<targetJdk>1.5</targetJdk>
<sourceEncoding>utf-8</sourceEncoding>
<rulesets>
<ruleset>/rulesets/[Link]</ruleset>
<ruleset>/rulesets/[Link]</ruleset>
<ruleset>/rulesets/[Link]</ruleset>
<ruleset>/rulesets/[Link]</ruleset>
<ruleset>/rulesets/[Link]</ruleset>
<ruleset>/rulesets/[Link]</ruleset>
<ruleset>/rulesets/[Link]</ruleset>
<ruleset>/rulesets/[Link]</ruleset>
<ruleset>/rulesets/migrating_to_15.xml</ruleset>
<ruleset>/rulesets/[Link]</ruleset>
<ruleset>/rulesets/[Link]</ruleset>
<ruleset>/rulesets/[Link]</ruleset>
</rulesets>
</configuration>
</plugin>

Findbugs
• Another Software quality tool

• Yes, the rules also make sense!

• Findbugs Homepage [[Link]

• Findbugs Eclipse plugin [[Link]

• SQE NetBeans plugin [[Link] (Use version from Kenai for 6.7 Support!)

Example 8. Findbugs Maven configuration


<plugin>
<groupId>[Link]</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<threshold>Low</threshold>
<effort>Max</effort>
<omitVisitors>FindDeadLocalStores</omitVisitors>
</configuration>
</plugin>

NCSS
• Non Commented Source Statements

• Simplified: The number of ;

• Better comparable than LOC (lines of code)

Example 9. NCSS Maven configuration


<reporting>

9
Practical Java Tips

<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>javancss-maven-plugin</artifactId>
<version>2.0-beta-2</version>
</plugin>
</plugins>
</reporting>

Logging
• Do Not use [Link] for logging

• Use a logging framework

How:

• acquire logger: private static final LOG = [Link]([Link]);

• [Link]("bla");

• [Link]("bla");

• logger can be configured for different log levels, and separately for each package / subpackage /
class

log4j
• Commonly used

• Only use in new projects if you have previous experience with it!

commons-logging
• apache project

• auto-detects logger from classpath

• use in libraries

[Link]
• built-in since Java 1.4

• Use in all user tools / gui projects

10

You might also like