1 ;; jde-junit.el --- runs the junit test in the current buffer.
4 ;; Author: Paul Kinnucan
5 ;; Author: Torsten Geise <torsten.geise@freenet.de>
6 ;; Maintainer: Paul Kinnucan
7 ;; Keywords: tools, processes
9 ;; Copyright (C) 2004, 2005, 2006 Paul Kinnucan, Torsten Geise
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)
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.
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.
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
35 ;; Please send any comments, bugs, or upgrade requests to
36 ;; Torsten Geise at torsten.geise@freenet.de.
41 (defgroup jde-junit nil
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"
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
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")))
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
73 :tag "Test Class Name Tag"
75 (string :tag "Tag" :value "T")
76 (choice :tag "Tag Type"
77 (const :tag "Prefix" :value t)
78 (const :tag "Suffix" :value nil))))
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)))
90 (concat tag testee-name)
91 (concat testee-name tag))))
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)))
102 (concat "^" tag "\\(.*\\)")
106 (concat tag "\\(.*\\)" tag "$")
108 (substring tester-name (match-beginning 1) (match-end 1))))
111 (defcustom jde-junit-test-class-template
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"
119 "(progn (require 'jde-javadoc) (jde-javadoc-insert-start-block))"
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 \"*/\")"
131 "(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
132 "\" extends TestCase \" "
141 " \"* Creates a new <code>\""
142 "(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
143 "\"</code> instance.\" '>'n"
145 " \"* @param name test name\" '>'n"
149 "(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
156 "\"super(name);\"'>'n"
163 "\"* @return a <code>TestSuite</code>\" '>'n"
165 "\"public static TestSuite suite()\" '>"
172 "\"TestSuite suite = new TestSuite ();\" '>'n"
174 "\"return suite;\" '>'n"
178 "\"* Entry point \" '>'n"
180 "\"public static void main(String[] args) \""
185 "\"junit.textui.TestRunner.run(suite());\"'>'n"
190 "(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
192 "*Template for new Java class.
193 Setting this variable defines a template instantiation
194 command `jde-junit-test-class', as a side-effect."
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)
203 "Insert a generic JUnit test class buffer skeleton."))
204 (set-default sym val)))
207 (defun jde-junit-test-class ()
208 "Instantiate a test class template."
210 (jde-junit-test-class-internal))
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."
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)
232 (tempo-forward-mark)))
234 (defcustom jde-junit-add-test-to-suite-template
236 "\"suite.addTest(new \""
237 "(file-name-sans-extension (file-name-nondirectory buffer-file-name))"
238 "\"(\\\"\" (P \"Test Name: \") \"\\\") \""
245 "\"public void runTest()\""
252 "(P \"Method to call: \") \"();\"'>'n"
256 "*Template for generating a test case for suite."
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)
265 "Insert JUnit test to suite."))
266 (set-default sym val)))
270 (defun jde-junit-add-test-to-suite ()
271 "Instantiate an addTest method."
273 (jde-junit-add-test-to-suite-internal))
277 (defun jde-junit-run ()
278 "Starts junit testrunner with buffer corresponding class name."
280 (if (equal major-mode 'jde-mode)
281 (let ((vm (jde-run-get-vm))
283 (if (string= jde-junit-working-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.")))
295 (defun jde-junit-show-options ()
296 "Show the JDE JUnit Options panel."
298 (customize-apropos "jde-junit" 'groups))
300 ;; Register and initialize the customization variables defined
302 (jde-update-autoloaded-symbols)
307 ;; This code will not appear in the compiled (.elc) file
308 (put 'test-jde-junit 'regression-suite t)
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
317 ("Test jde-junit-get-tester-name function"
318 (jde-junit-get-tester-name "DynamicClassLoader")
319 "TDynamicClassLoader"
325 ;; This code will not appear in the compiled (.elc) file
326 (defun jde-junit-self-test ()
327 "Runs jde-dbs self tests."
330 (list test-jde-dbs-proc))))
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.
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]
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.
345 ;; Revision 1.4 2004/11/13 17:01:39 jslopez
346 ;; Removes control characters.
348 ;; Revision 1.3 2004/10/18 04:20:15 paulk
349 ;; Add unit test support.
352 ;; end of jde-junit.el