/*
 * 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.catalina.startup;

import java.util.Set;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;

import org.apache.catalina.Context;

public class TestListener extends TomcatBaseTest {

    /**
     * Check that a ServletContainerInitializer can install a
     * {@link ServletContextListener} and that it gets initialized.
     * @throws Exception
     */
    public void testServletContainerInitializer() throws Exception {
        Tomcat tomcat = getTomcatInstance();

        Context context = tomcat.addContext("/",
                System.getProperty("java.io.tmpdir"));

        context.addServletContainerInitializer(new SCI(), null);
        tomcat.start();
        assertTrue(SCL.initialized);
    }

    /**
     * Check that a {@link ServletContextListener} cannot install a
     * {@link ServletContextInitializer}.
     * @throws Exception
     */
    public void testServletContextListener() throws Exception {
        Tomcat tomcat = getTomcatInstance();

        Context context = tomcat.addContext("/",
                System.getProperty("java.io.tmpdir"));

        // SCL2 pretends to be in web.xml, and tries to install a
        // ServletContextInitializer.
        context.addApplicationListener(SCL2.class.getName());
        tomcat.start();

        //check that the ServletContextInitializer wasn't initialized.
        assertFalse(SCL3.initialized);
    }

    public static class SCI implements ServletContainerInitializer {

        @Override
        public void onStartup(Set<Class<?>> c, ServletContext ctx)
                throws ServletException {
            ctx.addListener(new SCL());
        }
    }

    public static class SCL implements ServletContextListener {

        static boolean initialized = false;

        @Override
        public void contextInitialized(ServletContextEvent sce) {
            initialized = true;
        }

        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            // NOOP
        }
    }
    
    public static class SCL2 implements ServletContextListener {

        @Override
        public void contextInitialized(ServletContextEvent sce) {
            ServletContext sc = sce.getServletContext();
            sc.addListener(SCL3.class.getName());
        }

        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            // NOOP
        }
    }

    public static class SCL3 implements ServletContextListener {

        static boolean initialized = false;

        @Override
        public void contextInitialized(ServletContextEvent sce) {
            initialized = true;
        }

        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            // NOOP
        }
    }
}

@@ -61,19 +61,16 @@
  one
-%0A *
  or more
  con
@@ -65,16 +65,19 @@
  or more
+%0A *
  contrib
@@ -121,19 +121,16 @@
 ICE file
-%0A *
  distrib
@@ -138,16 +138,19 @@
 ted with
+%0A *
  this wo
@@ -178,19 +178,16 @@
 ormation
-%0A *
  regardi
@@ -209,17 +209,19 @@
 nership.
- 
+%0A *
  The ASF
@@ -243,16 +243,13 @@
 file
-%0A *
  to 
-y
+Y
 ou u
@@ -288,16 +288,16 @@
  2.0
+%0A *
  (the
-%0A *
  %22Li
@@ -355,16 +355,16 @@
 ance
-%0A *
  with
+%0A *
  the
@@ -416,19 +416,24 @@
 se at%0A *
+ 
 %0A *
+    
   http:/
@@ -471,16 +471,17 @@
 E-2.0%0A *
+ 
 %0A * Unle
@@ -534,19 +534,16 @@
 writing,
-%0A *
  softwar
@@ -543,16 +543,19 @@
 software
+%0A *
  distrib
@@ -597,19 +597,16 @@
 ed on an
-%0A *
  %22AS IS%22
@@ -612,16 +612,19 @@
 %22 BASIS,
+%0A *
  WITHOUT
@@ -655,19 +655,16 @@
 S OF ANY
-%0A *
  KIND, e
@@ -688,17 +688,19 @@
 implied.
- 
+%0A *
  See the
@@ -715,19 +715,16 @@
  for the
-%0A *
  specifi
@@ -759,16 +759,19 @@
 ions and
+%0A *
  limitat
@@ -774,19 +774,16 @@
 itations
-%0A *
  under t
@@ -798,18 +798,16 @@
 se.%0A */%0A
-%0A%0A
 package 

Use standard layout ASF license header