Sunday, January 25, 2009

Switched to Blogger

I have abandoned Wordpress and switched my blog to Google's Blogger. The main reason for this was the extreme amount of spam I was receiving via comments. I realise I could probably have installed a Wordpress plugin to solve this problem, but I felt that spam filtering is a feature that should come as standard, without having to perform further configuration.

Anyway, Google's Blogger software requires comment posters to use one of their online identities, which should reduce, if not eliminate the amount of spam I was receiving. That's another web solution moved into Google's cloud.

Saturday, February 23, 2008

Local development of a Facebook application

If you have developed a Facebook application, no doubt you were frustrated trying to test your application locally. I have a little technique here that will allow you to locally test your application, without having to comment out all of the facebook object declarations, yes that is what I was doing initially!

The problem here is that you shouldn't modify your code for testing purposes, as this gives room for these unintentional modifications to be transfered to the live environment. Alternatively, we don't want to be performing our initial testing of the application when it is live, and available for use.

So, for the solution to this, I am declaring an instance of a Facebook mock object when in the testing environment. The Facebook mock object contains all of the methods my code relies on from the Facebook object, but they return values for testing purposes. Here is an excerpt from the class, if you wish you can download it, and use it.


class FacebookMock{
public $api_client;

public function FacebookMock(){
$this->api_client = new FacebookAPIClientMock();
}

public function require_frame(){
return null;
}

const TEST_USER = "11111";

public function require_login(){
return self::TEST_USER'
}
}

So how does the application know whether it is in the testing environment, or the live environment? For each environment I have an environment specific php file that deals with configuration. In there I have the following declaration

define (ENVIRONMENT, "testing");

This value is then used in a condition in my code, to determine whether to use
the real Facebook object, or the mock object.


// this allows local testing, with a mock class
if(ENVIRONMENT == "testing"){
$this->facebook = new FacebookMock();
} else{
$this->facebook = new Facebook(FACEBOOK_API_KEY,
FACEBOOK_SECRET_KEY);
}

Because the Facebook object is loosely coupled
from my application, the above condition is only
used in one place, and
that is my FacebookProxy class.
This class decides which Facebook
object to use (condition above),
and contains methods that interact
with the Facebook API object.
For further information on loose coupling
and interfaces,
read my post
?.

Thursday, August 30, 2007

Watir text_field and UTF-8

At work I use Watir for functional testing of our websites. In the very near future we will be allowing our users to search in languages other than English. Therefore I needed to be able to test the submission of forms with non-English keywords.

I was having great difficulty with Waitr, using the following code:

ie.text_field(:name, 'name').set('mandarin-character')

This usually works fine for standard English characters, however when using Chinese or European accented characters Watir does not enter this into the text field, and skips it.

I had a look at the Watir mailing list and found a few people that were using excel to hold the UTF-8 word, getting the value from the cell, and passing it to the set() method.

However when I tried this approach I still experienced the same problem, Watir was just skipping the character.

While I was looking through the Watir rdoc I noticed a method for the text_field called value=(), I gave this a go,


ie.text_field(:name, 'name').value=('mandarin-word')

and it worked, I have not seen anybody else mention this so hopefully this will help some people. However the Watir rdoc does not advise using this method, but hey, it works!

"this method sets the value of the text field directly. It causes no events to be fired or exceptions to be raised, so generally shouldn't be used it is preferred to use the set method."

The only other statement I have in my script is the codepage declaration to handle UTF8


require "win32ole"
WIN32OLE.codepage = WIN32OLE::CP_UTF8

Sunday, April 22, 2007

Polymorphism and Run-time binding in Java

I have recently been wading through my Java tutorial book, and came across run-time binding, with very little description or explanation.
In my code I have an Employee class, and two subclasses; PartTimeEmployee and FullTimeEmployee. Employee class is an abstract class and contains a method which is overridden in both of the subclasses. Run-time binding allows me to initialise these sub-objects at run-time, and then invoke the overridden method when required, which in turn executes the correct method associated with that sub-object. But what happens if i want to invoke a method from one of these objects that is not overridden from the Parent class? Well without a description of this in my book, i took to the web, and here is what i found.
A quick explanation of what i am trying to achieve: I want to be able to display the salary of an employee, or their hourly pay, depending on whether they work full time or part time. While i am writing the code i know that i can invoke this method because at run-time, it will be the correct type, however my code does not know this, and it is good practice not to write code, that does not know what it is doing! I have a for loop which cycles through an array of Employee objects, some of which are FullTimeEmployee objects, and others PartTimeEmployee objects. So if I have a full time employee i want to display the annual salary. However this code (below) does not know that the employee is a full time employ.

System.out.println("Employee annual salary: " +
employeeList[i].getAnnualSalary());

Because my code does not know this, it also doesn't realise that it will have access to the getAnnualSalary() method. So here is the answer! We use

instanceof

and cast the Employee object to the correct type.

if(employeeList[i] instanceof FullTimeEmployee) {
FullTimeEmployee fe = (FullTimeEmployee) employeeList[i];
System.out.println("Employee annual salary: " +
fe.getAnnualSalary());
}

Our code now knows that it can use the getAnnualSalary() method.