16277313ba72f743074b5132830ccfbab20f1e274the.mindstormpackage org.testng.internal;
26277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
36277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
40f6db64c011bc71dcdb432d7d27730b04f5ef2c1Cédric Beustimport org.testng.ITestNGMethod;
5ed0917f31f0b66158bc786cd018fd95175f76f29Cédric Beustimport org.testng.collections.Lists;
60f6db64c011bc71dcdb432d7d27730b04f5ef2c1Cédric Beustimport org.testng.collections.Maps;
70f6db64c011bc71dcdb432d7d27730b04f5ef2c1Cédric Beust
86277313ba72f743074b5132830ccfbab20f1e274the.mindstormimport java.io.Serializable;
96277313ba72f743074b5132830ccfbab20f1e274the.mindstormimport java.util.Collection;
106277313ba72f743074b5132830ccfbab20f1e274the.mindstormimport java.util.List;
116277313ba72f743074b5132830ccfbab20f1e274the.mindstormimport java.util.Map;
126277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
136277313ba72f743074b5132830ccfbab20f1e274the.mindstorm/**
146277313ba72f743074b5132830ccfbab20f1e274the.mindstorm * This class wraps access to beforeGroups and afterGroups methods,
156277313ba72f743074b5132830ccfbab20f1e274the.mindstorm * since they are passed around the various invokers and potentially
166277313ba72f743074b5132830ccfbab20f1e274the.mindstorm * modified in different threads.
176277313ba72f743074b5132830ccfbab20f1e274the.mindstorm *
186277313ba72f743074b5132830ccfbab20f1e274the.mindstorm * @author <a href="mailto:cedric@beust.com">Cedric Beust</a>
196277313ba72f743074b5132830ccfbab20f1e274the.mindstorm * @author <a href='mailto:the_mindstorm@evolva.ro'>Alexandru Popescu</a>
206277313ba72f743074b5132830ccfbab20f1e274the.mindstorm * @since 5.3 (Mar 2, 2006)
216277313ba72f743074b5132830ccfbab20f1e274the.mindstorm */
226277313ba72f743074b5132830ccfbab20f1e274the.mindstormpublic class ConfigurationGroupMethods implements Serializable {
236277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  /** Use serialVersionUID for interoperability. */
246277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  private final static long serialVersionUID= 1660798519864898480L;
256277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
266277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  /** The list of beforeGroups methods keyed by the name of the group */
276277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  private final Map<String, List<ITestNGMethod>> m_beforeGroupsMethods;
286277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
296277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  /** The list of afterGroups methods keyed by the name of the group */
306277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  private final Map<String, List<ITestNGMethod>> m_afterGroupsMethods;
316277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
326277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  /** The list of all test methods */
336277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  private final ITestNGMethod[] m_allMethods;
346277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
356277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  /**A map that returns the last method belonging to the given group */
364a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm  private Map<String, List<ITestNGMethod>> m_afterGroupsMap= null;
376277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
386277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  public ConfigurationGroupMethods(ITestNGMethod[] allMethods,
396277313ba72f743074b5132830ccfbab20f1e274the.mindstorm                                   Map<String, List<ITestNGMethod>> beforeGroupsMethods,
400f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin                                   Map<String, List<ITestNGMethod>> afterGroupsMethods)
41a26b36e03089df34218359e55b6bb29e81ea000eCédric Beust  {
426277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    m_allMethods= allMethods;
436277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    m_beforeGroupsMethods= beforeGroupsMethods;
446277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    m_afterGroupsMethods= afterGroupsMethods;
456277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  }
460f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin
476589ee28ed149622f0ed658c9db5c274df477b1cCédric Beust  public Map<String, List<ITestNGMethod>> getBeforeGroupsMethods() {
486589ee28ed149622f0ed658c9db5c274df477b1cCédric Beust    return m_beforeGroupsMethods;
496589ee28ed149622f0ed658c9db5c274df477b1cCédric Beust  }
506589ee28ed149622f0ed658c9db5c274df477b1cCédric Beust
516589ee28ed149622f0ed658c9db5c274df477b1cCédric Beust  public Map<String, List<ITestNGMethod>> getAfterGroupsMethods() {
526589ee28ed149622f0ed658c9db5c274df477b1cCédric Beust    return m_afterGroupsMethods;
536589ee28ed149622f0ed658c9db5c274df477b1cCédric Beust  }
546277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
556277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  /**
566277313ba72f743074b5132830ccfbab20f1e274the.mindstorm   * @return true if the passed method is the last to run for the group.
576277313ba72f743074b5132830ccfbab20f1e274the.mindstorm   * This method is used to figure out when is the right time to invoke
586277313ba72f743074b5132830ccfbab20f1e274the.mindstorm   * afterGroups methods.
596277313ba72f743074b5132830ccfbab20f1e274the.mindstorm   */
606277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  public synchronized boolean isLastMethodForGroup(String group, ITestNGMethod method) {
616277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
62aec0b211238fc0ecc0095927c67ec4397311e705the.mindstorm    // If we have more invocation to do, this is not the last one yet
636277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    int invocationCount= method.getCurrentInvocationCount();
646277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    if(invocationCount < (method.getInvocationCount() * method.getParameterInvocationCount())) {
656277313ba72f743074b5132830ccfbab20f1e274the.mindstorm      return false;
666277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    }
676277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
686277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    // Lazy initialization since we might never be called
696277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    if(m_afterGroupsMap == null) {
704a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm      m_afterGroupsMap= initializeAfterGroupsMap();
716277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    }
726277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
734a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm    List<ITestNGMethod> methodsInGroup= m_afterGroupsMap.get(group);
740f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin
750f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin    if(null == methodsInGroup || methodsInGroup.isEmpty()) {
760f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin      return false;
770f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin    }
780f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin
794a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm    methodsInGroup.remove(method);
800f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin
81aec0b211238fc0ecc0095927c67ec4397311e705the.mindstorm    // Note:  == is not good enough here as we may work with ITestNGMethod clones
824a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm    return methodsInGroup.isEmpty();
836277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
846277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  }
856277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
864a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm  private synchronized Map<String, List<ITestNGMethod>> initializeAfterGroupsMap() {
870f6db64c011bc71dcdb432d7d27730b04f5ef2c1Cédric Beust    Map<String, List<ITestNGMethod>> result= Maps.newHashMap();
884a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm    for(ITestNGMethod m : m_allMethods) {
894a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm      String[] groups= m.getGroups();
904a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm      for(String g : groups) {
914a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm        List<ITestNGMethod> methodsInGroup= result.get(g);
924a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm        if(null == methodsInGroup) {
93ed0917f31f0b66158bc786cd018fd95175f76f29Cédric Beust          methodsInGroup= Lists.newArrayList();
944a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm          result.put(g, methodsInGroup);
954a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm        }
964a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm        methodsInGroup.add(m);
974a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm      }
984a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm    }
994a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm
1004a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm    return result;
1014a9f5e1bbd5511495182bbd21cb62ce6eb71985dthe.mindstorm  }
1020f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin
1036277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  public synchronized void removeBeforeMethod(String group, ITestNGMethod method) {
1046277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    List<ITestNGMethod> methods= m_beforeGroupsMethods.get(group);
1056277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    if(methods != null) {
1066277313ba72f743074b5132830ccfbab20f1e274the.mindstorm      Object success= methods.remove(method);
1076277313ba72f743074b5132830ccfbab20f1e274the.mindstorm      if(success == null) {
1086277313ba72f743074b5132830ccfbab20f1e274the.mindstorm        log("Couldn't remove beforeGroups method " + method + " for group " + group);
1096277313ba72f743074b5132830ccfbab20f1e274the.mindstorm      }
1106277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    }
1116277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    else {
1126277313ba72f743074b5132830ccfbab20f1e274the.mindstorm      log("Couldn't find any beforeGroups method for group " + group);
1136277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    }
1146277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  }
1156277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
1166277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  private void log(String string) {
1176277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    Utils.log("ConfigurationGroupMethods", 2, string);
1186277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  }
1196277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
1206277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  synchronized public Map<String, List<ITestNGMethod>> getBeforeGroupsMap() {
1216277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    return m_beforeGroupsMethods;
1226277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  }
1236277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
1246277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  synchronized public Map<String, List<ITestNGMethod>> getAfterGroupsMap() {
1256277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    return m_afterGroupsMethods;
1266277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  }
1276277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
1286277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  synchronized public void removeBeforeGroups(String[] groups) {
1296277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    for(String group : groups) {
1306277313ba72f743074b5132830ccfbab20f1e274the.mindstorm//      log("Removing before group " + group);
1316277313ba72f743074b5132830ccfbab20f1e274the.mindstorm      m_beforeGroupsMethods.remove(group);
1326277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    }
1336277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  }
1346277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
1356277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  synchronized public void removeAfterGroups(Collection<String> groups) {
1366277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    for(String group : groups) {
1376277313ba72f743074b5132830ccfbab20f1e274the.mindstorm//      log("Removing before group " + group);
1386277313ba72f743074b5132830ccfbab20f1e274the.mindstorm      m_afterGroupsMethods.remove(group);
1396277313ba72f743074b5132830ccfbab20f1e274the.mindstorm    }
1400f7e671c94aeedee2fbc796b3318d44b0297b6cdnullin
1416277313ba72f743074b5132830ccfbab20f1e274the.mindstorm  }
1426277313ba72f743074b5132830ccfbab20f1e274the.mindstorm
1436277313ba72f743074b5132830ccfbab20f1e274the.mindstorm}
144