Saturday, September 25, 2010

Playing with a DBMonster

I recently had the privilege of using DBMonster when I needed to generate some random test data for a MySQL database. What I am about to demonstrate is a simple use of the DBMonster tool.

The SQL statement for the simple table used in this writeup is as follows (of course you will have to create a database first):

CREATE TABLE person (
id int not null auto_increment,
last_name varchar(45) not null,
first_name varchar(45) not null,
gender varchar(6) not null,
date_of_birth date not null,
PRIMARY KEY (id)
);

Note that, the SQL statement is for MySQL only although if you remove the auto_increment statement it should work for your database.

Now that we are all set we can start to use DBMonster. The simplest way to start is by using DBMonster to reverse engineer the database table and generate a schema for itself. Then you can tweak the schema to your satisfaction.

To start (you will need access to the JDBC Driver for your database):
a) Download DBMonster and unzip. I will call this directory "<DBMonster_Home>".
b) In the <DBMonster_Home> directory, create a file call "dbmonster.properties". In this file insert the following (each on a separate line):
dbmonster.jdbc.driver=com.mysql.jdbc.Driver
dbmonster.jdbc.url=jdbc:mysql://localhost:3306/<database_name>
dbmonster.jdbc.username=<username>
dbmonster.jdbc.password=<password>
c) When this is done, launch the command prompt and navigate to the <DBMonster_Home> directory, at the command prompt type the following (without the inverted commas):
"java -classpath ../mysql-connector-java-5.1.6-bin.jar;dbmonster-core-1.0.3.jar pl.kernelpanic.dbmonster.Launcher -c dbmonster.properties --grab -o dbmonster-schema.xml"
d) A Schema file called "dbmonster-schema.xml" should have been generated for you. We will be making some changes to this file.

Note: Do not close the command prompt we will be reusing it later

The changes we will be making in the dbmonster-schema.xml file are:
a) We will reduce the number of rows to 50 unless you have the time to wait for 1000 entries
b) For the last_name and first_name fields, spaces should not be allowed. We will change that.
c) It makes no sense for gender to be anything other than Male or Female. Fortunately, DBMonster has a DictionaryGenerator we can use for this.

To proceed,
a) Open the dbmonster-schema.xml file in your favorite XML editor.
b) Look for <table name="person" rows="1000"> and change to <table name="person" rows="50">
c) Also look for <column name="last_name" databaseDefault="false">. Within this tag, there should be a <property name="allowSpaces" value="true"/> change that to <property name="allowSpaces" value="false"/>. Repeat this for the <column name="first_name" databaseDefault="false"> tag.
d) Create a file called "gender_dictionary.txt" in the <DBMonster_Home> directory and put the following in it (each on a separate line)
Male
Female
e) In the dbmonster-schema.xml file, replace the entire <column name="gender" databaseDefault="false"> tag withe the following:
<column name="gender" databaseDefault="false">
<generator type="pl.kernelpanic.dbmonster.generator.DictionaryGenerator">
<property name="unique" value="false"/>
<property name="dictFile" value="gender_dictionary.txt"/>
</generator>
</column>

We have finished with the dbmonster-schema.xml file. To generate the test data run the following at the command prompt (without the inverted commas):
"java -classpath ../mysql-connector-java-5.1.6-bin.jar;dbmonster-core-1.0.3.jar pl.kernelpanic.dbmonster.Launcher -s dbmonster-schema.xml -c dbmonster.properties"

Do not hesitate to visit the DBMonster Homepage to learn more.

Enjoy.

Saturday, September 18, 2010

All Religion is Flawed

Yes, you read that right "All Religion is Flawed". The goal of any religion is to portray the "Worshipped" of the chosen religion in a perspective that is far superior to the "worshipped" of alternative religions. And yet no one asks where all these chaos originated from. I was speaking to a guy the other day whose religion borders on the principle that the individual in his religion is God. That you made (or make) the decision of where you are now (or need to be) from where you have been (in terms of being born). So I asked him "Are you saying I have been here (the world) before?". He said "Yes". Then I said "Are you saying I chose to come back here (the world)?". He said "Yes". So I said to him "Wake up. It doesn't make any sense why would I choose to come back to this sh*t hole if I have already been here before. And on top of that to be my own God I will have to be all-knowing, all-powerful and all-seeing". He said "You are". Then I replied "That means that all of us in this world are our own Gods". He said "We are". Damn, talk about a classic case of "too many cooks spoil the broth". Imagine one billion plus (1, 000, 000, 000+) all-knowing, all-powerful and all-seeing Gods working around on this earth for their individual, selfish reasons. Talk about Chaos.

This isn't written in order to take a swipe at anyone's religion. I just got tired of listening to all the "Come and worship my God" or "My God is the shiznit" nonsense.

I don't know a lot of what I expect from a religion but here are a few things that I know and believe:

a) I am not God. Simply because I don't know it all, I don't see it all and I can't do it all
b) It is not possible for any human being (flesh and blood) to be God (same reason as (a))
c) There is a God. Simply because it takes creativity to create something as interesting as this world. The air I breathe, the water I drink, the food I eat, the women I see and so on. Its all perfect except for one (1) thing "chaos"
d) It is clear that there is good and there is evil. And both definitely do not like one another.
e) Human beings love to receive good things or things that make us feel good all the time (a promotion, lovely gifts, a check in the mail, a beautiful wife, a pretty girlfriend, a loving husband, a caring boyfriend, a cool car, a nice house, a tasty meal, a good hair do and so on. The list is endless). Which means we clearly lean towards good in our receiving but in our giving that's another story (the classic shades of gray symptom).

To summarize, a good-loving intelligent God created this world and created human beings to live in it. Note that I have reserved the use of the words: all-knowing, all-seeing and all-powerful to describe this God. Why did I say "good-loving"? Simply because I do not think that an "evil-loving" God will create beings who constantly love to receive good things. Evil likes evil, Good likes good. So it seems only natural that their offsprings would like what their parents like (at least on the receiving side of things).

Now the question arises "How did a good-loving God create something (or someone) that loves to receives good things all the time but on the giving side of things, it (he/she) can give either good or evil?". Good question. Unfortunately, I don't know the answer to that. If I did I would be all-knowing (you see my point).

However a few possibilities come to mind:
a) A fundamental flaw in the design (or in computer speak, a bug in the programming). If this is true it would definitely undermine the perception of an all-knowing God that many religions proclaim. I hope you now see why I reserved the word "all-knowing" to describe the God that created this world.
b) The design was done intentionally (or on purpose). Now this one is interesting. Why? Simply because it would mean good created something that could please evil. Maybe good has a crush on evil (pun intended). Reminds me of high school. This would definitely grant credibility to the "free-will" concept of some religions.

In summary, if it was a flaw in the design then why hasn't it been fixed all these thousands of years later. Does it mean that the Creator doesn't care or simply doesn't mind that evil festers. Or does it simply mean the Creator cannot fix it. That would mean the Creator is not "all-powerful" as some would have us believe. On the other side of things, if it was a purposeful design why is it a criminal gets punished for using his/her design feature. That is, if the feature is there and it is used is it valid to consider it a crime. "I wonder, I really wonder". The same applies to religions with an "after-life" concept. Either way you will get punished for evil whether it was the Creator's design fault that the ability to do evil was there in the first place or whether you exercised your "free-will" in doing the evil. Both ways you are screwed.

In conclusion, I am hoping you see why I began by saying "All Religion is Flawed". They all say the same thing "Follow me if you want to see the light". Unfortunately, there is no light at the end of all their tunnels. My advice, if you like receiving good so much, others like it as well so you should try as much as possible to give out as much good as you can on a daily basis (or their might be a shortage of good in the world).

Thank you, have a nice day and may the Creator (whoever that may be) be with you all. (Pun intended).

Tuesday, April 27, 2010

EJB Unit testing with OpenEJB 3.1.2 and Netbeans 6.8 (again without Maven)

I recently became interested in researching the possibility of unit-testing my Enterprise JavaBeans (EJBs) 3.0. This led me to look at OpenEJB. Upon my journey through the OpenEJB documentation, I found that there were many examples at the OpenEJB Download page. However these examples are based on the Maven way of doing things and although it was possible to run them in Eclipse it wasn't so easy to do the same in Netbeans as I was offline when I was trying the examples (quite frankly my Maven skills is down there with my graphics skills (that is, almost non-existent)).
After reading through the simple-stateless unit test example I decided to try and run it in my Netbeans 6.8, it went well so I decided to document it for others who might be interested. Btw, my operating system (OS) is Windows Vista but I believe this should work on any OS that supports Java.

The first steps are (duh):
a) Download and Install Netbeans 6.8
b) Download the OpenEJB Standalone Server (openejb-3.1.2.zip) and the OpenEJB Examples (openejb-examples-3.1.2.zip) from the OpenEJB website.

If you have done that launch Netbean 6.8 and
a) Choose New Project... from the menu.
b) Choose Java from the Categories list and select Java Application from the Projects list. Click on the Next button
c) Enter simple-stateless as the Project Name and click the Finish button
d) In the Projects explorer to your left, right-click on the Source Packages and choose New->Java Package... (if you don't see the Java Package... option choose Other... and look under the Java Categories list)
e) Enter org.superbiz.calculator as the Package Name and click Finish
f) Again, right-click on the Test Packages and choose New->Java Package...
g) Enter org.superbiz.calculator as the Package Name and click Finish
h) Again, right-click on the Source Packages and choose New->Folder (if you don't see the Folder option choose Other... and look under the Other Categories list)
i) Enter META-INF as the Folder Name and click Finish.
j) Unzip the OpenEJB Examples zip file that you downloaded and look inside the directory. You will see a directory called simple-stateless. Enter that directory and navigate to src->main->java->org->superbiz->calculator. Select all the Java files (namely: CalculatorImpl.java, CalculatorLocal.java and CalculatorRemote.java), copy (Ctrl+C) and paste it into Netbeans by first selecting the org.superbiz.calculator package of the Source Packages that you created, right-click and choose Paste (Ctrl+V)
k) Return to the simple-stateless directory and navigate to src->main->resources->META-INF. Copy the ejb-jar.xml file and paste it into Netbeans in the META-INF folder you created earlier.
l) Return again to the simple-stateless directory and navigate to src->test->java->org->superbiz->calculator. Copy the CalculatorTest.java file and paste it into Netbeans by first selecting the org.superbiz.calculator package of the Test Packages that you created, right-click and choose Paste.
m) Unzip the OpenEJB Statndalone Server that you downloaded. Go back to Netbeans and select Libraries from the Projects explorer. Right-click and choose Add Library...
n) Select Create... from the Add Library dialog box that appears. Enter OpenEJB as the Library Name in the Create New Library dialog box and click Ok.
o) Ensure that the Classpath tab is selected from the Customize Library dialog and click Add JAR/Folder. Navigate to the unzip OpenEJB Standalone Server and open the lib directory. Choose all the JAR files in the lib directory and click the Add JAR/Folder button of the Browse JAR/Folder file chooser.
p) Click Ok to close the Customize Library dialog box.
q) Make sure the OpenEJB library is selected from the Available Libraries of the Add Library dialog and click the Add Library button


That is all that is required. Now all you have to do is choose the CalculatorTest.java from the Test Packages, right-click and choose Run File (Shift+F6) and watch your test fail spectaculously. Cool isn't it.
Now right-click on the simple-stateless project and choose Clean and Build. When this is done, choose the CalculatorTest.java file and Run File again.
Now the unit test should pass.

Hope this wasn't too long. As I learn more I will definitely post it. Thanks