Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WithMessage |
|
| 1.4;1.4 |
1 | /******************************************************************************* | |
2 | * Copyright (c) 2010 Ketan Padegaonkar and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Brock Janiczak - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.swtbot.swt.finder.matchers; | |
12 | ||
13 | import java.lang.reflect.InvocationTargetException; | |
14 | import java.lang.reflect.Method; | |
15 | ||
16 | import org.eclipse.swt.widgets.Widget; | |
17 | import org.eclipse.swtbot.swt.finder.matchers.AbstractMatcher; | |
18 | import org.eclipse.swtbot.swt.finder.utils.SWTUtils; | |
19 | import org.hamcrest.Description; | |
20 | import org.hamcrest.Factory; | |
21 | import org.hamcrest.Matcher; | |
22 | ||
23 | /** | |
24 | * Matches {@link org.eclipse.swt.widgets.Widget}s with the specified message. | |
25 | * | |
26 | * @author Brock Janiczak <brockj [at] gmail [dot] com> | |
27 | * @version $Id$ | |
28 | * @since 2.0 | |
29 | */ | |
30 | class WithMessage<T extends Widget> extends AbstractMatcher<T> { | |
31 | ||
32 | /** The text */ | |
33 | protected String message; | |
34 | ||
35 | /** | |
36 | * Constructs the message text matcher with the given text. | |
37 | * | |
38 | * @param text the message to match on the {@link org.eclipse.swt.widgets.Widget} | |
39 | */ | |
40 | 5 | WithMessage(String text) { |
41 | 5 | this.message = text; |
42 | 5 | } |
43 | ||
44 | protected boolean doMatch(Object obj) { | |
45 | try { | |
46 | 4 | return getMessage(obj).equals(message); |
47 | 2 | } catch (Exception e) { |
48 | // do nothing | |
49 | } | |
50 | 2 | return false; |
51 | } | |
52 | ||
53 | /** | |
54 | * Gets the message of the object using the getText method. If the object doesn't contain a get message method an | |
55 | * exception is thrown. | |
56 | * | |
57 | * @param obj any object to get the message from. | |
58 | * @return the return value of obj#getMessage() | |
59 | * @throws NoSuchMethodException if the method "getMessage" does not exist on the object. | |
60 | * @throws IllegalAccessException if the java access control does not allow invocation. | |
61 | * @throws InvocationTargetException if the method "getMessage" throws an exception. | |
62 | * @see Method#invoke(Object, Object[]) | |
63 | */ | |
64 | static String getMessage(Object obj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { | |
65 | 4 | return ((String) SWTUtils.invokeMethod(obj, "getMessage")); //$NON-NLS-1$ |
66 | } | |
67 | ||
68 | public void describeTo(Description description) { | |
69 | 1 | description.appendText("with message '").appendText(message).appendText("'"); //$NON-NLS-1$ //$NON-NLS-2$ |
70 | 1 | } |
71 | ||
72 | /** | |
73 | * Matches a widget that has the specified exact message. | |
74 | * | |
75 | * @param message the message. | |
76 | * @return a matcher. | |
77 | * @since 2.0 | |
78 | */ | |
79 | @Factory | |
80 | public static <T extends Widget> Matcher<T> withMessage(String message) { | |
81 | 5 | return new WithMessage<T>(message); |
82 | } | |
83 | } |