Posts filed under 'Java'

Learn German from VW ads

Volkswagen Ads

Volkswagen Ads

“Schade, dass man nach Amerika nicht fahren kann” probably means “what a pity that a man can not drive to America.” At first, I thought that it meant “what a pity that a man can not travel to Americal” which does not make sense, because a man can travel to America by an airplane. The important verb word here is “fahren” which means “travel.” Then, I realized that the “fahren” word could mean “drive” as well. Then, it made sense to me.

Add comment June 21, 2009

JMS Topic

I had taken a lab course with this department. Without the lab assistant, I would not make it through. I would like to summarize a part of my lab work here related with JMS, so I will not forget. Full lab report here.

Following is the general architecture of  the system. JMS is used for communication between the NodeController and a Node Client.

 

The Overview Architecture of the Job Spooling System

The Overview Architecture of the Job Spooling System

 

 

I was doing a distributed application in Java. Part of the lab is sending a message from an EJB (version 3) to a client application on a different machine using JMS (Java Message Service).  First, we have to create a JMS Topic which is kind of a channel that we send can send a message too. Naming this technology with the word “Topic” is not a good idea, because this word is a common name. Thus, it is difficult to search for examples in Google.

 

I was using JBoss 4. I had to create a JMS Topic in the JBoss first by going to the JMX console of JBoss. I assume that you know how to install JBoss, and develop  EJB 3.0 applications.

1. Go to JBoss homepage at http://localhost:8080. The URL could be something else that you have set.

2. Go to JMX console by clicking at JMX Console under JBoss management.

3. Go to DestinationManager under jboss.mq.

4. Create a JMS topic named “pomtopic” by invoking the createTopic function. Supply param p1 and p2 with the value “pomtopic,” and then click “Invoke” as shown in the figure below. The “pomtopic” topic will be
created. Java application can send message to and receive message from this JMS topic.

I am not sure why there are two paramters (p1 and p2). Just try to use the same name. The name could be something else. The name will be referenced from the codes.

Creating a JMS Topic within a JMX Console of JBoss 4. Clicking invoke will create a JMS Topic.

Creating a JMS Topic within a JMX Console of JBoss 4. Clicking invoke will create a JMS Topic.

Following is the sender code of the JMS Topic. The sender code is in the EJB 3.0. Some variables are declared and instantiated automatically though dependency injection technique.

==========sender=============================

……

// Connection factory for JMS Topic communication
@Resource(mappedName=”TopicConnectionFactory”)
private ConnectionFactory connectionFactory;
//For JMS Topic communication, the name of JMS Topic is fixed to “pomtopic”.
//This topic must be created in JBoss JMX console before using, otherwise, it will throw an exception.

@Resource(mappedName=”pomtopic”)
private Topic topic;

// This function will send the message to the JMS topic.
//Derived from the JMS tutorial from Sun website
// @param msg represents the message that this function will send to the JMS topic which node client is //listening to.
//@return result of sending, True for successfully. False for when there is an exception occurred.

public boolean sendMessageToTopic(String msg)
{

try {

Connection connect = connectionFactory.createConnection( );
Session session = connect.createSession(true,0);
MessageProducer producer = session.createProducer(topic);
TextMessage textMsg = session.createTextMessage( );
textMsg.setText(msg);
producer.send(textMsg);
connect.close( );

 

} catch (JMSException ex) {

logger.debug(ex.getMessage());
return false;

 

}
return true;

 

}

….

==================================================

 

Next is the client code who is waiting for message. First, you need to create a connection to the JMS Topic first. The Java function below demonstrates the connection.

=====client : function for creating a JMS Topic connection=============

public  void connectByTopic() {

String                  topicName = null;
Context                 jndiContext = null;
TopicConnectionFactory  topicConnectionFactory = null;
TopicConnection         topicConnection = null;
TopicSession            topicSession = null;
Topic                   topic = null;
TopicSubscriber         topicSubscriber = null;

/*  Part of Code from Sun
* Create a JNDI API InitialContext object if none exists
* yet.
*/

try {

jndiContext =getInitialContext();// new InitialContext();

} catch (NamingException e) {

System.out.println(“Could not create JNDI API ” +
“context: ” + e.toString());
e.printStackTrace();
System.exit(1);

}

/*
* Look up connection factory and topic.  If either does
* not exist, exit.
*/

try {

topicConnectionFactory = (TopicConnectionFactory)
jndiContext.lookup(“TopicConnectionFactory”);
topic = (Topic) jndiContext.lookup(“pomtopic”);

} catch (NamingException e) {

System.out.println(“JNDI API lookup failed: ” +
e.toString());
e.printStackTrace();
System.exit(1);

}

/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create subscriber.
* Register message listener (TextListener).
* Receive text messages from topic.
* When all messages have been received, enter Q to quit.
* Close connection.
*/

try {

topicConnection = topicConnectionFactory.createTopicConnection();
topicSession =  topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
topicSubscriber = topicSession.createSubscriber(topic);
topicSubscriber.setMessageListener(this);
topicConnection.start();

 

} catch (JMSException e) {

System.out.println(“Exception occurred: ” +
e.toString());

} finally {

if (topicConnection != null) {
try {
topicConnection.close();
} catch (JMSException e) {}
}

}

}
==================================================

After creating a JMS Topic connection, then the Java client application needs to listen for a message.

=========Client : function for listening a JMS Topic message==========

public void onMessage(Message message) {

TextMessage msg = null;
try {

if (message instanceof TextMessage) {

msg = (TextMessage) message;
System.out.println(“Reading message: ” +
msg.getText());

//the following function will process the message sent from the server
processMessageAndExecute(msg.getText());

 

} else {

System.out.println(“Message of wrong type: ” +

message.getClass().getName());

}

} catch (JMSException e) {

System.out.println(“JMSException in onMessage(): ” +
e.toString());

} catch (Throwable t) {

System.out.println(“Exception in onMessage():” +
t.getMessage());

}

}

=================================================

Add comment August 18, 2008

Quick overview on Cobertura (Testing Coverage Tool)

I have mentioned Cobertura in this blog.

Cobertura is a tool similar to Coverlipse mentioned in this blog. Cobertura is a measurement tool to measure your unit tests’ coverage on your tested codes. I think that it is really a good tool. It is a bit difficult for a beginner, because it requires some configuration. You have to know how to use Ant (a build tool).  Here, I just provide a tutorial to get a quick overview of the Cobertura on Windows.

1. You need to have Ant installed on your computer. You can download from this page. Select apache-ant-1.7.1-bin.zip.  See this page for installing instruction. Be sure to add Ant to your environmental variable called PATH as illustrated in “Windows / OS 2″ section in the installing instruction page, so you can call Ant from any folders. After you install ant, try running just command “ant” in any folder in your command promt. If it displays something like Ant is not recognized as a command, it means that you have not set up the environment variable PATH correctly. See this page for instruction on how to set an envrionmental variable on Windows.

2. download the Cobertura from this page. Select cobertura-1.9-bin.zip.

3. Unzip into your folders. You will get cobertura-1.9 folder

4. Put your Java source files into the example folder. There is an example found in \cobertura-1.9\examples\basic. I put my Java source files which have been used as an example in this blog and this blog. It consists of one Java file, DoSomething.java, and its test case file, TestCountCharacter.java. Basically, put your codes in \cobertura-1.9\examples\basic\src folder.

Put your java files (unit test case and class to be tested) in the cobertura-1.9\examples\basic\src folder

Put your java files (unit test case and class to be tested) in the cobertura-1.9examplesasicsrc folder

5. Put JUnit.jar into \cobertura-1.9\lib.

See this blog for how to get JUnit.jar.

If you do not put this Jar file here, then build will be failed, because it can not compile some classes which use come classes found in JUnit.jar. The error is shown below. If your Java compiler can find JUnit.jar, then you do not need to add JUnit.jar into the lib folder.

Ant build is fail because JUnit has not been added into the lib function.

Ant build is fail because JUnit has not been added into the lib function.

6. Modify the ant build file found in \cobertura-1.9\examples\basic\build.xml.

On the line around 90, add your java test file into it. In my case, I have to add TestCountCharacter.java which contains the JUnit test cases, so I put “<include name=”TestCountCharacter.java” />” as shown below.
<batchtest todir=”${reports.xml.dir}” unless=”testcase”>
<fileset dir=”${src.dir}”>
<include name=”**/*Test.java” />
<include name=”TestCountCharacter.java” />
</fileset>
</batchtest>

7. Run the ant build file simply by typing “ant” in the \cobertura-1.9\examples\basic.

Behind the scene, Cobertura compiles, run test cases and produces the report on your test case coverage. See this page for full instruction.

Ant build success

Ant build success

8. View the report. Reports are automatically generated after the ant build. There are two reports: report on JUnit test case and report on test coverage.

You can view the report of your JUnit test case by opening up the index file in cobertura-1.9\examples\basic\reports\junit-html.

You can view the report of your test coverage by openning up the index file in cobertura-1.9\examples\basic\reports\cobertura-html.

Report on JUnit test cases is automatically generated and can be found in this folder

Report on JUnit test cases is automatically generated and can be found in this folder

Report on your test cases' coverage is automatically generated and can be found in this folder

Report on your test cases

As you see from the figure below, the line coverage is not 100% achieved because there some statements which have not been executed. The branch statement does not achieve 100% coverage, because there is a branch which has not been executed.

Cobertua report displays the test cases' coverage which are line coverage and branch coverage

Cobertua report displays the test cases

If you furhter click into the class DoSomething.java, it shows you which line has not been executed and how many time that line has been executed as shown below.

Cobertura shows test coverage (line (or statement) and branch)

Cobertura shows test coverage (line (or statement) and branch)

2 comments August 16, 2008

How to get JUnit.jar

If you do not have JUnit.jar in your computer, you can download from here. Just download any junit-x.x.jar where x is a version number. The Junit.jar is in the downloaded zip file.

2 comments August 16, 2008

Testing coverage with Cloverlipse in Eclipse 3.2

You want to make sure that your test cases have executed every thing in your function. You can use test coverage criteria (eg. statement, brance, data flow, etc) as a criteria to measure the quality of your test cases.

Or, you can use it to study old codes when you want to see which lines or functions are used without going through a cumbersome debugging process.

This article is continued from the previous tutorial of using JUnit in Eclipse. If you want to see the testing coverage, you can click as shown below.

Run JUni test with Coverclipse

Run JUni test with Coverlipse

I think that Coverlipse is based on statement coverage. There are many test coverage or aquadecy criterion. More info on test coverage can be found in wiki. Basically, you want to test everything in your code. To satisfy statement coverage, you need to execute all statements. To satisfy branch coverage, you need to execute all brances.  Coverclipse shows you which statement is executed by putting a green check on that line as shown below.

After running JUnit with coverclipse, the Coverclipse the line of code which you test case has executed by putting a green check on that line

After running JUnit with coverclipse, the Coverlipse the line of code which you test case has executed by putting a green check on that line

To see a test coverage clearly, I have modified the function that I tested. The function does not mean to do anything make sense. It just serves as an example for code coverage. As you can see below, there are some codes which are not excuted by test cases. It is shown by a red exclamation side on that line.

Coverclipse shows you which lines are not covered by your test cases by putting a red exclamation sign (!) on that line

Coverlipse shows you which lines are not covered by your test cases by putting a red exclamation sign (!) on that line

With Coverlipse, you can use to see which codes are used when you have to study bunch of old codes. It can give you a quick overview than going through a debug processes.

1 comment August 16, 2008

Unit testing with Eclipse and JUnit

Software testing is necessary to ensure the quality of the software. I would like to how can you make  a quick unit test in Eclipse using JUnit.

First of all, there are 2 main kinds of testing : blackbox testing and whitebox testing. Blackbox testing is when you derive a test case for a function without considering codes inside the function. You only care that you provide all possible inputs. There are many blackblox testing techniques. One of them is Category Partition Testing. Bascially, you simply divide all inputs into classes, and then just test one representive input of each class. For example, if a function X has an input of type Integer. You may partition this input into three classes which are negative numbers, zero and postive numbers. This is just an example. Partitioning inputs is depended on your function. Then you derive three unit test cases. One for each partition. If you have 2 inputs. Then you may have 2*3 = 6 combinations of inputs. Sometimes, you may have a lot of combination than you can possibly test. The slides (slides 15) and the paper Category Partition Testing suggest the use of constraint to limit some combinations.  There could be some possbile combination which will not happen for sure, so it can be considered as a constraint and removed out from testing.

Enough with the theory.

I use Eclipse 3.2 and it is already equiped with JUnit. We are going to use some classes from JUnit.jar. You may need to add the JUnit jar file into your Eclipse project if it does not automatically add JUnit into your project build path. (If Eclipse does not add JUnit into your project build path, then your Java class which requires classes found in JUnit.jar can not be compiled.) This  page gives you some idea of how to add JAR file in Eclipse. See this blog for how to get JUnit.jar.

You need to have a Java function that you want to test. A figure below shows an example of Java function.

a simple java function that you want to test

a simple java function that you want to test

Next, you can add a Junit test as shown below.

Adding a new Junit test case

Adding a new Junit test case

Then the “New JUnit Test Case” dialog is shown as illustrated below. You can select a class that you want to test in the textbox “Class under test.”

"New JUnit Test Case" dialog is shown

Next, then you can select a function that you want to test.

Selecting a function that you want to test

Selecting a function that you want to test

You will see a new Java class as shown below. Eclipse  may not be able to recognize some classes as shown below.

Eclipse does not recognize some classes found in JUnit.jar

Eclipse does not recognize some classes found in JUnit.jar

So you need to either add a JUnit jar file into your project. Or, eclipse may ask you whether you want to add Junit.jar into your classpath as shown below.

Eclipse asks whether you want to add JUnit.jar into the build path

Eclipse asks whether you want to add JUnit.jar into the build path

You may add more test cases. In the figure below, I have 4 test cases just for one function. Each test case is represented by one function. A function name needs to be started with the word “test.”  You can simply runs the JUnit as shown below. Normally, you use the assert something function to assure the result from the function. For example, if you expects that a function returns 3 given your inputs, then you assert like following:

assertTrue(returned_value == 3) ;

If the function returns 3, then this test case is passed. Otherwise, it is failed. Deriving test cases and keeping it for further use is a good idea. Developers usually have a version. With JUnit test cases, they can see if a new version of the function still working properly.

The figure below shows when all unit test cases are passed.

result of running JUnit test cases. In this case, all test cases are passed

result of running JUnit test cases. In this case, all test cases are passed

3 comments August 16, 2008

Bundles in OSGi

Interestingly, there is a new project called OSGi which is a specification for developing and managing software components life cycle in Java.

There is a new concept called “Bundle.” A bundle is actually a group of required Java files and resouces.  It is similar to a Jar file which is a zip file containing java class file and resources. However, a Jar file does not have information about OSGi which is used for managing the software component life cycle. It allows developers to add or remove a Java component (bundle) during runtime. Read here for more info on OSGi. Costin Leau wrote a good article on Bundle.

You can simply develop a bundle according to OSGi with Eclipse. Simply select a Plugin Project and choose Equinox as a platform. Equinox is like a container implementing the OSGi specification. You can run your bundles on Equinox.

For a typical java application, you would make sure that you have all necessary jar files. For OSGi, you have to specify the required bundles. Each required bundles will look for its required bundles on its own. If you have a legazy Jar file, you can turn an old jar files into a bundle as well.

If your applications need some kind of modifying components during runtime, you may consider OSGi.

Add comment July 28, 2008

Software Testing Course in Edinburgh

Previously, before I took Software Testing class, I unit-tested my work unsystematically and mannually. When I first study about testing, some books say that you should use “assert” (such as assertEqual in JUnit or Assert.AreEqual in .Net Framework) to reassure the value in the variables. At first, I did not understand why. Later on, I understood that it would make it easier for me to track when there is something wrong rather than debugging. I still remembers the old day when I spent time debugging in Eclipse or Visual Studio. Going line by line debugging took so much time and energy.

From the Software Testing class, http://www.inf.ed.ac.uk/teaching/courses/st/, I learned that I can do a Junit test. At first, I was thinking whether it would be benefitial if I make a test suite for my funtions. I realized that it is very useful. I remembered the old day, when there was a bug, I have to fix. Fixing a bug can cause a new bug. Having a suite test ready will make your life easier. I can test my bug fixing code just within a second to see whether it has an impact to other codes.

I also need to design my software to be easy to test as well.

If you want to see a sample of software testing sylable, you can take a look at the Edinburgh’s Software Testing course.

Add comment January 29, 2008


Categories

Top Posts

Recent Posts

Pages

Archives

 

November 2009
M T W T F S S
« Oct    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Blogroll

Books

Blog Stats

Top Clicks

Category Cloud

database Distributed application Education emergency response entrepreneurship firefighter (Feuerwehr) Flex/Flash German Language (Deutsch) Java Microsoft and .NET Risk Management (Disaster) Software Testing Uncategorized User Interface Design and Technology wcf Windows Workflow

Spam Blocked