| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AbstractMatcher |
|
| 5.0;5 |
| 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 | ||
| 15 | import org.apache.log4j.Logger; | |
| 16 | import org.eclipse.swt.widgets.Widget; | |
| 17 | import org.eclipse.swtbot.swt.finder.finders.PathGenerator; | |
| 18 | import org.eclipse.swtbot.swt.finder.utils.ClassUtils; | |
| 19 | import org.eclipse.swtbot.swt.finder.utils.MessageFormat; | |
| 20 | import org.eclipse.swtbot.swt.finder.utils.SWTUtils; | |
| 21 | import org.eclipse.swtbot.swt.finder.utils.TreePath; | |
| 22 | import org.hamcrest.BaseMatcher; | |
| 23 | import org.hamcrest.StringDescription; | |
| 24 | ||
| 25 | /** | |
| 26 | * A matcher that logs the result of matches. The match is done by subclasses. | |
| 27 | * | |
| 28 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
| 29 | * @version $Id$ | |
| 30 | */ | |
| 31 | 6783 | public abstract class AbstractMatcher<T> extends BaseMatcher<T> { |
| 32 | ||
| 33 | 1 | public static final Logger log = Logger.getLogger(AbstractMatcher.class); |
| 34 | ||
| 35 | public boolean matches(Object item) { | |
| 36 | 307159 | boolean result = false; |
| 37 | try { | |
| 38 | 307159 | result = doMatch(item); |
| 39 | 307159 | String text = ""; //$NON-NLS-1$ |
| 40 | 307159 | if (log.isDebugEnabled()) { |
| 41 | 0 | text = SWTUtils.getText(item); |
| 42 | try { | |
| 43 | 0 | if (text.length() > 20) |
| 44 | 0 | text = text.substring(0, 20) + "..."; //$NON-NLS-1$ |
| 45 | 0 | } catch (Exception e) { |
| 46 | // do nothing | |
| 47 | } | |
| 48 | 0 | if (result) { |
| 49 | 0 | log.debug(MessageFormat.format("matched {0} with text \"{1}\", using matcher: {2}", ClassUtils.simpleClassName(item), text, StringDescription.toString(this))); //$NON-NLS-1$ |
| 50 | } else | |
| 51 | 0 | log.trace(MessageFormat.format("did not match {0} with text \"{1}\", using matcher: {2}", ClassUtils.simpleClassName(item), text, StringDescription.toString(this))); //$NON-NLS-1$ |
| 52 | } | |
| 53 | ||
| 54 | 307159 | if (log.isTraceEnabled() && (item instanceof Widget)) { |
| 55 | 0 | PathGenerator pathGenerator = new PathGenerator(); |
| 56 | 0 | TreePath path = pathGenerator.getPath((Widget) item); |
| 57 | 0 | int segmentCount = path.getSegmentCount(); |
| 58 | ||
| 59 | 0 | String prefix = ""; //$NON-NLS-1$ |
| 60 | 0 | for (int i = 0; i < segmentCount - 1; i++) |
| 61 | 0 | prefix += " "; //$NON-NLS-1$ |
| 62 | 0 | prefix += "+---"; //$NON-NLS-1$ |
| 63 | 0 | log.trace(prefix + "Widget: " + ClassUtils.simpleClassName(item) + "{" + text + "}"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 64 | } | |
| 65 | 0 | } catch (Exception e) { |
| 66 | 0 | log.warn("Matcher threw an exception: ", e); //$NON-NLS-1$ |
| 67 | } | |
| 68 | 307159 | return result; |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | * Subclasses must override, this is the actual method that does the matching | |
| 73 | * | |
| 74 | * @param item the item to match. | |
| 75 | * @return <code>true</code> if the item matches, <code>false</code> otherwise. | |
| 76 | */ | |
| 77 | protected abstract boolean doMatch(Object item); | |
| 78 | ||
| 79 | } |