// Path: pie-core/src/test/java/com/coverity/security/pie/core/test/TestPolicyEnforcer.java
// public class TestPolicyEnforcer implements PolicyEnforcer {
// 
//     private static TestPolicyEnforcer instance = null;
// 
//     public static TestPolicyEnforcer getInstance() {
//         return instance;
//     }
// 
//     private SimplePolicy policy;
//     private PolicyConfig policyConfig;
//     private boolean isApplied = false;
// 
//     public TestPolicyEnforcer() {
//         if (instance != null) {
//             throw new IllegalStateException("Cannot instantiate more than one TestPolicyEnforcer at a time.");
//         }
//         instance = this;
//     }
// 
//     @Override
//     public void init(PieConfig pieConfig) {
//         this.policy = new SimplePolicy();
//         this.policyConfig = new PolicyConfig(policy.getName(), pieConfig);
//     }
// 
//     @Override
//     public SimplePolicy getPolicy() {
//         return policy;
//     }
// 
//     @Override
//     public PolicyConfig getPolicyConfig() {
//         return policyConfig;
//     }
// 
//     @Override
//     public void applyPolicy(ServletContext cx) {
//         isApplied = true;
//     }
// 
//     @Override
//     public void shutdown() {
//         instance = null;
//     }
// 
//     public boolean isApplied() {
//         return isApplied;
//     }
// }
// 
// Path: pie-core/src/test/java/com/coverity/security/pie/core/test/TomcatContainerWrapper.java
// public class TomcatContainerWrapper {
//     private static class SemaphoreStartupShutdownListener implements ServletContextListener, ServletContainerInitializer {
// 
//         private final Semaphore startupSemaphore;
//         private final Semaphore shutdownSemaphore;
// 
//         public SemaphoreStartupShutdownListener() {
//             this.startupSemaphore = new Semaphore(0);
//             this.shutdownSemaphore = new Semaphore(0);
//         }
// 
//         @Override
//         public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
//             ctx.addListener(this);
//         }
// 
//         @Override
//         public void contextInitialized(ServletContextEvent sce) {
//             startupSemaphore.release();
//         }
// 
//         @Override
//         public void contextDestroyed(ServletContextEvent sce) {
//             shutdownSemaphore.release();
//         }
//     }
// 
//     private final Tomcat tomcat;
//     private final SemaphoreStartupShutdownListener semaphoreStartupShutdownListener;
// 
//     public TomcatContainerWrapper() {
//         tomcat = new Tomcat();
//         tomcat.setPort(18885);
//         tomcat.setBaseDir(".");
//         tomcat.getHost().setAppBase(".");
// 
//         semaphoreStartupShutdownListener = new SemaphoreStartupShutdownListener();
//     }
// 
//     public TomcatContainerWrapper start() throws ServletException, LifecycleException, InterruptedException, MalformedURLException {
//         Context context = tomcat.addWebapp("/myapp", ".");
//         context.addServletContainerInitializer(semaphoreStartupShutdownListener, null);
//         tomcat.start();
//         semaphoreStartupShutdownListener.startupSemaphore.acquire();
//         return this;
//     }
// 
//     public TomcatContainerWrapper stop() throws InterruptedException, LifecycleException {
//         tomcat.stop();
//         tomcat.destroy();
//         semaphoreStartupShutdownListener.shutdownSemaphore.acquire();
//         return this;
//     }
// }
// 
// Path: pie-core/src/main/java/com/coverity/security/pie/util/IOUtil.java
// public class IOUtil {
//     public static void closeSilently(Closeable closeable) {
//         if (closeable != null) {
//             try {
//                 closeable.close();
//             } catch (IOException e) {
//                 // Do nothing
//             }
//         }
//     }
//     
//     public static String readFile(File file) throws IOException {
//         return toString(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
//     }
//     public static String readFile(String file) throws IOException {
//         return toString(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
//     }
//     public static String readFile(URL file) throws IOException {
//         return toString(file.openStream());
//     }
//     
//     public static String toString(InputStream is) throws IOException {
//         try {
//             return toString(new InputStreamReader(is, StandardCharsets.UTF_8));
//         } finally {
//             closeSilently(is);
//         }
//     }
//     public static String toString(Reader reader) throws IOException {
//         StringBuilder sb = new StringBuilder();
//         try {
//             char buffer[] = new char[4096];
//             int n;
//             while ((n = reader.read(buffer)) > 0) {
//                 sb.append(buffer, 0, n);
//             }
//             reader.close();
//         } catch (IOException e) {
//             closeSilently(reader);
//             throw e;
//         }
//         return sb.toString();
//     }
//     
//     
//     public static void writeFile(File file, String content) throws IOException {
//         writeFile(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8), content);
//     }
//     private static void writeFile(Writer fw, String content) throws IOException {
//         try {
//             fw.write(content);
//             fw.close();
//         } catch (IOException e) {
//             closeSilently(fw);
//             throw e;
//         }
//     }
//     
// }


import com.coverity.security.pie.core.test.TestPolicyEnforcer;
import com.coverity.security.pie.core.test.TomcatContainerWrapper;
import com.coverity.security.pie.util.IOUtil;
import org.apache.catalina.LifecycleException;
import org.testng.annotations.Test;
import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import static org.testng.Assert.*;
package com.coverity.security.pie.web;




public class PieInitializerTest {
    @Test
    public void testPieDisabled() throws ServletException, LifecycleException, IOException, InterruptedException {
        IOUtil.writeFile(new File("target/test-classes/pieConfig.properties"), "pie.enabled = false");

// Path: pie-core/src/test/java/com/coverity/security/pie/core/test/TestPolicyEnforcer.java
// public class TestPolicyEnforcer implements PolicyEnforcer {
// 
//     private static TestPolicyEnforcer instance = null;
// 
//     public static TestPolicyEnforcer getInstance() {
//         return instance;
//     }
// 
//     private SimplePolicy policy;
//     private PolicyConfig policyConfig;
//     private boolean isApplied = false;
// 
//     public TestPolicyEnforcer() {
//         if (instance != null) {
//             throw new IllegalStateException("Cannot instantiate more than one TestPolicyEnforcer at a time.");
//         }
//         instance = this;
//     }
// 
//     @Override
//     public void init(PieConfig pieConfig) {
//         this.policy = new SimplePolicy();
//         this.policyConfig = new PolicyConfig(policy.getName(), pieConfig);
//     }
// 
//     @Override
//     public SimplePolicy getPolicy() {
//         return policy;
//     }
// 
//     @Override
//     public PolicyConfig getPolicyConfig() {
//         return policyConfig;
//     }
// 
//     @Override
//     public void applyPolicy(ServletContext cx) {
//         isApplied = true;
//     }
// 
//     @Override
//     public void shutdown() {
//         instance = null;
//     }
// 
//     public boolean isApplied() {
//         return isApplied;
//     }
// }
// 
// Path: pie-core/src/test/java/com/coverity/security/pie/core/test/TomcatContainerWrapper.java
// public class TomcatContainerWrapper {
//     private static class SemaphoreStartupShutdownListener implements ServletContextListener, ServletContainerInitializer {
// 
//         private final Semaphore startupSemaphore;
//         private final Semaphore shutdownSemaphore;
// 
//         public SemaphoreStartupShutdownListener() {
//             this.startupSemaphore = new Semaphore(0);
//             this.shutdownSemaphore = new Semaphore(0);
//         }
// 
//         @Override
//         public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
//             ctx.addListener(this);
//         }
// 
//         @Override
//         public void contextInitialized(ServletContextEvent sce) {
//             startupSemaphore.release();
//         }
// 
//         @Override
//         public void contextDestroyed(ServletContextEvent sce) {
//             shutdownSemaphore.release();
//         }
//     }
// 
//     private final Tomcat tomcat;
//     private final SemaphoreStartupShutdownListener semaphoreStartupShutdownListener;
// 
//     public TomcatContainerWrapper() {
//         tomcat = new Tomcat();
//         tomcat.setPort(18885);
//         tomcat.setBaseDir(".");
//         tomcat.getHost().setAppBase(".");
// 
//         semaphoreStartupShutdownListener = new SemaphoreStartupShutdownListener();
//     }
// 
//     public TomcatContainerWrapper start() throws ServletException, LifecycleException, InterruptedException, MalformedURLException {
//         Context context = tomcat.addWebapp("/myapp", ".");
//         context.addServletContainerInitializer(semaphoreStartupShutdownListener, null);
//         tomcat.start();
//         semaphoreStartupShutdownListener.startupSemaphore.acquire();
//         return this;
//     }
// 
//     public TomcatContainerWrapper stop() throws InterruptedException, LifecycleException {
//         tomcat.stop();
//         tomcat.destroy();
//         semaphoreStartupShutdownListener.shutdownSemaphore.acquire();
//         return this;
//     }
// }
// 
// Path: pie-core/src/main/java/com/coverity/security/pie/util/IOUtil.java
// public class IOUtil {
//     public static void closeSilently(Closeable closeable) {
//         if (closeable != null) {
//             try {
//                 closeable.close();
//             } catch (IOException e) {
//                 // Do nothing
//             }
//         }
//     }
//     
//     public static String readFile(File file) throws IOException {
//         return toString(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
//     }
//     public static String readFile(String file) throws IOException {
//         return toString(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
//     }
//     public static String readFile(URL file) throws IOException {
//         return toString(file.openStream());
//     }
//     
//     public static String toString(InputStream is) throws IOException {
//         try {
//             return toString(new InputStreamReader(is, StandardCharsets.UTF_8));
//         } finally {
//             closeSilently(is);
//         }
//     }
//     public static String toString(Reader reader) throws IOException {
//         StringBuilder sb = new StringBuilder();
//         try {
//             char buffer[] = new char[4096];
//             int n;
//             while ((n = reader.read(buffer)) > 0) {
//                 sb.append(buffer, 0, n);
//             }
//             reader.close();
//         } catch (IOException e) {
//             closeSilently(reader);
//             throw e;
//         }
//         return sb.toString();
//     }
//     
//     
//     public static void writeFile(File file, String content) throws IOException {
//         writeFile(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8), content);
//     }
//     private static void writeFile(Writer fw, String content) throws IOException {
//         try {
//             fw.write(content);
//             fw.close();
//         } catch (IOException e) {
//             closeSilently(fw);
//             throw e;
//         }
//     }
//     
// }


// Path: pie-core/src/test/java/com/coverity/security/pie/web/PieInitializerTest.java
import com.coverity.security.pie.core.test.TestPolicyEnforcer;
import com.coverity.security.pie.core.test.TomcatContainerWrapper;
import com.coverity.security.pie.util.IOUtil;
import org.apache.catalina.LifecycleException;
import org.testng.annotations.Test;
import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import static org.testng.Assert.*;

package com.coverity.security.pie.web;




public class PieInitializerTest {
    @Test
    public void testPieDisabled() throws ServletException, LifecycleException, IOException, InterruptedException {
        IOUtil.writeFile(new File("target/test-classes/pieConfig.properties"), "pie.enabled = false");

        TomcatContainerWrapper tomcat = new TomcatContainerWrapper();