View | Details | Raw Unified | Return to bug 52853
Collapse All | Expand All

(-)test/webapp-3.0-spring/src/main/resources/logback.xml (+23 lines)
Line 0 Link Here
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<configuration>
3
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4
        <encoder>
5
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
6
        </encoder>
7
    </appender>
8
9
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
10
        <file>pizzas-r-us.log</file>
11
        <encoder>
12
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
13
        </encoder>
14
    </appender>
15
16
    <logger name="org.springframework" level="INFO" />
17
    <logger name="com.apache.tomcat.spring.example" level="DEBUG" />
18
19
    <root level="WARN">
20
        <appender-ref ref="STDOUT" />
21
        <appender-ref ref="FILE" />
22
    </root>
23
</configuration>
(-)test/webapp-3.0-spring/src/main/java/org/apache/tomcat/spring/example/config/WebMvcContextConfiguration.java (+14 lines)
Line 0 Link Here
1
package org.apache.tomcat.spring.example.config;
2
3
import org.springframework.context.annotation.Bean;
4
import org.springframework.context.annotation.Configuration;
5
6
import org.apache.tomcat.spring.example.IndexController;
7
8
@Configuration
9
public class WebMvcContextConfiguration {
10
    @Bean
11
    public IndexController indexController() {
12
        return new IndexController();
13
    }
14
}
(-)test/webapp-3.0-spring/src/main/webapp/WEB-INF/jsp/index.jsp (+10 lines)
Line 0 Link Here
1
<html>
2
    <head>
3
        <title>Welcome</title>
4
    </head>
5
    <body>
6
        <p>Welcome from Spring through embedded Tomcat!</p>
7
        <br/>
8
        <p>Passed down variable: ${var}</p>
9
    </body>
10
</html>
(-)test/webapp-3.0-spring/src/main/java/org/apache/tomcat/spring/example/IndexController.java (+15 lines)
Line 0 Link Here
1
package org.apache.tomcat.spring.example;
2
3
import org.springframework.stereotype.Controller;
4
import org.springframework.web.bind.annotation.RequestMapping;
5
import org.springframework.web.servlet.ModelAndView;
6
7
@Controller
8
public class IndexController {
9
    @RequestMapping(value = "/index.htm")
10
    public ModelAndView indexPage() {
11
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/index.jsp");
12
        modelAndView.addObject("var", "Hello!");
13
        return modelAndView;
14
    }
15
}
(-)test/org/apache/catalina/loader/TestHandleTypesWebappLoader.java (+51 lines)
Line 0 Link Here
1
package org.apache.catalina.loader;
2
3
import org.apache.catalina.core.StandardContext;
4
import org.apache.catalina.startup.Tomcat;
5
import org.apache.catalina.startup.TomcatBaseTest;
6
import org.junit.Test;
7
8
import java.io.File;
9
import java.net.MalformedURLException;
10
11
public class TestHandleTypesWebappLoader extends TomcatBaseTest {
12
    public static final String TEST_DIR = new File("test/webapp-3.0-spring").getAbsolutePath();
13
14
    @Test
15
    public void testStartInternalForLibrary() throws Exception {
16
        Tomcat tomcat = getTomcatInstance();
17
        StandardContext context = (StandardContext)tomcat.addWebapp(null, "/spring", TEST_DIR + "/src/main/webapp");
18
        context.setLoader(new WebappLoader());
19
        context.setUnpackWAR(false);
20
21
        // Add application classes as JAR file
22
        context.getLoader().addRepository(new File(TEST_DIR, "/target/lib/app.jar").toURI().toURL().toString());
23
24
        addLibsToRepository(context);
25
        tomcat.start();
26
    }
27
28
    @Test
29
    public void testStartInternalForClassesDirectory() throws Exception {
30
        Tomcat tomcat = getTomcatInstance();
31
        StandardContext context = (StandardContext)tomcat.addWebapp(null, "/spring", TEST_DIR + "/src/main/webapp");
32
        context.setLoader(new WebappLoader());
33
        context.setUnpackWAR(false);
34
35
        // Add application classes as directory
36
        context.getLoader().addRepository(new File(TEST_DIR, "/target/classes/main").toURI().toURL().toString());
37
        context.getLoader().addRepository(new File(TEST_DIR, "/src/main/resources").toURI().toURL().toString());
38
39
        addLibsToRepository(context);
40
        tomcat.start();
41
    }
42
43
    private void addLibsToRepository(StandardContext context) throws MalformedURLException {
44
        File folder = new File(TEST_DIR, "/lib");
45
        File[] libs = folder.listFiles();
46
47
        for(File lib : libs) {
48
            context.getLoader().addRepository(lib.toURI().toURL().toString());
49
        }
50
    }
51
}
(-)test/webapp-3.0-spring/src/main/java/org/apache/tomcat/spring/example/SpringExampleWebApplicationInitializer.java (+34 lines)
Line 0 Link Here
1
package org.apache.tomcat.spring.example;
2
3
import javax.servlet.ServletContext;
4
import javax.servlet.ServletException;
5
import javax.servlet.ServletRegistration;
6
7
import org.springframework.web.WebApplicationInitializer;
8
import org.springframework.web.context.WebApplicationContext;
9
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
10
import org.springframework.web.servlet.DispatcherServlet;
11
12
import org.apache.tomcat.spring.example.config.WebMvcContextConfiguration;
13
14
public class SpringExampleWebApplicationInitializer implements WebApplicationInitializer {
15
    @Override
16
    public void onStartup(final ServletContext servletContext) throws ServletException {
17
        registerDispatcherServlet(servletContext);
18
    }
19
20
    private void registerDispatcherServlet(final ServletContext servletContext) {
21
        WebApplicationContext dispatcherContext = createContext(WebMvcContextConfiguration.class);
22
        DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);
23
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);
24
        dispatcher.setLoadOnStartup(1);
25
        dispatcher.addMapping("*.htm");
26
    }
27
28
    private WebApplicationContext createContext(final Class<?>... annotatedClasses) {
29
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
30
        context.register(annotatedClasses);
31
        context.getEnvironment().setActiveProfiles("local");
32
        return context;
33
    }
34
}

Return to bug 52853