88import java .util .Collection ;
99import java .util .List ;
1010import java .util .Map ;
11+ import java .util .concurrent .ConcurrentHashMap ;
1112
1213/**
1314 * This class wraps access to beforeGroups and afterGroups methods,
@@ -28,15 +29,15 @@ public class ConfigurationGroupMethods {
2829 private final ITestNGMethod [] m_allMethods ;
2930
3031 /**A map that returns the last method belonging to the given group */
31- private Map <String , List <ITestNGMethod >> m_afterGroupsMap = null ;
32+ private Map <String , List <ITestNGMethod >> m_afterGroupsMap = null ;
3233
3334 public ConfigurationGroupMethods (ITestNGMethod [] allMethods ,
3435 Map <String , List <ITestNGMethod >> beforeGroupsMethods ,
3536 Map <String , List <ITestNGMethod >> afterGroupsMethods )
3637 {
37- m_allMethods = allMethods ;
38- m_beforeGroupsMethods = beforeGroupsMethods ;
39- m_afterGroupsMethods = afterGroupsMethods ;
38+ m_allMethods = allMethods ;
39+ m_beforeGroupsMethods = new ConcurrentHashMap <>( beforeGroupsMethods ) ;
40+ m_afterGroupsMethods = new ConcurrentHashMap <>( afterGroupsMethods ) ;
4041 }
4142
4243 public ITestNGMethod [] getAllTestMethods () {
@@ -56,7 +57,7 @@ public Map<String, List<ITestNGMethod>> getAfterGroupsMethods() {
5657 * This method is used to figure out when is the right time to invoke
5758 * afterGroups methods.
5859 */
59- public synchronized boolean isLastMethodForGroup (String group , ITestNGMethod method ) {
60+ public boolean isLastMethodForGroup (String group , ITestNGMethod method ) {
6061
6162 // If we have more invocation to do, this is not the last one yet
6263 if (method .hasMoreInvocation ()) {
@@ -65,7 +66,11 @@ public synchronized boolean isLastMethodForGroup(String group, ITestNGMethod met
6566
6667 // Lazy initialization since we might never be called
6768 if (m_afterGroupsMap == null ) {
68- m_afterGroupsMap = initializeAfterGroupsMap ();
69+ synchronized (this ) {
70+ if (m_afterGroupsMap == null ) {
71+ m_afterGroupsMap = initializeAfterGroupsMap ();
72+ }
73+ }
6974 }
7075
7176 List <ITestNGMethod > methodsInGroup = m_afterGroupsMap .get (group );
@@ -81,8 +86,8 @@ public synchronized boolean isLastMethodForGroup(String group, ITestNGMethod met
8186
8287 }
8388
84- private synchronized Map <String , List <ITestNGMethod >> initializeAfterGroupsMap () {
85- Map <String , List <ITestNGMethod >> result = Maps .newHashMap ();
89+ private Map <String , List <ITestNGMethod >> initializeAfterGroupsMap () {
90+ Map <String , List <ITestNGMethod >> result = Maps .newConcurrentMap ();
8691 for (ITestNGMethod m : m_allMethods ) {
8792 String [] groups = m .getGroups ();
8893 for (String g : groups ) {
@@ -98,7 +103,7 @@ private synchronized Map<String, List<ITestNGMethod>> initializeAfterGroupsMap()
98103 return result ;
99104 }
100105
101- public synchronized void removeBeforeMethod (String group , ITestNGMethod method ) {
106+ public void removeBeforeMethod (String group , ITestNGMethod method ) {
102107 List <ITestNGMethod > methods = m_beforeGroupsMethods .get (group );
103108 if (methods != null ) {
104109 Object success = methods .remove (method );
@@ -115,22 +120,22 @@ private void log(String string) {
115120 Utils .log ("ConfigurationGroupMethods" , 2 , string );
116121 }
117122
118- synchronized public Map <String , List <ITestNGMethod >> getBeforeGroupsMap () {
123+ public Map <String , List <ITestNGMethod >> getBeforeGroupsMap () {
119124 return m_beforeGroupsMethods ;
120125 }
121126
122- synchronized public Map <String , List <ITestNGMethod >> getAfterGroupsMap () {
127+ public Map <String , List <ITestNGMethod >> getAfterGroupsMap () {
123128 return m_afterGroupsMethods ;
124129 }
125130
126- synchronized public void removeBeforeGroups (String [] groups ) {
131+ public void removeBeforeGroups (String [] groups ) {
127132 for (String group : groups ) {
128133// log("Removing before group " + group);
129134 m_beforeGroupsMethods .remove (group );
130135 }
131136 }
132137
133- synchronized public void removeAfterGroups (Collection <String > groups ) {
138+ public void removeAfterGroups (Collection <String > groups ) {
134139 for (String group : groups ) {
135140// log("Removing before group " + group);
136141 m_afterGroupsMethods .remove (group );
0 commit comments