001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright (c) 2005-2019 The ConQAT Project                               |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| 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|                                                                          |
017+-------------------------------------------------------------------------*/
018package org.conqat.lib.commons.test;
019
020import static org.assertj.core.api.Assertions.assertThat;
021
022import org.junit.jupiter.api.extension.AfterEachCallback;
023import org.junit.jupiter.api.extension.BeforeEachCallback;
024import org.junit.jupiter.api.extension.ExtensionContext;
025
026/**
027 * Extension for test cases that checks whether the test accidentally modifies
028 * the {@code user.dir} system property.
029 */
030public class CheckUserWorkingDirExtension implements BeforeEachCallback, AfterEachCallback {
031
032        /**
033         * The key for the "user working directory" system property. (usually it's just
034         * called "working directory").
035         *
036         * Not 'protected' by design, since changing the working directory in tests is
037         * an exceptional case.
038         */
039        private static final String USER_WORKING_DIR_PROPERTY_KEY = "user.dir";
040
041        /** The working-directory path before the test starts. */
042        private String workingDirBeforeTest;
043
044        /**
045         * Store the working directory, so we can check for modification after the test.
046         */
047        @Override
048        public void beforeEach(ExtensionContext context) {
049                workingDirBeforeTest = System.getProperty(USER_WORKING_DIR_PROPERTY_KEY);
050        }
051
052        /**
053         * Assert that the test does not modify the working directory, or at least
054         * resets it. Modifying the working directory would affect following tests.
055         */
056        @Override
057        public void afterEach(ExtensionContext context) {
058                if (workingDirBeforeTest != null) {
059                        assertThat(System.getProperty(USER_WORKING_DIR_PROPERTY_KEY).equals(workingDirBeforeTest))
060                                        .as("Working directory was changed and not reset during tests.").isTrue();
061                }
062        }
063}