001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.util.tester;
018
019import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
020import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
021import org.junit.jupiter.api.extension.ExtensionContext;
022import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
023
024/**
025 * Manages {@link WicketTester} instance
026 *
027 * @author igor
028 */
029public class WicketTesterExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback, TestExecutionExceptionHandler
030{
031        private WicketTester tester;
032
033        /**
034         * Allows setup of the tester instance
035         *
036         * @return tester
037         */
038        protected WicketTester create()
039        {
040                return new WicketTester();
041        }
042
043        /**
044         * Gets the tester instance.
045         *
046         * @return tester instance or {@code null} if called outside the rule's scope
047         */
048        public WicketTester getTester()
049        {
050                return tester;
051        }
052
053        @Override
054        public void beforeTestExecution(ExtensionContext context) {
055                tester = create();
056        }
057
058        @Override
059        public void afterTestExecution(ExtensionContext context) {
060                tester.destroy();
061                tester = null;
062        }
063
064        @Override
065        public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
066                tester.destroy();
067                tester = null;
068
069                throw throwable;
070        }
071}