3939import org .junit .jupiter .api .extension .ParameterResolutionException ;
4040import org .junit .jupiter .api .extension .ParameterResolver ;
4141import org .junit .jupiter .api .io .CleanupMode ;
42+ import org .junit .platform .commons .PreconditionViolationException ;
4243import org .junit .platform .commons .support .AnnotationSupport ;
4344import org .junit .platform .commons .support .ModifierSupport ;
4445
@@ -51,8 +52,8 @@ public void beforeAll(ExtensionContext context) throws Exception {
5152 Path loggingPath = null ;
5253 for (final Field field : fields ) {
5354 if (loggingPath != null ) {
54- StatusLogger . getLogger ()
55- . warn ( "Multiple static fields with @TempLoggingDir annotation are not supported." );
55+ throw new PreconditionViolationException (
56+ "Multiple static fields with @TempLoggingDir annotation are not supported." );
5657 } else {
5758 final CleanupMode cleanup = determineCleanupMode (field );
5859 loggingPath = createLoggingPath (context , cleanup ).getPath ();
@@ -79,8 +80,8 @@ public void beforeEach(ExtensionContext context) throws Exception {
7980 final Object instance = context .getRequiredTestInstance ();
8081 for (final Field field : fields ) {
8182 if (loggingPath != null ) {
82- StatusLogger . getLogger ()
83- . warn ( "Multiple instance fields with @TempLoggingDir annotation are not supported." );
83+ throw new PreconditionViolationException (
84+ "Multiple instance fields with @TempLoggingDir annotation are not supported." );
8485 } else {
8586 final CleanupMode cleanup = determineCleanupMode (field );
8687 loggingPath = createLoggingPath (context , cleanup ).getPath ();
@@ -102,8 +103,11 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
102103 @ Override
103104 public Object resolveParameter (ParameterContext parameterContext , ExtensionContext extensionContext )
104105 throws ParameterResolutionException {
105- final TempLoggingDir annotation =
106- parameterContext .findAnnotation (TempLoggingDir .class ).get ();
106+ final TempLoggingDir annotation = parameterContext
107+ .findAnnotation (TempLoggingDir .class )
108+ .orElseThrow (() -> new PreconditionViolationException (String .format (
109+ "Missing `%s` annotation on parameter `%s`" ,
110+ TempLoggingDir .class .getSimpleName (), parameterContext )));
107111 // Get or create a temporary directory
108112 PathHolder holder = ExtensionContextAnchor .getAttribute (PathHolder .class , PathHolder .class , extensionContext );
109113 if (holder == null || !extensionContext .equals (holder .getMainContext ())) {
@@ -142,12 +146,16 @@ private Path determinePerClassPath(ExtensionContext context) {
142146 final Path basePath = (baseDir != null ? Paths .get (baseDir , "target" ) : Paths .get ("." )).resolve ("logs" );
143147 final Class <?> clazz = context .getRequiredTestClass ();
144148 final Package pkg = clazz .getPackage ();
145- final String dir =
146- pkg .getName ().replaceAll ("[.$]" , File .separatorChar == '\\' ? "\\ \\ " : File .separator );
149+ final String dir = pkg .getName ()
150+ .replaceAll ("org\\ .apache\\ .(logging\\ .)?log4j\\ ." , "" )
151+ .replaceAll ("[.$]" , File .separatorChar == '\\' ? "\\ \\ " : File .separator );
147152 // Create a temporary directory that uses the simple class name as prefix
148153 Path packagePath = basePath .resolve (dir );
149154 Files .createDirectories (packagePath );
150- return Files .createTempDirectory (packagePath , clazz .getSimpleName ());
155+ // Use a UNIX timestamp to (roughly) sort directories by execution time.
156+ return Files .createTempDirectory (
157+ packagePath ,
158+ String .format ("%s_%08x_" , clazz .getSimpleName (), System .currentTimeMillis () / 1000 ));
151159 } catch (final IOException e ) {
152160 throw new ExtensionContextException ("Failed to create temporary directory." , e );
153161 }
0 commit comments