Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WithText |
|
| 1.4285714285714286;1.429 |
1 | /******************************************************************************* | |
2 | * Copyright (c) 2008 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 | * Ketan Padegaonkar - initial API and implementation | |
10 | * Ketan Padegaonkar - http://swtbot.org/bugzilla/show_bug.cgi?id=126 | |
11 | *******************************************************************************/ | |
12 | package org.eclipse.swtbot.swt.finder.matchers; | |
13 | ||
14 | import java.lang.reflect.InvocationTargetException; | |
15 | import java.lang.reflect.Method; | |
16 | ||
17 | import org.eclipse.swt.widgets.Text; | |
18 | import org.eclipse.swt.widgets.Widget; | |
19 | import org.eclipse.swtbot.swt.finder.utils.SWTUtils; | |
20 | import org.hamcrest.Description; | |
21 | import org.hamcrest.Factory; | |
22 | import org.hamcrest.Matcher; | |
23 | ||
24 | /** | |
25 | * Matches widgets if the getText() method of the widget matches the specified text. | |
26 | * | |
27 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
28 | * @version $Id$ | |
29 | * @since 2.0 | |
30 | */ | |
31 | public class WithText<T extends Widget> extends AbstractMatcher<T> { | |
32 | ||
33 | /** The text */ | |
34 | protected String text; | |
35 | ||
36 | /** | |
37 | * A flag to denote if this should ignore case. | |
38 | * | |
39 | * @since 1.2 | |
40 | */ | |
41 | 65 | protected boolean ignoreCase = false; |
42 | ||
43 | /** | |
44 | * Constructs this matcher with the given text. | |
45 | * | |
46 | * @param text the text to match on the {@link org.eclipse.swt.widgets.Widget} | |
47 | */ | |
48 | WithText(String text) { | |
49 | 63 | this(text, false); |
50 | 63 | } |
51 | ||
52 | /** | |
53 | * Constructs this matcher with the given text. | |
54 | * | |
55 | * @param text the text to match on the {@link org.eclipse.swt.widgets.Widget} | |
56 | * @param ignoreCase Determines if this should ignore case during the comparison. | |
57 | * @since 1.2 | |
58 | */ | |
59 | 65 | WithText(String text, boolean ignoreCase) { |
60 | 65 | text = text.replaceAll("\\r\\n", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
61 | 65 | text = text.replaceAll("\\r", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
62 | 65 | this.text = text; |
63 | 65 | this.ignoreCase = ignoreCase; |
64 | 65 | } |
65 | ||
66 | // FIXME: optimize the if() code block, use strategy or something else. | |
67 | protected boolean doMatch(Object obj) { | |
68 | try { | |
69 | 548 | boolean result = false; |
70 | 548 | if (ignoreCase) |
71 | 52 | result = getText(obj).equalsIgnoreCase(text); |
72 | else | |
73 | 496 | result = getText(obj).equals(text); |
74 | 533 | return result; |
75 | 15 | } catch (Exception e) { |
76 | // do nothing | |
77 | } | |
78 | 15 | return false; |
79 | } | |
80 | ||
81 | /** | |
82 | * Gets the text of the object using the getText method. If the object doesn't contain a get text method an | |
83 | * exception is thrown. | |
84 | * | |
85 | * @param obj any object to get the text from. | |
86 | * @return the return value of obj#getText() | |
87 | * @throws NoSuchMethodException if the method "getText" does not exist on the object. | |
88 | * @throws IllegalAccessException if the java access control does not allow invocation. | |
89 | * @throws InvocationTargetException if the method "getText" throws an exception. | |
90 | * @see Method#invoke(Object, Object[]) | |
91 | */ | |
92 | static String getText(Object obj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { | |
93 | 31480 | return ((String) SWTUtils.invokeMethod(obj, "getText")).replaceAll(Text.DELIMITER, "\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
94 | } | |
95 | ||
96 | public void describeTo(Description description) { | |
97 | 21 | description.appendText("with text '").appendText(text).appendText("'"); //$NON-NLS-1$ //$NON-NLS-2$ |
98 | 21 | } |
99 | ||
100 | /** | |
101 | * Matches a widget that has the specified exact text. | |
102 | * | |
103 | * @param text the label. | |
104 | * @return a matcher. | |
105 | * @since 2.0 | |
106 | */ | |
107 | @Factory | |
108 | public static <T extends Widget> Matcher<T> withText(String text) { | |
109 | 63 | return new WithText<T>(text); |
110 | } | |
111 | ||
112 | /** | |
113 | * Matches a widget that has the specified text, ignoring case considerations. | |
114 | * | |
115 | * @param text the label. | |
116 | * @return a matcher. | |
117 | * @since 2.0 | |
118 | */ | |
119 | @Factory | |
120 | public static <T extends Widget> Matcher<T> withTextIgnoringCase(String text) { | |
121 | 2 | return new WithText<T>(text, true); |
122 | } | |
123 | ||
124 | } |