/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */

package org.apache.logging.log4j.util;

import java.io.FilePermission;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.Permission;
import java.util.PropertyPermission;

import org.apache.logging.log4j.junit.SecurityManagerTestRule;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

/**
 * Test related to https://issues.apache.org/jira/browse/LOG4J2-2274.
 * <p>
 * Using a security manager can mess up other tests so this is best used from
 * integration tests (classes that end in "IT" instead of "Test" and
 * "TestCase".)
 * </p>
 * 
 * @see PropertyFilePropertySource
 * @see SecurityManager
 * @see System#setSecurityManager(SecurityManager)
 * @see PropertyPermission
 */
public class PropertyFilePropertySourceSecurityManagerIT {

	@Rule
	public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());

	private static final String TEST_FIXTURE_PATH = "src/test/resources/PropertiesUtilTest.properties";

	/**
	 * Always throws a SecurityException for any environment variables permission
	 * check.
	 */
	private class TestSecurityManager extends SecurityManager {

		@Override
		public void checkPermission(Permission permission) {
			if (permission instanceof FilePermission && permission.getName().endsWith(TEST_FIXTURE_PATH)) {
				throw new SecurityException();
			}
		}
	}

	/**
	 * Makes sure we do not blow up with exception below due to a security manager
	 * rejecting environment variable access in
	 * {@link SystemPropertiesPropertySource}.
	 * 
	 * <pre>
	 * </pre>
	 */
	@Test
	public void test() {
		Assert.assertTrue(TEST_FIXTURE_PATH, Files.exists(Paths.get(TEST_FIXTURE_PATH)));
		PropertiesUtil propertiesUtil = new PropertiesUtil(TEST_FIXTURE_PATH);
		Assert.assertEquals(null, propertiesUtil.getStringProperty("a.1"));
	}
}

@@ -1081,16 +1081,46 @@
 Assert;%0A
+import org.junit.BeforeClass;%0A
 import o
@@ -1130,24 +1130,24 @@
 junit.Rule;%0A
-
 import org.j
@@ -1615,16 +1615,155 @@
 erIT %7B%0A%0A
+%09@BeforeClass%0A%09public static void beforeClass() %7B%0A%09%09Assert.assertTrue(TEST_FIXTURE_PATH, Files.exists(Paths.get(TEST_FIXTURE_PATH)));%0A%09%7D%0A%09%0A
 %09@Rule%0A%09
@@ -2169,16 +2169,22 @@
 mission(
+final 
 Permissi
@@ -2587,92 +2587,14 @@
 %7B%0A%09%09
-Assert.assertTrue(TEST_FIXTURE_PATH, Files.exists(Paths.get(TEST_FIXTURE_PATH)));%0A%09%09
+final 
 Prop

Allow SystemPropertiesPropertySource to run with a SecurityManager that rejects system property access. This test passes for me on Windows 10 but fails on Jenkins. Reorder a sanity check so that it takes place before the SecurityManager test rule is evaluated.