8
8
import java .io .InputStream ;
9
9
import java .lang .annotation .Annotation ;
10
10
import java .util .ArrayList ;
11
- import java .util .Arrays ;
12
11
import java .util .List ;
12
+ import java .util .Objects ;
13
13
import java .util .Properties ;
14
14
import java .util .logging .Logger ;
15
- import java .util .stream .Collectors ;
16
15
import lombok .Getter ;
17
16
import org .junit .AssumptionViolatedException ;
18
17
import org .junit .experimental .categories .Category ;
@@ -29,7 +28,7 @@ public class SauceBindingsWatcher extends TestWatcher {
29
28
private final DataCenter dataCenter ;
30
29
private SauceSession session ;
31
30
private WebDriver driver ;
32
- private static final String buildName = CITools .getBuildName () + ": " + CITools .getBuildNumber ();
31
+ private static final String build = CITools .getBuildName () + ": " + CITools .getBuildNumber ();
33
32
34
33
public SauceBindingsWatcher () {
35
34
this (new SauceOptions (), DataCenter .US_WEST );
@@ -44,21 +43,29 @@ public static Builder builder() {
44
43
return new Builder ();
45
44
}
46
45
46
+ public static void enable () {
47
+ System .setProperty ("sauce.enabled" , "true" );
48
+ }
49
+
47
50
@ Override
48
51
protected void starting (Description description ) {
49
- updateOptions (description );
52
+ sauceOptions .sauce ().setBuild (build );
53
+ updateTestName (description );
54
+ updateCustomData ();
55
+ updateTags (description );
50
56
51
57
session = new SauceSession (sauceOptions );
52
58
session .setDataCenter (dataCenter );
53
59
driver = session .start ();
54
60
}
55
61
56
- private void updateOptions (Description description ) {
62
+ public void updateTestName (Description description ) {
57
63
String className = description .getClassName ();
58
64
String simpleClassName = className .substring (className .lastIndexOf ("." ) + 1 );
59
65
sauceOptions .sauce ().setName (simpleClassName + ": " + description .getMethodName ());
60
- sauceOptions . sauce (). setBuild ( buildName );
66
+ }
61
67
68
+ private void updateCustomData () {
62
69
Properties prop = new Properties ();
63
70
try (InputStream input = getClass ().getResourceAsStream ("/app.properties" )) {
64
71
prop .load (input );
@@ -67,23 +74,23 @@ private void updateOptions(Description description) {
67
74
} catch (IOException ignored ) {
68
75
sauceOptions .sauce ().getCustomData ().put ("sauce-bindings-junit4" , "unknown" );
69
76
}
77
+ }
70
78
71
- List <String > tags = sauceOptions .sauce ().getTags ();
79
+ private void updateTags (Description description ) {
80
+ List <String > tags =
81
+ Objects .requireNonNullElseGet (sauceOptions .sauce ().getTags (), ArrayList ::new );
72
82
73
83
List <Annotation > annotations = (List <Annotation >) description .getAnnotations ();
74
84
for (Annotation annotation : annotations ) {
75
85
if (annotation instanceof Category ) {
76
86
Category category = (Category ) annotation ;
77
87
for (Class <?> categoryClass : category .value ()) {
78
- if (tags == null ) {
79
- tags = new ArrayList <>();
80
- }
81
88
tags .add (categoryClass .getSimpleName ());
82
89
}
83
90
}
84
91
}
85
92
86
- if (tags != null ) {
93
+ if (! tags . isEmpty () ) {
87
94
sauceOptions .sauce ().setTags (tags );
88
95
}
89
96
}
@@ -103,14 +110,7 @@ protected void succeeded(Description description) {
103
110
protected void failed (Throwable e , Description description ) {
104
111
if (session != null ) {
105
112
try {
106
- session .annotate ("Failure Reason: " + e .getMessage ());
107
-
108
- Arrays .stream (e .getStackTrace ())
109
- .map (StackTraceElement ::toString )
110
- .filter (line -> !line .contains ("sun" ))
111
- .forEach (session ::annotate );
112
-
113
- session .stop (false );
113
+ session .stop (e );
114
114
} catch (NoSuchSessionException ex ) {
115
115
LOGGER .severe (
116
116
"Driver quit prematurely; Remove calls to `driver.quit()` to allow"
@@ -123,16 +123,7 @@ protected void failed(Throwable e, Description description) {
123
123
public void skipped (AssumptionViolatedException e , Description description ) {
124
124
LOGGER .fine ("Test Aborted: " + e .getMessage ());
125
125
if (session != null ) {
126
- session .annotate ("Test Skipped; marking completed instead of failed" );
127
- session .annotate ("Reason: " + e .getMessage ());
128
-
129
- String stackTrace =
130
- Arrays .stream (e .getStackTrace ())
131
- .map (StackTraceElement ::toString )
132
- .collect (Collectors .joining ("\n " ));
133
- session .annotate (stackTrace );
134
-
135
- session .abort ();
126
+ session .abort (e );
136
127
}
137
128
}
138
129
0 commit comments