Java Notes
Eclipse notes
Setup
- Import the Java code style formatter: code-style-formatter-v1.0.xml
- Import eclipse preferences: eclipse-preferences.epf
- html, htm, css file assoications
- Text Editors: tab width to 2, insert spaces for tabs, print margin, line numbers
- Sync cvs / svn after checking out
Connecting to a remote app for debugging
- Run > Debug Configurations > New
Connection Type Standard (socket Attach) Host localhost Port 8000
Java notes
Bytes in Java
int b = 0x80; System.out.println(b); // prints "255"
Bitwise and Bit Shift Operators
int bitmask = 0x000F; int val = 0x2222; System.out.println(val & bitmask); // prints "2"
C style String formatting
System.out.println( String.format( "Congratulations, %d list%s found:", arr.size(), arr.size() == 1 ? "" : "s" ) );
Simple Date Format
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" ); Calendar calendar = new GregorianCalendar(); String date = sdf.format( calendar.getTime() );
Date in the past
Calendar calendar = new GregorianCalendar(); calendar.add( Calendar.MONTH, -3 ); SimpleDateFormat sdf = new SimpleDateFormat( "dd/MM/yyyy" ); String date = sdf.format( calendar.getTime() );
The for loop with Generics - Java 6
for (String s : alist) { System.out.println(s); }
Log4J
- Logging levels:
TRACE DEBUG INFO WARN ERROR FATAL
Reference JMock
import org.jmock.Mock; import org.jmock.cglib.MockObjectTestCase; import org.jmock.core.Constraint; public class SomeClassTest extends MockObjectTestCase { public void testSomeMethodName() throws Exception { /* * Create objects for test. */ String username = "testUsername"; String password = "testPassword"; Map<String, Integer> securityPermissions = new HashMap<String, Integer>(); Agent agent = new Agent(); agent.setDatabaseSessionId( "23" ); agent.setInterventionId( "2343" ); /* * Create mocks for test. */ Mock mockedCsscLoginStoredProcedure = mock( CsscLoginStoredProcedure.class ); mockedCsscLoginStoredProcedure.expects( once() ).method( "login" ).with( eq( username ), eq( password ) ).will( returnValue( agent ) ); Mock mockedCsscSecurityPermissionDao = mock( CsscSecurityPermissionDao.class ); mockedCsscSecurityPermissionDao.expects( once() ).method( "findSecurityPermissions" ).with( eq( username ) ).will( returnValue( securityPermissions ) ); Mock mockedAgentDao = mock( AgentDao.class ); mockedAgentDao.expects( once() ).method( "getAgentDetails" ).with( eq( agent ) ).will( returnValue( agent ) ); /* * Set the mock objects in the service. */ service .setCsscLoginStoredProcedure( (CsscLoginStoredProcedure) mockedCsscLoginStoredProcedure .proxy() ); service .setCsscSecurityPermissionDao( (CsscSecurityPermissionDao) mockedCsscSecurityPermissionDao .proxy() ); service.setAgentDao( (AgentDao) mockedAgentDao.proxy() ); /* * Run it. */ Agent returnedAgent = service.contactCenterLogin( username, password ); /* * Test it. */ assertEquals( agent.getDatabaseSessionId(), returnedAgent .getDatabaseSessionId() ); assertEquals( agent.getInterventionId(), returnedAgent.getInterventionId() ); assertEquals( agent.getPasswordDaysLeft(), returnedAgent .getPasswordDaysLeft() ); assertEquals( agent.getSecurityPermissions(), returnedAgent .getSecurityPermissions() ); } }
JMock notes
request.expects( once() ).method( "getContextPath" ).will( returnValue( "TestContextPath" ) ); response.expects( once() ).method( "addCookie" ).with( isA( Cookie.class ) ); response.expects( once() ).method( "setLocale" ).with( eq( new Locale( TAPESTRY_LOCALE_COOKIE_NAME ) ) );
Reference Junit testing for thrown exception
try { methodThatShouldThrowSomeException(); fail( "Expected SomeException to be thrown" ); } catch( SomeException e ) { // Expected SomeException to be thrown. }
Loading properties
URL url= Thread.currentThread().getContextClassLoader().getResource("/WEB-INF/system.properites"); InputStream is = url.openStream();
InputStream is = getServletContext().getResourceAsStream("/WEB-INF/system.properites");
getServletContext().getResourceAsStream("Foo");
ServletContext.getRealPath();
Properties p = new Properties();
p.load( is );
Starting an application with a debug listener
- To start tomcat or resin with a debug port open, add the following arguments to the start:
-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
- In this case, connections from localhost to 8000 will be accepted.
- Port 8000 is the default port for Eclipse.
Setting up jmx localhost
- Start the java application with -Dcom.sun.management.jmxremote.
- Then start jconsole from the localhost.
Jetty configuration
Fixing the windows shortcut
- The default behavior of a windows folder short cut is to open a folder without the left "folders" tree and to only ever open one window (clicking the link just brings the open window to foreground.)
- Replace the target in the Properties window to:
%windir%\explorer.exe /n,/e,C:\some\folder