Java Open Source Practice (2) -ejbdoclet exercise
Wuder
Keywords: xdoclet, ant, ejbdoclet, jbosside, eclipse, ejb
1. Preparation before development
Download and install,
JDK5.0
Ant1.6.2
----- Refer to the previous article to learn how to set the environment variables of JDK and ANT.
XDoclet1.2.2
JBoss4.0.1 (Windows)
Eclipse (Windows Eclipse-sDK-3.1M5A-WIN32.ZIP)
Jbosside (Eclipse plugin JBosside-1.4.1-jre1.5-e31.zip)
---- Eclipse's plugin corresponds to the Plugins or Features directory.
2, organize source code.
1) Open Eclipse, create a new Java project, named EJBTEST.
2) Under this project, add a directory (below is the relationship between these directories and root directories)
Test
Test / bin
Test / EJB
TEST / EJB / SRC
TEST / EJBTEST
TEST / EJBTEST / SRC
Lib
LIB / J2EE (in the jboss directory, find jboss-j2ee.jar, put it in this directory.)
LIB / XDoclet (in the xdoclet directory, put the contents in the lib directory, put it in this directory.)
The following document structure can be seen by the Resource view of Eclipse.
3) Add a directory in the following path and place the testbean.java file.
PATH: TEST / EJB / SRC / COM / WUDER / EJB / Session / Beans /
The file content is:
Import java.rmi.remoteexception;
Import java.util.properties;
Import junit.framework.assert;
Import junit.framework.testcase;
Import com.wuder.test.ejb.session.util.testutil;
Import com.wuder.test.ejb.session.Intf.testhome;
Import com.wuder.test.ejb.session.Intf.test;
Public Class TestBeantest Extends Testcase {
Private test C;
Public static void main (String [] args) {
Testcase test = new testbeantest ("testgetteststring");
Junit.textui.teStrunner.Run (Test);
}
Protected void setup () throws exception {
Super.setup ();
Properties env = NULL;
Try {
ClassLoader Cl = this.getClass (). GetClassLoader ();
Java.io.inputStream in = cl.getResourceceasstream ("ejb.properties");
Env = new property ();
Env.Load (in);
System.out.Println ("Load JNDi Properties");
} catch (exception eXC) {
System.out.println ("no ejb.properties found, using default");}
TestHome h = testil.getHome (ENV);
C = H.CREATE ();
}
protected void teardown () throws exception {
Super.teardown ();
}
Public void testteststring () {
Try {
System.out.println (c.getteststring ());
Assert.assertequals ("2005/03/30", c.getteststring ());
} catch (remoteException E) {
E.PrintStackTrace ();
}
}
Public TestBeantest (string arg0) {
Super (arg0);
}
}
4) In / Test directory, put them in both files.
Butld.xml
The content is:
XML Version = "1.0"?>
path>
target>
homeInterface>
remoteInterface>
localhomeinterface>
localinterface>
EntityPK>
valueObject>
utilobject>
ejbdoclet>
target>
javac>
target>
jar>
target>
jar>
target>
target>
provject>
2. File: Project.properties
The content is:
EJB-DEP-URL = E: /java/jboss-4.0.1sp1/server/default/deploy3, build
Right-click on the build.xml file, then click Run as >> Ant Build, appearing in the console, as follows
...
EJB-Dep:
Build Successful
Total Time: 4 Seconds
Represents Build success, press F5, after refresh, you will find more directory Target in Test. This is the role of Ant, XDoclet. Among them, slowly pondering.
4, write test code
So two files in the directory TEST / EJBTEST / SRC.
EJB.PROPERTIES
The content is:
Java.naming.factory.initial = org.jnp.interfaces.namingContextFactory
Java.naming.Provider.URL = jnp: // localhost: 1099
Java.naming.factory.url.pkgs = org.jboss.naming: Org.jnp.interfaces
2. TestBeantest.java
The content is:
Import java.rmi.remoteexception;
Import java.util.properties;
Import junit.framework.assert;
Import junit.framework.testcase;
Import com.wuder.test.ejb.session.util.testutil;
Import com.wuder.test.ejb.session.Intf.testhome;
Import com.wuder.test.ejb.session.Intf.test;
Public Class TestBeantest Extends Testcase {
Private test C;
Public static void main (String [] args) {
Testcase test = new testbeantest ("testgetteststring");
Junit.textui.teStrunner.Run (Test);
}
Protected void setup () throws exception {
Super.setup ();
Properties env = NULL;
Try {
ClassLoader Cl = this.getClass (). GetClassLoader ();
Java.io.inputStream in = cl.getResourceceasstream ("ejb.properties");
Env = new property ();
Env.Load (in);
System.out.Println ("Load JNDi Properties");
} catch (exception eXC) {
System.out.Println ("no ejb.properties found, using default");
}
TestHome h = testil.getHome (ENV);
C = H.CREATE ();
}
protected void teardown () throws exception {
Super.teardown ();
}
Public void testteststring () {
Try {
System.out.println (C.Getteststring ()); assert.assertequals ("2005/03/30", c.getteststring ());
} catch (remoteException E) {
E.PrintStackTrace ();
}
}
Public TestBeantest (string arg0) {
Super (arg0);
}
}
5, adjust
The purpose of adjustment is to let you see the true function of Eclipse, you can also complete this exercise using the text editor.
At the left navigator window, right-click EJBTest. Select "Properties" and then select "Java Build Path"
Press below.
Define two USER Library and put it in which JBoss-4.0.1sp1 / server / default / lib, jboss-4.0.1sp1 / client, XDoclet / lib, JAR files are imported.
Select the Java view of Eclipse, you can see the following. At this time you can enjoy the convenience of Eclipse to the programming.
6, test
1) Start JBoss4.0. Mouse Right Key Testejb Debug >> JBoss >> Add a debug instance to select the default method. Point Debug. Start JBoss.
2) Run the test code.