| 1 | /* |
| 2 | * Copyright (C) 2022, EfficiOS Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | package org.lttng.ust.agent.utils; |
| 20 | |
| 21 | import java.util.Optional; |
| 22 | |
| 23 | import org.junit.jupiter.api.extension.AfterTestExecutionCallback; |
| 24 | import org.junit.jupiter.api.extension.BeforeTestExecutionCallback; |
| 25 | import org.junit.jupiter.api.extension.ExtensionContext; |
| 26 | import org.junit.jupiter.api.extension.ExtensionContext.Namespace; |
| 27 | import org.junit.jupiter.api.extension.ExtensionContext.Store; |
| 28 | import org.junit.jupiter.api.extension.TestWatcher; |
| 29 | |
| 30 | /** |
| 31 | * Test extension that will print the name of the test being run to stdout. |
| 32 | */ |
| 33 | public class TestPrintExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback, TestWatcher { |
| 34 | |
| 35 | private static final String START_TIME = "start time"; |
| 36 | |
| 37 | |
| 38 | @Override |
| 39 | public void beforeTestExecution(ExtensionContext context) throws Exception { |
| 40 | System.out.println("\nStarted " + context.getDisplayName() + "\n"); |
| 41 | getStore(context).put(START_TIME, Long.valueOf(System.currentTimeMillis())); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void afterTestExecution(ExtensionContext context) throws Exception { |
| 46 | long startTime = getStore(context).remove(START_TIME, long.class).longValue(); |
| 47 | long duration = System.currentTimeMillis() - startTime; |
| 48 | |
| 49 | System.out.println("\nEnded " + context.getDisplayName() + " in " + duration + " ms\n"); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void testDisabled(ExtensionContext context, Optional<String> reason) { |
| 54 | System.out.println("\nSKIPPING TEST: " + context.getDisplayName() + "\n"); |
| 55 | } |
| 56 | |
| 57 | private Store getStore(ExtensionContext context) { |
| 58 | return context.getStore(Namespace.create(getClass(), context.getRequiredTestMethod())); |
| 59 | } |
| 60 | } |