9 Replies Latest reply on Jul 17, 2009 11:04 AM by peterj

    Junit failed.

      Hi,

      I have the following problem when I tried to establish a JNDI connectoin from a remote machine to the EJB3 JBOSS server thought JNDI.

      # ant test
      Buildfile: build.xml

      compile-test:
      [javac] Compiling 4 source files

      test:
      [junit] Testsuite: Client
      [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
      [junit]
      [junit] Null Test: Caused an ERROR
      [junit] Client (wrong name: com/ip6networks/calling_card_registration/test/Client)
      [junit] java.lang.NoClassDefFoundError: Client (wrong name: com/ip6networks/calling_card_registration/test/Client)
      [junit] at java.lang.ClassLoader.defineClass1(Native Method)
      [junit] at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
      [junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
      [junit] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
      [junit] at java.lang.Class.forName0(Native Method)
      [junit] at java.lang.Class.forName(Class.java:247)
      [junit]
      [junit]
      [junit] Test Client FAILED

      BUILD SUCCESSFUL
      Total time: 4 seconds

      Main program:
      # cat Client.java
      package com.ip6networks.calling_card_registration.test;

      import com.ip6networks.calling_card_registration.test.Calculator;
      import com.ip6networks.calling_card_registration.test.CalculatorRemote;
      import com.ip6networks.calling_card_registration.test.ServiceLocator;

      public class Client
      {
      public static void main(String[] args) throws Exception
      {
      Calculator calculator = getSession();
      System.out.println("in Client class");
      System.out.println("1 + 1 = " + calculator.add(1, 1));
      System.out.println("1 - 1 = " + calculator.subtract(1, 1));

      }

      private static Calculator getSession() throws Exception {
      Calculator mgr = null;
      mgr = (Calculator) ServiceLocator.getInstance().getService("Calculator");
      if (mgr == null) {
      //throw new SystemErrorException("Unable to connect to service");
      throw new Exception("Unable to connect to service");
      } else {
      return mgr;
      }
      }
      }

      It complained ServiceLocator..... cant be casted to Calculator.

      Here is the ServiceLocator program:

      # cat ServiceLocator.java
      package com.ip6networks.calling_card_registration.test;

      import javax.naming.Context;
      import javax.naming.InitialContext;
      import java.util.*;
      import org.apache.commons.logging.LogFactory;
      import org.apache.commons.logging.Log;

      public class ServiceLocator {
      private Map<String, java.lang.Object> cache;
      private static ServiceLocator ourInstance = new ServiceLocator();
      private static final Log logger = LogFactory
      .getLog(ServiceLocator.class);

      public static ServiceLocator getInstance() {
      return ourInstance;
      }

      private ServiceLocator() {
      this.cache = Collections.synchronizedMap(
      new HashMap<String, java.lang.Object>());
      }

      public java.lang.Object getService(String serviceName) {
      String serviceHost = "192.168.1.242";
      if (this.cache.containsKey(serviceName)) {
      return this.cache.get(serviceName);
      }

      try {
      java.lang.Object service = this.getContext(
      serviceHost).lookup(
      serviceName + "Bean/remote");
      cache.put(serviceName, service);
      return service;
      } catch (Exception e) {
      e.printStackTrace();
      logger.error("Unable to bind to service "
      + serviceName + ". Hostname: " + serviceHost, e);
      return null;
      } catch (Throwable e) {
      e.printStackTrace();
      logger.error("Unable to bind to service "
      + serviceName + ". Hostname: " + serviceHost, e);
      return null;
      }

      }

      public java.lang.Object getService(String serviceName, String serviceHost) {
      String key = serviceHost + "." + serviceName;
      if (this.cache.containsKey(key)) {
      return this.cache.get(key);
      } else {
      try {
      java.lang.Object service = this.getContext(
      serviceHost).lookup(
      serviceName + "Bean/remote");
      cache.put(key, service);
      return service;
      } catch (Exception e) {
      logger.error("Unable to bind to service "
      + serviceName + ". Hostname: " + serviceHost);
      return null;
      }
      }
      }

      private Context getContext(String serviceHost) throws javax.naming.NamingException
      {
      Properties properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY,
      "org.jnp.interfaces.NamingContextFactory");
      properties.put(Context.URL_PKG_PREFIXES,
      "org.jboss.naming:org.jnp.interfaces");
      properties.put(Context.PROVIDER_URL, serviceHost + ":1099");
      return new InitialContext(properties);
      }
      }


      Thanks
      Sam

        • 1. Re: Junit failed.
          peterj

          Looking at this code and the error message, I believe that the error message has nothing to do with the code, but rather that the junit task within ant is incorrect. As a guess, I suspect that the Client.class file is in the wrong location relative to the classpath - the error seems to say that it found Client.class in a base directory rather than within the com/ip6networks/calling_card_registration/test/ directory.

          If the above does not help you, please post the full target that include the junit task. When posting that, please enclose it within 'code' tags - select the XML text and click the Code button above the editor window. Also, use the Preview button to ensure proper formatting before clicking Submit.

          • 2. Re: Junit failed.

            Hi,

            Here is the build.xml file I used:

             # cat build.xml-back1
            <?xml version="1.0"?>
            
            <project name="portlet" basedir="." default="deploy">
             <import file="../build-common-portlet.xml" />
            
             <target name="compile">
             <mkdir dir="docroot/WEB-INF/classes" />
            
             <path id="plugin-lib.classpath">
             <pathelement location="${app.server.classes.portal.dir}" />
             <fileset dir="${app.server.lib.portal.dir}" includes="*.jar" />
             <pathelement location="docroot/WEB-INF/classes" />
             </path>
            
             <javac
             classpathref="plugin.classpath"
             compiler="${javac.compiler}"
             debug="${javac.debug}"
             deprecation="${javac.deprecation}"
             destdir="docroot/WEB-INF/classes"
             nowarn="${javac.nowarn}"
             srcdir="docroot/WEB-INF/src"
             />
            
             <antcall target="build-common-plugin.compile" />
             </target>
            
             <target name="build-lang">
             <antcall target="build-lang-cmd">
             <param name="lang.dir" value="docroot/WEB-INF/src/content/test" />
             <param name="lang.file" value="Language" />
             </antcall>
             </target>
            
            <property name="tst-dir" location="docroot/WEB-INF/src/com/ip6networks/calling_card_registration/test" />
             <property name="TALK" value="false" />
            
             <path id="classpath.base">
             </path>
             <path id="classpath.test">
             <pathelement location="/usr/liferay-portal-5.2.3/dev/portlets/calling_card_reg/docroot/WEB-INF/lib/junit.jar" />
             <pathelement location="${tst-dir}" />
             <path refid="classpath.base" />
             </path>
            
             <target name="compile-test">
             <javac srcdir="${tst-dir}"
             verbose="${TALK}"
             >
             <classpath refid="classpath.test"/>
             </javac>
             </target>
            
             <target name="test" depends="compile-test">
             <junit>
             <classpath refid="classpath.test" />
             <formatter type="brief" usefile="false" />
             <test name="Client" />
             </junit>
             </target>
            
             <target name="clean-compile-test">
             <delete verbose="${TALK}">
             <fileset dir="${tst-dir}" includes="**/*.class" />
             </delete>
             </target>
             <target name="clean-test" depends="clean-compile-test" />
            
            </project>
            
            


            Here is the path for all the source code for TESTING:

            # pwd
            /usr/liferay-portal-5.2.3/dev/portlets/calling_card_reg/docroot/WEB-INF/src/com/ip6networks/calling_card_registration/test
            # ls -l
            total 14
            drwxr-xr-x 2 root wheel 512 Jul 15 16:58 ./
            drwxr-xr-x 4 root wheel 512 Jul 14 22:30 ../
            -rw-r--r-- 1 root wheel 147 Jul 14 23:05 Calculator.java
            -rw-r--r-- 1 root wheel 150 Jul 14 23:06 CalculatorRemote.java
            -rw-r--r-- 1 root wheel 1060 Jul 14 23:06 Client.java
            -rw-r--r-- 1 root wheel 2929 Jul 14 23:06 ServiceLocator.java
            
            


            junit.jar is located in the following folder:
            /usr/liferay-portal-5.2.3/dev/portlets/calling_card_reg/docroot/WEB-INF/lib/


            Thanks
            Sam

            • 3. Re: Junit failed.

              Hi, I don't know how to change the build.xml file so that it reflect my *class files for Junit.

              Any suggestion is highly appreciated.

              Thanks
              Sam

              • 4. Re: Junit failed.
                peterj

                Looks like you are going to learn how packages and classpaths relate (this is often a hurdle one has to overcome when learning Java).

                Your class is named Client and is in the com.ip6networks.calling_card_registration.test package. This means that the JVM will scan the classpath looking for a file named:
                com/ip6networks/calling_card_registration/test/Client.class

                This means that the classpath should be set to:
                /usr/liferay-portal-5.2.3/dev/portlets/calling_card_reg/docroot/WEB-INF/src/
                in your case. Then the JVM will look for the file:
                /usr/liferay-portal-5.2.3/dev/portlets/calling_card_reg/docroot/WEB-INF/src/com/ip6networks/calling_card_registration/test/Client.class

                In addition, when you tell JUnit about the class, you need to use the full class name, which includes the package name:
                com.ip6networks.calling_card_registration.test.Client

                That should give you enough information to fix the build.xml.

                • 5. Re: Junit failed.

                   

                  "PeterJ" wrote:
                  Looks like you are going to learn how packages and classpaths relate (this is often a hurdle one has to overcome when learning Java).

                  Your class is named Client and is in the com.ip6networks.calling_card_registration.test package. This means that the JVM will scan the classpath looking for a file named:
                  com/ip6networks/calling_card_registration/test/Client.class

                  This means that the classpath should be set to:
                  /usr/liferay-portal-5.2.3/dev/portlets/calling_card_reg/docroot/WEB-INF/src/
                  in your case. Then the JVM will look for the file:
                  /usr/liferay-portal-5.2.3/dev/portlets/calling_card_reg/docroot/WEB-INF/src/com/ip6networks/calling_card_registration/test/Client.class

                  In addition, when you tell JUnit about the class, you need to use the full class name, which includes the package name:
                  com.ip6networks.calling_card_registration.test.Client

                  That should give you enough information to fix the build.xml.

                  Hi,

                  If you take a look at my buld.xml file I have published previously, it has a classpath declared there, but it pointed to a different path. In my Junit section, do I need to set a different "classpath" id to point to the "test" directory?

                  Thanks
                  Sam

                  • 6. Re: Junit failed.
                    peterj

                    Please do not quote entire posts - it makes the topic flow harder to follow.

                    If you take a look at my buld.xml file I have published previously, it has a classpath declared there, but it pointed to a different path.


                    I know this, why do you think that I told you to change it?

                    I really gave you everything that I think should help you fix the build.xml. But here are the changes I was suggesting - once you see them look over my previous post again. (By the way, I hope the build.xml is located at /usr/liferay-portal-5.2.3/dev/portlets/calling_card_reg otherwise the tst-dir property setting is not correct.)

                    . . .
                    <property name="tst-dir" location="docroot/WEB-INF/src" />
                    . . .
                     <junit>
                     . . .
                     <test name="com.ip6networks.calling_card_registration.test.Client" />
                     </junit>


                    • 7. Re: Junit failed.

                      Followed your suggested change, it might work for the Junit test case, but it failed with the "ordinary" case:

                      calling_card_reg # ant test
                      Buildfile: build.xml
                      
                      compile-test:
                       [javac] Compiling 11 source files
                       [javac] /usr/liferay-portal-5.2.3/dev/portlets/calling_card_reg/docroot/WEB-INF/src/com/ip6networks/calling_card_registration/struts/action/CallingCardRegistrationAction.java:29: package com.liferay.portal.struts does not exist
                       [javac] import com.liferay.portal.struts.PortletAction;
                       [javac] ^
                       [javac] /usr/liferay-portal-5.2.3/dev/portlets/calling_card_reg/docroot/WEB-INF/src/com/ip6networks/calling_card_registration/struts/action/CallingCardRegistrationAction.java:34: package javax.portlet does not exist
                       [javac] import javax.portlet.ActionRequest;
                      


                      However I don't expect running "ant test" will caused the code above to be compiled. How to "exclude" the compilation only goes to "com.ip6networks.calling_card_registration.test" rather than "com.ip6networks.calling_card_registration.Action" ?

                      Thanks very much for patient.

                      Sam

                      • 8. Re: Junit failed.
                        peterj

                        The compile doesn't work because you did not tell it what to compiles - you need an includes directive:

                        <javac srcdir="${tst-dir}"
                         verbose="${TALK}"
                         includes="**/*.java"
                         >
                         <classpath refid="classpath.test"/>
                         </javac>
                        


                        • 9. Re: Junit failed.
                          peterj

                          Looks like we are continuing this discussion at:
                          http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4244351