Source Code Cross Referenced for UISpecAssert.java in  » Testing » UISpec4J » org » uispec4j » assertion » 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 » Testing » UISpec4J » org.uispec4j.assertion 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.uispec4j.assertion;
002:
003:        import junit.framework.AssertionFailedError;
004:        import junit.framework.TestCase;
005:        import org.uispec4j.UISpec4J;
006:        import org.uispec4j.UISpecTestCase;
007:        import org.uispec4j.utils.Utils;
008:
009:        import javax.swing.*;
010:
011:        /**
012:         * Checks the validity of Assertion objects.<p/>
013:         * These methods are not meant to be used directly from within tests - you can use
014:         * them through {@link UISpecTestCase} or your own
015:         * {@link TestCase} implementation.
016:         */
017:        public class UISpecAssert {
018:
019:            /**
020:             * Checks that the given assertion succeeds (with a retry strategy).
021:             * The {@link Assertion#check()} method is called until the timeout
022:             * specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
023:             */
024:            public static void assertTrue(Assertion assertion) {
025:                assertTrue(null, assertion);
026:            }
027:
028:            /**
029:             * Checks that the given assertion succeeds (with a retry strategy).
030:             * The {@link Assertion#check()} method is called until the timeout
031:             * specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
032:             * If it fails an AssertionFailedError is thrown with the given message.
033:             */
034:            public static void assertTrue(String message, Assertion assertion) {
035:                checkAssertion(message, assertion, UISpec4J
036:                        .getAssertionTimeLimit());
037:            }
038:
039:            /**
040:             * Checks that the given assertion fails (with a retry strategy).
041:             * The {@link Assertion#check()} method is called until the timeout
042:             * specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
043:             */
044:            public static void assertFalse(final Assertion assertion) {
045:                assertTrue(not(assertion));
046:            }
047:
048:            /**
049:             * Checks the given assertion fails (with a retry strategy).
050:             * The {@link Assertion#check()} method is called until the timeout
051:             * specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
052:             * If it succeeds an AssertionFailedError is thrown with the given message.
053:             */
054:            public static void assertFalse(String message,
055:                    final Assertion assertion) {
056:                assertTrue(message, not(assertion));
057:            }
058:
059:            /**
060:             * Waits until the given assertion becomes true.
061:             * The {@link Assertion#check()} method is called until the timeout
062:             * specified as parameter is reached.
063:             */
064:            public static void waitUntil(Assertion assertion, long waitTimeLimit) {
065:                checkAssertion(null, assertion, waitTimeLimit);
066:            }
067:
068:            /**
069:             * Waits until the given assertion becomes true.
070:             * The {@link Assertion#check()} method is called until the timeout
071:             * specified as parameter is reached.
072:             * If it fails an AssertionFailedError is thrown with the given message.
073:             */
074:            public static void waitUntil(String message, Assertion assertion,
075:                    long waitTimeLimit) {
076:                checkAssertion(message, assertion, waitTimeLimit);
077:            }
078:
079:            /**
080:             * Checks the given assertion equals the expected parameter (with a retry strategy).
081:             * The {@link Assertion#check()} method is called until the timeout
082:             * specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
083:             */
084:            public static void assertEquals(boolean expected,
085:                    Assertion assertion) {
086:                assertEquals(null, expected, assertion);
087:            }
088:
089:            /**
090:             * Checks the given assertion equals the expected parameter (with a retry strategy).
091:             * The {@link Assertion#check()} method is called until the timeout
092:             * specified by {@link UISpec4J#setAssertionTimeLimit(long)} is reached.
093:             * If it fails an AssertionFailedError is thrown with the given message.
094:             */
095:            public static void assertEquals(String message, boolean expected,
096:                    Assertion assertion) {
097:                if (expected) {
098:                    assertTrue(message, assertion);
099:                } else {
100:                    assertFalse(message, assertion);
101:                }
102:            }
103:
104:            /**
105:             * Returns a negation of the given assertion.
106:             */
107:            public static Assertion not(final Assertion assertion) {
108:                return new Assertion() {
109:                    public void check() {
110:                        try {
111:                            assertion.check();
112:                            throw new FailureNotDetectedError();
113:                        } catch (FailureNotDetectedError e) {
114:                            throw new AssertionFailedError();
115:                        } catch (Throwable e) {
116:                        }
117:                    }
118:                };
119:            }
120:
121:            /**
122:             * Returns the intersection of the {@link Assertion} parameters.
123:             */
124:            public static Assertion and(final Assertion[] assertions) {
125:                return new Assertion() {
126:                    public void check() throws Exception {
127:                        for (int i = 0; i < assertions.length; i++) {
128:                            assertions[i].check();
129:                        }
130:                    }
131:                };
132:            }
133:
134:            /**
135:             * Returns the union of the {@link Assertion} parameters.
136:             */
137:            public static Assertion or(final Assertion[] assertions) {
138:                return new Assertion() {
139:                    public void check() {
140:                        for (int i = 0; i < assertions.length; i++) {
141:                            try {
142:                                assertions[i].check();
143:                                break;
144:                            } catch (Throwable e) {
145:                                if (i == assertions.length - 1) {
146:                                    throw new AssertionFailedError();
147:                                }
148:                            }
149:                        }
150:                    }
151:                };
152:            }
153:
154:            private static void checkAssertion(String message,
155:                    Assertion assertion, long waitTimeLimit) {
156:                try {
157:                    Utils.waitForPendingAwtEventsToBeProcessed();
158:                    assertion.check();
159:                } catch (Throwable e) {
160:                    retry(message, assertion, waitTimeLimit);
161:                }
162:            }
163:
164:            /**
165:             * @noinspection ForLoopThatDoesntUseLoopVariable
166:             */
167:            private static void retry(String message, Assertion assertion,
168:                    long waitTimeLimit) {
169:                long elapsedTime = 0;
170:                for (int i = 0; elapsedTime < waitTimeLimit; i++) {
171:                    int waitTime = i < 10 ? 20 : 200;
172:                    Utils.sleep(waitTime);
173:                    elapsedTime += waitTime;
174:                    try {
175:                        assertion.check();
176:                        return;
177:                    } catch (Throwable e) {
178:                    }
179:                }
180:                try {
181:                    assertion.check();
182:                } catch (AssertionFailedError e) {
183:                    if (message == null) {
184:                        throw e;
185:                    }
186:                    throw new AssertionFailedError(message);
187:                } catch (Exception e) {
188:                    if (message != null)
189:                        throw new RuntimeException(message, e);
190:
191:                    else {
192:                        throw new RuntimeException(e.getMessage(), e);
193:                    }
194:                }
195:            }
196:
197:            private static class FailureNotDetectedError extends
198:                    AssertionFailedError {
199:            }
200:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.