Java Notes

Eclipse notes

Setup

Connecting to a remote app for debugging

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

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

-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

Setting up jmx localhost

Jetty configuration

Fixing the windows shortcut


Browse Space

- Pages
- Blog
- Labels
- Attachments
- Bookmarks
- Mail
- Advanced

Explore Confluence

- Popular Labels
- Notation Guide

Your Account

Log In

 

Other Features

Add Content