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).

24 August 2010

I think I'm maturing...

A CAC triggered an involuntary, or more appropriately, unwanted growth spur in my delicate and slow maturity.  Knowing that eventually, all these will lead to something greater, I feel contented with where I am right now... then CAC!

I guess it will one of those moments where I would look back and say to myself, this was an important time of my life. This is where revolution started. As all triggers are, temporary, instantaneous, so will CAC be; a force that pushed me into the next step without any regard to my preferred pace.

Nonetheless, I am grateful. Who would have thought that a mere CAC can do all these things. Though I wish the next time this happens, it will be less of a trigger and more of a tug.

PS: The saying "Ignorance is bliss" is so true. Trust me.

PS(2): New laptop in 2 weeks = Stracraft 2. Woohoo!!!

01 July 2010

How many months...?

It has been quite a while since I posted something new. I was not that busy. I even had the time to watch and finished a lot of old series and animes. I guess I just loss the interest to blog. Maybe I'll just pop in every now and then just to reminisce on my old, childish endeavor.

PS: Ever been in a situation where there are too much stuff to think that you give up analyzing probable solutions to all possibilities and just let tomorrow unfolds?

18 November 2009

A Calculated Retreat or a Leap Towards the Unknown

I'm at a crossroad, not knowing what the future holds yet desperately trying to act as if nothing can faze me.

Settling in a different country has its own pros and cons. Depending on the timing, it can become a blessed turn of events or a regretful decision. Apparently, I am stuck at the middle. On the one hand, my age gives me ample time to experience life at a country that have more opportunities. Yet on the other, my accomplishments are not recognized. Which means either I wasted years of my life which will simply occupy 2 lines in my resume or I put all hope into thinking that somewhere in this first-world country, a chance will magically create a chance to show what I got.

Of course the most obvious choice is the one that moves towards the future, and yet the biggest hindrance would not be the deficiencies but the fear of failing. Oh kakorrhaphiophobia, why do you have to exist?

I guess I'm just afraid to pin all hope into a single course of action and see myself fail. In my life, either I get what I wanted or I don't hope that much. Disappointment is a hard feeling to get over. I've never been heart broken so I have no comparison.

Sooner or later, I need to face the truth, swallow my fear and just jump. Regardless of what is the outcome, whether it be spikes and swords of a comfy foam. But as of now, I'm still sheltering my fear.

PS: Living in a country with cold climate combined with cloudy weather and dark skies does not help.

12 September 2009

A Methaphorical Circumcision

It has been a while since I had the time or the energy to post something new. I been living in a colder country for at least a month now. Somehow, I still haven't get it together. It was my hope that a new environment would, in one way or the other, stir up my inner bum and push me away from the soft and warm embrace of my bed. Still nothing...

Like the rest of the cultured humans, I also wanted most, if not all, of the normal things bachelors dreams of. A grumpy old boss, friends with which you can bitch about your boss, and a love that awaits you after a 10-hour meeting with old grumpy.

In 4 days, I will be interviewed for a job that I am assured to get. The details are irrelevant, but know this, it will mark the day of my adulthood. I never actually considered myself an adult. Let us just say, my parents, never treated me as such but come Sept 22, this boy will be metaphorically circumcised.

So, back to the present, I have no concrete plan as of now. Although it is crystal clear what things should I do: Job1, Job2, and Study. There is no specific time frame as of now. However, ideally, at least one job is a full time job in my chosen field, a part time job that address my weaknesses, while getting my grad degree.

Easier said than done. I know. Just regularly waking up before 8 is a great tribulation, how much more doing 3 things that requires 100% of one's effort. There is a big possibility that I won't be able to do all these things. But hey, if I'm going to set my goals, I'm going to put it way up there.

PS: Best wishes to my cousin who just got married last Sept 9.