Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3931543dc3b73e800732fe1d0f6e6422e85b21af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*******************************************************************************
 * Copyright (c) 2011 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.skynet.core.rule;

import java.util.Collection;
import junit.framework.Assert;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactCache;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

/**
 * <p>
 * Not related to {@link Rule}.
 * </p>
 * <p>
 * Checks Artifact Cache for dirty artifacts. Executes after a passing test. Fails tests that have passed but left dirty
 * artifacts in the cache.
 * </p>
 * <p>
 * In the future the behavior of this class could be modified to clean up after a test, regardless of test's context.
 * </p>
 */
public class OseeHousekeepingRule implements MethodRule {

   @Override
   public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
      return new Statement() {
         @Override
         public void evaluate() throws Throwable {
            base.evaluate();
            verify(method.getName(), target.getClass().getName());
         }
      };
   }

   public static void verify(String methodName, String className) throws Throwable {
      final Collection<Artifact> dirtyArtifacts = ArtifactCache.getDirtyArtifacts();

      if (!dirtyArtifacts.isEmpty()) {
         StringBuilder entireMessage = new StringBuilder();
         entireMessage.append("Dirty artifacts in Artifact Cache:");
         for (Artifact artifact : dirtyArtifacts) {
            entireMessage.append(String.format("\n[%s] of type [%s] found while executing: %s.%s()",
               artifact.getName(), artifact.getArtifactType(), className, methodName));
         }
         Assert.fail(entireMessage.toString());
      }
   }
}

Back to the top