September 2006 - Posts

I'll be totally honest... I'm not really excited about the TDD hype. But I feel like it's something important and even when I feel lazy(what the hell is wrong with me!??) I managed to atleast give it a shot. I've been reading this morning about JUnit and what it's all about.

http://junit.sourceforge.net/doc/testinfected/testing.htm

The above URL gave me an introduction to JUnit. Eventhough most bloggers here already did that, I'd like to say thanks  to the author because his article was number one in google when a keyed in "junit tutorial"

For those who are wanting to see JUnit in action, I don't think the article I mentioned was sufficient, you'll probably be needing this also...

http://open.ncsu.edu/se/tutorials/junit/

which will give you an idea on how to use Eclipes' built-in support for JUnit.

Ok, let's get on it.

First on, I assume you already downloaded the latest version of JUnit. Unpack the zip file, and for your own convenience try putting the jar file in JDK_INSTALL/jre/lib/ext folder or put it in your CLASSPATH environment variable.

following the first article I gave you, type in the following code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package test;

public class Money
{
  private int fAmount;
  private String fCurrency;
  
  public Money(int amount, String currency)
  {
    this.fAmount = amount;
    this.fCurrency = currency;
  }
  
  public int amount()
  {
   return fAmount;  
  }
  
  public String currency()
  {
    return fCurrency;
  }
  
  public Money add(Money m)
  {
    return new Money( ( m.amount() + this.amount() ) , currency() );
  }

}

put this is a package named test. Now on the same package, create a class named MoneyTest. Note the following convention when making a unit test

classNameTest
methodNameTest

though I may defy them for the sake of this example(I'm following two articles). Here's the code listing for MoneyTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package test;

import junit.framework.TestCase;
import junit.framework.Assert;

public class MoneyTest extends TestCase
{

public void testSimpleAdd()
{
  Money m12CHF = new Money(12, "CHF");
  Money m14CHF = new Money(14, "CHF");
  Money expected = new Money(26, "CHF");
  
  Money result = m12CHF.add(m14CHF);
  Assert.assertTrue( expected.equals(result) );
}

}


This test, as of this time doesn't do anything but to test if the two objects' values are equal. According to this link, If it's not defined for a (user) class(the equals method  is not overidden), it behaves the same as ==). And so, my test returned a failed result. Here's my screenshot for the simple test I made...




I wonder how I could use this with our current project. Problem is I am currently in a fast-phased project... There's not even a time for documentation(only code based).  For those of you who are interested in using JUnit outside of Eclipse, I have researched a little for you. You can find the instructions in this link...

http://rollerjm.free.fr/pro/Junit.html

When I get the time, I'll try to experiment more on this. As for now(YAWN)... I need to get some sleep... Hope that help. :)

Posted by lamia | 4 comment(s)
Filed under:
It was kind of shocking last Friday when our client didn't approve our project plan. Instead of having a 4 month duration that would involve design, coding and delivery our time was cut down to 1 month(20 days to be exact). I am pretty confident though that we could beat the deadline for even such a short time. It's Saturday, this morning I was researching on how to implement a Connection pool with Tomcat and also I was reading a paper that I printed in the office about how to make your own Connection pool class. This blog entry isn't related to anything I said. I just love to tell things to people that isn't directly related to my point hehehe.

I was up tonight at about 9:00 PM. I was suppose to read about Allegro, a game programming library for C/C++ which I plan to use in my hobby project. C/C++ was also a part of my life even though my work has nothing to do with it and although I have done nothing useful with it. But out of boredom as I was searching/reviewing about C++ file organization or whatever they call it(I was researching on how to separate the .h and .c implementation) I happen to see a link to a assembly x86 tutorial. Now... I haven't touched assembly even once...

Assemly is a language that I can say... A virgin in my eyes. I haven't coded anything assembly in my life before. Yes, I have a book on it which I haven't even flipped a single page from. I am not intimidated by the fact that assembly is a low-level, powerful language that existed even the day I was born... At least it's not J2EE/JEE from which you get confused with the large array of technologies available. There is only one assembly, and yet it's hard for me to start coding in it... I wonder why...

To help me remember that things I've read, I am writing this post.

First of all, I figured that assembly uses a code structure like most language do. For example, in C you have the topmost level to declare the libraries you plan to use, followed by variables and then the main method. There's also  something like that in assembly though a lot of difference may arise, if you've ever coded in your life this shouldn't be a problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.model tiny                 ; Tell compiler to build a tiny program model
.stack
.data
msg db "My First ASM PROGRAM!!!! WHOOOO", 13, 10, "$"
.code
START:
mov ax, seg msg ; mov the contents of seg msg into AX register
mov ds, ax ; mov the contents of AX into DS
mov dx, offset msg ; mov the offset of msg into dx
mov ah, 09 ; tell 21h interupt to print msg to screen
int 21h

mov ax, 4c00h ; Exit The Program
int 21h ; Safely
END START

 
Code above was taken from http://www.free2code.net/plugins/articles/read.php?id=154 which I think is a good intro for assembly newbies like me.

It's really a waste of time trying to explain what the program does here when someone else already did. Besides, I'm getting sleepy... If I decide to make something out of what I read today then that will be fun. I just finished downloading TASM and I also got a link for list of interrupts, the only thing left is motivation.
Posted by lamia | 1 comment(s)
Filed under: ,
Hmmm... I always had a single connection object active for the entire app. After I discovered that only one servlet is instantiated for every servlet class in the application this problem quickly popped in my mind. Actually, this was a worry I just ignored in the past since I had total control over the app's code and could easilly make changes whenever something goes wrong. Now, we''re working for a client and I can say that I don't really have that much control over everything. I tried to research and before anything gets into trouble I decided to re-design my connection class and make it thread-safe. I think this is an old issue but few Java developers really cared about this. Besides, this is advantageous to me because I could make my app take advantage of Java's multi-threading capability and thus make my app run faster!

Hmmm... I only have a link now which I plan to study tomorrow... I think it's kinda old but should still be useful..

http://archive.coreservlets.com/Chapter18.html


ConnectionPool class, here I come!
Posted by lamia | with no comments
Filed under: , ,
I know I've been in the field for quite sometime now... However, over and over I get to ask myself the question how many servlet instance is created in the container? At first I thought that multiple instances are created for each user and then reused over and over again... I was wrong...

Only one instace of a Servlet is created but multiple threads are created for each user to access it.

Another question came into my mind... How about static fields?

Well unless an object is synchronized, I guess both static and non-static fields are not thread safe. I dunno what effect synchronizing an objet will have on the application... I'm really not that good with multithreading(yet, but I'm working on it).

So how can other people say that Java is slow as a server-side programming language when only one servlet instance is created? Well... I have to say... Blame it to those model 1 developers who code in straight JSP (you *@#$3#@424!!!). FYI, JSP's are compiled at runtime so putting the business logic in it will really make your app slooowww...

Hmmm... It was quite a relief having to know how my container works...
Posted by lamia | with no comments
Filed under: , ,
Hmmm... I'm on my first task at my new company. The project I'm involved with is a PHP to Java conversion(Ewwww!). I have to say I really don't like it but it should give me the experience and discipline needed to stay on the job. Besides, it should be fun to work with other people from a distant place. We were trying to mimic their server settings. I downloaded PHP(5.1.6) and Apache Webserver's(2.2.3) latest release. To my surprise, after installing and configuring everything Apache won't accept my LoadModule entry for PHP. I've checked the docs... Hmmm... I'm sure I did everything right... It took a few hours of investigation before I read in the PHP install text that the installation procedure was for  Apache Webserver 2.0.x. And so... I downgraded to Apache 2.0.59 and voila! Everything worked perfectly and I can now see a nice php info screen on my browser!
Posted by lamia | 3 comment(s)
Filed under:
Hmmm... There have been a lot of things I wanted to learn lately... But without an actual project to work with, it will be quite impossible to learn fast... I'm the type of person who tends to study about a lot of things. May it be about computers, nature, food, girls, or your hamster's nose... To tell you people what I've been up to lately, I've been reading on the following subjects:

- Object Relational Mapping (ORM)
    particularly on Hibernate. But I never got the chance to try this yet although I have all the jar files setup already and I somehow have an idea of what I need to do.

- Web Services

    I have read a few articles about this one. I heard this is easilly done in PHP. Hmmm... I'll get back at you someday I promise! I'll probably keep an eye on Apache Axis

- JUnit

    In one of my previous interviews, they said that I should start reading/trying JUnit so I'll be ready when I join them. Alhtough, I never got the chance to because I was already hired by another employer.:) It seems like everybody's going TDD so I better make a move!

- Jasper Reports
    I'm just starting to read about this now. Maybe I can integrate this to my current project and + points in my resume bwahahaha! Shame on me for not knowing this technology until now... Every J2EE developer should be familliar with this reporting API.

- Oracle
  
My frustration of all the DBMS. I'll be happy just to be able to write a single app that has oracle as its backend. Now that Oracle express is out.... Hehehe... You just wait.:)

I've also been reading on the following books

- Head First Design Patterns
    Little by little I read on this book so I could gain more knowledge on design patterns that I don't know about...

- Effective Java
    One of my mentors said "Hindi kumpleto pagiging Java programmer" if you haven't read this book. It's good, only that you really got to have a good hold of Java first before you can absorb everything the author says... A must read for every Java Developer!

- Developing Games in Java
    I just had this book. I made my sis order this through Amazon since I don't have a credit card(I'm too afraid to have one). Although this is not related to my team's current game project(don't ask about it, it's total trash) I need to get some ideas like collision detection algorithm so we could design the architecture of the game right. Our game is a very very very simple sidescrolling shooter currently under the planning phase hehehe and will be made using C++ and Allegro game library.


Hmmm... That coffee seems to be having an effect on me now... Time to get back to work!
Posted by lamia | 2 comment(s)
Filed under: ,
I still remember back then when I was working as a trainee in one of my former companies. My boss gave a requirement wherein a user will only have specific areas of the application(this is a webapp) which he can gain access. I have to admit that it was one hard task for an inexperienced developer like me back then... It actually still is right now... I have very little idea and to think that everytime you access a page, you require database access so the app would verify if you are indeed allowed to access that facility. How is this done?

Basically, you have a list of pages with let's say page_id, page_name fields... I'll assume you also have  a users_tbl with user_id, username, password, etc. fields in it


+--------------------------+
users_tbl
--------------------------
user_id
username
password
...
...
+--------------------------+

+--------------------------+
pages_tbl
----------------------------
page_id
page_name
+--------------------------+


Now, I have a  lot of doubts with this second step wherein you have a many to many relationship and has a user_id, and page_id referenced to it.


+--------------------------+
permissions_tbl
----------------------------
permission_id
user_id
page_id
+--------------------------+





This is all just from the top of my head... I wasn't actually able to implement this so if any of you guys has a suggestion or much better if you've done something like this before please correct my mistakes and give suggestions. Thank you.




Posted by lamia | with no comments
Filed under:
I was doing a small project last night. Youm wouldn't believe how much the ol <jsp:include page=""/> tag has given me problems. First of all, what I was doing was include a JSP page. That included JSP page has a <c:import page=""/> call in it that calls a request to a Servlet, that servlet forwards to another JSP

main.jsp
|
|
---included.jsp
    |
    |
    ---servlet call [forwards]---->another.jsp


on main.jsp, when I use <jsp:include/>, the page just gets messed up. I didn't have similar problems when working with Struts(or maybe I didn't use this technique but I don't remember). Some of the parts of main.jsp particulary the ones before the <jsp:include/> tag was ommited. Hmmm...

<c:import/> to the rescue again!

Ok, I had no other choice but to use <c:import/>. Why not use just <c:import/> in the first place? I dunno... It's just a matter of preference. I try to use <jsp:include/> whenever including a static page and <c:import/> that has calls to a servlet that forwards to another page. I think that <c:import/> is actually capable of including/importing parts of other external apps/website. Judging from its attribute, <c:import url=""/>, it should be able to do just that. :)
Posted by lamia | with no comments
Filed under: ,
I encountered this error while working on the final module of my project earlier. The error was obvious to me though that it occured in one of my included JSP's.

<center>
<jsp:include page="/WEB-INF/templates/usermenutemplate.jsp"/>
</center>
<br/>
<br/>
<jsp:include page="/WEB-INF/templates/addpositiontemplate.jsp"/>
<hr/>
<c:import url="/actions/viewpositions.do"/>
<br/>
<br/>



normally, I wouldn't have any problems doing this. But it looks like there was quite a huge amount of processing going on with those included JSP's and even the Servlet from which this set of JSP tags page was forwarded from.

//Sample servlet code that demonstrates how much processing is being done on the servlet.

DBHelper dao = new DBHelper();
    
request.setAttribute("positionTypesList", dao.selectPositionTypes() );
    
return mapping.findForward(FORWARD_managepositions); 

There was no time think of optimizing the code. I had to resolve to a simple hack and maybe it was really the last card that I have.

Problem was actually solved with this single line of code

<%@ page buffer="16kb" %>

I think the default buffer size for JSP is 8kb. When you get too much output to generate more than that it results to a IllegalStateException: Cannot forward after response has been committed. Why? Because I think the buffer is being flushed right away even before the response is completed so we have to set the default buffer size explicitly. Fore more info on how to solve this problem, check out these links.

http://forum.java.sun.com/thread.jspa?threadID=683891&messageID=3983241
http://www.jguru.com/faq/view.jsp?EID=480147
http://www.velocityreviews.com/forums/t151515-servlet-file-download-javalangillegalstateexception-cannot-forward-after-response-has-been-committed.html
http://forum.java.sun.com/thread.jspa?forumID=45&threadID=204859
http://forum.java.sun.com/thread.jspa?threadID=604166
Posted by lamia | 4 comment(s)
Filed under: , , ,