hi
I have java file that sends JMS message to a queue in the JBOSS server. I have 
a junit testcase that tests sending of messages to the queue.I have included 
all the necessary jars with the default scope. But when I run the command 'mvn 
test', I see
testcase gives NullPointerException. It failes to get connection to the queue. 
Can anyone help me out. I have attached the source file, test file and the POM

Thanks
Ganesh

                                
---------------------------------
 Find out what India is talking about on  - Yahoo! Answers India 
 Send FREE SMS to your friend's mobile from Yahoo! Messenger Version 8. Get it 
NOW
<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd";>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.apache.commons.logging</groupId>
  <artifactId>commons-logging</artifactId>
  <packaging>jar</packaging>
  <version>1.0.2</version>
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
  <description>hfdahfdhfdiooiasdfhio</description>
	<dependencies>
		<dependency>
			<groupId>jdom</groupId>
			<artifactId>jdom</artifactId>
			<version>1.0</version>
		</dependency>				
		<dependency>
			<groupId>ant</groupId>
			<artifactId>ant</artifactId>
			<version>1.6.5</version>
		</dependency>
		<dependency>
			<groupId>ant</groupId>
			<artifactId>ant-launcher</artifactId>
			<version>1.6.5</version>
		</dependency>

		<dependency>
			<groupId>ant</groupId>
			<artifactId>ant-junit</artifactId>
			<version>1.6.5</version>
		</dependency>
		<dependency>
			<groupId>jboss</groupId>
			<artifactId>jbossall-client</artifactId>
			<version>4.0.4.GA</version>
		</dependency>
		<dependency>
			<groupId>jboss</groupId>
			<artifactId>jbossall-client</artifactId>
			<version>4.0.4.GA</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
		</dependency>
	</dependencies>
	
 
</project>
/*
 *   Copyright (c) 2006, CA.  All rights reserved.
 *   This software and all information contained therein is confidential and proprietary and may not be duplicated,
 *   disclosed or reproduced in whole or in part for any purpose except as authorized by the applicable license agreement,
 *   without the express written authorization of CA. All authorized reproductions must be marked with this language.
 *   TO THE EXTENT PERMITTED BY APPLICABLE LAW, CA PROVIDES THIS SOFTWARE “AS IS” WITHOUT WARRANTY OF ANY KIND, INCLUDING
 *   WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
 *   IN NO EVENT WILL CA BE LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY LOSS OR DAMAGE, DIRECT OR INDIRECT, FROM THE
 *   USE OF THIS MATERIAL, INCLUDING WITHOUT LIMITATION, LOST PROFITS, BUSINESS INTERRUPTION, GOODWILL, OR LOST DATA,
 *   EVEN IF CA IS EXPRESSLY ADVISED OF SUCH LOSS OR DAMAGE.

 *   Modification details           Developer name           Modification Date
 *   Initial Code                    mutvi02                 October 26, 2006, 7:50 PM
 */

package com.ca;

import java.io.BufferedWriter;
import java.util.Date;
import javax.naming.*;
import javax.jms.*;
import java.util.Hashtable;
import java.io.*;

//import org.apache.log4j.Logger;

/**
 *  FeedbackQueueHandler is a Singleton class which enqueues objects of type
 *  ExecutionMessage into FeedbackQueue
 */
public class FeedbackQueueHandler
{

    // used for logging
//    private Logger log = Logger.getLogger(FeedbackQueueHandler.class);

    // holds the singleton instance for this class
    private static FeedbackQueueHandler reference = null;
    static BufferedWriter bw;
    // needed inorder to lookup the JNDI tree
    private InitialContext context;

    // represents the starting point for other operations
    private ConnectionFactory connectionFactory;

    // represents an active connection to the JMS provider which is
    // an application server
    private Connection connection;


    // Singleton Class - so constructor is private
    // connection and other needed instances are required to be created
    // only once. So there will only be one connection (it is heavy) and
    // one or more sessions (lightweight).

    private void writeFile(String str){
		try{
			if(bw!=null){
		bw.write(str+"\n");
		bw.flush();
			}
	}
	catch(IOException ie){}
	}
    private FeedbackQueueHandler()
    {
        try
        {
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

            // Connects to the JMS Server
            env.put(Context.PROVIDER_URL,"jnp://localhost:1099");
            env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces" );
            context = new InitialContext(env);
	    if(context==null)
		    writeFile("Context is null\n");
	    writeFile("Getting connection factory\n");
            connectionFactory = (ConnectionFactory)context.lookup("ConnectionFactory");
	    writeFile("Getting connection\n");
	    connection = connectionFactory.createConnection();
            writeFile("Connection is created from factory");

        }
        catch(NamingException ex)
        {
		writeFile("Check null\n");
			writeFile(ex.toString());
 //           log.error(ex + " Lookup for ConnectionFactory failed");
        }
        catch(JMSException ex)
        {
			writeFile(ex.getMessage());
 //           log.error(ex + " Creation of Connection failed");
        }
    }

    /** @return singleton instance of type QueueHandler
     * synchronized - makes it thread safe
     */
    public static synchronized FeedbackQueueHandler getFeedbackQueueHandler()
    {
	    try{
		bw=new BufferedWriter(new FileWriter(new File("E:\\DTools\\MavenProject\\sampleProject\\log.log"),true));
	    }
	    catch(Exception e){}
        if(reference == null)
            reference = new FeedbackQueueHandler();
        return reference;
    }


    /** This method is overridden as part of the step
     * to make this class singleton - no clones can
     * can exist now
     */
    public Object clone() throws CloneNotSupportedException
    {
        throw new CloneNotSupportedException();
    }

    /** @param an instance of type MachineStatusMessage
     *  @return none
     *  Purpose: an overloaded version of enqueue which puts a message of type
     *           ExecutionStatusMessage into the FeedbackQueue
     */
    public void enqueue(Date date)
    {
        Session session = null;
        String destinationName = "queue/TestStatusQueue" ;
        MessageProducer messageProducer;
        ObjectMessage message;

        try

        {
            session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            Queue feedbackQueue = (Queue)context.lookup(destinationName);
            messageProducer = session.createProducer(feedbackQueue);

            message = session.createObjectMessage();
            message.setObject(date);
            messageProducer.send(message);
            connection.start();
 //           log.info("MachineStatusMessage enqueued by FeedbackQueueHandler");
        }
        catch(JMSException jmsException)
        {
			writeFile(jmsException.getMessage());
 //           log.error(jmsException + " JMSException - Cannot create connection");
        }
        catch(NamingException namingException)
        {
			writeFile(namingException.getMessage());
 //           log.error(namingException + " Cannot find the destination - " + destinationName);
        }
        finally
        {
            try
            {
                session.close();
            }
            catch(JMSException ex)
            {
 //               log.error(ex + " Exception occurred -- cannot close the connection");
            }

        }

    }
}
/*
 * SampleTestSuite.java
 * JUnit based test
 *
 * Created on November 9, 2006, 6:15 PM
 */
package com.ca.test;
import com.ca.*;
import junit.framework.*;
import java.io.File;
import java.util.Date;


/**
 *
 * @author kumga03
 */
public class SampleTest extends TestCase
{

	public SampleTest(String name)
	{	super(name);
	}

	public void setUp() throws Exception
	{
            FeedbackQueueHandler f=FeedbackQueueHandler.getFeedbackQueueHandler();
            if(f!=null)
            f.enqueue(new Date());
	}

	public void tearDown() throws Exception
	{
	}

	public void testSuccess()
	{
		assertEquals(1+1,2);
	}

	public void testFailure()
	{
		assertEquals(1+1,3);
	}

	public void testError()
	{
		File f=null;
		f.mkdir();
	}

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to