Source Code Cross Referenced for GroovyTestCase.java in  » Scripting » groovy-1.0 » groovy » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » groovy 1.0 » groovy.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         $Id: GroovyTestCase.java 4201 2006-11-05 10:23:50Z paulk $
003:
004:         Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006:         Redistribution and use of this software and associated documentation
007:         ("Software"), with or without modification, are permitted provided
008:         that the following conditions are met:
009:
010:         1. Redistributions of source code must retain copyright
011:            statements and notices.  Redistributions must also contain a
012:            copy of this document.
013:
014:         2. Redistributions in binary form must reproduce the
015:            above copyright notice, this list of conditions and the
016:            following disclaimer in the documentation and/or other
017:            materials provided with the distribution.
018:
019:         3. The name "groovy" must not be used to endorse or promote
020:            products derived from this Software without prior written
021:            permission of The Codehaus.  For written permission,
022:            please contact info@codehaus.org.
023:
024:         4. Products derived from this Software may not be called "groovy"
025:            nor may "groovy" appear in their names without prior written
026:            permission of The Codehaus. "groovy" is a registered
027:            trademark of The Codehaus.
028:
029:         5. Due credit should be given to The Codehaus -
030:            http://groovy.codehaus.org/
031:
032:         THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033:         ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034:         NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035:         FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
036:         THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037:         INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038:         (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039:         SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040:         HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041:         STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042:         ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043:         OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045:         */
046:        package groovy.util;
047:
048:        import groovy.lang.Closure;
049:        import groovy.lang.GroovyRuntimeException;
050:        import groovy.lang.GroovyShell;
051:
052:        import java.util.logging.Logger;
053:        import java.lang.reflect.Method;
054:        import java.lang.reflect.Modifier;
055:
056:        import junit.framework.TestCase;
057:
058:        import org.codehaus.groovy.runtime.InvokerHelper;
059:
060:        /**
061:         * A default JUnit TestCase in Groovy. This provides a number of helper methods
062:         * plus avoids the JUnit restriction of requiring all test* methods to be void
063:         * return type.
064:         *
065:         * @author <a href="mailto:bob@werken.com">bob mcwhirter</a>
066:         * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
067:         * @author Dierk Koenig (the notYetImplemented feature, changes to shouldFail)
068:         * @version $Revision: 4201 $
069:         */
070:        public class GroovyTestCase extends TestCase {
071:
072:            protected static Logger log = Logger.getLogger(GroovyTestCase.class
073:                    .getName());
074:            private static int counter;
075:            private boolean useAgileDoxNaming = false;
076:
077:            public GroovyTestCase() {
078:            }
079:
080:            /**
081:             * Overload the getName() method to make the test cases look more like AgileDox
082:             * (thanks to Joe Walnes for this tip!)
083:             */
084:            public String getName() {
085:                if (useAgileDoxNaming) {
086:                    return super .getName().substring(4).replaceAll("([A-Z])",
087:                            " $1").toLowerCase();
088:                } else {
089:                    return super .getName();
090:                }
091:            }
092:
093:            public String getMethodName() {
094:                return super .getName();
095:            }
096:
097:            /**
098:             * Asserts that the arrays are equivalent and contain the same values
099:             *
100:             * @param expected
101:             * @param value
102:             */
103:            protected void assertArrayEquals(Object[] expected, Object[] value) {
104:                String message = "expected array: "
105:                        + InvokerHelper.toString(expected) + " value array: "
106:                        + InvokerHelper.toString(value);
107:                assertNotNull(message + ": expected should not be null",
108:                        expected);
109:                assertNotNull(message + ": value should not be null", value);
110:                assertEquals(message, expected.length, value.length);
111:                for (int i = 0, size = expected.length; i < size; i++) {
112:                    assertEquals("value[" + i + "] when " + message,
113:                            expected[i], value[i]);
114:                }
115:            }
116:
117:            /**
118:             * Asserts that the array of characters has a given length
119:             *
120:             * @param length expected length
121:             * @param array the array
122:             */
123:            protected void assertLength(int length, char[] array) {
124:                assertEquals(length, array.length);
125:            }
126:
127:            /**
128:             * Asserts that the array of ints has a given length
129:             *
130:             * @param length expected length
131:             * @param array the array
132:             */
133:            protected void assertLength(int length, int[] array) {
134:                assertEquals(length, array.length);
135:            }
136:
137:            /**
138:             * Asserts that the array of objects has a given length
139:             *
140:             * @param length expected length
141:             * @param array the array
142:             */
143:            protected void assertLength(int length, Object[] array) {
144:                assertEquals(length, array.length);
145:            }
146:
147:            /**
148:             * Asserts that the array of characters contains a given char
149:             *
150:             * @param expected expected character to be found
151:             * @param array the array
152:             */
153:            protected void assertContains(char expected, char[] array) {
154:                for (int i = 0; i < array.length; ++i) {
155:                    if (array[i] == expected) {
156:                        return;
157:                    }
158:                }
159:
160:                StringBuffer message = new StringBuffer();
161:
162:                message.append(expected).append(" not in {");
163:
164:                for (int i = 0; i < array.length; ++i) {
165:                    message.append("'").append(array[i]).append("'");
166:
167:                    if (i < (array.length - 1)) {
168:                        message.append(", ");
169:                    }
170:                }
171:
172:                message.append(" }");
173:
174:                fail(message.toString());
175:            }
176:
177:            /**
178:             * Asserts that the array of ints contains a given int
179:             *
180:             * @param expected expected int
181:             * @param array the array
182:             */
183:            protected void assertContains(int expected, int[] array) {
184:                for (int i = 0; i < array.length; ++i) {
185:                    if (array[i] == expected) {
186:                        return;
187:                    }
188:                }
189:
190:                StringBuffer message = new StringBuffer();
191:
192:                message.append(expected).append(" not in {");
193:
194:                for (int i = 0; i < array.length; ++i) {
195:                    message.append("'").append(array[i]).append("'");
196:
197:                    if (i < (array.length - 1)) {
198:                        message.append(", ");
199:                    }
200:                }
201:
202:                message.append(" }");
203:
204:                fail(message.toString());
205:            }
206:
207:            /**
208:             * Asserts that the value of toString() on the given object matches the
209:             * given text string
210:             *
211:             * @param value the object to be output to the console
212:             * @param expected the expected String representation
213:             */
214:            protected void assertToString(Object value, String expected) {
215:                Object console = InvokerHelper.invokeMethod(value, "toString",
216:                        null);
217:                assertEquals("toString() on value: " + value, expected, console);
218:            }
219:
220:            /**
221:             * Asserts that the value of inspect() on the given object matches the
222:             * given text string
223:             *
224:             * @param value the object to be output to the console
225:             * @param expected the expected String representation
226:             */
227:            protected void assertInspect(Object value, String expected) {
228:                Object console = InvokerHelper.invokeMethod(value, "inspect",
229:                        null);
230:                assertEquals("inspect() on value: " + value, expected, console);
231:            }
232:
233:            /**
234:             * Asserts that the script runs without any exceptions
235:             *
236:             * @param script the script that should pass without any exception thrown
237:             */
238:            protected void assertScript(final String script) throws Exception {
239:                GroovyShell shell = new GroovyShell();
240:                shell.evaluate(script, getTestClassName());
241:            }
242:
243:            protected String getTestClassName() {
244:                return "TestScript" + getMethodName() + (counter++) + ".groovy";
245:            }
246:
247:            /**
248:             * Asserts that the given code closure fails when it is evaluated
249:             *
250:             * @param code
251:             * @return the message of the thrown Throwable
252:             */
253:            protected String shouldFail(Closure code) {
254:                boolean failed = false;
255:                String result = null;
256:                try {
257:                    code.call();
258:                } catch (Throwable e) {
259:                    failed = true;
260:                    result = e.getMessage();
261:                }
262:                assertTrue("Closure " + code + " should have failed", failed);
263:                return result;
264:            }
265:
266:            /**
267:             * Asserts that the given code closure fails when it is evaluated
268:             * and that a particular exception is thrown.
269:             *
270:             * @param clazz the class of the expected exception
271:             * @param code the closure that should fail
272:             * @return the message of the expected Throwable
273:             */
274:            protected String shouldFail(Class clazz, Closure code) {
275:                Throwable th = null;
276:                try {
277:                    code.call();
278:                } catch (GroovyRuntimeException gre) {
279:                    th = gre;
280:                    while (th.getCause() != null && th.getCause() != gre) { // if wrapped, find the root cause
281:                        th = th.getCause();
282:                        if (th != gre && (th instanceof  GroovyRuntimeException)) {
283:                            gre = (GroovyRuntimeException) th;
284:                        }
285:                    }
286:                } catch (Throwable e) {
287:                    th = e;
288:                }
289:
290:                if (th == null) {
291:                    fail("Closure " + code
292:                            + " should have failed with an exception of type "
293:                            + clazz.getName());
294:                } else if (!clazz.isInstance(th)) {
295:                    fail("Closure " + code
296:                            + " should have failed with an exception of type "
297:                            + clazz.getName() + ", instead got Exception " + th);
298:                }
299:                return th.getMessage();
300:            }
301:
302:            /**
303:             *  Returns a copy of a string in which all EOLs are \n.
304:             */
305:            protected String fixEOLs(String value) {
306:                return value.replaceAll("(\\r\\n?)|\n", "\n");
307:            }
308:
309:            /**
310:             * Runs the calling JUnit test again and fails only if it unexpectedly runs.<br/>
311:             * This is helpful for tests that don't currently work but should work one day,
312:             * when the tested functionality has been implemented.<br/>
313:             * The right way to use it is:
314:             * <pre>
315:             * public void testXXX() {
316:             *   if (GroovyTestCase.notYetImplemented(this)) return;
317:             *   ... the real (now failing) unit test
318:             * }
319:             * </pre>
320:             * Idea copied from HtmlUnit (many thanks to Marc Guillemot).
321:             * Future versions maybe available in the JUnit distro.
322:             * The purpose of providing a 'static' version is such that you can use the
323:             * feature even if not subclassing GroovyTestCase.
324:             * @return <false> when not itself already in the call stack
325:             */
326:            public static boolean notYetImplemented(TestCase caller) {
327:                if (notYetImplementedFlag.get() != null) {
328:                    return false;
329:                }
330:                notYetImplementedFlag.set(Boolean.TRUE);
331:
332:                final Method testMethod = findRunningJUnitTestMethod(caller
333:                        .getClass());
334:                try {
335:                    log.info("Running " + testMethod.getName()
336:                            + " as not yet implemented");
337:                    testMethod.invoke(caller, new Class[] {});
338:                    fail(testMethod.getName()
339:                            + " is marked as not yet implemented but passes unexpectedly");
340:                } catch (final Exception e) {
341:                    log
342:                            .info(testMethod.getName()
343:                                    + " fails which is expected as it is not yet implemented");
344:                    // method execution failed, it is really "not yet implemented"
345:                } finally {
346:                    notYetImplementedFlag.set(null);
347:                }
348:                return true;
349:            }
350:
351:            /**
352:             * Convenience method for subclasses of GroovyTestCase, identical to
353:             * <pre> GroovyTestCase.notYetImplemented(this); </pre>.
354:             * @see #notYetImplemented(junit.framework.TestCase)
355:             * @return  <false> when not itself already in the call stack
356:             */
357:            public boolean notYetImplemented() {
358:                return notYetImplemented(this );
359:            }
360:
361:            /**
362:             * From JUnit. Finds from the call stack the active running JUnit test case
363:             * @return the test case method
364:             * @throws RuntimeException if no method could be found.
365:             */
366:            private static Method findRunningJUnitTestMethod(Class caller) {
367:                final Class[] args = new Class[] {};
368:
369:                // search the inial junit test
370:                final Throwable t = new Exception();
371:                for (int i = t.getStackTrace().length - 1; i >= 0; --i) {
372:                    final StackTraceElement element = t.getStackTrace()[i];
373:                    if (element.getClassName().equals(caller.getName())) {
374:                        try {
375:                            final Method m = caller.getMethod(element
376:                                    .getMethodName(), args);
377:                            if (isPublicTestMethod(m)) {
378:                                return m;
379:                            }
380:                        } catch (final Exception e) {
381:                            // can't access, ignore it
382:                        }
383:                    }
384:                }
385:                throw new RuntimeException(
386:                        "No JUnit test case method found in call stack");
387:            }
388:
389:            /**
390:             * From Junit. Test if the method is a junit test.
391:             * @param method the method
392:             * @return <code>true</code> if this is a junit test.
393:             */
394:            private static boolean isPublicTestMethod(final Method method) {
395:                final String name = method.getName();
396:                final Class[] parameters = method.getParameterTypes();
397:                final Class returnType = method.getReturnType();
398:
399:                return parameters.length == 0 && name.startsWith("test")
400:                        && returnType.equals(Void.TYPE)
401:                        && Modifier.isPublic(method.getModifiers());
402:            }
403:
404:            private static final ThreadLocal notYetImplementedFlag = new ThreadLocal();
405:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.