Initial Commit
[packages] / xemacs-packages / jde / lisp / jde-junit.el
1 ;; jde-junit.el --- runs the junit test in the current buffer.
2 ;; $Revision: 1.1 $
3
4 ;; Author: Paul Kinnucan
5 ;; Author: Torsten Geise <torsten.geise@freenet.de>
6 ;; Maintainer: Paul Kinnucan
7 ;; Keywords: tools, processes
8
9 ;; Copyright (C) 2004, 2005, 2006 Paul Kinnucan, Torsten Geise
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This package is developed to perform junit test cases from within the 
29 ;; current buffer. Most defuns are copied from jde sources and modified to
30 ;; do the right things to JUnit. 
31 ;; This package should be best integrated to the JDEE. That means this 
32 ;; package uses the jde project files to store junit specific settings and
33 ;; so on.
34
35 ;; Please send any comments, bugs, or upgrade requests to
36 ;; Torsten Geise at torsten.geise@freenet.de.
37
38
39 (require 'jde)
40
41 (defgroup jde-junit nil
42   "JDE JUnit"
43   :group 'jde
44   :prefix "jde-junit-")
45
46 (defcustom jde-junit-working-directory ""
47   "*Path of the working directory for the test run.
48 If you specify a path, the JDE launches the test run from the
49 directory specified by the path. Otherwise the test run will be launched 
50 from the current buffer's directory"
51   :group 'jde-junit
52   :type 'file)
53
54 ;; (makunbound 'jde-junit-testrunner-type)
55 (defcustom jde-junit-testrunner-type "junit.textui.TestRunner"
56   "Defines the test runner to be used. If you specify a custom
57 test runner, enter the class name of the test runner in the
58 edit field."
59   :group 'jde-junit
60   :tag "Test Runner"
61   :type '(choice
62           (const :tag "Text UI" :value "junit.textui.TestRunner")
63           (const :tag "Swing GUI" :value "junit.swingui.TestRunner")
64           (const :tag "AWT GUI" :value "junit.awtui.TestRunner")
65           (string :tag "Custom UI")))
66
67 ;; (makunbound 'jde-junit-tester-name-tag)
68 (defcustom jde-junit-tester-name-tag (cons "T" "prefix")
69   "Specifies a tag appended or prefixed to the name of a testee class to
70 create the name of the corresponding tester class, e.g., T or Test, as
71 in TFoo or FooTest."
72   :group 'jde-junit
73   :tag "Test Class Name Tag"
74   :type '(cons
75           (string :tag "Tag" :value "T")
76           (choice :tag "Tag Type"
77            (const :tag "Prefix" :value t)
78            (const :tag "Suffix" :value nil))))
79
80
81 ;;JUnit templates
82
83 (defun jde-junit-get-tester-name (testee-name)
84   "Gets the name of a tester class from the name
85 of the testee class by appending or prefixing
86 `jde-junit-tester-name-tag'."
87   (let ((tag (car jde-junit-tester-name-tag))
88         (prefixp (cdr jde-junit-tester-name-tag)))
89   (if prefixp
90       (concat tag testee-name)
91     (concat testee-name tag))))
92
93 (defun jde-junit-get-testee-name (tester-name)
94   "Gets the name of a testee class from the name
95 of the tester class by removing prefixed or
96 affixed `jde-junit-tester-name-tag'."
97   (let ((tag (car jde-junit-tester-name-tag))
98         (prefixp (cdr jde-junit-tester-name-tag)))
99     (if prefixp
100         (progn
101           (string-match 
102            (concat "^" tag "\\(.*\\)")
103            tester-name))
104       (progn
105         (string-match 
106          (concat tag "\\(.*\\)" tag "$")
107          tester-name)))
108     (substring tester-name (match-beginning 1) (match-end 1))))
109
110
111 (defcustom jde-junit-test-class-template
112   (list
113    "(funcall jde-gen-boilerplate-function)"
114    "(jde-gen-get-package-statement)"
115    "\"import junit.framework.Test;\" '>'n"
116    "\"import junit.framework.TestCase;\" '>'n"
117    "\"import junit.framework.TestSuite;\" '>'n"
118    "'n"
119    "(progn (require 'jde-javadoc) (jde-javadoc-insert-start-block))"
120    "\" * \""
121    "\" Unit Test for class \""
122    "(jde-junit-get-testee-name (file-name-sans-extension (file-name-nondirectory buffer-file-name))) '>'n"
123    "\" \" (jde-javadoc-insert-empty-line)"
124    "\" \" (jde-javadoc-insert-empty-line)"
125    "\" * Created: \" (current-time-string) '>'n"
126    "\" \" (jde-javadoc-insert-empty-line)"
127    "\" \" (jde-javadoc-insert 'tempo-template-jde-javadoc-author-tag)"
128    "\" \" (jde-javadoc-insert 'tempo-template-jde-javadoc-version-tag)"
129    "\" \" (jde-javadoc-insert 'tempo-template-jde-javadoc-end-block \"*/\")"
130    "\"public class \""
131    "(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
132    "\" extends TestCase \" "
133     
134    "(if jde-gen-k&r "
135    "()"
136    "'>'n)"
137    "\"{\"'>'n"
138    "'n" 
139    
140    " \" /** \" '>'n"
141    " \"* Creates a new <code>\""
142    "(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
143    "\"</code> instance.\" '>'n"
144    " \"*\" '>'n" 
145    " \"* @param name test name\" '>'n"
146    " \"*/\"'>'n"
147
148    "\"public \""
149    "(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
150    "\" (String name)\""
151
152    "(if jde-gen-k&r "
153    "()"
154    "'>'n)"
155    "\"{\"'>'n"
156    "\"super(name);\"'>'n"
157
158    "\"}\"'>"
159    "'>'n"
160    "'n"
161     
162    "\"/**\" '>'n"
163    "\"* @return a <code>TestSuite</code>\" '>'n"
164    "\"*/\" '>'n"
165    "\"public static TestSuite suite()\" '>" 
166     
167    "(if jde-gen-k&r "
168    "()"
169    "'>'n)"
170    "\"{\"'>'n"
171
172    "\"TestSuite suite = new TestSuite ();\" '>'n"
173    "'>'n"
174    "\"return suite;\" '>'n"
175    "\"}\"'>'n'n"
176    
177    "\"/** \" '>'n"
178    "\"* Entry point \" '>'n"
179    "\"*/ \" '>'n"
180    "\"public static void main(String[] args) \""
181    "(if jde-gen-k&r "
182    "()"
183    "'>'n)"
184    "\"{\"'>'n"
185    "\"junit.textui.TestRunner.run(suite());\"'>'n"
186    "\"}\"'>'n"
187    
188    "\"}\">"
189    "\"// \""
190    "(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
191    "'>'n")
192   "*Template for new Java class.
193 Setting this variable defines a template instantiation
194 command `jde-junit-test-class', as a side-effect."
195   :group 'jde-junit
196   :type '(repeat string)
197   :set '(lambda (sym val)
198           (defalias 'jde-junit-test-class-internal
199             (tempo-define-template
200              "java-junit-test-class-buffer-template"
201              (jde-gen-read-template val)
202              nil
203              "Insert a generic JUnit test class buffer skeleton."))
204           (set-default sym val)))
205
206 ;;;###autoload
207 (defun jde-junit-test-class ()
208   "Instantiate a test class template."
209   (interactive)
210   (jde-junit-test-class-internal))
211
212 ;;;###autoload
213 (defun jde-junit-test-class-buffer ()
214   "Create a buffer containing a skeleton unit test class having the same name as the
215 root name of the buffer. This command prompts you to enter the file name
216 of the test class. It assumes that the file name has the form CLASSTest.java
217 where CLASS is the name of the class to be tested, e.g., MyAppTest.java. Use 
218 `jde-gen-junit-add-test-to-suite' to add tests to the test suite. Use of
219 tests generated with this template requires the JUnit test framework. For
220 more information, see http://www.junit.org."
221   (interactive)
222   (let ((tester-name
223          (jde-junit-get-tester-name 
224           (file-name-sans-extension 
225            (file-name-nondirectory buffer-file-name)))))
226     (find-file (concat tester-name ".java"))
227     (jde-junit-test-class-internal)
228     (beginning-of-buffer)
229     (search-forward "{")
230     (backward-char 1)
231     (c-indent-exp)
232     (tempo-forward-mark)))
233
234 (defcustom jde-junit-add-test-to-suite-template
235   '(
236     "\"suite.addTest(new \""
237     "(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
238     "\"(\\\"\" (P \"Test Name: \") \"\\\") \""
239     
240     "(if jde-gen-k&r "
241     "()"
242     "'>'n)"
243     "\"{\"'>'n"
244     
245     "\"public void runTest()\""
246     
247     "(if jde-gen-k&r "
248     "()"
249     "'>'n)"
250     "\"{\"'>'n"
251     
252     "(P \"Method to call: \") \"();\"'>'n"
253     "\"}\"'>'n"
254     "\"});\"'>'n"
255    )
256   "*Template for generating a test case for suite."
257   :group 'jde-junit
258   :type '(repeat string)
259   :set '(lambda (sym val)
260           (defalias 'jde-junit-add-test-to-suite-internal
261             (tempo-define-template
262              "Adding JUnit test to suit"
263              (jde-gen-read-template val)
264              nil
265              "Insert JUnit test to suite."))
266           (set-default sym val)))
267
268
269 ;;;###autoload
270 (defun jde-junit-add-test-to-suite ()
271   "Instantiate an addTest method."
272   (interactive)
273   (jde-junit-add-test-to-suite-internal))
274
275
276 ;;;###autoload
277 (defun jde-junit-run ()
278   "Starts junit testrunner with buffer corresponding class name."
279   (interactive)
280    (if (equal major-mode 'jde-mode)
281        (let ((vm (jde-run-get-vm))
282              (working-directory 
283               (if (string= jde-junit-working-directory "")
284                   default-directory
285                 (jde-normalize-path 'jde-junit-working-directory))))
286          (oset vm :main-class jde-junit-testrunner-type )
287          (jde-run-set-app-args (concat (jde-db-get-package)
288                                        (file-name-sans-extension 
289                                         (file-name-nondirectory (buffer-file-name)))))
290          (cd working-directory)
291          (jde-run-vm-launch vm))
292      (error "The jde-junit-run command works only in a Java source buffer.")))
293
294 ;;;###autoload
295 (defun jde-junit-show-options ()
296   "Show the JDE JUnit Options panel."
297   (interactive)
298   (customize-apropos "jde-junit" 'groups))
299
300 ;; Register and initialize the customization variables defined
301 ;; by this package.
302 (jde-update-autoloaded-symbols)
303
304
305
306 (eval-when-compile
307   ;; This code will not appear in the compiled (.elc) file
308   (put 'test-jde-junit 'regression-suite t)
309   (setq test-jde-junit
310    '("test-jde-junit"
311      ;; Each test in the suite is of the form:
312      ;;   ([description] probe grader)
313      ;;   DESCRIPTION - string
314      ;;   PROBE -  a sexp which runs the actual test
315      ;;   GRADER - the desired result or a sexp which determines
316      ;;   how we did
317      ("Test jde-junit-get-tester-name function"
318       (jde-junit-get-tester-name "DynamicClassLoader")
319       "TDynamicClassLoader"
320       )
321       )))
322
323
324 (eval-when-compile
325   ;; This code will not appear in the compiled (.elc) file
326   (defun jde-junit-self-test () 
327     "Runs jde-dbs self tests."
328     (interactive)
329     (apply 'regress 
330            (list test-jde-dbs-proc))))
331
332 (provide 'jde-junit)
333
334 ;; Change History
335 ;; $Log: jde-junit.el,v $
336 ;; Revision 1.1  2007-11-26 15:16:47  michaels
337 ;; Update jde to author version 2.3.5.1.
338 ;;
339 ;; Revision 1.5.2.1  2006/03/05 03:49:50  paulk
340 ;; Fix typo in jde-unit-get-testee-name. Thanks to Christophe Garion [garion@supaero.fr]
341 ;;
342 ;; Revision 1.5  2005/01/18 04:58:35  paulk
343 ;; Fix a bug in jde-junit-run command that causes a Lisp error whenever it is run.
344 ;;
345 ;; Revision 1.4  2004/11/13 17:01:39  jslopez
346 ;; Removes control characters.
347 ;;
348 ;; Revision 1.3  2004/10/18 04:20:15  paulk
349 ;; Add unit test support.
350 ;;
351
352 ;; end of jde-junit.el