/*
 * Copyright 2015-2016 the original author or authors.
 *
 * 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
 */

package com.example.timing;

import static org.junit.gen5.api.extension.ExtensionPointRegistry.Position.INNERMOST;

import java.lang.reflect.Method;

import org.junit.gen5.api.extension.AfterEachExtensionPoint;
import org.junit.gen5.api.extension.BeforeEachExtensionPoint;
import org.junit.gen5.api.extension.ExtensionPointRegistry;
import org.junit.gen5.api.extension.ExtensionRegistrar;
import org.junit.gen5.api.extension.TestExtensionContext;

/**
 * Simple extension that <em>times</em> the execution of test methods and
 * prints the results to {@link System#out}.
 *
 * @since 5.0
 */
public class TimingExtension implements ExtensionRegistrar {

	private static final String TIMING_KEY_PREFIX = "TIMING:";

	@Override
	public void registerExtensions(ExtensionPointRegistry registry) {
		registry.register(this::beforeEach, BeforeEachExtensionPoint.class, INNERMOST);
		registry.register(this::afterEach, AfterEachExtensionPoint.class, INNERMOST);
	}

	private void beforeEach(TestExtensionContext context) throws Exception {
		Method testMethod = context.getTestMethod();
		context.putAttribute(createKey(testMethod), System.currentTimeMillis());
	}

	private void afterEach(TestExtensionContext context) throws Exception {
		Method testMethod = context.getTestMethod();
		String key = createKey(testMethod);
		long start = (long) context.getAttribute(key);
		long end = System.currentTimeMillis();

		System.out.println(String.format("Method [%s] took %s ms", testMethod.getName(), (end - start)));
		context.removeAttribute(key);
	}

	private String createKey(Method testMethod) {
		return TIMING_KEY_PREFIX + testMethod;
	}

}

@@ -970,23 +970,16 @@
 private 
-static 
 final St
@@ -987,37 +987,40 @@
 ing 
-TIMING_KEY_PREFIX = %22TIMING:%22
+namespace = getClass().getName()
 ;%0A%0A%09
@@ -1342,87 +1342,32 @@
 %7B%0A%09%09
-Method testMethod = context.getTestMethod();%0A%09%09context.putAttribute(createKey(t
+context.put(context.getT
 estM
@@ -1371,16 +1371,17 @@
 stMethod
+(
 ), Syste
@@ -1401,16 +1401,27 @@
 Millis()
+, namespace
 );%0A%09%7D%0A%0A%09
@@ -1545,90 +1545,64 @@
 ;%0A%09%09
-String key = createKey(testMethod);%0A%09%09long start = (long) context.getAttribute(key
+long start = (long) context.remove(testMethod, namespace
 );%0A%09
@@ -1607,19 +1607,24 @@
 %0A%09%09long 
-end
+duration
  = Syste
@@ -1644,16 +1644,24 @@
 Millis()
+ - start
 ;%0A%0A%09%09Sys
@@ -1713,16 +1713,17 @@
 ok %25s ms
+.
 %22, testM
@@ -1731,159 +1731,20 @@
 thod
-.getName(), (end - start)));%0A%09%09context.removeAttribute(key);%0A%09%7D%0A%0A%09private String createKey(Method testMethod) %7B%0A%09%09return TIMING_KEY_PREFIX + testMethod
+, duration))
 ;%0A%09%7D

Refactor TimingExtension to use key/namespace support in ExtensionContext