449156 FM.qxd 6/24/03 10:12 AM Page i Jakarta Pitfalls Time-Saving Solutions for Struts, Ant, JUnit, and Cactus Bill Dudney Jonathan Lehr 449156 FM.qxd 6/24/03 10:12 AM Page ii Executive Publisher: Robert Ipsen Vice President and Publisher: Joe Wikert Executive Editor: Robert Elliott Assistant Development Editor: Eileen Bien Calabro Editorial Manager: Kathryn A. Malm Senior Production Editor: Angela Smith Text Design & Composition: Wiley Composition Services This book is printed on acid-free paper. ∞ Copyright © 2003 by Bill Dudney and Jonathan Lehr. All rights reserved. Published by Wiley Publishing, Inc., Indianapolis, Indiana 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 Rose- wood Drive, Danvers, MA01923, (978) 750-8400, fax (978) 646-8700. Requests to the Pub- lisher 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: [email protected]. 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, inci- dental, 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, the Wiley Publishing logo and related trade dress are trademarks or registered trademarks of Wiley Publishing, Inc., in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or ven- dor 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: ISBN: 0-471-44915-6 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 449156 FM.qxd 6/24/03 10:12 AM Page iii For Sarah —BD For my wife, Kathryn —JL 449156 FM.qxd 6/24/03 10:12 AM Page iv 449156 FM.qxd 6/24/03 10:12 AM Page v Contents Acknowledgments ix About the Authors xi Introduction xiii Chapter1 Testing: Cactus and JUnit 1 Pitfall 1.1: No Assert 4 Example 6 Solving Pitfall 1.1: Assert the Intent 9 Step-by-Step 9 Example 10 Example 2: Cactus 15 Pitfall 1.2: Unreasonable Assert 20 Example 21 Solving Pitfall 1.2: Assert the Intent 23 Pitfall 1.3: Console-Based Testing 24 Example 25 Solving Pitfall 1.3: System.out Becomes Assert 28 Step-by-Step 29 Example 29 Example 2: Cactus 31 Pitfall 1.4 Unfocused Test Method 34 Example 35 Solving Pitfall 1.4: Keep It Simple 38 Step-by-Step 38 Example 39 v 449156 FM.qxd 6/24/03 10:12 AM Page vi vi Contents Pitfall 1.5: Failure to Isolate Each Test 45 Example 46 Solving Pitfall 1.5: Use setUp and tearDown and Introduce Test Decorators 50 Step-by-Step 51 Example 52 Example 2: Introduce Test Decorators 55 Pitfall 1.6: Failure to Isolate Subject 58 Example 59 Solving Pitfall 1.6: Introduce Mock Objects 62 Step-by-Step 62 Example 62 Chapter2 Struts ActionForms 67 Pitfall 2.1: Copy/Paste Formatting 70 Example 72 Solving Pitfall 2.1: Consolidate and Generalize Formatting Code 74 Step-by-Step 75 Example 76 Pitfall 2.2: Copy/Paste Conversion 96 Example 98 Solving Pitfall 2.2: Consolidate and Generalize Bean Population Code 101 Step-by-Step 101 Example 102 Pitfall 2.3: Copy/Paste Validation 119 Example 121 Solving Pitfall 2.3: Consolidate and Generalize Validation Code 123 Step-by-Step 126 Example 127 Chapter3 Struts Actions 147 Pitfall 3.1: Business-Tier Code in Actions 149 Example 151 Solving Pitfall 3.1: Move Business-Tier Code to BusinessDelegate 157 Step-by-Step 158 Example 159 Pitfall 3.2: Copy/Paste Code in Actions 172 Example 172 Solving Pitfall 3.2: Move Common Code to Base Class 173 Step-by-Step 174 Example 174 449156 FM.qxd 6/24/03 10:12 AM Page vii Contents vii Pitfall 3.3: Accessing ActionForms in the Session 182 Example 183 Solving Pitfall 3.3: Add ActionForm Locator Method to Base Class 187 Step-by-Step 188 Example 188 Pitfall 3.4: Overloaded ActionMappings 192 Example 192 Solving Pitfall 3.4: Create Separate ActionMappings for Navigation and Form Submission 195 Step-by-Step 195 Example 195 Chapter4 Struts TagLibs and JSPs 197 Pitfall 4.1: Hard-Coded Strings in JSPs 199 Example 200 Solving Pitfall 4.1: Move Common Strings to Resource Bundles 201 Step-by-Step 201 Example 201 Pitfall 4.2 Hard-Coded Keys in JSPs 203 Example 203 Solving Pitfall 4.2:Replace Hard-Coded Keys with Constants 204 Step-by-Step 205 Example 205 Pitfall 4.3: Not Using Struts Tags for Error Messaging 209 Example 210 Solving Pitfall 4.3: Replace Custom Messaging with Struts Messaging 210 Step-by-Step 211 Example 211 Pitfall 4.4: Calculating Derived Values in JSPs 214 Example 214 Solving Pitfall 4.4: Move Calculations to Value Object 215 Step-by-Step 216 Example 216 Pitfall 4.5: Performing Business Logic in JSPs 218 Example 219 Solving Pitfall 4.5: Move Business Logic to a Helper Class 220 Step-by-Step 221 Example 221 Pitfall 4.6: Hard-Coded Options in HTMLSelect Lists 224 Example 224 Solving Pitfall 4.6: Move Options Values to a Helper Class 225 Step-by-Step 225 Example 226 449156 FM.qxd 6/24/03 10:12 AM Page viii viii Contents Pitfall 4.7: Not Checking for Duplicate Form Submissions 229 Example 229 Solving Pitfall 4.7: Add Tokens to Generated JSP 230 Step-by-Step 230 Example 230 Chapter5 Ant 233 Pitfall 5.1: Copy-and-Paste Reuse 235 Example 235 Solving Pitfall 5.1: Introduce Antcall 237 Step-by-Step 237 Example 238 Pitfall 5.2: No Distinction between Different Types of Builds 243 Example 244 Solving Pitfall 5.2: Introduce Properties File 245 Step-by-Step 245 Example 246 Pitfall 5.3: Building Subprojects 249 Example 249 Solving Pitfall 5.3: Centralize the Build 251 Step-by-Step 252 Example 253 Pitfall 5.4: No Logging from Custom Tasks 257 Example 257 Solving Pitfall 5.4: Add Logging 259 Step-by-Step 259 Example 260 Appendix A Pitfall Catalog 263 AppendixB References 273 AppendixC What’s on the Web Site 277 Index 279 449156 FM.qxd 6/24/03 10:12 AM Page ix Acknowledgments I would like to thank first and foremost Christ, for all He has done in my life to teach me to be more than I was and to inspire me to be more than I am. I would also like to thank my wonderful wife Sarah, without her sup- port and love I’d be lost. And I’d also like to thank my great kids that keep life interesting. Andrew, Isaac, Anna and Sophia you are the definition of joy. I’d also like to thank my mom for always making me look the word up in the dictionary even though I complained enough to deserve to be sent to my room. I’d also like to thank Jon Crater and Bill Willis for all their great feedback on the content of this book. It’s a better book because of them. I would also like to thank my co-workers Chris Noe and Sridhar Valavala for teaching me so much, and for listening to my endless Monty Python quotes. My hovercraft is indeed full of eels. And finally I’d like to thank Eileen Bien Calabro for all her hard work on turning my gibberish into English and helping me to deliver a better book. I hope you learn as much from reading this book as I did in writing it. —Bill Dudney Writing this book has truly been an adventure for me, and I am grateful to my co-author, Bill Dudney, for inviting me to participate. As is the case I suppose with most technical books, this one is the work of many hands, and I am indebted to Bill Willis, Jon Crater, and Eileen Calabro for their invaluable assistance. For the past year and a half, I have had the very good fortune of working with a wonderful team of developers who have shared many insights that helped deepen my understanding of the Struts framework and the possi- bilities of web applications in general. In particular, I owe much to Carl ix
Description: