Coverage Report - org.eclipse.swtbot.swt.finder.ReferenceBy
 
Classes in this File Line Coverage Branch Coverage Complexity
ReferenceBy
92%
39/42
94%
17/18
1.542
ReferenceBy$1
100%
5/5
100%
8/8
1.542
ReferenceBy$10
100%
3/3
N/A
1.542
ReferenceBy$2
100%
6/6
100%
8/8
1.542
ReferenceBy$3
100%
4/4
100%
8/8
1.542
ReferenceBy$4
100%
4/4
62%
5/8
1.542
ReferenceBy$5
75%
3/4
60%
6/10
1.542
ReferenceBy$6
75%
6/8
N/A
1.542
ReferenceBy$7
37%
3/8
N/A
1.542
ReferenceBy$8
85%
6/7
N/A
1.542
ReferenceBy$9
100%
4/4
N/A
1.542
 
 1  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  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder;
 12  
 
 13  
 import java.util.ArrayList;
 14  
 import java.util.Collections;
 15  
 import java.util.Comparator;
 16  
 import java.util.List;
 17  
 
 18  
 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
 19  
 import org.eclipse.swtbot.swt.finder.utils.StringUtils;
 20  
 
 21  
 /**
 22  
  * @since 2.0
 23  
  */
 24  1
 public enum ReferenceBy {
 25  1
         TEXT(100) {
 26  
                 protected boolean isCompatibleWith(ReferenceBy other) {
 27  279
                         return other != LABEL && other != TOOLTIP && other != MNEMONIC && other != MESSAGE;
 28  
                 }
 29  
 
 30  
                 public String matcherMethod() {
 31  1
                         return "withText(" + argumentName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
 32  
                 }
 33  
 
 34  
                 public String methodNameSuffix() {
 35  1
                         return ""; //$NON-NLS-1$
 36  
                 }
 37  
         },
 38  1
         MNEMONIC(100) {
 39  
                 protected boolean isCompatibleWith(ReferenceBy other) {
 40  151
                         return other != LABEL && other != TOOLTIP && other != TEXT && other != MESSAGE;
 41  
                 }
 42  
 
 43  
                 public String matcherMethod() {
 44  1
                         return "withMnemonic(" + argumentName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
 45  
                 }
 46  
 
 47  
                 public String methodNameSuffix() {
 48  1
                         return ""; //$NON-NLS-1$
 49  
                 }
 50  
 
 51  
                 public String argumentName() {
 52  1
                         return "mnemonicText"; //$NON-NLS-1$
 53  
                 }
 54  
         },
 55  1
         LABEL(100) {
 56  
                 protected boolean isCompatibleWith(ReferenceBy other) {
 57  87
                         return other != TEXT && other != TOOLTIP && other != MNEMONIC && other != MESSAGE;
 58  
                 }
 59  
 
 60  
                 public String matcherMethod() {
 61  1
                         return "withLabel(" + argumentName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
 62  
                 }
 63  
         },
 64  1
         TOOLTIP(90) {
 65  
                 protected boolean isCompatibleWith(ReferenceBy other) {
 66  48
                         return other != TEXT && other != LABEL && other != MNEMONIC  && other != MESSAGE;
 67  
                 }
 68  
 
 69  
                 public String matcherMethod() {
 70  1
                         return "withTooltip(" + argumentName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
 71  
                 }
 72  
         },
 73  1
         MESSAGE(90) {
 74  
                 protected boolean isCompatibleWith(ReferenceBy other) {
 75  32
                         return other != TEXT && other != LABEL && other != MNEMONIC && other != TOOLTIP && other != IN_GROUP;
 76  
                 }
 77  
 
 78  
                 public String matcherMethod() {
 79  0
                         return "withMessage(" + argumentName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
 80  
                 }
 81  
         },
 82  1
         ID_KEY_VALUE(100) {
 83  
                 protected boolean isCompatibleWith(ReferenceBy other) {
 84  44
                         return false;
 85  
                 }
 86  
 
 87  
                 public String methodArgument() {
 88  1
                         return "String key, String value"; //$NON-NLS-1$
 89  
                 }
 90  
 
 91  
                 public String matcherMethod() {
 92  1
                         return "withId(key, value)"; //$NON-NLS-1$
 93  
                 }
 94  
 
 95  
                 public String paramJavaDoc() {
 96  0
                         return "@param key the key set on the widget.\n" + "@param value the value for the key.\n"; //$NON-NLS-1$ //$NON-NLS-2$
 97  
                 }
 98  
 
 99  
                 public String methodNameSuffix() {
 100  1
                         return "WithId"; //$NON-NLS-1$
 101  
                 }
 102  
 
 103  
                 public String argumentName() {
 104  0
                         return "key/value"; //$NON-NLS-1$
 105  
                 }
 106  
         },
 107  1
         ID_VALUE(100) {
 108  
                 protected boolean isCompatibleWith(ReferenceBy other) {
 109  21
                         return false;
 110  
                 }
 111  
 
 112  
                 public String methodArgument() {
 113  0
                         return "String value"; //$NON-NLS-1$
 114  
                 }
 115  
 
 116  
                 public String matcherMethod() {
 117  0
                         return "withId(value)"; //$NON-NLS-1$
 118  
                 }
 119  
 
 120  
                 public String paramJavaDoc() {
 121  0
                         return "@param value the value for the key {@link " + SWTBotPreferences.class.getName() + "#DEFAULT_KEY}.\n"; //$NON-NLS-1$ //$NON-NLS-2$
 122  
                 }
 123  
 
 124  
                 public String methodNameSuffix() {
 125  0
                         return "WithId"; //$NON-NLS-1$
 126  
                 }
 127  
 
 128  
                 public String argumentName() {
 129  0
                         return "value"; //$NON-NLS-1$
 130  
                 }
 131  
         },
 132  1
         NONE(100) {
 133  
                 protected boolean isCompatibleWith(ReferenceBy other) {
 134  11
                         return false;
 135  
                 }
 136  
 
 137  
                 public String methodArgument() {
 138  1
                         return ""; //$NON-NLS-1$
 139  
                 }
 140  
 
 141  
                 public String methodNameSuffix() {
 142  1
                         return ""; //$NON-NLS-1$
 143  
                 }
 144  
 
 145  
                 public String matcherMethod() {
 146  1
                         return ""; //$NON-NLS-1$
 147  
                 }
 148  
 
 149  
                 public String paramJavaDoc() {
 150  0
                         return ""; //$NON-NLS-1$
 151  
                 }
 152  
         },
 153  1
         IN_GROUP(80) {
 154  
                 public String methodNameSuffix() {
 155  1
                         return "InGroup"; //$NON-NLS-1$
 156  
                 }
 157  
 
 158  
                 public String matcherMethod() {
 159  1
                         return "inGroup(inGroup)"; //$NON-NLS-1$
 160  
                 }
 161  
         };
 162  8
         private final int        priority;
 163  
 
 164  18
         private ReferenceBy(int priority) {
 165  9
                 this.priority = priority;
 166  9
         }
 167  
 
 168  
         protected boolean isCompatibleWith(ReferenceBy other) {
 169  5
                 return true;
 170  
         }
 171  
 
 172  
         protected boolean canExistByItself() {
 173  9
                 return true;
 174  
         }
 175  
 
 176  
         public String describeJavaDoc() {
 177  0
                 return "with the specified <code>" + argumentName() + "</code>"; //$NON-NLS-1$ //$NON-NLS-2$
 178  
         }
 179  
 
 180  
         public String methodArgument() {
 181  4
                 return "String " + argumentName(); //$NON-NLS-1$
 182  
         }
 183  
 
 184  
         public String argumentName() {
 185  7
                 return StringUtils.toCamelCase(name().toLowerCase());
 186  
         }
 187  
 
 188  
         public String methodNameSuffix() {
 189  2
                 return "With" + StringUtils.capitalize(name().toLowerCase()); //$NON-NLS-1$
 190  
         }
 191  
 
 192  
         public abstract String matcherMethod();
 193  
 
 194  
         protected static Comparator<ReferenceBy> comparator() {
 195  13
                 return new Comparator<ReferenceBy>() {
 196  
                         public int compare(ReferenceBy o1, ReferenceBy o2) {
 197  4
                                 return o2.priority - o1.priority;
 198  
                         }
 199  
 
 200  
                 };
 201  
         }
 202  
 
 203  
         /**
 204  
          * @param references the references
 205  
          * @return all possible combinations of the specified combinations
 206  
          */
 207  
         public static List<List<ReferenceBy>> getCombinations(ReferenceBy... references) {
 208  1
                 return sort(filter(new CombinationGenerator<ReferenceBy>(references.length, references)));
 209  
         }
 210  
 
 211  
         private static List<List<ReferenceBy>> sort(List<List<ReferenceBy>> combinations) {
 212  15
                 for (List<ReferenceBy> list : combinations) {
 213  13
                         Collections.sort(list, ReferenceBy.comparator());
 214  
                 }
 215  1
                 return combinations;
 216  
         }
 217  
 
 218  
         private static List<List<ReferenceBy>> filter(CombinationGenerator<ReferenceBy> uptoCombinationGenerator) {
 219  1
                 ArrayList<List<ReferenceBy>> result = new ArrayList<List<ReferenceBy>>();
 220  513
                 for (List<ReferenceBy> list : uptoCombinationGenerator) {
 221  511
                         if (list.size() == 1) {
 222  9
                                 if (list.get(0).canExistByItself())
 223  9
                                         result.add(list);
 224  502
                         } else if (isCompatible(list))
 225  4
                                 result.add(list);
 226  
                 }
 227  1
                 return result;
 228  
         }
 229  
 
 230  
         private static boolean isCompatible(List<ReferenceBy> list) {
 231  1075
                 for (ReferenceBy aReference : list) {
 232  1800
                         for (ReferenceBy otherReference : list) {
 233  1160
                                 if (aReference != otherReference)
 234  654
                                         if (!aReference.isCompatibleWith(otherReference))
 235  498
                                                 return false;
 236  
                         }
 237  
                 }
 238  4
                 return true;
 239  
         }
 240  
 
 241  
         public String javadoc() {
 242  0
                 return "the " + argumentName() + " on the widget."; //$NON-NLS-1$ //$NON-NLS-2$
 243  
         }
 244  
 
 245  
         public String paramJavaDoc() {
 246  0
                 return "@param " + argumentName() + " " + javadoc() + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 247  
         }
 248  
 
 249  
 }