Oracle JDeveloper 10g Reviewer's Guide
Oracle JDeveloper 10g Reviewer's Guide
Reviewers Guide
March 2004
INTRODUCTION ................................................................. 2
INTRODUCTION
This guide will help you experience application development with Oracle
JDeveloper 10g. This guide provides step-by-step instructions that leads you
through using various features and technologies in Oracle JDeveloper.
This guide, however, doesn’t cover the full breadth of functionality available in
Oracle JDeveloper 10g. It focuses on the process of building J2EE applications
using various technology stacks. It doesn’t cover other JDeveloper features such
as UML modeling, code debugging, application profiling, code tuning, Swing and
J2ME clients, and database modeling.
For more information, tutorials, demos, and online help, visit the Oracle
Technology Network (OTN), at http://otn.oracle.com/products/jdev/.
Technology Scopes
One way Oracle JDeveloper simplifies the Java world to developers is by using
Technology Scopes. Java has many APIs and technologies that can be used when
building an application. However, each project usually uses only a specific set of
these technologies. In Oracle JDeveloper you can define applications templates
that contain a certain set of technologies that will be used in this specific
application. This is the “Technology Scope” for that application. Defining a
specific technology scope means that JDeveloper will only show the relevant
options in the menus and galleries for this specific project. JDeveloper comes pre-
packaged with several predefined technology scopes and you can add your own
technology scopes.
Let’s define a new Technology Scope in JDeveloper.
1. Start up Oracle JDeveloper 10g (run the file
[jdevdirectory]\jdev\bin\jdevw.exe).
2. In the menu choose File->New and in the General node choose
Application Workspace.
3. Have a look at the pre-defined Application Templates looking at their
description.
4. Click on the Manage Templates button
5. Click the New Button to create a new Application Template
6. Give a name to your template
7. While standing on the your new template click the New button again to
create a project template
8. Specify the project template and project name in the dialog and click ok.
9. In the available technologies list shuttle Java and Extensible Markup
Language (XML) to the right. Click OK.
In this section you’ll see how Oracle JDeveloper 10g provides you with a helpful
code editor as well as utilities that can save you mundane coding.
First let’s see the ways the code editor helps you write code faster.
1. Right Click on the new project you created and choose New.
2. Note how the gallery only shows you options related to your specific
Technology Scope. Change the Filter By list to show All Technologies
to see the full range of available technologies, and then change it back to
your project technologies
3. Choose Java Class from the General menu.
4. Check the 'Create Main Method' checkbox and click OK.
5. The source code of your new class is opened in the code editor.
6. Enter the following code in the main method URL u = new
URL("http://otn.oracle.com");
7. The blue line beneath URL indicate that this type of object is not known.
Place your cursor on URL and note the tool tip that suggests the correct
import that will fix the problem.
8. Press Alt+Enter to import java.net.URL.
9. There is another curly blue line under new
URL("http://otn.oracle.com") this indicates that there is a semantic
problem with this section.
10. Place the mouse cursor above this section to see a tool tip that explains
the problem. In this case it is a missing exception that we didn’t catch.
11. To define a try-catch block easily, click and highlight the line problematic
line and from the right click context menu option select surround with.
12. From the list of options choose try-catch. Note that the correct
exception is automatically caught.
13. Your cursor is now position in the catch{} section. We want to add code
here to print the error message. Oracle JDeveloper allows you to use
code templates to speed up your coding. Type sop and press ctrl+enter
and this will expend to System.out.println(); statement.
Visual Development
In this section we’ll see how Oracle JDeveloper 10g can minimize manual coding
by using a visual and declarative approach instead.
1. Right Click on the new project you created and choose New.
2. Choose JavaBeans->Bean from the gallery.
3. Accept the defaults in the Create Bean dialog.
4. Your new bean is opened in the Visual Editor.
5. Make sure that the Property Inspector window is open (ctrl+shift+I).
6. Change the layout property of the panel to XYLayout.
7. Make sure the component palette is open (ctrl+shift+p) and switch it to
show the Swing components.
8. Click on the JTextField object and place it on the panel. You can drag and
drop it to change its location and size.
9. In the property inspector change the background property of the button
to another color.
10. Click on the Source tab at the bottom of the visual editor to switch your
view of the bean to show the actual Java code. Notice the lines of code that
were generated from your visual manipulations.
11. To see both the visual and code editor at the same time use the splitter at
the top of the scroll bar to split the screen horizontally. So you can see the
Design editor on top and Source editor on the bottom. (another way to split
the screen is to right click the tab for the file you are editing and choose
Split Document).
12. Change the values of the JTextField background color in code and see it
change in the design editor and vice versa.
13. If you want to see multiple files at the same time you can drag the top tab
with the name of the file either to the bottom of the screen or to the side to
further split your code view.
Beyond Java coding, Oracle JDeveloper 10g also provides helpful features for the
development of XML based applications. In this section we’ll step through a
couple of the XML related features
1. From the menu choose File->Open.
2. Choose open the following file: [JDeveloper install
directort]\jlib\schemas.zip\xsd\XMLSchema.xsd
3. This is a XML Schema document. Oracle JDeveloper provides a visual
repersentation of the file for easier development.
4. Step through the diagram clicking to expend different nodes.
5. Note that when you click on a node or an attribute you can access their
properties in the property inspector.
6. You can split the editor to view both the Design view and the actual XML
source.
7. The structure pane on can help you navigate the XML file easily.
8. Right click on the project and choose New->XML->XML Schema
In this section we’ll design the flow of our application using the visual editor for
the struts-config.xml file. The page flow modeler provides a visual representation
of the content of this XML file.
From the page flow modeler you can directly access the source of the pages to
edit them. In this section we’ll use the HTML/JSP Visual Editor to design our
pages.
1. We’ll edit your login page using the visual HTML editor. Double click on
the /login.jsp icon to open the page in the visual editor.
2. Write some text for your page heading like “Welcome to the HR application”
and in a line below that write “Please login to the application”. Mark the first
line and using the formatting tools at the top of the editor change its type to
heading1. Mark the second line and change it to heading2.
3. In the component palette change the view to see CSS instead of HTML
objects. Click on the JDeveloper CSS to assign this CSS to the page.
4. Change the component palette view to see Struts HTML components.
5. Place the cursor under the text and from the component palette choose
form.
6. In the dialog that pops up set the action property to login.do and click ok.
7. Change the component palette view to see HTML components.
8. While the cursor is still placed inside the Struts Form, choose the Table
component to create a 2 by 2 table.
In this section we’ll add the business logic to our Controller layer that will define
the application flow. We’ll start by adding a Struts formbean, a formbean is the
way information is passed between the controller and the view layer.
1. Make sure you can see the Structure window ctrl+shift+s.
2. Right click on the Struts Config node and choose New -> Forms Beans.
3. Right click on the newly created Form Beans node and choose New ->
Form Bean.
4. While standing on the newly created Form Bean in the Property Inspector
change the name property to login.
5. Click on the type property and the browse button and navigate to choose
org.apache.struts.action.DynaActionForm .
Oracle JDeveloper lets you browse and develop database objects. To do this you’ll
need to set-up a connection to your database.
(We are using the HR sample user that you get with a default installation of the
Oracle Database. You might need to unlock the user after the default installation
using the following command:
ALTER USER "HR" IDENTIFIED BY "hr" ACCOUNT UNLOCK;
If you are encountering any problems with this step you might want to ask your
DBA for help).
1. Switch to the Connections Tab.
2. Right click the Database and choose New Database Connection . . ..
3. In the Database Connection Wizard, review the information on the Welcome
page and click Next.
4. In the Connection name field type HR.
5. Click Next.
6. On the Authentication page:
a. In the Username and Password fields, type HR.
b. Select Deploy Password.
c. Click Next.
7. On the Connection page:
a. In the Host name field, type the name (or IP address) of the computer
where the database is located. The default ‘localhost’ should work if
the database is installed on your local PC.
For the Business Services layer we want to have Java objects that can interact
with the database. We’ll use Oracle ADF Business Components for this layer.
Oracle ADF Business Components (an evolution of the BC4J framework from
previous JDeveloper versions) is a framework that manages every aspect of
interaction with your database, including tasks such as Object Relational Mapping,
Persistence, Transaction Management and Connection Pooling.
You can create Oracle ADF Business Components directly from a wizard, but will
go through a UML modeler to get a better picture of what we are doing.
2. In the Model node right click and choose New.
3. Choose Diagrams and Business Components Diagram from the gallery.
4. Rename your diagram and Click OK.
5. Make sure that you can see the connection Tab and expand the HR node
so you’ll be able to see the various Tables in this schema.
6. Drag and drop the Departments and Employees table onto the diagram
area.
7. The dialog window lets you choose what to create from the tables. If you
switch to see the All technologies instead of just the Project Technologies
Oracle ADF Business Components provide a very easy way to add simple
validation rules to objects. Lets add a validation that will make sure the value of
an employee salary is inside a specific range.
7. Double click on the Employee Entity Object in the diagram to open the
Entity Object Editor.
8. Go to the Java node, here you can tell JDeveloper to generate the Java files
for each of the components, this can be helpful when you want to override
any of the methods that the objects provide. For example you can indicate
that you want to generate the setter and getter method and then you can add
business logic to the setSalary method. Since our validation is a simple one we
can use meta-data to define it instead of code.
9. Go to the Validation node choose the Salary attribute and click the New
button to create a new validation rule
10. Choose RangeValidator from the Rules list.
11. Type a minimum value of 0 and a maximum value of 99999.
12. Type in an Error message that will be displayed in case the value is not valid
something like “Salary must be between 0 and 99,999”.
13. Click OK in the dialog and OK again to return to the diagram and save your
changes.
Rename your diagram and Click OK.
Oracle ADF Business Components is based on two layers that separates the
persistence layer (entity objects) from the data access layer (view objects). So far
we worked with the Entity object. The Entity objects are responsible for
communicating with the database directly. On top of the Entity objects Oracle
ADF Business Components provide View objects. View objects are the interfaces
that the developer will work with. This separation provides further reusability and
maintainability. An Application Module is a collection of View objects that usually
correspond to a use case of your application.
The next step for us is to create the rest of the layers to complete the Oracle
ADF Business Components layer.
Now that you finished defining the Oracle ADF Business Components layer it
would be nice to test it and see that all the definitions you made actually work.
This is easy to do using the Application Module tester. A simple Swing based
graphical user interface.
1. Right Click on AppModule and choose Test.
2. Accept all the defaults and click Connect.
3. A Java application appears, you can use it to work with your application
module. Double click on the EmpDeptFkLink1 and you’ll see a simple
master detail window.
4. Browse through the various departments. Note how the relationship is
automatically managed for you by the ADF Business Components. You will
also notice that the HireDate field is displayed according to the properties you
set before.
5. Try to update the Salary field to “-8” and you’ll notice the error message you
specify when you try to leave the field. Correct the value to a valid one.
6. Exit the application and save all your work.
In this section we developed the Business Services layer and ran a simple client
server application to test it.
In this section we’ll create a simple application that manipulates the data exposed
by the Oracle ADF Business Components business services layer. The application
will enable users to browse departments and update an employee’s details.
We’ll use Struts to control the flow between two JSP pages that bind to our
business services via the Model layer. Oracle ADF delivers a very easy way to
bind the Control and View Layers to Business Services of any type through its
Model abstraction layer. This innovative architecture is the base for JSR-227.
Departments Browser
The next step is to allow the user to choose a specific employee and edit its data.
To do this we’ll create a new data page.
1. Place a Data Page on the page flow diagram and rename it to /editEmp.
2. Double click the editEmp data page to edit its HTML and add a page
heading “Editing Departments”.
3. From the Data Control Palette choose the EmployeesView2 object and
from the Drop As list choose Input Form. Drag and Drop the Departments
object onto the HTML page.
Until now we used the drag and drop data binding from the model to the JSP only
for complete records, but this operation can also be done at the item level. For
example let’s make sure that users can’t update the department id of an employee
by replacing that text field with a display field.
4. In the page editor select the departmentId text field and delete it.
5. From the Data Control Palette expand the EmployeesView2 and click
the DepartmnetID field.
6. From the Drag and Drop As list choose Value and drag and drop it to the
empty space where the text field used to be.
Now we need to link back from the edit mode to the view page.
7. Back in the page flow diagram Create a Forward link from the
browseDepts to editEmp.
One missing part in our application is transaction management that will let the
user commit or rollback his changes. Oracle ADF makes it very easy to add such
functionality using pre-built actions. Transaction management is managed at the
Oracle ADF Business Components layer.
1. Open the /browseDepts datapage in the visual editor.
2. Click the Last button in the navigation bar right click and choose Table-
>Split Cell. Split the cell to 3 columns to create additional two empty cell.
3. In the Data Control Palette collapse the tree so only the first drill level is
visible. Expand the Operations Node.
4. Choose the Commit Operation. Drag and drop it into the empty cell you
created right next to the Last button. This will add a commit button.
We’ll build a simple master detail form. To do this we’ll create a new data page.
1. Place a Data Page in the diagram and call it /masterDetail.
2. Double click to edit the content of the masterDetail page, in the dialog that
pops up make sure to choose the masterDetail.uix option from the list.
3. The page will be opened in the UIX visual editor. Go to the Data Control
Palette and expand the model to see the DepartmentsView1 and its fields.
4. Notice that the model contains the master detail relationship and shows you
the EmployeesView2 under the departments object. Click on
EmployeesView2 and from the Drag and Drop as list choose Master
Detail Many to Many. Drag EmployeeView2 onto the middle of the page.
5. In the page flow diagram right click the deptEmpDA data action and
choose Run.
A page that let you browse the departments and employees is displayed. Let’s
explore some of the unique features that this simple ADF UIX page provides.
• ADF UIX automatically provides you with range browsing for both the
departments and employees. So you can scroll 10 records at a time, and even
jump to a specific record range.
• Select different departments on the page to view their employee’s details.
Notice that when you change a department and the new department
employees get displayed – only the employees part of the page is rendered
from scratch and not the whole HTML page – this unique ADF UIX feature
is called Partial Page Rendering.
• Click on the heading of the different columns in the tables to order the
records by the specific column.
These are only some of the features you get with the rich set of user interface
components that Oracle ADF UIX offers. The ADF UIX look and feel can be
customized to suite your needs. Let’s use another look and feel for our page.
Oracle offers a light-weight yet powerful J2EE container known as OC4J. You
can download OC4J from http://otn.oracle.com/products/oc4j. In the next
section we’ll be using OC4J 10g (9.0.4). First lets install the J2EE container.
1. Start a command line window and run the following script in it [jdev-
root]\jdev\bin\setvars.bat. This scripts sets the environment variables
needed to run your application correctly.
2. Unzip the OC4J.zip file to a directory for example c:\oc4j. From now on
we’ll refer to this directory as [oc4j-root]
3. Go to [oc4j-root]\j2ee\home and execute the command java –jar oc4j.jar –
install.
4. You’ll be prompted to insert the password for the admin user (twice).
5. Now start OC4J using the command java –jar oc4j.jar .
In order to run ADF based applications on J2EE servers you need to install the
Oracle ADF runtime libraries first. In the next section we’ll step through the
installation process on the Oracle Application Server Containers for J2EE
(OC4J)– the process is similar on other J2EE servers as well.
1. From the tools menu choose ADF Runtime Installer->Standalone OC4J.
2. In step 1 locate your OC4J root directory (C:\oc4j).
3. Step through the wizards steps and click Finish.
4. Stop and restart the OC4J for the changes to take effect.
1. In the connection tab right click the Application Server node and choose
New Application Server Connection.
2. In step 1 name the connection OC4J and select the Standalone OC4J
connection type.
3. In step 2 fill in the admin password and check the Deploy Password
option.
4. In step 3 update the Local Directory where admin.jar for OC4J is
installed to [oc4j-root]\j2ee\home.
5. Click next and test your connection.
6. Click Finish.
The standard way to deploy J2EE applications with Web interfaces is through the
use of WAR files. JDeveloper has wizard support for creating all the types of
J2EE deployment files.
1. Right click on the ViewController node in the application navigator and
choose New->Deployment profiles->WAR file.
2. Update the deployment profile name to hr and click ok.
3. In the Deployment Profile Properties wizard in the General node choose
the Specify J2EE Web Context Root radio button and write hr as the
value.
4. Accept all the other default in the profiler editor window and click ok.
5. In the application navigator right click on the hr.deploy file and choose
Deploy to -> OC4J.
6. Test your application by accessing the URL
http://127.0.0.1:8888/hr/browseDept.do
In order to run ADF based applications on J2EE servers you need to install the
Oracle ADF runtime libraries first. In the next section we’ll step through the
installation process on the JBoss server – the process is similar on other J2EE
servers as well.
1. In the connection tab right click the Application Server node and choose
New Application Server Connection.
2. In step 1 name the connection JBoss and select the JBoss 3.2.x connection
type.
3. In step 2 use the browse button to locate your deployment directory
(C:\jboss-3.2.3\server\default\deploy).
4. Click Finish.
The standard way to deploy J2EE applications with Web interfaces is through the
use of WAR files. JDeveloper has wizard support for creating all the types of
J2EE deployment files.
1. Right click on the ViewController node in the application navigator and
choose New->Deployment profiles->WAR file.
2. Update the deployment profile name to hr and click ok.
3. In the Deployment Profile Properties wizard in the General node choose
the Specify J2EE Web Context Root radio button and write hr as the
value.
4. Accept all the other default in the profiler editor window and click ok.
5. In the application navigator right click on the hr.deploy file and choose
Deploy to -> JBoss.
6. Test your application by accessing the URL
http://127.0.0.1:8080/hr/browseDept.do
1. In the application navigator right click the ViewController node and choose
Open Struts Flow Diagram
2. From the component palette add one Data Action (dataAction1), One
Data Page (dataPage1) and one Page (untitled1.jsp) to the diagram.
3. Create a Forward link from the Data Action to the Data Page.
In this section we’ll create a couple of EJB by reverse engineering the structure of
tables in the database.
1. In the Application Navigator, right-click the Applications node and choose
New Application Workspace.
2. Change the Application Name to HrApplication.
3. In the Application Templates field, choose Web Application [JSP, Struts,
EJB] from the drop down list. Click OK on the Create Application
Workspace dialog.
4. In the Application Navigator, right-click Model (under the HRApplication
node) and choose New .
5. In the New Gallery, under the Categories list, expand Business Tier, and
select Enterprise JavaBeans. In the Items list, select EJB Diagram. Click
OK.
6. Accept the default package name, but change the name of the diagram to
EJB HR Diagram.
7. In the Navigator, click the Connections tab. This will bring the Connection
Navigator to the front.
8. In the Connection Navigator, expand the nodes for Database -> HR -
>Tables. (If you are missing the HR connection, follow the database
Session Façade is the most widely used of all EJB design patterns. Session Facade
allows you to properly partition the business logic in your system to help minimize
dependencies between client and server, while forcing use cases to execute in one
network call and in one transaction. In this step you will add a local reference
from a session bean to the entity beans.
11. In the Component Palette (View | Component Palette menu option), click
Session Bean and then click inside the EJB diagram.
12. The EJB wizard welcome page appears click Next.
13. On the Name and Options page, change the EJB Name to hrApp (case
senstive). Click Next.
14. On the Class Definitions page, accept the default values and click Next.
15. On the EJB Home and Component Interfaces page select the box for
Include Local Interfaces.
16. Click Finish.
17. In the Component Palette choose EJB Local Reference.
18. In the diagram, click on the session bean (hrApp), drag to the entity
bean (Departments) and click again.
19. The local reference is displayed as a line between hrApp and Departments.
1. On the EJB diagram, click in the first empty cell of the hrApp session
bean and add a new attribute showDepartments : Collection (note case
sensitivity).
2. On the diagram, right click hrApp and choose Go to Source |
hrAppBean.java Bean Class.
3. The Code Editor opens.
4. In the Code Editor, scroll down to the getShowDepartments() method.
5. In the Code Editor, modify this method so that it returns a collection of
Department DTOs:
public Collection getShowDepartments()
{
try
{
ArrayList al = new ArrayList();
Collection col = this.getDepartmentsLocalHome().findAll();
DepartmentsLocal dept;
Iterator it = col.iterator();
while(it.hasNext())
{
DepartmentsLocalDTO deptsDTO = new
DepartmentsLocalDTO((DepartmentsLocal)it.next());
al.add(deptsDTO);
}
return al;
}
catch (FinderException e)
{
System.out.println(e.toString());
throw new javax.ejb.EJBException(e);
}
catch (NamingException e)
{
System.out.println(e.toString());
throw new javax.ejb.EJBException(e);
}
}
Now that we have the EJB set up and implementing some design patterns we
want to create a data model from it that we can use with Oracle ADF to develop
a user interface.
1. Select the hrApp bean in the application navigator right click and choose
Create Data Control.
Currently the data control knows that it's supposed to work with a collection, but
doesn't know what kind of objects that collection will consist of. We specify the
bean class by setting a new property in the hrAppLocalDataControl.xml file. We’ll
see how JDeveloper lets us edit an XML file using visual tools.
2. Select the hrAppLocalDataControl.xml file in the application navigator.
3. In the structure window click the showDepartment attribute.
4. In the property inspector change the Bean Class property to
model.DepartmentsLocalDTO.
Now the EJB functions like any other business service for our Model layer, it is
very simple to build a View layer for it now.
1. Right Click on the ViewController node in the application navigator and
choose Open Struts Page Flow Diagram.
2. From the component palette choose Data Page and click in an empty
space in the diagram. Rename the page to /dept
3. Double click on the /dept page to edit its JSP content in the visual editor
4. From the Data Control Palette select showDepartments and from the
Drop As list select Read Only Table drag and drop it on the empty page.
5. Back in the page flow diagram right click on /dept and choose Run.
One of the great things about the Java universe is the many frameworks and tools
available for Java developers. Many of these frameworks and tools are open
source and free. In addition to using Struts, Oracle JDeveloper integrates three of
the leading open source developer’s tools:
• JUnit – a Java based code testing framework
• CVS – a software configuration management tool
• Ant – a build tool
In the following section you’ll see how the integration of these frameworks and
tools inside Oracle JDeveloper provides you with full life cycle support.
Using CVS
In this section we’ll create a simple project and see how we can use the open-
source Current Version System (CVS) tool to manage the life cycle of the files in
this project. First let’s set up CVS on our machine and in JDeveloper.
Setting-up CVS
In this section we’ll create a simple money class that we’ll work with in the rest of
the open source section.
1. In the Application Navigator, right-click the Applications node and choose
New Application Workspace.
2. Change the Application Name to Money the Application Package Prefix
should be money and the Application Template should be No Template
[All Technologies].
3. Right click on the Project node in the application navigator and choose
New from the General category choose Java Class.
4. Name the class money and click ok.
5. In the editor that gets open update the class code to be:
package money.mypackage;
public class money
{
private int fAmount;
private String fCurrency;
In this section you’ll see the basic work with CVS inside JDeveloper and how you
can easily manage versions and compare them.
1. In the Application Navigator, right-click the Project and choose Import
Module.
2. In step 1 set connection name money. All the other values in the wizards
should be kept to their default so click Finish.
3. The Client node in your application navigator now has a little pearl icon
indicating that it is now checked out.
4. Expand the application navigator and double click money.java to edit its
source.
5. Add a remark to the source:
• /*this class implements the money object*/
6. Save the file and note the change in the icon for the file showing a star
indicating the file has changed.
7. Right click the money.java file in the application navigator and choose
Compare With -> Previous Revision.
8. In the compare window you can see the changes to the file clearly indicated.
9. Right click the money.java file in the application navigator and choose
Versioning -> View History. This shows you the history of the file.
10. Right click the money.java file in the application navigator and choose
Versioning -> Commit. Insert a comment saying “remark added” and click
ok. Your file was checked in to the repository.
JUnit is an open-source testing framework for Java. You use JUnit to test your
code to make sure it is functioning properly. In the following section we’ll use
JUnit to test the money class we have just created. This section is not aimed at
teaching JUnit but rather show you the way you use it from inside JDeveloper.
First we’ll create a text fixture containing variables that will be used in two tests.
In the following steps we’ll define two tests to test the add and equals method of
the money class.
7. In the Application Navigator, right-click the Project node and choose New
and choose Unit Tests(JUnit) -> Test Case.
8. In the wizard step 1 click browse to choose the money.client.money
Class.
9. Expand the money.client.money methods and check the add method to
generate test cases for them. Click Next.
10. Accept all the defaults in Step 2. Click Next.
11. In step 3 of the wizard click the Add button and add the
money.mypackage.test.TestFixture1 test fixture. Click Finish.
12. A new test case has been created for you.
13. Add the test code. Modify the testadd method:
public void testadd()
{
money expected= new money(26, "CHF");
money result= fixture1.f12CHF.add(fixture1.f14CHF);
assertTrue(expected.equals(result));
}
14. Right click and choose make to compile your test case.
Now we’ll add another test case for the equals method.
15. In the Application Navigator, right-click the Project node and choose New
and choose Unit Tests(JUnit) -> Test Case.
Once you have one or more test cases you need to create a test suite that you can
run with JUnit.
1. In the Application Navigator, right-click the Project node and choose New
and choose Unit Tests(JUnit) -> Test Suite.
2. In the wizard accept the defaults for Step 1, In step 2 click the Add button
browse the money package and add the both moneyTester1 and
moneyTester2 test cases.
3. Complete the wizard and click Finish.
4. In the Application Navigator, right-click the AllTests1.java node and
choose Run. JUnit will run your test and everything should work fine. You
can see the results of the two tests in the Test Hierarchy tab.
Now lets change something in the code and see if our test will catch the problem.
5. In the Application Navigator, double-click the money.java file to edit it.
6. Change the add method to return the following:
return new money(1+amount()+m.amount(), currency());
7. Compile your code.
Using Ant
Ant is a build tool that is used to create batch processes to manage your project.
In this section we won’t teach you Ant, but we’ll show you how to use Ant from
inside JDeveloper.
Now we’ll add a new target to our Ant file that will test our project after its
compilation.
5. Edit the build.xml file and add the following text:
<target name="unit.test" depends="compile">
<java classname="junit.textui.TestRunner"
classpathref="classpath"
dir="."
fork="true">
<arg value="-noloading"/>
<arg value="money.mypackage.test.AllTests1"/>
</java>
</target>
6. Now you can run this specific target from JDeveloper. Right click on
build.xml and choose Build Target->unit.test.
Oracle Corporation
World Headquarters
500 Oracle Parkway
Redwood Shores, CA 94065
U.S.A.
Worldwide Inquiries:
Phone: +1.650.506.7000
Fax: +1.650.506.7200
www.oracle.com