1919package org .apache .jena .arq .junit .runners ;
2020
2121import java .util .ArrayList ;
22- import java .util .Iterator ;
2322import java .util .List ;
2423import java .util .function .Function ;
2524
2625import org .apache .jena .arq .junit .manifest .*;
27- import org .apache .jena .atlas .io .IndentedWriter ;
28- import org .apache .jena .rdf .model .Statement ;
29- import org .apache .jena .sparql .junit .EarlReport ;
30- import org .apache .jena .sparql .vocabulary .VocabTestQuery ;
26+
3127import org .junit .runner .Description ;
3228import org .junit .runner .Runner ;
3329import org .junit .runner .notification .RunNotifier ;
4743 * This class sorts out the annotations, including providing before/after class, then
4844 * creates a hierarchy of tests to run.
4945 *
50- * @see RunnerOneTest
46+ * @see SetupManifests
5147 */
5248public abstract class AbstractRunnerOfTests extends ParentRunner <Runner > {
5349 private Description description ;
@@ -57,138 +53,48 @@ public abstract class AbstractRunnerOfTests extends ParentRunner<Runner> {
5753
5854 public AbstractRunnerOfTests (Class <? > klass , Function <ManifestEntry , Runnable > maker ) throws InitializationError {
5955 super (klass );
60- String label = getLabel (klass );
56+ String label = SetupManifests . getLabel (klass );
6157 if ( label == null )
6258 label = klass .getName ();
63- String prefix = getPrefix (klass );
64- String [] manifests = getManifests (klass );
65- if ( manifests .length == 0 )
59+
60+ // Get the annotation arguments.
61+ String prefix = SetupManifests .getPrefix (klass );
62+ description = Description .createSuiteDescription (label );
63+ prepare (klass , maker ,prefix );
64+ }
65+
66+ private void prepare (Class <? > klass , Function <ManifestEntry , Runnable > maker , String prefix ) throws InitializationError {
67+ List <String > manifests = SetupManifests .getManifests (klass );
68+ if ( manifests .isEmpty () )
6669 //System.err.println("No manifests: "+label);
6770 throw new InitializationError ("No manifests" );
68- description = Description .createSuiteDescription (label );
71+ prepare (manifests , klass .getSimpleName (), maker , prefix );
72+ }
6973
74+ private void prepare (List <String > manifests , String traceName , Function <ManifestEntry , Runnable > maker , String prefix ) throws InitializationError {
75+ // For each manifest
7076 for ( String manifestFile : manifests ) {
7177 //System.out.println("** "+klass.getSimpleName()+" -- "+manifestFile);
72- if ( PrintManifests ) {
73- out .println ("** " +klass .getSimpleName ()+" -- " +manifestFile );
74- out .incIndent ();
78+ if ( SetupManifests .PrintManifests ) {
79+ if ( traceName != null )
80+ SetupManifests .out .println ("** " +traceName +" -- " +manifestFile );
81+ else
82+ SetupManifests .out .println ("** Manifest: " +manifestFile );
83+ SetupManifests .out .incIndent ();
7584 }
7685 Manifest manifest = Manifest .parse (manifestFile );
77- if ( PrintManifests ) {
86+ if ( SetupManifests . PrintManifests ) {
7887 // Record manifests.
79- Manifest .walk (manifest , m ->out .println (m .getFileName ()+" :: " +m .getName ()), e ->{});
88+ Manifest .walk (manifest , m ->SetupManifests . out .println (m .getFileName ()+" :: " +m .getName ()), e ->{});
8089 }
81- Runner runner = build (null , manifest , maker , prefix );
90+ Runner runner = SetupManifests . build (null , manifest , maker , prefix );
8291 description .addChild (runner .getDescription ());
8392 children .add (runner );
84- if ( PrintManifests )
85- out .decIndent ();
86- }
87- if ( PrintManifests )
88- out .flush ();
89- }
90-
91- // Print all manifests, top level and included.
92- private static boolean PrintManifests = false ;
93- private static IndentedWriter out = IndentedWriter .stdout ;
94-
95- /**
96- * Do one level of tests. test are {@link Runnable Runnables} that each succeed or fail with an exception.
97- */
98- public static RunnerOneManifest build (EarlReport report , Manifest manifest , Function <ManifestEntry , Runnable > maker , String prefix ) {
99- Description description = Description .createSuiteDescription (manifest .getName ());
100- if ( PrintManifests )
101- out .println (manifest .getFileName ()+" :: " +manifest .getName ());
102- RunnerOneManifest thisLevel = new RunnerOneManifest (manifest , description );
103-
104- Iterator <String > sub = manifest .includedManifests ();
105- while (sub .hasNext () ) {
106- if ( PrintManifests )
107- out .incIndent ();
108-
109- String mf = sub .next ();
110- Manifest manifestSub = Manifest .parse (mf );
111- Runner runner = build (report , manifestSub , maker , prefix );
112- thisLevel .add (runner );
113- if ( PrintManifests )
114- out .decIndent ();
115- }
116-
117- // Check entries do have test targets.
118-
119- manifest .entries ().forEach (entry ->{
120- if ( entry .getAction () == null )
121- throw new RuntimeException ("Missing: action [" +entry .getEntry ()+"]" );
122- if ( entry .getName () == null )
123- throw new RuntimeException ("Missing: label [" +entry .getEntry ()+"]" );
124- });
125-
126- prepareTests (report , thisLevel , manifest , maker , prefix );
127- return thisLevel ;
128- }
129-
130- private static String prepareTestLabel (ManifestEntry entry , String prefix ) {
131- String label = fixupName (entry .getName ());
132- if ( prefix != null )
133- label = prefix +label ;
134-
135- // action URI or action -> qt:query
136- String str = null ;
137-
138- if ( entry .getAction () != null ) {
139- if ( entry .getAction ().isURIResource () )
140- str = entry .getAction ().getURI ();
141- else if ( entry .getAction ().isAnon () ) {
142- Statement stmt = entry .getAction ().getProperty (VocabTestQuery .query );
143- if ( stmt != null && stmt .getObject ().isURIResource () )
144- str = stmt .getObject ().asResource ().getURI ();
145- }
146- }
147-
148- if ( str != null ) {
149- int x = str .lastIndexOf ('/' );
150- if ( x > 0 && x < str .length () ) {
151- String fn = str .substring (x +1 ) ;
152- label = label +" (" +fn +")" ;
153- }
154- }
155- return label ;
156- }
157-
158- public static void prepareTests (EarlReport report , RunnerOneManifest level , Manifest manifest , Function <ManifestEntry , Runnable > maker , String prefix ) {
159- manifest .entries ().forEach (entry ->{
160- String label = prepareTestLabel (entry , prefix );
161- Runnable runnable = maker .apply (entry );
162- if ( runnable != null ) {
163- Runner r = new RunnerOneTest (label , runnable , entry .getURI (), report );
164- level .add (r );
165- }
166- });
167- }
168-
169- public static String fixupName (String string ) {
170- // Eclipse used to parse test names and () were special.
171- // string = string.replace('(', '[');
172- // string = string.replace(')', ']');
173- return string ;
174- }
175-
176- private static String getLabel (Class <? > klass ) {
177- Label annotation = klass .getAnnotation (Label .class );
178- return ( annotation == null ) ? null : annotation .value ();
179- }
180-
181- private static String getPrefix (Class <? > klass ) {
182- Prefix annotation = klass .getAnnotation (Prefix .class );
183- return ( annotation == null ) ? null : annotation .value ();
184- }
185-
186- private static String [] getManifests (Class <? > klass ) throws InitializationError {
187- Manifests annotation = klass .getAnnotation (Manifests .class );
188- if ( annotation == null ) {
189- throw new InitializationError (String .format ("class '%s' must have a @Manifests annotation" , klass .getName ()));
93+ if ( SetupManifests .PrintManifests )
94+ SetupManifests .out .decIndent ();
19095 }
191- return annotation .value ();
96+ if ( SetupManifests .PrintManifests )
97+ SetupManifests .out .flush ();
19298 }
19399
194100 @ Override
0 commit comments