Coverage Report - org.eclipse.swtbot.swt.finder.SWTBotAssert
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotAssert
62%
25/40
50%
2/4
1
SWTBotAssert$RegexMatcher
100%
7/7
N/A
1
 
 1  3
 /*******************************************************************************
 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  
  *     Hans Schwaebli - http://swtbot.org/bugzilla/show_bug.cgi?id=108
 11  
  *     Hans Schwaebli - http://swtbot.org/bugzilla/show_bug.cgi?id=112
 12  
  *******************************************************************************/
 13  
 package org.eclipse.swtbot.swt.finder;
 14  
 
 15  
 import static org.hamcrest.MatcherAssert.assertThat;
 16  
 import static org.hamcrest.Matchers.containsString;
 17  
 import static org.hamcrest.Matchers.not;
 18  
 import static org.hamcrest.Matchers.sameInstance;
 19  
 import static org.junit.Assert.assertEquals;
 20  
 import static org.junit.Assert.assertTrue;
 21  
 
 22  
 import java.util.regex.Pattern;
 23  
 
 24  
 import org.eclipse.swt.widgets.Widget;
 25  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 26  
 import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;
 27  
 import org.hamcrest.Description;
 28  
 import org.hamcrest.TypeSafeMatcher;
 29  
 import org.junit.Assert;
 30  
 
 31  
 /**
 32  
  * The SWTBotAssert provides extra capabilities for comparing widgets and other UI items. A set of assert methods.
 33  
  * Messages are only displayed when an assert fails. See {@link Assert}
 34  
  * 
 35  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 36  
  * @version $Id$
 37  
  * @since 2.0
 38  
  */
 39  0
 public abstract class SWTBotAssert {
 40  
 
 41  
         /**
 42  
          * 
 43  
          * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 44  
          * @version $Id$
 45  
          */
 46  
         private static final class RegexMatcher extends TypeSafeMatcher<String> {
 47  
                 /**  */
 48  
                 private final Pattern        pattern;
 49  
 
 50  
                 /**
 51  
                  * @param pattern
 52  
                  */
 53  6
                 private RegexMatcher(Pattern pattern) {
 54  3
                         this.pattern = pattern;
 55  3
                 }
 56  
 
 57  
                 public boolean matchesSafely(String item) {
 58  3
                         return pattern.matcher(item).matches();
 59  
                 }
 60  
 
 61  
                 public void describeTo(Description description) {
 62  1
                         description.appendText("matching regex (").appendValue(pattern).appendText(")");
 63  1
                 }
 64  
         }
 65  
 
 66  
         /**
 67  
          * Asserts that two widgets do not refer to the same object.
 68  
          * 
 69  
          * @see Assert#assertNotSame(String, Object, Object)
 70  
          * @param expected the object you don't expect
 71  
          * @param actual the object to compare to unexpected
 72  
          */
 73  
         public static void assertNotSameWidget(Widget expected, Widget actual) {
 74  8
                 assertThat(expected, not(sameInstance(actual)));
 75  8
         }
 76  
 
 77  
         /**
 78  
          * Asserts the two widgets do not refer to the same object. The message will be used if the test fails.
 79  
          * 
 80  
          * @param message the identifying message or null for the AssertionError
 81  
          * @param expected the object you don't expect
 82  
          * @param actual the object to compare to unexpected
 83  
          */
 84  
         public static void assertNotSameWidget(String message, Widget expected, Widget actual) {
 85  0
                 assertThat(message, expected, not(sameInstance(actual)));
 86  0
         }
 87  
 
 88  
         /**
 89  
          * Asserts that the <code>needle</code> is contained within the <code>hayStack</code>.
 90  
          * 
 91  
          * @param needle the text to search in the <code>hayStack</code>.
 92  
          * @param hayStack the text to look within.
 93  
          */
 94  
         public static void assertContains(String needle, String hayStack) {
 95  4
                 assertThat(hayStack, containsString(needle));
 96  3
         }
 97  
 
 98  
         /**
 99  
          * Asserts that the <code>needle</code> is not present in the <code>hayStack</code>.
 100  
          * 
 101  
          * @param needle the text to search in the <code>hayStack</code>.
 102  
          * @param hayStack the text to look within.
 103  
          */
 104  
         public static void assertDoesNotContain(String needle, String hayStack) {
 105  2
                 assertThat(hayStack, not(containsString(needle)));
 106  1
         }
 107  
 
 108  
         /**
 109  
          * Asserts that two widgets refer to the same widget.
 110  
          * 
 111  
          * @param expected the expected widget
 112  
          * @param actual the widget to compare to expected
 113  
          */
 114  
         public static void assertSameWidget(Widget expected, Widget actual) {
 115  9
                 assertThat(actual, sameInstance(expected));
 116  9
         }
 117  
 
 118  
         /**
 119  
          * Asserts that two widgets refer to the same widgets.
 120  
          * 
 121  
          * @param message the identifying message or <code>null</code> for the AssertionError
 122  
          * @param expected the expected widget
 123  
          * @param actual the widget to compare to expected
 124  
          */
 125  
         public static void assertSameWidget(String message, Widget expected, Widget actual) {
 126  0
                 assertThat(message, actual, sameInstance(expected));
 127  0
         }
 128  
 
 129  
         /**
 130  
          * A helper to explicitly convey that the test has passed. Does nothing.
 131  
          */
 132  
         public static void pass() {
 133  
                 // Do nothing
 134  1
         }
 135  
 
 136  
         /**
 137  
          * Assert that the given string is the same as the widgets text.
 138  
          * 
 139  
          * @param expected the expected text
 140  
          * @param widget the widget to get the text from to compare.
 141  
          */
 142  
         public static void assertText(String expected, Widget widget) {
 143  22
                 assertEquals(expected, SWTUtils.getText(widget));
 144  22
         }
 145  
 
 146  
         /**
 147  
          * Assert that the given string is the same as the widgets text.
 148  
          * 
 149  
          * @param expected the expected text
 150  
          * @param widget the widget to get the text from to compare.
 151  
          */
 152  
         public static void assertText(String expected, AbstractSWTBot<? extends Widget> widget) {
 153  5
                 assertEquals(expected, widget.getText());
 154  5
         }
 155  
 
 156  
         /**
 157  
          * Assert that the text on the widget contains the expected text.
 158  
          * 
 159  
          * @param expected the expected text.
 160  
          * @param widget the widget
 161  
          */
 162  
         public static void assertTextContains(String expected, Widget widget) {
 163  25
                 assertThat(SWTUtils.getText(widget), containsString(expected));
 164  25
         }
 165  
 
 166  
         /**
 167  
          * Assert that the text on the widget contains the expected text.
 168  
          * 
 169  
          * @param expected the expected text
 170  
          * @param widget the widget
 171  
          */
 172  
         public static void assertTextContains(String expected, AbstractSWTBot<? extends Widget> widget) {
 173  34
                 assertThat(widget.getText(), containsString(expected));
 174  34
         }
 175  
 
 176  
         /**
 177  
          * Assert that the text on the widget does not contain the expected text.
 178  
          * 
 179  
          * @param expected the expected text
 180  
          * @param widget the widget
 181  
          */
 182  
         public static void assertTextDoesNotContain(String expected, Widget widget) {
 183  0
                 assertThat(SWTUtils.getText(widget), not(containsString(expected)));
 184  0
         }
 185  
 
 186  
         /**
 187  
          * Assert that the text on the widget does not contain the expected text.
 188  
          * 
 189  
          * @param expected the expected text
 190  
          * @param widget the widget
 191  
          */
 192  
         public static void assertTextDoesNotContain(String expected, AbstractSWTBot<? extends Widget> widget) {
 193  0
                 assertThat(widget.getText(), not(containsString(expected)));
 194  0
         }
 195  
 
 196  
         /**
 197  
          * Asserts that the widget is enabled.
 198  
          * 
 199  
          * @param widget the widget.
 200  
          */
 201  
         public static void assertEnabled(AbstractSWTBot<? extends Widget> widget) {
 202  4
                 assertTrue("Expected widget " + widget + " to be enabled.", widget.isEnabled()); //$NON-NLS-1$ //$NON-NLS-2$
 203  3
         }
 204  
 
 205  
         /**
 206  
          * Asserts that the widget is not enabled.
 207  
          * 
 208  
          * @param widget the widget.
 209  
          */
 210  
         public static void assertNotEnabled(AbstractSWTBot<? extends Widget> widget) {
 211  3
                 assertTrue("Expected widget " + widget + " to be disabled.", !widget.isEnabled()); //$NON-NLS-1$ //$NON-NLS-2$
 212  2
         }
 213  
 
 214  
         /**
 215  
          * Asserts that the widget is visible.
 216  
          * 
 217  
          * @param widget the widget.
 218  
          */
 219  
         public static void assertVisible(AbstractSWTBot<? extends Widget> widget) {
 220  0
                 assertTrue("Expected widget " + widget + " to be visible.", widget.isVisible()); //$NON-NLS-1$ //$NON-NLS-2$
 221  0
         }
 222  
 
 223  
         /**
 224  
          * Asserts that the widget is not visible.
 225  
          * 
 226  
          * @param widget the widget.
 227  
          */
 228  
         public static void assertNotVisible(AbstractSWTBot<? extends Widget> widget) {
 229  0
                 assertTrue("Expected widget " + widget + " to be visible.", !widget.isVisible()); //$NON-NLS-1$ //$NON-NLS-2$
 230  0
         }
 231  
 
 232  
         /**
 233  
          * Assert that the widget text matches the regex.
 234  
          * 
 235  
          * @param regex the regex.
 236  
          * @param actual the widget.
 237  
          */
 238  
         public static void assertMatchesRegex(String regex, AbstractSWTBot<? extends Widget> actual) {
 239  3
                 assertMatchesRegex(regex, actual.getText());
 240  2
         }
 241  
         
 242  
         /**
 243  
          * Assert that the widget text matches the regex.
 244  
          * 
 245  
          * @param regex the regex.
 246  
          * @param actual the widget.
 247  
          */
 248  
         public static void assertMatchesRegex(String regex, Widget actual) {
 249  0
                 assertMatchesRegex(regex, SWTUtils.getText(actual));
 250  0
         }
 251  
         
 252  
         /**
 253  
          * Assert that the widget text matches the regex.
 254  
          * 
 255  
          * @param regex the regex.
 256  
          * @param actual the widget.
 257  
          */
 258  
         public static void assertMatchesRegex(String regex, String actual) {
 259  3
                 assertThat(actual, new RegexMatcher(Pattern.compile("([\r\n]|.)*" + regex + "([\r\n]|.)*")));
 260  2
         }
 261  
 
 262  
 }