www.it-ebooks.info Java™ Open Source Programming With XDoclet, JUnit, WebWork, Hibernate Joe Walnes Ara Abrahamian Mike Cannon-Brookes Pat Lightbody www.it-ebooks.info www.it-ebooks.info Java™ Open Source Programming With XDoclet, JUnit, WebWork, Hibernate Joe Walnes Ara Abrahamian Mike Cannon-Brookes Pat Lightbody www.it-ebooks.info Vice President and Executive Group Publisher: Richard Swadley Vice President and Executive Publisher: Bob Ipsen Vice President and Publisher: Joseph B Wikert Executive Editorial Director: Mary Bednarek Editorial Manager: Kathryn A Malm Executive Editor: Robert Elliott Senior Production Editor: Fred Bernardi Development Editor: Kevin Shafer Production Editor: Pamela Hanley Media Development Specialist: Kit Malone Permissions Editor: Carmen Krikorian Text Design & Composition: Wiley Composition Services Copyright © 2004 by Wiley Publishing, Inc., Indianapolis, Indiana All rights reserved Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600 Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, E-mail: permcoordinator@wiley.com Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose No warranty may be created or extended by sales representatives or written sales materials The advice and strategies contained herein may not be suitable for your situation You should consult with a professional where appropriate Neither the publisher nor author shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, or other damages For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002 Trademarks: Wiley and the Wiley Publishing logo are trademarks or registered trademarks of John Wiley & Sons, Inc and/or its affiliates in the United States and other countries Java is a trademark of Sun Microsystems, Inc All other trademarks are the property of their respective owners Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book Wiley also publishes its books in a variety of electronic formats Some content that appears in print may not be available in electronic books Library of Congress Cataloging-in-Publication Data: Java Open source programming : with Xdoclet, JUnit, WebWork, Hibernate (Java Open Source Library) / Joe Walnes [et al.] p cm ISBN 0-471-46362-0 (PAPER/WEBSITE) Java (Computer program language) Open source software I Walnes, Joe, 1978QA76.73.J38J3785 2003 005.2'762 dc22 2003020242 ISBN: 0-471-46362-0 Printed in the United States of America 10 www.it-ebooks.info Contents Acknowledgments xiii About the Authors xv Introduction xvii Part One Introduction Chapter Overview of the Book Using Open Source Technologies Understanding Design and Development Philosophies 3 Test First Less Is More Always Ask the Dumb Questions Exploring the PetSoar Project Sticking to the Basics Summary Chapter Application Overview Looking at Yet Another Pet Store? Understanding the Importance of Maintainability Understanding the Requirements of PetSoar Examining the Architecture and Technologies Looking at the Architecture Looking at Utility Libraries Using Persistence and Searching Using the Web Front End Testing Summary 5 6 7 9 11 11 12 12 14 14 15 16 16 iii www.it-ebooks.info iv Contents Part Two Building Your Open Source Toolbox 17 Chapter Unit Testing with JUnit Types of Testing Using JUnit 19 19 20 Features of JUnit Writing a Unit Test Running a Unit Test Running Multiple Tests Everything You Need to Know about JUnit Assertion Methods Exception Handling Test Suites Test Runners Setting Up and Tearing Down the Environment Extensions Chapter Summary 38 Testing Object Interactions with Mocks Testing Object Interactions 39 39 Exploring Some Pitfalls of Testing State Too Many Dependencies Too Much Exposure Too Much State to Manage Too Hard to Test Exploring the Alternative: Testing Interactions Using Mock Objects 39 40 40 40 41 41 42 Example Scenario Understanding the Role of a Mock Object Understanding the Mock Objects Library Using Dynamic Mocks Creating Mocks Substituting Objects Defining the Expectations Understanding Argument Constraints Verifying Expectations Setting Up Return Values Chapter 21 21 23 25 28 28 29 30 30 34 37 43 44 45 46 47 47 48 48 49 50 Summary 51 Storing Objects with Hibernate Understanding the Complexities of Persistence Persisting Objects with Hibernate 53 53 54 Creating the Persistent Classes Mapping the Classes to a Database www.it-ebooks.info 55 57 58 58 59 59 60 Contents Configuring Hibernate Obtaining a Session Storing Objects in the Database Retrieving Objects from the Database Querying Persistent Objects Persisting Relationships Between Objects Persisting Hierarchies of Objects Understanding the Hibernate Toolset Comparing Hibernate with Competing Technologies Hibernate vs EJB Hibernate vs JDO Hibernate vs DAO Frameworks Chapter 60 63 64 67 68 70 76 79 79 79 81 81 Summary 81 Model View Controller with WebWork Understanding Model View Controller (MVC) 83 83 Examining the Model Layer Examining the View Layer Examining the Controller Layer Tying It All Together Looking at Reasons to Use MVC Understanding MVC, WebWork, and XWork Exploring XWork Exploring WebWork Taking an In-depth Look at Actions A Simple XWork Example Configuring XWork Structuring Your Actions (Action Composition) Calling an Action from XWork Using Parameters and the ActionContext Applying Newton’s Third Law of Physics Understanding XWork Results and Action Chaining Examining WebWork Results and the Servlet Environment Configuring WebWork Understanding the Role of the Dispatcher Namespaces Exploring Example Views in JSP and Velocity HelloWorld in JSP JSP Tags Looking at Component-Based Web Development Themes Writing Your Own Component One Small Problem Expressing Yourself Using Basic Expressions Properties Method Calls Static Fields and Method Calls www.it-ebooks.info 84 85 85 85 86 87 87 88 89 90 91 91 93 94 95 96 96 98 99 100 101 101 102 104 107 108 108 109 110 110 111 111 v vi Contents Using Advanced Expressions Dealing with Collections Constructors Context Variables and the Root Variable Understanding the ValueStack Accessing Stack Elements Examples Using the JSP Tags Exploring Type Conversion Digging into a Date Example Specifying Default Conversion Rules Specifying New Conversion Rules Separating Concerns with Interceptors Looking at Configuration and Interceptor Stacks Using LoggingInterceptor Building Your Own Interceptor Validation — A Powerful Interceptor Exploring an Example without XWork Validation Framework Exploring an Example with XWork Validation Framework Using Built-In and Custom Validators Using the Expression Validator Chapter 115 115 117 117 118 118 119 120 120 120 122 123 125 Summary 125 Simplifying Layout with SiteMesh Identifying Problems with Layout Using the Object-Oriented Solution 127 127 132 Decorator Design Pattern Composite Design Pattern Combining the Patterns Implementing the Solution with SiteMesh SiteMesh Fundamentals Creating a Decorator Composing Pages Exploring SiteMesh Getting to the Content Mapping Decorators Using Tips and Tricks Group Decorators Together Don’t Be Afraid to Include CSS Is Your Friend Minimize HTML Separate Your Concerns Chapter 111 112 113 113 113 114 114 133 134 135 137 138 140 142 145 145 148 149 150 150 150 151 151 Summary 151 Adding Search Capabilities with Lucene Understanding the Complexities of Searching Introducing Lucene 153 153 154 Understanding the Elements of Lucene Indexing a Document www.it-ebooks.info 154 155 Contents Searching Documents Reindexing and Removing an Indexed Document Using Advanced Searching Customizing the Tokenization Process Chapter 159 161 161 162 Summary 164 Generating Configuration Files with XDoclet Introducing XDoclet Understanding Attribute Oriented Programming with XDoclet 165 165 Understanding the Syntax of Attributes Running XDoclet Using Advanced Hibernate OR Mapping with XDoclet Using XDoclet for Generating More Sophisticated Artifacts Understanding XDoclet Tasks and Subtasks EJBDoclet WebDoclet JMXDoclet JDODoclet HibernateDoclet Using XDoclet Effectively Summary 166 168 170 173 175 179 179 180 180 180 181 181 183 Chapter 10 Communication and Tools Exploring PetSoar Development Managing Imperfect Communication Communicating in Every Way Using Source Communication Using Communication as a Learning Tool Exploring Our Toolbox Source Configuration Management — CVS Knowledge Management — Wiki Mailing List — Majordomo Real-time Discussion — IRC and Instant Messaging IDEs — IDEA and Eclipse Issue Tracking and Task Management — JIRA Using Continuous Integration Tying the Tools Together Summary Chapter 11 Time-Saving Tools Understanding Utility Components Understanding OSCache Introducing SampleNews.com Using the Loop Test Exploring the OSCache Tag Library Understanding OSCache Concepts Cache Key Scope Duration www.it-ebooks.info 185 185 188 188 189 190 191 191 191 192 192 193 193 193 195 195 197 197 198 198 199 201 202 202 202 203 vii .. .Java? ?? Open Source Programming With XDoclet, JUnit, WebWork, Hibernate Joe Walnes Ara Abrahamian Mike Cannon-Brookes Pat Lightbody www.it-ebooks.info www.it-ebooks.info Java? ?? Open Source Programming. .. Cataloging-in-Publication Data: Java Open source programming : with Xdoclet, JUnit, WebWork, Hibernate (Java Open Source Library) / Joe Walnes [et al.] p cm ISBN 0-471-46362-0 (PAPER/WEBSITE) Java (Computer... Persisting Hierarchies of Objects Understanding the Hibernate Toolset Comparing Hibernate with Competing Technologies Hibernate vs EJB Hibernate vs JDO Hibernate vs DAO Frameworks Chapter 60 63 64 67