Permalink
Browse files

Initial commit.

Version 1.0.0-alpha1.
  • Loading branch information...
0 parents commit 58068de60a8addf037ee8d83bb6dcbf1e1f673f9 @dfurodet dfurodet committed Oct 27, 2011
Showing with 19,102 additions and 0 deletions.
  1. +202 −0 LICENSE
  2. +8 −0 NOTICE
  3. +61 −0 README
  4. BIN doc/mt-illustration1.odp
  5. BIN doc/mt-illustration2.odp
  6. BIN doc/mt-support.odt
  7. BIN doc/mt-support.pdf
  8. BIN doc/tutorial.odt
  9. BIN doc/tutorial.pdf
  10. +71 −0 examples/com/vmware/lmock/example/geek/Geek.java
  11. +112 −0 examples/com/vmware/lmock/example/geek/GeekDay.java
  12. +192 −0 examples/com/vmware/lmock/example/geek/GeekDayTest.java
  13. +56 −0 examples/com/vmware/lmock/example/list/StringRepository.java
  14. +83 −0 examples/com/vmware/lmock/example/list/StringRepositoryTest.java
  15. +64 −0 examples/com/vmware/lmock/example/tutorial/AppendExample.java
  16. +64 −0 examples/com/vmware/lmock/example/tutorial/ArrayExample.java
  17. +89 −0 examples/com/vmware/lmock/example/tutorial/CheckerExample.java
  18. +40 −0 examples/com/vmware/lmock/example/tutorial/ClassSpecializationExample.java
  19. +56 −0 examples/com/vmware/lmock/example/tutorial/DefaultInvocationHandlersExample.java
  20. +54 −0 examples/com/vmware/lmock/example/tutorial/MasqueradeExample.java
  21. +50 −0 examples/com/vmware/lmock/example/tutorial/PriorityExample.java
  22. +44 −0 examples/com/vmware/lmock/example/tutorial/ReferencesInDirectivesExample.java
  23. +50 −0 examples/com/vmware/lmock/example/tutorial/SimpleMTExample.java
  24. +44 −0 examples/com/vmware/lmock/example/tutorial/SimpleMTExampleWithMasquerade.java
  25. +102 −0 examples/com/vmware/lmock/example/tutorial/SimpleMockExample.java
  26. +68 −0 examples/com/vmware/lmock/example/tutorial/StubExample.java
  27. +105 −0 makefile
  28. +83 −0 src/com/vmware/lmock/checker/ByteChecker.java
  29. +266 −0 src/com/vmware/lmock/checker/CharacterChecker.java
  30. +30 −0 src/com/vmware/lmock/checker/CharacterPropertyChecker.java
  31. +42 −0 src/com/vmware/lmock/checker/Checker.java
  32. +118 −0 src/com/vmware/lmock/checker/ComparableChecker.java
  33. +83 −0 src/com/vmware/lmock/checker/DoubleChecker.java
  34. +73 −0 src/com/vmware/lmock/checker/EnumChecker.java
  35. +81 −0 src/com/vmware/lmock/checker/FloatChecker.java
  36. +83 −0 src/com/vmware/lmock/checker/IntegerChecker.java
  37. +83 −0 src/com/vmware/lmock/checker/LongChecker.java
  38. +67 −0 src/com/vmware/lmock/checker/OccurrenceChecker.java
  39. +196 −0 src/com/vmware/lmock/checker/Occurrences.java
  40. +83 −0 src/com/vmware/lmock/checker/ShortChecker.java
  41. +156 −0 src/com/vmware/lmock/checker/StringChecker.java
  42. +85 −0 src/com/vmware/lmock/checker/ThreadChecker.java
  43. +45 −0 src/com/vmware/lmock/clauses/HasActorClauses.java
  44. +15 −0 src/com/vmware/lmock/clauses/HasAppendClauses.java
  45. +23 −0 src/com/vmware/lmock/clauses/HasAppendScenarioClause.java
  46. +23 −0 src/com/vmware/lmock/clauses/HasAppendStubsClause.java
  47. +66 −0 src/com/vmware/lmock/clauses/HasArgumentSpecificationClauses.java
  48. +36 −0 src/com/vmware/lmock/clauses/HasDirectiveClauses.java
  49. +54 −0 src/com/vmware/lmock/clauses/HasExpectationSpecificationClauses.java
  50. +81 −0 src/com/vmware/lmock/clauses/HasInvocationResultSpecificationClauses.java
  51. +40 −0 src/com/vmware/lmock/clauses/HasOccurrencesSpecificationClauses.java
  52. +54 −0 src/com/vmware/lmock/clauses/HasStubSpecificationClauses.java
  53. +154 −0 src/com/vmware/lmock/clauses/InnerSchemerFactoryClauses.java
  54. +26 −0 src/com/vmware/lmock/exception/CheckerCreationException.java
  55. +26 −0 src/com/vmware/lmock/exception/CheckerException.java
  56. +23 −0 src/com/vmware/lmock/exception/EmptyRoleException.java
  57. +26 −0 src/com/vmware/lmock/exception/ExpectationCreationException.java
  58. +32 −0 src/com/vmware/lmock/exception/ExpectationError.java
  59. +26 −0 src/com/vmware/lmock/exception/IllegalClassDefinitionException.java
  60. +26 −0 src/com/vmware/lmock/exception/IllegalClauseException.java
  61. +26 −0 src/com/vmware/lmock/exception/IllegalOccurrencesDefinitionException.java
  62. +32 −0 src/com/vmware/lmock/exception/IncoherentArgumentListException.java
  63. +32 −0 src/com/vmware/lmock/exception/IncompatibleReturnValueException.java
  64. +28 −0 src/com/vmware/lmock/exception/IncompatibleThrowableException.java
  65. +26 −0 src/com/vmware/lmock/exception/InvocationCheckerCreationException.java
  66. +27 −0 src/com/vmware/lmock/exception/InvocationResultCheckerException.java
  67. +47 −0 src/com/vmware/lmock/exception/LMRuntimeException.java
  68. +26 −0 src/com/vmware/lmock/exception/MTException.java
  69. +26 −0 src/com/vmware/lmock/exception/MasqueradeException.java
  70. +24 −0 src/com/vmware/lmock/exception/MissingInvocationException.java
  71. +26 −0 src/com/vmware/lmock/exception/MockCreationException.java
  72. +28 −0 src/com/vmware/lmock/exception/MockException.java
  73. +38 −0 src/com/vmware/lmock/exception/MockReferenceException.java
  74. +26 −0 src/com/vmware/lmock/exception/SchemerException.java
  75. +49 −0 src/com/vmware/lmock/exception/ThreadNotFoundException.java
  76. +30 −0 src/com/vmware/lmock/exception/UnexpectedInvocationError.java
  77. +38 −0 src/com/vmware/lmock/exception/UnsatisfiedOccurrenceError.java
  78. +137 −0 src/com/vmware/lmock/impl/ArrayChecker.java
  79. +151 −0 src/com/vmware/lmock/impl/CheckerList.java
  80. +188 −0 src/com/vmware/lmock/impl/ClassChecker.java
  81. +89 −0 src/com/vmware/lmock/impl/Cleaner.java
  82. +152 −0 src/com/vmware/lmock/impl/ExceptionGuard.java
  83. +135 −0 src/com/vmware/lmock/impl/Expectation.java
  84. +41 −0 src/com/vmware/lmock/impl/ExpectationBuilder.java
  85. +138 −0 src/com/vmware/lmock/impl/ExpectationList.java
  86. +63 −0 src/com/vmware/lmock/impl/ExpectationListBuilder.java
  87. +203 −0 src/com/vmware/lmock/impl/ExpectationReport.java
  88. +89 −0 src/com/vmware/lmock/impl/Invocation.java
  89. +172 −0 src/com/vmware/lmock/impl/InvocationChecker.java
  90. +238 −0 src/com/vmware/lmock/impl/InvocationCheckerBuilder.java
  91. +26 −0 src/com/vmware/lmock/impl/InvocationCheckerClosureHandler.java
  92. +299 −0 src/com/vmware/lmock/impl/InvocationCheckerListBuilder.java
  93. +238 −0 src/com/vmware/lmock/impl/InvocationHooks.java
  94. +67 −0 src/com/vmware/lmock/impl/InvocationProcessor.java
  95. +197 −0 src/com/vmware/lmock/impl/InvocationResult.java
  96. +141 −0 src/com/vmware/lmock/impl/InvocationResultChecker.java
  97. +44 −0 src/com/vmware/lmock/impl/InvocationResultProvider.java
  98. +101 −0 src/com/vmware/lmock/impl/Logger.java
  99. +297 −0 src/com/vmware/lmock/impl/Mock.java
  100. +492 −0 src/com/vmware/lmock/impl/MockInvocationDispatcher.java
  101. +31 −0 src/com/vmware/lmock/impl/MockInvocationHandler.java
  102. +32 −0 src/com/vmware/lmock/impl/MockInvocationHandlerType.java
  103. +106 −0 src/com/vmware/lmock/impl/MockLinker.java
  104. +94 −0 src/com/vmware/lmock/impl/ObjectCheckerFactory.java
  105. +158 −0 src/com/vmware/lmock/impl/Scenario.java
  106. +101 −0 src/com/vmware/lmock/impl/SimpleObjectChecker.java
  107. +260 −0 src/com/vmware/lmock/impl/Story.java
  108. +92 −0 src/com/vmware/lmock/impl/StoryDefaults.java
  109. +305 −0 src/com/vmware/lmock/impl/StoryProcessor.java
  110. +98 −0 src/com/vmware/lmock/impl/StoryTrack.java
  111. +82 −0 src/com/vmware/lmock/impl/StoryTracker.java
  112. +95 −0 src/com/vmware/lmock/impl/Stub.java
  113. +41 −0 src/com/vmware/lmock/impl/StubBuilder.java
  114. +57 −0 src/com/vmware/lmock/impl/StubListBuilder.java
  115. +95 −0 src/com/vmware/lmock/impl/StubMap.java
  116. +113 −0 src/com/vmware/lmock/impl/StubProcessor.java
  117. +107 −0 src/com/vmware/lmock/impl/Stubs.java
  118. +50 −0 src/com/vmware/lmock/impl/SystemSpecifics.java
  119. +93 −0 src/com/vmware/lmock/masquerade/ArgumentSpecificationProvider.java
  120. +309 −0 src/com/vmware/lmock/masquerade/Role.java
  121. +499 −0 src/com/vmware/lmock/masquerade/Schemer.java
  122. +245 −0 src/com/vmware/lmock/masquerade/SchemerFactory.java
  123. +78 −0 src/com/vmware/lmock/masquerade/StoryManager.java
  124. +242 −0 src/com/vmware/lmock/mt/Actor.java
  125. +31 −0 src/com/vmware/lmock/mt/ActorUpdateListener.java
  126. +39 −0 src/com/vmware/lmock/mt/ActorUpdateNotification.java
  127. +131 −0 src/com/vmware/lmock/mt/MTDispatcher.java
  128. +45 −0 src/com/vmware/lmock/mt/ThreadList.java
  129. +131 −0 src/com/vmware/lmock/mt/ThreadMatcher.java
  130. +21 −0 src/com/vmware/lmock/mt/ThreadMatcherNotificationHandler.java
  131. +26 −0 src/com/vmware/lmock/trace/ActivityLogger.java
  132. +66 −0 src/com/vmware/lmock/trace/Trace.java
  133. +327 −0 tests/com/vmware/lmock/test/AdvancedMTTest.java
  134. +754 −0 tests/com/vmware/lmock/test/AnyClauseSpecificationTest.java
  135. +993 −0 tests/com/vmware/lmock/test/AnyClauseTest.java
  136. +83 −0 tests/com/vmware/lmock/test/AppendTest.java
  137. +102 −0 tests/com/vmware/lmock/test/ByteCheckerTest.java
  138. +183 −0 tests/com/vmware/lmock/test/CharacterCheckerTest.java
  139. +274 −0 tests/com/vmware/lmock/test/Dalton.java
  140. +108 −0 tests/com/vmware/lmock/test/DoubleCheckerTest.java
  141. +58 −0 tests/com/vmware/lmock/test/EnumCheckerTest.java
  142. +110 −0 tests/com/vmware/lmock/test/FloatCheckerTest.java
  143. +114 −0 tests/com/vmware/lmock/test/IntegerCheckerTest.java
  144. +379 −0 tests/com/vmware/lmock/test/InvocationHooksTest.java
  145. +97 −0 tests/com/vmware/lmock/test/LMAsserts.java
  146. +111 −0 tests/com/vmware/lmock/test/LongCheckerTest.java
  147. +269 −0 tests/com/vmware/lmock/test/MTDispatcherTest.java
  148. +379 −0 tests/com/vmware/lmock/test/MasqueradeTest.java
  149. +236 −0 tests/com/vmware/lmock/test/MatchingExpectationSpecificationTest.java
  150. +1,435 −0 tests/com/vmware/lmock/test/MatchingExpectationTest.java
  151. +234 −0 tests/com/vmware/lmock/test/MatchingStubSpecificationTest.java
  152. +191 −0 tests/com/vmware/lmock/test/Messenger.java
  153. +22 −0 tests/com/vmware/lmock/test/MessengerProcessor.java
  154. +267 −0 tests/com/vmware/lmock/test/MiscellaneousTest.java
  155. +40 −0 tests/com/vmware/lmock/test/MockTest.java
  156. +316 −0 tests/com/vmware/lmock/test/OccurencesSpecificationTest.java
Sorry, we could not display the entire diff because it was too big.
202 LICENSE
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
8 NOTICE
@@ -0,0 +1,8 @@
+Lmock - version 2.0
+Copyright (c) 2011 VMware, Inc. All Rights Reserved.
+
+This product is licensed to you under the Apache License, Version 2.0 (the
+"License"). You may not use this product except in compliance with the License.
+
+How to contact vmware: lmock@vmware.com
+
61 README
@@ -0,0 +1,61 @@
+General information about this source tree
+==========================================
+Thanks for downloading Lmock. This README file contains directives about how to play with those sources.
+
+Overall organization
+--------------------
+The main directories of this project are:
+* src: the Lmock sources
+* tests: JUnit tests of Lmock
+* doc: the project documentation
+
+The examples sub-directory contains informal examples of JUnit tests that do not necessarily pass.
+The tests are aimed to run on a basic java environment with JUnit4. For Android, you should use the converter provided by tool/send2Droid, as explained later in this document.
+
+You may edit Lmock with your favorite IDE:
+* Netbeans: please use the formatter provided in tools/nb-formatter.zip to align with the coding conventions of this project
+* Other IDEs: there is no specific support (but it works well in Eclipse)
+
+You may also use the provided makefile to rebuild Lmock.
+
+Building Lmock with make
+------------------------
+The build process assumes that you have the following in your path:
+* Standard java tools: javac, jar, javadoc
+* findbugs
+* Optionally doxygen, to create the doxygen documentation of this project (make _doxydoc)
+
+For other tools (emma test coverage and junit) we provide a copy of the proper jar files in the tools.
+
+When typing make, the following happens:
+* Build the Lmock library
+* Run the JUnit tests two times:
+** One time with no traces
+** One time with the traces, in order to help debugging, if needed
+
+The second run of tests generates a coverage report, using emma.
+
+As a result, you get:
+* A lib directory, containing the generated packages:
+** lmock-VERSION.jar: the Lmock library
+** lmock-javadoc-VERSION.jar: javadoc of the Lmock API
+** lmock-src-VERSION.jar: the source package (useful if you want to have inline documentation in IDEs such as Eclipse)
+* findbugs.xml: the report produced by findbugs
+* lmock.fbp: the findbugs configuration file
+* coverage: EMMA coverage report
+
+Exporting files to an Android project
+-------------------------------------
+You have to decide where to create that project (e.g. MYDIR).
+Invoke tools/send2Droid MYDIR to get the following result:
+* A copy of the Lmock sources in MYDIR/lmockAndroid (so that you can tweak the project with your Android configuration, including a manifest)
+* MYDIR/lmockAndroidTest: a copy of the Lmock JUnit test suite, complying with JUnit3
+
+NOTE: notice that today, two test fail in Android... In fact, it's not actually a failure: we get what we expect, but not in the way we were expecting it. The bug fix is a work in progress.
+
+Support
+-------
+For any question or support request, please send an e-mail to: lmock@vmware.com.
+
+Enjoy!
+
BIN doc/mt-illustration1.odp
Binary file not shown.
BIN doc/mt-illustration2.odp
Binary file not shown.
BIN doc/mt-support.odt
Binary file not shown.
BIN doc/mt-support.pdf
Binary file not shown.
BIN doc/tutorial.odt
Binary file not shown.
BIN doc/tutorial.pdf
Binary file not shown.
71 examples/com/vmware/lmock/example/geek/Geek.java
@@ -0,0 +1,71 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.geek;
+
+/**
+ * Interface representing a geek.
+ */
+public interface Geek {
+ /**
+ * Exception thrown when the geek cannot eat anymore.
+ */
+ public class StomachOverflowException extends Exception {
+ private static final long serialVersionUID = 3061385086334819193L;
+ }
+
+ /**
+ * Drinks coffee.
+ */
+ public void drinksCoffee();
+
+ /**
+ * Eats some cookies.
+ *
+ * @param count
+ * the number of cookies eaten
+ * @throws StomachOverflowException
+ * When the geek ate too much cookies.
+ */
+ public void eatsCookies(int count) throws StomachOverflowException;
+
+ /**
+ * @return An array containing all the stuff on the geek's desk.
+ */
+ public Object[] getsStuffOnDesk();
+
+ /**
+ * Puts a variable number of objects on the geek's desk.
+ *
+ * @param misc
+ * the varargs list of objects
+ */
+ public void putsStuffOnDesk(Object... misc);
+
+ /**
+ * Reads /. news.
+ *
+ * @param newTitles
+ * the array of news titles
+ */
+ public void readsSlashDot(String[] newTitles);
+
+ /**
+ * Chats on IRC.
+ *
+ * @return Another geek if any, so can be null.
+ *
+ */
+ public Geek chatsOnIRC();
+
+ /**
+ * Writes some code.
+ *
+ * @return The number of written lines.
+ */
+ public int writesCode();
+}
112 examples/com/vmware/lmock/example/geek/GeekDay.java
@@ -0,0 +1,112 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.geek;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.vmware.lmock.example.geek.Geek.StomachOverflowException;
+
+/**
+ * Class representing a geek day.
+ *
+ * The geek can perform morning or afternoon actions. One can know the number of
+ * code lines written so far, if the geek can eat more cookies, list the news
+ * read he read, list the stuff on the geek's desk.
+ */
+public class GeekDay {
+ private final Geek geek;
+ private int linesOfCodeWritten = 0;
+ private boolean canEatMoreCookies = true;
+ private final List<Object> stuffOnDesk = new ArrayList<Object>();
+ private final List<String> newsRead = new ArrayList<String>();
+
+ /**
+ * Constructs a GeekDay for a given geek.
+ *
+ * @param geek
+ * the geek
+ */
+ public GeekDay(Geek geek) {
+ this.geek = geek;
+ }
+
+ /**
+ * Performs the geek's morning actions.
+ *
+ * The geek drinks coffee, then writes some code. If he has written more
+ * than 200 lines of code, he drinks coffee again and attempts to eat as
+ * many cookies as lines of code written.
+ */
+ public void morning() {
+ geek.drinksCoffee();
+
+ int writesCode = geek.writesCode();
+ linesOfCodeWritten += writesCode;
+
+ if (writesCode > 200) {
+ geek.drinksCoffee();
+ if (canEatMoreCookies) {
+ try {
+ geek.eatsCookies(writesCode);
+ } catch (StomachOverflowException e) {
+ canEatMoreCookies = false;
+ }
+ }
+ }
+ }
+
+ /**
+ * Performs the geek's afternoon actions.
+ *
+ * The geek puts some stuff on his desk, reads some /. news, chats on IRC
+ * with another geek and gets the stuff back from his desk.
+ */
+ public void afternoon() {
+ String stuff1 = "Starwars mug";
+ String stuff2 = "Scala book";
+ String stuff3 = "Galaxy Tab";
+ Object stuff4 = null;
+ geek.putsStuffOnDesk(stuff1, stuff2, stuff3, stuff4);
+
+ stuffOnDesk.add(stuff1);
+ stuffOnDesk.add(stuff2);
+ stuffOnDesk.add(stuff3);
+ stuffOnDesk.add(stuff4);
+
+ String[] news = new String[2];
+ news[0] = "Monkey Island on iPad";
+ news[1] = "Quake3 on Galaxy S";
+ geek.readsSlashDot(news);
+ newsRead.add(news[0]);
+ newsRead.add(news[1]);
+
+ Geek otherGeek = geek.chatsOnIRC();
+ if (otherGeek != null) {
+ otherGeek.chatsOnIRC();
+ }
+
+ geek.getsStuffOnDesk();
+ }
+
+ public int getNumberOfCodeLinesWritten() {
+ return linesOfCodeWritten;
+ }
+
+ public boolean canEatMoreCookies() {
+ return canEatMoreCookies;
+ }
+
+ public List<Object> listStuffPutOnDesk() {
+ return new ArrayList<Object>(stuffOnDesk);
+ }
+
+ public List<String> listNewsRead() {
+ return new ArrayList<String>(newsRead);
+ }
+}
192 examples/com/vmware/lmock/example/geek/GeekDayTest.java
@@ -0,0 +1,192 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.geek;
+
+import static com.vmware.lmock.checker.Occurrences.any;
+import static com.vmware.lmock.checker.Occurrences.atLeast;
+import static com.vmware.lmock.checker.Occurrences.atMost;
+import static com.vmware.lmock.checker.Occurrences.between;
+import static com.vmware.lmock.checker.Occurrences.exactly;
+import static com.vmware.lmock.checker.Occurrences.never;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import com.vmware.lmock.example.geek.Geek.StomachOverflowException;
+import com.vmware.lmock.impl.Mock;
+import com.vmware.lmock.impl.Scenario;
+import com.vmware.lmock.impl.Story;
+
+/**
+ * An example of mocking using the Geek interface.
+ */
+public class GeekDayTest {
+ /** A mock of a geek. */
+ private final Geek geek = Mock.getObject(Geek.class);
+ /** A mock of another geek. */
+ private final Geek anotherGeek = Mock.getObject(Geek.class);
+
+ /**
+ * Very basic example of expectations.
+ */
+ @Test
+ public void lazyMorning10LinesOfCode() {
+ GeekDay geekDay = new GeekDay(geek);
+ final int linesOfCode = 10;
+
+ Story story = Story.create(new Scenario() {
+ {
+ expect(geek).drinksCoffee();
+
+ expect(geek).writesCode();
+ willReturn(linesOfCode);
+ }
+ });
+ story.begin();
+ geekDay.morning();
+ assertEquals(linesOfCode, geekDay.getNumberOfCodeLinesWritten());
+ assertTrue(geekDay.canEatMoreCookies());
+ story.end();
+ }
+
+ /**
+ * Example that shows expectations with various occurrences.
+ *
+ * @throws StomachOverflowException
+ */
+ @Test
+ public void lessLazyMorning500LinesOfCode() throws StomachOverflowException {
+ GeekDay geekDay = new GeekDay(geek);
+ final int linesOfCode = 500;
+
+ Story story = Story.create(new Scenario() {
+ {
+ expect(geek).drinksCoffee();
+ occurs(exactly(1));
+
+ expect(geek).writesCode();
+ occurs(atLeast(1));
+ willReturn(linesOfCode);
+
+ expect(geek).drinksCoffee();
+ occurs(any());
+
+ expect(geek).eatsCookies(linesOfCode);
+ occurs(between(0, 1));
+ }
+ });
+ story.begin();
+ geekDay.morning();
+ assertEquals(linesOfCode, geekDay.getNumberOfCodeLinesWritten());
+ assertTrue(geekDay.canEatMoreCookies());
+ story.end();
+ }
+
+ /**
+ * Example that shows how to expect an exception. It also shows that
+ * Expectation method calls can be chained.
+ *
+ * @throws StomachOverflowException
+ */
+ @Test
+ public void lessLazyMorning500LinesOfCodeButTooManyCookies()
+ throws StomachOverflowException {
+ GeekDay geekDay = new GeekDay(geek);
+ final int linesOfCode = 500;
+
+ Story story = Story.create(new Scenario() {
+ {
+ expect(geek).drinksCoffee();
+
+ expect(geek).writesCode();
+ occurs(atMost(1)).willReturn(linesOfCode);
+
+ expect(geek).drinksCoffee();
+
+ expect(geek).eatsCookies(linesOfCode);
+ occurs(any()).willThrow(new StomachOverflowException());
+ }
+ });
+ story.begin();
+ geekDay.morning();
+ assertEquals(linesOfCode, geekDay.getNumberOfCodeLinesWritten());
+ assertFalse(geekDay.canEatMoreCookies());
+ story.end();
+ }
+
+ /**
+ * Example that shows varargs and array arguments in an expectation. Also
+ * shows the usage of matchers for the arguments.
+ *
+ * @throws StomachOverflowException
+ */
+ @Test
+ public void lazyAfternoonPutStuffOnDeskAndReadSlashDot()
+ throws StomachOverflowException {
+ GeekDay geekDay = new GeekDay(geek);
+ Story story = Story.create(new Scenario() {
+ {
+ expect(geek).putsStuffOnDesk(with("Starwars mug"),
+ with("Scala book"), aNonNullOf(Object.class),
+ anyOf(Object.class));
+
+ expect(geek).readsSlashDot(
+ new String[]{"Monkey Island on iPad",
+ "Quake3 on Galaxy S"});
+
+ expect(geek).chatsOnIRC();
+
+ expect(geek).getsStuffOnDesk();
+ willReturn(new Object[4]);
+
+ expect(geek).writesCode();
+ occurs(never());
+ }
+ });
+ story.begin();
+ geekDay.afternoon();
+ assertEquals(4, geekDay.listStuffPutOnDesk().size());
+ assertEquals(2, geekDay.listNewsRead().size());
+ story.end();
+ }
+
+ /**
+ * Example that shows that a mock can be returned by one expectation and be
+ * invoked itself.
+ *
+ * @throws StomachOverflowException
+ */
+ @Test
+ public void lazyAfternoonChatWithAnotherGeek()
+ throws StomachOverflowException {
+ GeekDay geekDay = new GeekDay(geek);
+ Story story = Story.create(new Scenario() {
+ {
+ expect(geek).putsStuffOnDesk(anyOf(Object.class),
+ anyOf(Object.class), anyOf(Object.class),
+ anyOf(Object.class));
+
+ expect(geek).readsSlashDot(anyOf(String[].class));
+
+ expect(geek).chatsOnIRC();
+ willReturn(anotherGeek);
+
+ expect(anotherGeek).chatsOnIRC();
+
+ expect(geek).getsStuffOnDesk();
+ }
+ });
+ story.begin();
+ geekDay.afternoon();
+ assertEquals(4, geekDay.listStuffPutOnDesk().size());
+ assertEquals(2, geekDay.listNewsRead().size());
+ story.end();
+ }
+}
56 examples/com/vmware/lmock/example/list/StringRepository.java
@@ -0,0 +1,56 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.list;
+
+import java.util.List;
+
+/**
+ * A repository of strings, using lists.
+ *
+ * <p>
+ * Implements a FIFO policy to store and retrieve strings.
+ * </p>
+ */
+public class StringRepository {
+ /** List of strings currently in the repository. */
+ private final List<String> stringList;
+
+ /**
+ * Creates a new repository, using a specific list to store the strings.
+ *
+ * @param stringList
+ * the list into which this object stores and retrieves strings.
+ */
+ public StringRepository(List<String> stringList) {
+ this.stringList = stringList;
+ }
+
+ /**
+ * Extracts a string from the list, using a FIFO policy.
+ *
+ * @return The extracted string, null if the FIFO is empty.
+ */
+ public String get() {
+ if (stringList.isEmpty()) {
+ return null;
+ } else {
+ String result = stringList.remove(0);
+ return result;
+ }
+ }
+
+ /**
+ * Adds a string to the repository.
+ *
+ * @param data
+ * the new string
+ */
+ public void put(String data) {
+ stringList.add(data);
+ }
+}
83 examples/com/vmware/lmock/example/list/StringRepositoryTest.java
@@ -0,0 +1,83 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.list;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.List;
+
+import org.junit.Test;
+
+import com.vmware.lmock.impl.Mock;
+import com.vmware.lmock.impl.Scenario;
+import com.vmware.lmock.impl.Story;
+
+/**
+ * An example of mocking using the well-known java list interface.
+ */
+public class StringRepositoryTest {
+ /** A mock of the list used by the tested string repository. */
+ @SuppressWarnings("unchecked")
+ private final List<String> stringList = Mock.getObject(List.class);
+
+ /**
+ * Gets a string from an empty repository.
+ */
+ @Test
+ public void testGetFromEmptyList() {
+ StringRepository instance = new StringRepository(stringList);
+ Story story = Story.create(new Scenario() {
+ {
+ expect(stringList).isEmpty();
+ willReturn(true);
+ }
+ });
+ story.begin();
+ assertNull(instance.get());
+ story.end();
+ }
+
+ /**
+ * Puts and gets strings to/from a repository.
+ */
+ @Test
+ public void testPutAndGet() {
+ StringRepository instance = new StringRepository(stringList);
+ Story story = Story.create(new Scenario() {
+ {
+ // Allow new strings as many times as needed. Don't check the
+ // added strings, and don't check the occurrences.
+ expect(stringList).add(anyOf(String.class));
+ // The user will extract the different strings added during
+ // the test until getting an empty list.
+ expect(stringList).isEmpty();
+ willReturn(false);
+ expect(stringList).remove(0);
+ willReturn("hello");
+
+ expect(stringList).isEmpty();
+ willReturn(false);
+
+ expect(stringList).remove(0);
+ willReturn("world");
+
+ expect(stringList).isEmpty();
+ willReturn(true);
+ }
+ });
+
+ story.begin();
+ instance.put("hello");
+ instance.put("world");
+ assertEquals("hello", instance.get());
+ assertEquals("world", instance.get());
+ assertNull(instance.get());
+ story.end();
+ }
+}
64 examples/com/vmware/lmock/example/tutorial/AppendExample.java
@@ -0,0 +1,64 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.tutorial;
+
+import static com.vmware.lmock.checker.Occurrences.atLeast;
+import static org.junit.Assert.assertNull;
+
+import java.util.List;
+
+import org.junit.Test;
+
+import com.vmware.lmock.impl.Mock;
+import com.vmware.lmock.impl.Scenario;
+import com.vmware.lmock.impl.Story;
+
+/**
+ * Tutorial example: an illustration of the append clause.
+ */
+public class AppendExample {
+ @SuppressWarnings("unchecked")
+ private final List<String> list = Mock.getObject(List.class);
+ private final Scenario expectFirstElementReturnsNull = new Scenario() {
+ {
+ expect(list).get(0);
+ expect().willReturn(null).occurs(atLeast(1));
+ }
+ };
+
+ @Test
+ public void testWithAppend() {
+ Story story = Story.create(new Scenario() {
+ {
+ append(expectFirstElementReturnsNull);
+ expect(list).add("hello!");
+ occurs(1);
+ }
+ });
+
+ story.begin();
+ assertNull(list.get(0));
+ list.add("hello!");
+ story.end();
+ }
+
+ @Test
+ public void testOmitToPickFirstElementFromList() {
+ Story story = Story.create(new Scenario() {
+ {
+ append(expectFirstElementReturnsNull);
+ expect(list).add("hello!");
+ occurs(1);
+ }
+ });
+
+ story.begin();
+ list.add("hello!");
+ story.end();
+ }
+}
64 examples/com/vmware/lmock/example/tutorial/ArrayExample.java
@@ -0,0 +1,64 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.tutorial;
+
+import org.junit.Test;
+
+import com.vmware.lmock.impl.Mock;
+import com.vmware.lmock.impl.Scenario;
+import com.vmware.lmock.impl.Story;
+
+/**
+ * Tutorial example: using arrays as arguments.
+ */
+public class ArrayExample {
+ interface Reservoir {
+ void fill(String[] data);
+ }
+
+ static class Supplier {
+ private final Reservoir reservoir;
+
+ Supplier(Reservoir reservoir) {
+ this.reservoir = reservoir;
+ }
+
+ void supply(String... data) {
+ reservoir.fill(data);
+ }
+ }
+ final Reservoir reservoir = Mock.getObject(Reservoir.class);
+
+ @Test
+ public void testArrayWithConstantReference() {
+ Story story = Story.create(new Scenario() {
+ {
+ expect(reservoir).fill(new String[]{"water", "mud"});
+ }
+ });
+ story.begin();
+ new Supplier(reservoir).supply("water", "mud");
+ story.end();
+ }
+
+ @Test
+ public void testArrayWithVariableReference() {
+ final String[] reference = new String[]{"water", "mud"};
+ Story story = Story.create(new Scenario() {
+ {
+ expect(reservoir).fill(reference);
+ }
+ });
+ story.begin();
+ new Supplier(reservoir).supply("water", "mud");
+ reference[1] = "oil";
+ new Supplier(reservoir).supply("water", "oil");
+ story.end();
+
+ }
+}
89 examples/com/vmware/lmock/example/tutorial/CheckerExample.java
@@ -0,0 +1,89 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.tutorial;
+
+import org.junit.Test;
+
+import com.vmware.lmock.checker.Checker;
+import com.vmware.lmock.impl.Mock;
+import com.vmware.lmock.impl.Scenario;
+import com.vmware.lmock.impl.Story;
+
+/**
+ * Tutorial example: using checkers.
+ */
+public class CheckerExample {
+ class User {
+ private final String firstName, lastName;
+
+ User(String firstName, String lastName) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ }
+
+ String lastName() {
+ return lastName;
+ }
+
+ String firstName() {
+ return firstName;
+ }
+ }
+
+ interface UserDao {
+ void persist(User user);
+ }
+
+ class UserDaoService {
+ private final UserDao dao;
+
+ UserDaoService(UserDao dao) {
+ this.dao = dao;
+ }
+
+ void createUser(String firstName, String lastName) {
+ dao.persist(new User(firstName, lastName));
+ }
+ }
+
+ static class UserChecker implements Checker<User> {
+ private final String firstName, lastName;
+
+ UserChecker(String firstName, String lastName) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ }
+
+ @Override
+ public boolean valueIsCompatibleWith(User value) {
+ return firstName.equals(value.firstName())
+ && lastName.equals(value.lastName());
+ }
+
+ @Override
+ public Class<?> getRelatedClass() {
+ return User.class;
+ }
+ }
+
+ @Test
+ public void testUserChecker() {
+ final UserDao dao = Mock.getObject(UserDao.class);
+ Story story = Story.create(new Scenario() {
+ {
+ expect(dao).persist(with(new UserChecker("john", "doe")));
+ occurs(1);
+ }
+ });
+
+ story.begin();
+ UserDaoService instance = new UserDaoService(dao);
+ instance.createUser("john", "doe");
+ story.end();
+ }
+}
40 examples/com/vmware/lmock/example/tutorial/ClassSpecializationExample.java
@@ -0,0 +1,40 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.tutorial;
+
+import java.util.List;
+
+import org.junit.Test;
+
+import com.vmware.lmock.impl.Mock;
+import com.vmware.lmock.impl.Scenario;
+import com.vmware.lmock.impl.Story;
+
+import static com.vmware.lmock.checker.Occurrences.*;
+
+/**
+ * Tutorial example: specialization of classes with argument clauses.
+ */
+public class ClassSpecializationExample {
+ @SuppressWarnings("unchecked")
+ private final List<Object> list = Mock.getObject(List.class);
+
+ @Test
+ public void testAddWithStringsOnly() {
+ Story story = Story.create(new Scenario() {
+ {
+ expect(list).add(aNonNullOf(String.class));
+ occurs(atLeast(1));
+ }
+ });
+ story.begin();
+ list.add("hello world");
+ list.add(5);
+ story.end();
+ }
+}
56 examples/com/vmware/lmock/example/tutorial/DefaultInvocationHandlersExample.java
@@ -0,0 +1,56 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.tutorial;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import java.util.List;
+
+import org.junit.Test;
+
+import com.vmware.lmock.impl.Mock;
+import com.vmware.lmock.impl.Scenario;
+import com.vmware.lmock.impl.Story;
+import com.vmware.lmock.impl.Stubs;
+
+/**
+ * Tutorial example: overriding default handlers.
+ */
+public class DefaultInvocationHandlersExample {
+ @SuppressWarnings("unchecked")
+ final List<String> list1 = Mock.getObject(List.class);
+ @SuppressWarnings("unchecked")
+ final List<String> list2 = Mock.getObject(List.class);
+
+ @Test
+ public void testDefaultInvocationHandlerExample() {
+ // The default implementation of common methods:
+ assertFalse(list1.equals(list2));
+ assertEquals("Mock(List)$0", list1.toString());
+ assertEquals("Mock(List)$1", list2.toString());
+
+ // Overwite those methods and replay the test:
+ Story story = Story.create(new Scenario(), new Stubs() {
+ {
+ stub(list1).equals(list2);
+ willReturn(true);
+ stub(list1).toString();
+ willReturn("list#1");
+ stub(list2).toString();
+ willReturn("list#2");
+ }
+ });
+
+ story.begin();
+ assertEquals(list1, list2);
+ assertEquals("list#1", list1.toString());
+ assertEquals("list#2", list2.toString());
+ story.end();
+ }
+}
54 examples/com/vmware/lmock/example/tutorial/MasqueradeExample.java
@@ -0,0 +1,54 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.tutorial;
+
+import static com.vmware.lmock.checker.Occurrences.atLeast;
+import static com.vmware.lmock.masquerade.Schemer.aNonNullOf;
+import static com.vmware.lmock.masquerade.Schemer.begin;
+import static com.vmware.lmock.masquerade.Schemer.end;
+import static com.vmware.lmock.masquerade.Schemer.willInvoke;
+import static com.vmware.lmock.masquerade.Schemer.willReturn;
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.junit.Test;
+
+import com.vmware.lmock.impl.Mock;
+
+/**
+ * Tutorial example: use of masquerades.
+ */
+public class MasqueradeExample {
+ @Test
+ public void testAList() {
+ @SuppressWarnings("unchecked")
+ final List<String> list = Mock.getObject(List.class);
+
+ begin();
+
+ // The user may verify that the list is empty and add strings.
+ willReturn(true).when(list).isEmpty();
+ willInvoke(atLeast(1)).of(list).add(aNonNullOf(String.class));
+
+ // Run this portion of test...
+ if (list.isEmpty()) {
+ list.add("string 1");
+ list.add("string 2");
+ }
+
+ // Now the user will read back the values.
+ willInvoke(1).willReturn("string 1").when(list).get(0);
+ willInvoke(1).willReturn("string 2").when(list).get(1);
+
+ assertEquals("string 1", list.get(0));
+ assertEquals("string 2", list.get(1));
+
+ end();
+ }
+}
50 examples/com/vmware/lmock/example/tutorial/PriorityExample.java
@@ -0,0 +1,50 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.tutorial;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.junit.Test;
+
+import com.vmware.lmock.impl.Mock;
+import com.vmware.lmock.impl.Scenario;
+import com.vmware.lmock.impl.Story;
+import com.vmware.lmock.impl.Stubs;
+
+/**
+ * Tutorial example: shows how the stubs prevail on expectations.
+ */
+public class PriorityExample {
+ @Test
+ public void testStubAndExpectationPriority() {
+ @SuppressWarnings("unchecked")
+ final List<String> stringList = Mock.getObject(List.class);
+ Story story = Story.create(new Scenario() {
+ {
+ expect(stringList).indexOf(anyOf(String.class));
+ willReturn(0);
+ occurs(1);
+ }
+ }, new Stubs() {
+ {
+ stub(stringList).indexOf(aNonNullOf(String.class));
+ willReturn(1);
+ stub(stringList).indexOf("hello world");
+ willReturn(2);
+ }
+ });
+
+ story.begin();
+ assertEquals(2, stringList.indexOf("hello world"));
+ assertEquals(1, stringList.indexOf("hello again!"));
+ assertEquals(0, stringList.indexOf(null));
+ story.end();
+ }
+}
44 examples/com/vmware/lmock/example/tutorial/ReferencesInDirectivesExample.java
@@ -0,0 +1,44 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.tutorial;
+
+import static com.vmware.lmock.masquerade.Schemer.begin;
+import static com.vmware.lmock.masquerade.Schemer.end;
+import static com.vmware.lmock.masquerade.Schemer.willInvoke;
+import static com.vmware.lmock.masquerade.Schemer.willReturn;
+
+import java.util.List;
+
+import org.junit.Test;
+
+import com.vmware.lmock.impl.Mock;
+
+public class ReferencesInDirectivesExample {
+ @SuppressWarnings("unchecked")
+ private static final List<String> list1 = Mock.getObject(List.class);
+ @SuppressWarnings("unchecked")
+ private static final List<String> list2 = Mock.getObject(List.class);
+
+ @Test
+ public void testWithReference() {
+ begin();
+ willReturn("hello").when(list1).get(0);
+ willInvoke(1).of(list2).add(list1.get(0));
+
+ list2.add(list1.get(0));
+ end();
+ }
+
+ @Test
+ public void testWithCyclicReference() {
+ begin();
+ willReturn("hello").when(list1).get(0);
+ willInvoke(1).of(list1).add(list1.get(0));
+ end();
+ }
+}
50 examples/com/vmware/lmock/example/tutorial/SimpleMTExample.java
@@ -0,0 +1,50 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.tutorial;
+
+import com.vmware.lmock.impl.Scenario;
+import com.vmware.lmock.mt.Actor;
+import static com.vmware.lmock.mt.Actor.*;
+import com.vmware.lmock.impl.Story;
+import com.vmware.lmock.impl.Stubs;
+import org.junit.Test;
+
+/**
+ * An illustration of how to create a story with multiple actors.
+ */
+public class SimpleMTExample {
+ /**
+ * Creates a story with multiple actors, defining different combinations
+ * allowed by Lmock.
+ */
+ @Test
+ public void testWithMultipleThread() {
+ // TODO DEFINE ACTUAL THREADS HERE
+ Thread thread1 = new Thread();
+ Thread thread2 = new Thread();
+ Thread thread3 = new Thread();
+
+ Scenario scenario12 = new Scenario() {
+ {
+ // TODO FILL IN THE SCENARIO HERE
+ }
+ };
+ Stubs stubs23 = new Stubs() {
+ {
+ // TODO FILL IN THE STUBS HERE
+ }
+ };
+ Actor actor1 = anActorForThread(thread1).following(scenario12);
+ Actor actor2 = anActorForThread(thread2).following(scenario12).using(stubs23);
+ Actor actor3 = anActorForThread(thread3).using(stubs23);
+ Story story = Story.create(actor1, actor2, actor3);
+ story.begin();
+ // TODO IMPLEMENT THE TEST
+ story.end();
+ }
+}
44 examples/com/vmware/lmock/example/tutorial/SimpleMTExampleWithMasquerade.java
@@ -0,0 +1,44 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.tutorial;
+
+import com.vmware.lmock.masquerade.Role;
+import static com.vmware.lmock.masquerade.Schemer.*;
+import com.vmware.lmock.mt.Actor;
+import static com.vmware.lmock.mt.Actor.*;
+import org.junit.Test;
+
+/**
+ * One actor has its own role. Two other actors play the same role. The two roles share a common scenario.
+ */
+public class SimpleMTExampleWithMasquerade {
+ /**
+ * Creates a story with multiple actors, defining different combinations
+ * allowed by Lmock.
+ */
+ @Test
+ public void testWithMultipleThread() {
+ // TODO DEFINE ACTUAL THREADS HERE
+ Thread thread1 = new Thread();
+ Thread thread2 = new Thread();
+ Thread thread3 = new Thread();
+
+ Actor actor1 = anActorForThread(thread1);
+ Actor actor2 = anActorForThread(thread2);
+ Actor actor3 = anActorForThread(thread3);
+
+ Role role1 = new Role(actor1);
+ Role role2 = new Role(actor2, actor3);
+ role2.shareScenarioWith(role1);
+
+ begin(role1, role2);
+ // TODO: write the directives, using roleX.will...
+ // AND RUN THE TEST.
+ end();
+ }
+}
102 examples/com/vmware/lmock/example/tutorial/SimpleMockExample.java
@@ -0,0 +1,102 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.tutorial;
+
+import static com.vmware.lmock.checker.Occurrences.exactly;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.util.List;
+
+import org.junit.Test;
+
+import com.vmware.lmock.impl.Mock;
+import com.vmware.lmock.impl.Scenario;
+import com.vmware.lmock.impl.Story;
+
+/**
+ * Tutorial example: introducing the basic concepts.
+ */
+public class SimpleMockExample {
+ @SuppressWarnings("unchecked")
+ private final List<String> stringList = Mock.getObject(List.class);
+ Scenario scenario = new Scenario() {
+ {
+ expect(stringList).get(0);
+ willReturn(null).occurs(exactly(1));
+ expect(stringList).add("hello");
+ occurs(exactly(1));
+ }
+ };
+
+ @Test
+ public void testWillPass() {
+ Story story = Story.create(scenario);
+ story.begin();
+ if (stringList.get(0) == null) {
+ stringList.add("hello");
+ }
+ story.end();
+ }
+
+ @Test
+ public void testWillFail() {
+ Story story = Story.create(scenario);
+ story.begin();
+ stringList.add("hello");
+ story.end();
+ }
+
+ @Test
+ public void testWillPass2() {
+ Story story = Story.create(new Scenario() {
+ {
+ expect(stringList).get(0);
+ occurs(1);
+ willReturn(null);
+ expect(stringList).add("hello");
+ occurs(1);
+ expect(stringList).get(0);
+ willReturn("hello");
+ }
+ });
+ story.begin();
+ if (stringList.get(0) == null) {
+ stringList.add("hello");
+ assertEquals("hello", stringList.get(0));
+ } else {
+ fail("was not expecting data in the list...");
+ }
+ story.end();
+ }
+
+ @Test
+ public void testWillFail2() {
+ Story story = Story.create(new Scenario() {
+ {
+ expect(stringList).get(0);
+ occurs(1);
+ willReturn(null);
+ expect(stringList).add("hello");
+ occurs(1);
+ expect(stringList).get(0);
+ willReturn("hello");
+ expect(stringList).get(1);
+ willThrow(new IndexOutOfBoundsException());
+ }
+ });
+ story.begin();
+ if (stringList.get(0) == null) {
+ stringList.add("hello world");
+ assertEquals("hello", stringList.get(0));
+ } else {
+ fail("was not expecting data in the list...");
+ }
+ story.end();
+ }
+}
68 examples/com/vmware/lmock/example/tutorial/StubExample.java
@@ -0,0 +1,68 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.example.tutorial;
+
+import org.junit.Test;
+
+import com.vmware.lmock.impl.Mock;
+import com.vmware.lmock.impl.Story;
+import com.vmware.lmock.impl.Stubs;
+
+/**
+ * Tutorial example: introducing stubs.
+ */
+public class StubExample {
+ interface IntlContext {
+ String getLanguage();
+
+ String getCountry();
+
+ String getCurrency();
+
+ String getTimezone();
+
+ String getDateTimeFormat();
+
+ String getMetricSystem();
+
+ String getTemperatureUnit();
+ }
+
+ static public class TimeUtil {
+ private final IntlContext context;
+
+ public TimeUtil(IntlContext context) {
+ this.context = context;
+ }
+
+ public void displayTime() {
+ System.out.println("using intl context: " + context.getTimezone()
+ + " " + context.getDateTimeFormat());
+ // Implement the actual method, using the context.
+ }
+ }
+
+ @Test
+ public void testDisplayTime() {
+ final IntlContext context = Mock.getObject(IntlContext.class);
+ Stubs stubs = new Stubs() {
+ {
+ stub(context).getDateTimeFormat();
+ willReturn("yy/mm/dd");
+ stub(context).getTimezone();
+ willReturn("UTC+1");
+ }
+ };
+
+ Story story = Story.create(null, stubs);
+ story.begin();
+ TimeUtil tu = new TimeUtil(context);
+ tu.displayTime();
+ story.end();
+ }
+}
105 makefile
@@ -0,0 +1,105 @@
+# ##############################################################################
+## Makefile for lmock project.
+## The input directories are src (for the sources) and tests.
+## The outputs are:
+## - build/src and build/tests: the class files generated when compiling
+## sources
+## - lib: the generated jar files: lmock.jar and lmockTests.jar
+## - findbugs.xml: findbugs report
+## - lmock-javadoc: documentation of the lmock API as javadoc
+## - A jar package including the sources
+## - doxydoc: full documentation of the project
+# ##############################################################################
+
+## Version tag, please keep in sync with the version
+VERSION=1.0.0-alpha1
+
+# ------------------------------------------------------------------------------
+# List of tools used to build the application.
+# You need to have the following binaries in your path:
+# - java tools: javac, jar, javadoc
+# - doxygen
+# - findbugs
+# ------------------------------------------------------------------------------
+JAVAC=javac -encoding utf8 -target 1.6
+JAR=jar cf
+JAREXEC=jar cfm
+JAVADOC=javadoc -public -sourcepath src -d lmock-javadoc -windowtitle "lmock" -doctitle lmock
+DOXYGEN=doxygen tools/Doxyfile
+FINDBUGS=findbugs -textui -conserveSpace -xml -outputFile findbugs.xml
+
+# ------------------------------------------------------------------------------
+# INPUT AND RESULTING STRUCTURE
+# ------------------------------------------------------------------------------
+
+## Where we write the produced libraries...
+LIB_OUTPUT_DIR=lib
+lmockJar=$(LIB_OUTPUT_DIR)/lmock-$(VERSION).jar
+lmockJavadocJar=$(LIB_OUTPUT_DIR)/lmock-javadoc-$(VERSION).jar
+lmockTestsJar=$(LIB_OUTPUT_DIR)/lmockTests-$(VERSION).jar
+lmockSrcJar=../$(LIB_OUTPUT_DIR)/lmock-src-$(VERSION).jar
+
+## Where we put the built classes.
+CLASS_OUTPUT_DIR=build
+lmockClazz=$(CLASS_OUTPUT_DIR)/src
+lmockTestsClazz=$(CLASS_OUTPUT_DIR)/tests
+lmockTestManifest=$(CLASS_OUTPUT_DIR)/Manifest.txt
+
+## The JUnit library
+jUnit=tools/junit-4.9b2.jar
+
+# ------------------------------------------------------------------------------
+# SOURCES TO PROCEED
+# ------------------------------------------------------------------------------
+SOURCES=$(shell find src -name "*.java")
+TEST_SOURCES=$(shell find tests -name "*.java")
+
+# ------------------------------------------------------------------------------
+# BUILD
+# ------------------------------------------------------------------------------
+
+all: $(lmockJar) $(lmockTestsJar) _junit _findbugs _javadoc _src_package
+clean:
+ -rm -rf lmock-javadoc
+ -rm -rf doxydoc
+ -rm -rf bin
+ -rm -rf coverage
+ -rm -rf $(CLASS_OUTPUT_DIR)
+ -rm -rf $(LIB_OUTPUT_DIR)
+ -rm -f findbugs.xml
+ -rm -f lmock.fbp
+
+$(lmockJar): $(SOURCES)
+ mkdir -p $(lmockClazz)
+ mkdir -p $(LIB_OUTPUT_DIR)
+ $(JAVAC) -d $(lmockClazz) $(SOURCES)
+ $(JAR) $(lmockJar) -C $(lmockClazz) .
+
+$(lmockTestsJar): $(TEST_SOURCES) $(lmockJar)
+ mkdir -p $(lmockTestsClazz)
+ mkdir -p $(LIB_OUTPUT_DIR)
+ $(JAVAC) -classpath $(lmockJar):$(jUnit) -d $(lmockTestsClazz) $(TEST_SOURCES)
+ @echo "Main-Class: com.vmware.lmock.test.TestRunner" > $(lmockTestManifest)
+ $(JAREXEC) $(lmockTestsJar) $(lmockTestManifest) -C $(lmockTestsClazz) .
+
+_junit: $(lmockJar) $(lmockTestsJar)
+# Run once with no trace to quickly validate
+ java -classpath $(lmockJar):$(jUnit):$(lmockTestsJar) com.vmware.lmock.test.TestRunner
+# Now do the same with traces and do the test coverage
+ java -cp tools/emma.jar emmarun -cp tools/junit-4.9b2.jar:$(lmockJar):$(lmockTestsJar) -sp src -r html com.vmware.lmock.test.TestRunner -t
+
+_findbugs: $(lmockJar)
+# Create a configuration file aligned with the current version
+ @cat tools/lmock.fbp | sed s="@LMOCKJAR@"=$(lmockJar)=g > lmock.fbp
+ $(FINDBUGS) -project lmock.fbp
+
+_javadoc: $(lmockJar)
+ $(JAVADOC) $(SOURCES)
+ $(JAR) $(lmockJavadocJar) lmock-javadoc
+
+_src_package:
+ cd src && $(JAR) $(lmockSrcJar) com
+
+_doxydoc: $(lmockJar)
+ $(DOXYGEN)
+
83 src/com/vmware/lmock/checker/ByteChecker.java
@@ -0,0 +1,83 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.checker;
+
+import com.vmware.lmock.exception.CheckerCreationException;
+
+/**
+ * A checker to validate bytes within ranges of values.
+ *
+ * <p>
+ * Such a checker, created by a factory method, validates the bytes comprised
+ * within a certain range.
+ * </p>
+ */
+public final class ByteChecker extends ComparableChecker<Byte> {
+ /**
+ * Basic creator of the checker.
+ *
+ * @param min
+ * the lower bound of the allowed range (null if none)
+ * @param max
+ * the upper bound of the allowed range (null if none)
+ * @throws CheckerCreationException
+ * Incoherent range specification.
+ */
+ private ByteChecker(Byte min, Byte max) {
+ super(Byte.class, min, max);
+ }
+
+ /**
+ * Creates a byte checker to validate values greater or equal to a given byte.
+ *
+ * @param min
+ * the minimum value allowed
+ * @return A checker to test values greater or equal to the min.
+ */
+ public static ByteChecker valuesGreaterOrEqualTo(byte min) {
+ return new ByteChecker(min, null);
+ }
+ /**
+ * A checker to test values greater or equal to 0.
+ */
+ public static final ByteChecker positiveValues = valuesGreaterOrEqualTo((byte) 0);
+
+ /**
+ * Creates a byte checker to validate values lower or equal to a byte.
+ *
+ * @param max
+ * the maximum value allowed by the checker
+ * @return A checker to test values lower or equal to the max.
+ */
+ public static ByteChecker valuesLowerOrEqualTo(byte max) {
+ return new ByteChecker(null, max);
+ }
+ /**
+ * A checker to test values lower or equal to 0.
+ */
+ public static final ByteChecker negativeValues = valuesLowerOrEqualTo((byte) 0);
+
+ /**
+ * Creates a byte checker to validate a value within a range.
+ *
+ * <p>
+ * The checker validates the values within the range <code>[min,max]</code>.
+ * </p>
+ *
+ * @param min
+ * the lower bound
+ * @param max
+ * the upper bound
+ * @return The created checker.
+ * @throws CheckerCreationException
+ * Incoherent range.
+ */
+ public static ByteChecker valuesBetween(byte min, byte max) {
+ return new ByteChecker(min, max);
+ }
+}
266 src/com/vmware/lmock/checker/CharacterChecker.java
@@ -0,0 +1,266 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.checker;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.vmware.lmock.exception.CheckerCreationException;
+
+/**
+ * A checker to validate characters within ranges of values.
+ *
+ * <p>
+ * A character checker allows to:
+ * </p>
+ * <ul>
+ * <li>validate that a character is within a certain range</li>
+ * <li>or verify that a character has a specific properties (such as
+ * <code>isSpace</code>) using the constructor <code>verify</code></li>
+ * </ul>
+ *
+ * <p>
+ * This implementation only considers the most common character properties,
+ * assuming that more advanced features are worth defining a new class of
+ * checkers.
+ * </p>
+ */
+public final class CharacterChecker extends ComparableChecker<Character> {
+ /** When null, use the comparable checker, rely on the property otherwise. */
+ private final List<CharacterPropertyChecker> propertyCheckers =
+ new ArrayList<CharacterPropertyChecker>();
+
+ /**
+ * Creates a character checker to implement a comparison.
+ *
+ * @param min
+ * the lower bound of the allowed range (null if none)
+ * @param max
+ * the upper bound of the allowed range (null if none)
+ */
+ private CharacterChecker(Character min, Character max) {
+ super(Character.class, min, max);
+ }
+
+ /**
+ * Creates a character checker to verify a set of properties.
+ *
+ * @param propertyCheckers
+ * the checkers that validates the property
+ */
+ private CharacterChecker(CharacterPropertyChecker... propertyCheckers) {
+ super(Character.class, null, null);
+ for (CharacterPropertyChecker checker : propertyCheckers) {
+ this.propertyCheckers.add(checker);
+ }
+ }
+
+ /** @return <code>true</code> if the validation relies on a property. */
+ private boolean validateWithProperty() {
+ return !propertyCheckers.isEmpty();
+ }
+
+ /** Checks a value regarding the set of properties, applying a logical or. */
+ private boolean valueIsCompatibleWithProperties(Character value) {
+ for (CharacterPropertyChecker checker : propertyCheckers) {
+ if (checker.valueVerifies(value)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ @Override
+ public boolean valueIsCompatibleWith(Character value) {
+ if (validateWithProperty()) {
+ return valueIsCompatibleWithProperties(value);
+ } else {
+ return super.valueIsCompatibleWith(value);
+ }
+ }
+
+ @Override
+ public String toString() {
+ if (validateWithProperty()) {
+ StringBuilder builder = new StringBuilder("Character={");
+ for (CharacterPropertyChecker checker : propertyCheckers) {
+ builder.append(' ');
+ builder.append(checker.getDescription());
+ }
+ builder.append(" }");
+ return builder.toString();
+ } else {
+ return super.toString();
+ }
+ }
+
+ /**
+ * Creates a character checker to validate a value is lower or equal to a given character.
+ *
+ * @param min
+ * the minimum value allowed
+ * @return A checker to test values greater or equal to the min.
+ */
+ public static CharacterChecker valuesGreaterOrEqualTo(char min) {
+ return new CharacterChecker(min, null);
+ }
+
+ /**
+ * Creates a character checker to validate a value is greater or equal to a given character.
+ *
+ * @param max
+ * the maximum value allowed by the checker
+ * @return A checker to test values lower or equal to the max.
+ */
+ public static CharacterChecker valuesLowerOrEqualTo(char max) {
+ return new CharacterChecker(null, max);
+ }
+
+ /**
+ * Creates a character checker to validate a value within a range.
+ *
+ * <p>
+ * The checker validates the values within the range <code>[min,max]</code>.
+ * </p>
+ *
+ * @param min
+ * the lower bound
+ * @param max
+ * the upper bound
+ * @return The created checker.
+ * @throws CheckerCreationException
+ * Incoherent range.
+ */
+ public static CharacterChecker valuesBetween(char min, char max) {
+ return new CharacterChecker(min, max);
+ }
+
+ /**
+ * Creates a character checker verifying that values comply with a certain
+ * number of properties.
+ *
+ * <p>
+ * The properties are expressed by the pre-defined static property checkers.
+ * The method combines all those checkers together, so that an input
+ * character is allowed if and only if it verifies at least one of the
+ * defined properties (it's a <b>logical or</b>).
+ * </p>
+ *
+ * <p>
+ * <i>Note</i>: if no property is defined, the checker allows <b>all</b> the
+ * values.
+ * </p>
+ *
+ * @param properties
+ * the list of properties to verify
+ * @return The created checker.
+ */
+ public static CharacterChecker verifyOneOf(CharacterPropertyChecker... properties) {
+ return new CharacterChecker(properties);
+ }
+ /** The input value must be defined in unicode. */
+ public static final CharacterPropertyChecker isDefined =
+ new CharacterPropertyChecker() {
+ public boolean valueVerifies(char value) {
+ return Character.isDefined(value);
+ }
+
+ public String getDescription() {
+ return "isDefined";
+ }
+ };
+ /** The input value must be a digit. */
+ public static final CharacterPropertyChecker isDigit =
+ new CharacterPropertyChecker() {
+ public boolean valueVerifies(char value) {
+ return Character.isDigit(value);
+ }
+
+ public String getDescription() {
+ return "isDigit";
+ }
+ };
+ /** The input value must be an ISO control character. */
+ public static final CharacterPropertyChecker isISOControl =
+ new CharacterPropertyChecker() {
+ public boolean valueVerifies(char value) {
+ return Character.isISOControl(value);
+ }
+
+ public String getDescription() {
+ return "isISOControl";
+ }
+ };
+ /** The input value must be a letter. */
+ public static final CharacterPropertyChecker isLetter =
+ new CharacterPropertyChecker() {
+ public boolean valueVerifies(char value) {
+ return Character.isLetter(value);
+ }
+
+ public String getDescription() {
+ return "isLetter";
+ }
+ };
+ /** The input value must be a letter or a digit. */
+ public static final CharacterPropertyChecker isLetterOrDigit =
+ new CharacterPropertyChecker() {
+ public boolean valueVerifies(char value) {
+ return Character.isLetterOrDigit(value);
+ }
+
+ public String getDescription() {
+ return "isLetterOrDigit";
+ }
+ };
+ /** The input value must be lower case. */
+ public static final CharacterPropertyChecker isLowerCase =
+ new CharacterPropertyChecker() {
+ public boolean valueVerifies(char value) {
+ return Character.isLowerCase(value);
+ }
+
+ public String getDescription() {
+ return "isLowerCase";
+ }
+ };
+ /** The input value must be a Unicode space character. */
+ public static final CharacterPropertyChecker isSpaceChar =
+ new CharacterPropertyChecker() {
+ public boolean valueVerifies(char value) {
+ return Character.isSpaceChar(value);
+ }
+
+ public String getDescription() {
+ return "isSpaceChar";
+ }
+ };
+ /** The input value must be upper case. */
+ public static final CharacterPropertyChecker isUpperCase =
+ new CharacterPropertyChecker() {
+ public boolean valueVerifies(char value) {
+ return Character.isUpperCase(value);
+ }
+
+ public String getDescription() {
+ return "isUpperCase";
+ }
+ };
+ /** The input value is a Java whitespace. */
+ public static final CharacterPropertyChecker isWhitespace =
+ new CharacterPropertyChecker() {
+ public boolean valueVerifies(char value) {
+ return Character.isWhitespace(value);
+ }
+
+ public String getDescription() {
+ return "isWhitespace";
+ }
+ };
+}
30 src/com/vmware/lmock/checker/CharacterPropertyChecker.java
@@ -0,0 +1,30 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.checker;
+
+/**
+ * Checks one property of a character.
+ *
+ * <p>
+ * A character property checker is a specific unit of test of a character
+ * checker, focusing on a single property of the checked characters.
+ * </p>
+ */
+public interface CharacterPropertyChecker {
+ /**
+ * Checks the value of a character regarding a specific property.
+ *
+ * @param value
+ * the checked value
+ * @return <code>true</code> if the checked property is verified.
+ */
+ boolean valueVerifies(char value);
+
+ /** @return A string representing the checked property */
+ String getDescription();
+}
42 src/com/vmware/lmock/checker/Checker.java
@@ -0,0 +1,42 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.checker;
+
+/**
+ * Generic interface defining a validation item for an argument.
+ *
+ * <p>
+ * Checkers are called when running stories to verify that an argument provided
+ * during an invocation is compatible with an expected value.
+ * </p>
+ *
+ * @param <T>
+ * the class to which this checker is dedicated
+ */
+public interface Checker<T> {
+ /**
+ * Tells whether an argument is compatible with the expectations of this checker.
+ *
+ * @param value
+ * the argument value
+ * @return <code>true</code> if the argument is compatible.
+ */
+ public boolean valueIsCompatibleWith(T value);
+
+ /**
+ * Returns the class of values handled by this checker.
+ *
+ * <p>
+ * Since this class is specialized to a given class of objects (<code>T</code>), this method returns
+ * <code>T.class</code>.
+ * </p>
+ *
+ * @return The class handled by this checker.
+ */
+ public Class<?> getRelatedClass();
+}
118 src/com/vmware/lmock/checker/ComparableChecker.java
@@ -0,0 +1,118 @@
+/* **************************************************************************
+ * Copyright (C) 2010-2011 VMware, Inc. All rights reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0.
+ * Please see the LICENSE file to review the full text of the Apache License 2.0.
+ * You may not use this product except in compliance with the License.
+ * ************************************************************************** */
+package com.vmware.lmock.checker;
+
+import com.vmware.lmock.exception.CheckerCreationException;
+
+/**
+ * Base class representing a checker for comparable objects.
+ *
+ * <p>
+ * The use of comparable allows to define generic methods, such as lower, equal
+ * or greater than and range checking.
+ * </p>
+ *
+ * @param <T>
+ * the comparable class to which the checker is dedicated
+ */
+public class ComparableChecker<T extends Comparable<T>> implements Checker<T> {
+ /** Related comparable class. */
+ private final Class<T> clazz;
+ /** Lower bound of the allowed range. */
+ private final T min;
+ /** Upper bound of the allowed range. */
+ private final T max;
+
+ /**
+ * Checks a value against the lower bound of the allowed range.
+ *
+ * @param value
+ * the checked value
+ * @return <code>true</code> if the min value is null or the value is
+ * greater than the min.
+ */
+ private boolean valueIsGreaterOrEqualToMin(T value) {
+ return min == null || value.compareTo(min) >= 0;
+ }
+
+ /**
+ * Checks a value against the upper bound of the allowed range.
+ *
+ * @param value
+ * the checked value
+ * @return <code>true</code> if the min value is null or the value is
+ * greater than the min.
+ */
+