Monday, October 19, 2009

Midterm Questions and Answers

Here are the answers to my 10 questions:

1. Define the term 'security through obscurity' and describe why it is not a good practice.

Security through obscurity refers to the use of secrecy as a mean to develop a secure application or system. It is usually not a good practice because it can still contain vulnerabilities that could have otherwise been detected if it were publicly disclosed.

2. Explain why writing "Please send your reply to...." after posting your question on a forum is bad.

Because most posters would like to earn recognition through answering your questions, and by asking them to reply privately would not help them achieve that goal.

3. What does the command 'ant -f emma.build.xml' do?

Emma is a coverage tool that generates an html report indicating how much lines of your code is actually being executed. The command emma.build.xml executes Emma.

Consider the following program written in Java for questions #4 - 10.

/**
* Simple program that fills an array with integers.
* @author Daniel Colton
*/
PUBLIC class MyList {
PUBLIC Integer TOP = 0; // Tracks the TOP INDEX
PUBLIC
Integer[] intArray = new Integer[10]; // Array OF integers
/**
* Create an instance of MyList and insert 10 integers
* into it.
*/
PUBLIC static void main(String[] args) {
MyList list
= NULL;
MyList newList = new MyList();
FOR (INT i = 0; i < 10; i++){
list.
ADD(i);
}
}
/**
* Adds an integer into the array.
*/
PUBLIC void ADD(Integer i) {
intArray[top]
= i;
TOP++;
}
/**
* Prints the ith elemnt of the array.
*/
PUBLIC void PRINT(Integer i) {
String
MESSAGE = new String("List";
System.out.println(MESSAGE+"["i+"] = "intArray[i]);
}
}


4. List at least two lines that can be considered a CheckStyle error.

- Missing '@param' statement above lines 'public void add(Integer i)' and 'public void print(Integer i)'.
- Missing javadoc coments above data variable declarations at lines 'public Integer top = 0;' and 'public Integer[] intArray = new Integer[10];'.
- Use of ambiguous naming convention for variables such as 'i' at lines 'public void add(Integer i)' and 'public void print(Integer i)'

5. List at least two lines that can be considered a PMD error.

- Unused variable 'newList' at line 'MyList newList = new MyList();'.
- String variable 'MESSAGE' at line 'String MESSAGE = new String("List");' should be declared as a string literal (String MESSAGE = "List").

6. List at least one line that can be considered a FindBug error.

- A null pointer dereferace for the 'List' variable at line 'MyList list = null;'.

7. Rewrite the program so that it passes all three of the above mentioned tests.

I also added the two functions, getValue and onProgramEnded, which are useful when writing JUnit tests for Questions #8, 9, and 10.

/**
* Simple program that fills an array with integers.
* @author Daniel Colton
*/
PUBLIC class MyList {
/** Tracks the top index. */
PUBLIC Integer TOP = 0;
/** Array of integers */
PUBLIC Integer[] intArray = new Integer[10];
/**
* Create an instance of MyList and insert 10 integers
* into it.
*/
PUBLIC static void main(String[] args) {
MyList list
= new MyList();
FOR (INT i = 0; i < 10; i++){
list.
ADD(i);
}
list.onProgramEnded
();
}
/**
* Adds an integer into the array.
* @param index refers to the index position in the intArray.
*/
PUBLIC void ADD(Integer INDEX) {
intArray[top]
= INDEX;
TOP++;
}
/**
* Prints the ith elemnt of the array.
* @param index refers to the index position in the intArray.
*/
PUBLIC void PRINT(Integer INDEX) {
String
MESSAGE = "List"
System.out.println
(MESSAGE+"["INDEX+"] = "intArray[index]);
}

/**
* Returns the ith element of the intArray.
* @param index refers to the index position in the intArray.
*/
PUBLIC INT getValue(Integer INDEX){
RETURN intArray[index];
}
/**
* This method is called after the program executes.
*/
PUBLIC void onProgramEnded(){
}
}


8. Write an assertion test for the program that you have re-written in question #7.

Following assertion test checks to see if all values are properly inserted into the integer array:

import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
* This is an acceptance test that tests if an integer value
* is inserted into the array.
*/
PUBLIC class TestMyList {
@Test
PUBLIC void testIfInserted() {
MyList list
= new MyList();
FOR (INT i = 0; i < 10; i++){
list.
ADD(i);
}
boolean isInserted
;
FOR (INT i = 0; i < 10; i++){
IF (intArray.getValue(i) != NULL) {
isInserted
= true;
}
ELSE {
isInserted
= false;
}
assertTrue
("Check if inserted" isInserted);
}
}
}


9. Write a behavioural test for the program that you have re-written in question #7.

The following program checks if the values are inserted in the correct order (from 0 to 9):

import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
* This is a behavioural test that tests if the integer values
* are inserted in the correct order.
*/
PUBLIC class TestMyList2 {
@Test
PUBLIC void testValuesInserted() {
MyList list
= new MyList();
FOR (INT i = 0; i < 10; i++){
list.
ADD(i);
}
FOR (INT i = 0; i < 10; i++){
assertEquals
("Check insert order" list.get(i), i);
}
}
}


10. Write a unit test for the program that you have re-written in question #7.

The following program tests if the getValue method returns the correct value from the integer array:

import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
* This is a Unit test that tests if the getValue method returns
* the correct value from the list.
*/
PUBLIC class TestMyList3 {
@Test
PUBLIC void testGetValue() {
MyList list
= new MyList();
FOR (INT i = 0; i < 10; i++){
list.
ADD(i);
}
assertEquals
("Test getValue" list.get(0), 0);
assertEquals("Test getValue" list.get(9), 9);
assertEquals("Test getValue" list.get(4), 4);
assertEquals("Test getValue" list.get(7), 7);
}
}

Wednesday, October 14, 2009

Hosting MistaRoboto with SVN

We are nearing the half way mark of the semester and now we are beginning to prepare for our upcoming group projects. Our next task is to study how to use Subversion with Google Hosting.

Subversion is a tool that enables you and your group members to upload local copy of your project onto the online repository.

Using Subversion with Google Hosting can never be easier. I was able to upload my project almost issue free. The only problem I had with uploading my project was that I accidentally deleted parts of my project and for a little while I was not able to run verify on it. I was able to determine the source of the problem fairly quickly however. I simply copied down to my local repository the files that were missing in the online repository, and committed the changes. The missing files were back on the repository and the project was back and ready to go.

Throughout this exercise and learning this new Google Hosting functionality, I am now more confident that I will be able to organise a group project. I think that one important feature of Subversion is that we have the option to roll back to previous versions of the project if something goes very wrong in our latest project version. I would like to be able to effectively use all the functionalities provided by Subversion

Wednesday, October 7, 2009

Testing Stage

Now that we finished developing our robocode project, it is now time for some testing. Testing a project may sound easy at first, but in fact, it is one of the most difficult tasks when it comes to project development. As a part of our class exercise, we were asked to design some test cases that would be useful when we actually go and create the test cases later on. I must say that it was not easy to come up with an ideal test case for my lab partner's robocode project. It took me a good half an hour of thinking and brainstorming to finally come up with an idea that may work well for his project.

Designing test cases for my own robot also was a difficult task. It seemed to me that in order for my robot to pass an acceptance test, it must at least beat the strongest of the sample robots, that is, Walls. I simply created an acceptance test case where my robot and Walls battle with each other for 5 rounds. If my robot successfully beats Walls 5 matches in a row, then it passes the first phase of my acceptance test. Another acceptance case would be a very similar one, but my robot now has to beat Tracker. I chose to implement Tracker because this robot does not have a set routine movement like Walls and some of the other sample robots. Passing my two acceptance case would mean that the robot is capable of beating a robot that is strong and has a set routine movement, and is also capable of beating a robot that does not have a set routing movement.

My first behavior test was rather simple. Since my robot does somewhat involve random movements, and it is programmed to move forward until it hits a wall, it should at least touch 3 of the 4 walls in the battlefield before the match ends. My first behavior test checks just that. My other behavior test checks to see if the robot is actually moving perpendicular to the enemy. This part of the test was difficult to implement because by the time onTurnEnd() method is called, the target robot may have moved to a another position. I resolved this issue by giving the robot a much larger range for its 'perpendicular movement'. For instance, moving 45 degree angle towards the enemy would still be considered as a 'perpendicular movement'.

For my Unit tests, I basically tested to see if the part of my code that controls the turning of the robot is working correctly. I also had some difficulties implementing this test case because the robot only turns about 10 degrees per turn. This makes it difficult to keep track of the robot's turn rate and how much it had already turned. My other Unit test case checks for the function that takes the robot out of the corners. This is done simply by checking whether the robot does a 90 degree turn sometime during a battle.

After finishing up all of my test cases, I continued to improve my project by using a tool called Emma. Emma is a tool that provides a greater insight to your code, by allowing you to view which part of your code is actually being ran and which part isn't. This tool is helpful especially when it comes to testing and I hope to use this tool for my future projects as needed.

My Robocode project with the above test cases can be downloaded Here.