]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.testing/src/org/simantics/db/testing/common/RegressionFilter.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / bundles / org.simantics.db.testing / src / org / simantics / db / testing / common / RegressionFilter.java
1 package org.simantics.db.testing.common;
2
3 import java.util.EnumSet;
4 import java.util.HashSet;
5 import java.util.Set;
6
7 import org.junit.Test;
8 import org.junit.runner.Description;
9 import org.junit.runner.manipulation.Filter;
10 import org.simantics.db.testing.annotation.Always;
11 import org.simantics.db.testing.annotation.Fails;
12 import org.simantics.db.testing.annotation.Obsolete;
13 import org.simantics.utils.FileUtils;
14
15 public class RegressionFilter extends Filter {
16
17         public static final String CLASSIFICATION_KEY = "regressionFilterClassification";
18         public static final String CLASSIFICATION_KEY_1 = "1";
19         public static final String CLASSIFICATION_KEY_2 = "2";
20         public static final String CLASSIFICATION_KEY_3 = "3";
21         public static final String CLASSIFICATION_KEY_4 = "4";
22         
23         private Failures tests;
24         
25     public RegressionFilter() {
26         super();
27         getTestCaseNames();
28     }
29
30     public String describe() {
31         return "Filters out all failing tests.";
32     }
33
34     enum Classification {
35         REGRESSION1,REGRESSION2,REGRESSION3,REGRESSION4,ALWAYS
36     }
37     
38     EnumSet<Classification> ALWAYS_CLASSES = EnumSet.of(Classification.REGRESSION1, Classification.REGRESSION2, Classification.ALWAYS);
39     
40     public Classification classify(Description d) {
41         
42         String testName = d.getDisplayName();
43 //      System.err.println("classify " + testName);
44
45         Always a = d.getAnnotation(Always.class);
46         if(a != null) return Classification.ALWAYS;
47         
48         Obsolete o = d.getAnnotation(Obsolete.class);
49         if(o != null) return Classification.REGRESSION4;
50
51         Fails f = d.getAnnotation(Fails.class); 
52         if(f != null) return Classification.REGRESSION3;
53
54         Test t = d.getAnnotation(Test.class);
55         if(t == null) return Classification.ALWAYS;
56         
57         if (tests == null) return Classification.REGRESSION1;
58
59         for (int i = 0; i < tests.names.length; i++)
60             if (testName.contains(tests.names[i]))
61                 return Classification.REGRESSION2;
62         
63         return Classification.REGRESSION1;
64         
65     }
66     
67     private Classification getPass() {
68         String s = System.getProperty(CLASSIFICATION_KEY);
69         if(CLASSIFICATION_KEY_1.equals(s)) return Classification.REGRESSION1;
70         else if(CLASSIFICATION_KEY_2.equals(s)) return Classification.REGRESSION2;
71         else if(CLASSIFICATION_KEY_3.equals(s)) return Classification.REGRESSION3;
72         else if(CLASSIFICATION_KEY_4.equals(s)) return Classification.REGRESSION4;
73         else return null;
74     }
75     
76     public boolean shouldRun(Description d) {
77         Classification cls = classify(d);
78 //      System.err.println("-" + cls);
79         if(Classification.ALWAYS == cls) return true;
80         Classification pass = getPass();
81         if(pass == null && ALWAYS_CLASSES.contains(cls)) return true;
82         return pass == cls;
83     }
84
85     private void getTestCaseNames() {
86
87         if(tests != null) return;
88         
89         try {
90                 
91             String csv = FileUtils.getContents("failures.dat").replace("\r", "");
92             tests = new Failures();
93             Set<String> ss = new HashSet<String>();
94             for(String s : csv.split("\n"))
95                 if(!s.isEmpty()) ss.add(s);
96             tests.names = ss.toArray(new String[ss.size()]);
97             
98 //            System.err.println("tests.names = " + Arrays.toString(tests.names));
99                 
100         } catch (Exception e) {
101                 
102                 tests = new Failures();
103                 tests.names = new String[] { };
104                 
105         }
106         
107     }
108     
109 }