View Javadoc
1   package fr.ifremer.tutti.persistence.test;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.junit.rules.TestRule;
30  import org.junit.runner.Description;
31  import org.junit.runner.notification.Failure;
32  import org.junit.runners.model.Statement;
33  
34  import java.util.HashMap;
35  import java.util.Map;
36  
37  /**
38   * Created on 2/23/15.
39   *
40   * @author Tony Chemit - chemit@codelutin.com
41   * @since 3.14
42   */
43  public class CleanResourcesRule implements TestRule {
44  
45      /** Logger. */
46      private static final Log log = LogFactory.getLog(CleanResourcesRule.class);
47  
48      private static final Map<Description, DatabaseResource> RESSOURCES_BY_DESCRIPTIONS = new HashMap<>();
49  
50      public static void registerDescription(Description description, DatabaseResource databaseResource) {
51          RESSOURCES_BY_DESCRIPTIONS.put(description, databaseResource);
52      }
53  
54      public static void cleanResources(Description description) {
55  
56          DatabaseResource databaseResource = RESSOURCES_BY_DESCRIPTIONS.get(description);
57  
58          if (databaseResource != null) {
59  
60              if (log.isInfoEnabled()) {
61                  log.info("Try to clean resources for test: " + description);
62              }
63  
64              Failure failure = TuttiRunListener.getFailureForDescription(description);
65  
66  
67              if (failure == null) {
68  
69                  if (log.isInfoEnabled()) {
70                      log.info("Clean resources (no failure found) for test: " + description);
71                  }
72                  databaseResource.cleanResources(description);
73  
74              } else {
75  
76                  if (log.isWarnEnabled()) {
77                      log.warn("Keep resources ((found failure) for test: " + description);
78                  }
79  
80              }
81  
82          }
83  
84      }
85  
86      @Override
87      public Statement apply(final Statement base, final Description description) {
88  
89          return new Statement() {
90              @Override
91              public void evaluate() throws Throwable {
92                  try {
93                      base.evaluate();
94                  } finally {
95                      after(description);
96                  }
97              }
98          };
99      }
100 
101     protected void after(Description description) {
102 
103         cleanResources(description);
104 
105         for (Description childDescription : description.getChildren()) {
106 
107             cleanResources(childDescription);
108 
109         }
110 
111     }
112 
113 }