07 November 2012

Disable Logging For unit Test

Step 1: Create a class that will filter certain log messages. This will be your Logger's custom filter. In the example below, it will iterate through all the mFilterMessages and if the error message matches, starts with or ends with one of the strings stored in the mFilterMessages, it will not create a log entry.

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Filter;
import java.util.logging.LogRecord;

public class CustomLogFilter implements Filter {
    private List mFilterMessages;

    public CustomLogFilter(){
        super();
        mFilterMessages = new ArrayList();
    }

    public CustomLogFilter(List filterMessages){
        mFilterMessages = new ArrayList();
        mFilterMessages.addAll(filterMessages);
    }

    @Override
    public boolean isLoggable(LogRecord lr){
        String msg = lr.getMessage();
        if (mFilterMessages.contains(msg))
        {
            return false;
        }
        else
        {
            for (String filterMessage : mFilterMessages)
            {
                if (msg.startsWith(filterMessage))
                {
                    return false;
                }
                else if (msg.endsWith(filterMessage))
                {
                    return false;
                }
                else if (msg.contains(filterMessage))
                {
                    return false;
                }
            }
        }
        return true;
    }
}

Step 2: Create some sort of util class that will easily set/reset your custom filter.

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Filter;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import org.apache.commons.logging.LogFactory;

public class CustomLogDisabler{
    private Logger mLogger;
    private static boolean mIsActive;
    private static Filter mOldLogFilter;
    private List mFilterMessages = new ArrayList();

    public static CustomLogDisabler newInstance(String pDefaultLoggerName) {
        CustomLogDisabler cld = new CustomLogDisabler(pDefaultLoggerName);
        return cld;
    }

    protected CustomLogDisabler() {}

    protected CustomLogDisabler(String pDefaultLoggerName){
        mIsActive = false;
        mLogger = LogManager.getLogManager().getLogger(pDefaultLoggerName);
        if (mLogger == null) {
            LogFactory.getLog(pDefaultLoggerName);
            mLogger = LogManager.getLogManager().getLogger(pDefaultLoggerName);
        }
        mOldLogFilter = mLogger.getFilter();
    }

    public boolean isActivated() {
        if (mIsActive) {
            return true;
        }
        return false;
    }

    public void activate() {
        mIsActive = true;
        Filter newFilter = new CustomLogFilter(mFilterMessages);
        mLogger.setFilter(newFilter);
    }

    public void deactivate() {
        mIsActive = false;
        mOldLogFilter = null;
        mFilterMessages = new ArrayList();
        mLogger.setFilter(mOldLogFilter);
    }

    public void addMessage(String pMessage) {
        mFilterMessages.add(pMessage);
    }
}

Step 3a: In your JUnit test add private field

private CustomLogDisabler mLogDisabler;

Step 3b: In your JUnit @Before method, set mLogDisabler = null

@Before
public void setUp() {
    mLogDisabler = null;
    ...
}

Step 3c: In your JUnit @After method, reset mLogDisabler

@After
public void tearDown(){
    if (mLogDisabler != null){
        mLogDisabler.deactivate();
    }
}

Step 3d: In your @Test method, initialize and activate the mLogDisabler. In this example, what you're testing is the class "YourClassName" which is in package "com.a.b". By adding the "Error reading this" to your mLogDisabler, any log messages that matches, start with or ends with "Error reading this" will not be created.

@Test
public void testRead() {
    mLogDisabler = CustomLogDisabler.newInstance("com.a.b.YourClassName");
    mLogDisabler.addMessage("Error reading this");
    mLogDisabler.activate();

    //your testing goes here
}

27 October 2012

Eclipse Github Setup

Download and install GIT for Windows (http://git-scm.com/downloads)

Set up system environment variables GIT_SSH pointing to ssh binaries (usually  C:\Program Files\Git\bin\ssh.exe) and user account environment variables HOME = %USERPROFILE%

In Eclipse, install EGit and GitHub Mylyn Connector.

In Eclipse, create new ssh key (Windows->Preferences->SSH2) and upload public key (*.pub) to Github. Make sure the Eclipse variable ssh2 home is set to where you save the created key. Usually found in Windows->Preferences->SSH2.

In Eclipse, create new project. Right click project and choose Team->Share->Git. Then Team->Add index. Then Team->Commit. This will save it in your local git repository.

In Github create repository and note its HTTP and/or SSH URL. Usually its https://github.com/[username]/[reponame].git or ssh://git@github.com:[username]/[reponame].git

In Eclipse, right click project and select Team->Remote->Push. When SSH is use, username=git password is emtpy. When HTTPS is use, enter your Githug username and password and select "Store in Secure Store"

10 April 2012

Eclipse SVN Setup

Requirements

  • Eclipse IDE for Java EE developers
    • with Subversive plugin
  • Tomcat 6 binary distribution (or what's on production)
  • Java 1.6 (or what's on production)

Environment Setup

  • Setup eclipse
    • Setup the Installed JREs
      • Window > Preferences > Java > Installed JREs. Select Add if a JRE is not yet configured.
      • Select Standard VM in the JRE Type window and click Next.
      • Populate the JRE Home field with the location of your Java installation.
    • Setup Server Runtime Environment.
      • Window > Preferences > Server > Runtime Environments. Select Add if a Tomcat Server is not yet configured.
      • Select Apache > Apache Tomcat v.6.0 in New Service Runtime Environment. Click Next.
      • Set the Tomcat installation directory to the location of the Tomcat binary distribution folder.
      • Select the right JRE.
  • Check out the customer project
    • Open the SVN Repository Exploring Perspective (Window > Open Perspective > SVN Repository Exploring)
    • Right click on the SVN Repositories view > New > Repository Location...
    • Enter https://svn.... in the URL field and enter in Authentication info.
    • A new repository location should appear in the SVN Repositories view. Right click the repository and select Check Out.
      • This action will checkout the project into your workspace.
    • Once done, return to the Java EE perspective and ensure a new project named "customer-portal" is in your Package Explorer (or Project Explorer) view.
  • Exclude Eclipse resource files (.settings folder, .project file, .classpath file) and /build folders from SVN to prevent from being checked in (especially the /build folder).
    • First, determine if you can see the resource files by expanding the customer-portal project. If you can see them, skip the next step.
    • If you cannot see the resource files, in the Package Explorer view, click on the View Menu button (it's a white arrow pointing down, next to the package explorer's Collapse All, Link with Editor, Minimize, Maximize controls), and select Resources...
      • Deselect .*resources from the Select the elements to exclude from the view box.
      • Click OK. The resources files should now appear in the package explorer view
    • Select the three resource files (.settings folder, .project file, .classpath file) and right click > Team > Add to svn:ignore...
    • Select OK.
    • Do the same for the /build folder (if it does not exist yet, create the folder "build" in the root of the customer-portal project).
      • A question mark should appear in the folder indicating that it's not version controlled.
      • Right click the folder > Team > Add to svn:ignore. Click OK.
  • Load the project's build file
    • Open the Ant view by selecting Window > Show View > Ant
    • Expand the customer-portal project and locate build.xml file
    • Drag and drop the build file onto the Ant view.
    • The build file should now appear in the view, which once expanded shows a list of all tasks in the build file.
  • Confirm the environment is setup correctly by running sandbox-war or production-war, and that the war files appear under /build (a refresh F5 might be needed to show the folders).