2008 Packt Publishing

2008 Packt Publishing

Swing Extreme Testing The Extreme Approach to Complete Java Application Testing Tim Lavers Lindsay Peters BIRMINGHAM - MUMBAI Swing Extreme Testing Copyright © 2008 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: May 2008 Production Reference: 1260508 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-847194-82-4 www.packtpub.com Cover Image by Vinayak Chittar ([email protected]) Credits Authors Project Manager Tim Lavers Abhijeet Deobhakta Lindsay Peters Project Coordinator Reviewers Lata Basantani Prabhakar Chaganti Valentin Crettaz Indexer Hemangini Bari Senior Acquisition Editor David Barnes Proofreader Angie Butcher Development Editor Ved Prakash Jha Production Coordinator Aparna Bhagat Technical Editor Himanshu Panchal Cover Work Aparna Bhagat Editorial Team Leader Mithil Kulkarni About the Authors Tim Lavers is a Senior Software Engineer at Pacific Knowledge Systems, which produces LabWizard, the gold-standard for rules-based knowledge acquisition software. In developing and maintaining LabWizard for almost ten years, Tim has worked with many Java technologies including network programming, Swing, reflection, logging, JavaHelp, Web Services, RMI, WebStart, preferences, internationalization, concurrent programming, XML, and databases as well as tools such as Ant and CruiseControl. His job also includes a healthy mix of user training, technical support, and support to marketing. In his previous job, he wrote servlets and built an image processing library. Along with his professional programming, he writes and maintains the distributed testing tool 'GrandTestAuto'. Previously he has published a JavaWorld article on RMI as well as a number of mathematical papers. Tim's hobbies include running and playing the piano. Lindsay Peters is the Chief Technical Officer for Pacific Knowledge Systems. He has 25 years experience in software management, formal analysis, algorithm development, software design, and implementation for large commercial and defense systems. Ten years ago Lindsay and his team were early adopters of Java, coupled with more rigorous design processes such as Design by Contract. He then helped transition the development team to the Extreme Programming model. Out of this exciting and successful experience grew the "Extreme Testing" approach. In the early 80's Lindsay managed a software team who were one of the first to incorporate the newly discovered simulated annealing algorithm into a commercial application, solving a previously intractable real-world problem which was the optimum assignment of radio frequencies to collocated mobile radios. Apart from software development and artificial intelligence systems, Lindsay has an interest in mathematical convexity and has helped to progress the "Happy Ending" problem. He is also involved in politics and in the last Australian Federal election stood as the Greens candidate for the seat of Bennelong. We gratefully acknowledge the support of Pacific Knowledge Systems in providing a software development environment that fostered the development of the Java Extreme Testing approach. We are also grateful to Pacific Knowledge Systems for allowing us to use code examples and screen shots from the LabWizard product. We would like to thank the technical reviewers for their many excellent suggestions. Any remaining shortcomings are, of course, our own. Thanks also to the team at Packt: David, Ved, Lata, and Himanshu. Their patience and responsiveness have made our job a lot easier and more enjoyable than it might otherwise have been. Tim would like to thank his wife, Lorien, for her continual support and encouragement with this project. As well as a morale-building role, she has helped with the wording of several passages in the book. Finally, Lindsay would like to thank his wife Kath for cheerfully picking up all the slack, and his father Harry for his continued inspiration. To our children Isobel, Hugh and Julia and Francis, Mariel, Vicente, Amparo, Bridie and Harry About the Reviewers Prabhakar Chaganti is the CTO of HelixBrain—a unique startup that provides technology services consulting and is an incubator nurturing some very cool software as service applications that are being built on the Ruby on Rails platform. His interests include Linux, Ruby, Python, Java, and Virtualization. He won the community choice award for the most innovative virtual appliance in the 2006 VMWare Ultimate Global Virtual Appliance Challenge. He is also the author of "GWT Java AJAX Programming" published in 2007. Valentin Crettaz holds a master degree in Information and Computer Science from the Swiss Federal Institute of Technology in Lausanne, Switzerland (EPFL). After he finished studying in 2000, Valentin worked as a software engineer with SRI International (Menlo Park, USA) and as a principal engineer in the Software Engineering Laboratory at EPFL. In 2002, as a good patriot, he came back to Switzerland to co-found a start-up called Condris Technologies, a company that provides IT development and consulting services and specializes in the creation of innovative next-generation software architecture solutions as well as secure wireless telecommunication infrastructures. Since 2004, Valentin serves as a senior IT consultant in one of the largest private banks in Switzerland, where he works on next-generation e-banking platforms. Starting in 2008, Valentin created Consulthys, a new venture that strongly focuses on leveraging Web 2.0 technologies in order to reduce the cultural gap between IT and business people. Valentin's main research interests include service-oriented architecture and web services, Web 2.0, and Ajax. During his spare time, he hangs out as a moderator at JavaRanch.com and JavaBlackBelt.com, two of the largest Java development communities on the web. Valentin also serves as a technical editor at Manning Publications (CT, US) and as a technical reviewer at O'Reilly & Associates (CA, US) and Packt Publishing (UK). Table of Contents Preface 1 Chapter 1: What Needs Testing? 13 An Example 14 What Classes Do We Test? 16 Test First—Always! 19 What Classes Don't We Test? 21 What Methods Need Testing? 22 What Methods Don't We Test? 24 Invoking Hidden Methods and Constructors 25 Unit Test Coverage 27 Who Should Implement the Unit Tests? 28 What About Legacy Code? 29 Where Does Integration Testing Fit In? 29 Documentation of Unit Tests 29 Testing at the Application Level 30 Who Should Implement the Function Tests? 31 Automated Test Execution 31 A Hierarchy of Tests 33 What Language Should Our Tests Be In? 34 Is it Really Possible? 34 Summary 36 Chapter 2: Basics of Unit Testing 37 A Simple Example 37 The Default Implementation 38 Test Cases 39 Design by Contract and Non-Defensive Programming 40 Test Code Example 45 Bootstrapping Our Implementation 48 Table of Contents Load Testing 49 Summary 49 Chapter 3: Infrastructure for Testing 51 Where Should the Unit Tests Go? 51 Where Should the Function and Load Tests Go? 53 Management of Test Data 54 What Do We Require of a Test Data Management System? 55 Temporary Files 57 Summary 58 Chapter 4: Cyborg—a Better Robot 59 The Design of Cyborg 59 Using the Keyboard 60 Mousing Around 63 Checking the Screen 65 Summary 66 Chapter 5: Managing and Testing User Messages 67 Some Problems with Resource Bundles 67 A Solution 69 The UserStrings Class 70 ResourcesTester 73 How ResourcesTester Works 74 Getting More from UserStrings 78 Summary 79 Chapter 6: Making Classes Testable with Interfaces 81 The LabWizard Comment Editor 81 The Wizard 83 A Test for Wizard 85 A Test for Step 86 Handlers in LabWizard 90 Summary 90 Chapter 7: Exercising UI Components in Tests 91 The LabWizard Login Screen 91 The Design of LoginScreen 94 UI Wrappers 96 The Correct Implementation of UILoginScreen 98 A Handler Implementation for Unit Testing 99 Setting Up our Tests 100 Our First Test 102 Further Tests 104 [ ii ] Table of Contents Some Implicit Tests 105 Other User Interfaces 105 Summary 105 Chapter 8: Showing, Finding, and Reading Swing Components 107 Setting Up User Interface Components in a Thread-Safe Manner 108 Finding a Component 110 Testing Whether a Message is Showing 112 Searching for Components by Name 113 Reading the State of a Component 114 Case Study: Testing Whether an Action Can Be Cancelled 115 The Official Word on Swing Threading 117 Summary 118 Chapter 9: Case Study: Testing a 'Save as' Dialog 119 The Ikon Do It 'Save as' Dialog 119 Outline of the Unit Test 121 UI Helper Methods 122 Dialogs 123 Getting the Text of a Text Field 124 Frame Disposal 125 Unit Test Infrastructure 125 The UISaveAsDialog Class 125 The ShowerThread Class 127 The

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    323 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us