File: TestJspC.java

package info (click to toggle)
tomcat11 11.0.18-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,520 kB
  • sloc: java: 370,500; xml: 56,763; jsp: 4,787; sh: 1,304; perl: 324; makefile: 25; ansic: 14
file content (170 lines) | stat: -rw-r--r-- 5,683 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * 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.jasper;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class TestJspC {

    private JspC jspc;
    private File outputDir;

    @Before
    public void init() {
        File tempDir = new File(System.getProperty("tomcat.test.temp",
                "output/tmp"));
        outputDir = new File(tempDir, "jspc");
        jspc = new JspC();
    }

    @After
    public void cleanup() throws IOException {
        remove(outputDir);
    }

    @Test
    public void precompileWebapp_2_2() throws IOException {
        File appDir = new File("test/webapp-2.2");
        File webappOut = new File(outputDir, appDir.getName());
        precompile(appDir, webappOut);
        verify(webappOut);
    }

    @Test
    public void precompileWebapp_2_3() throws IOException {
        File appDir = new File("test/webapp-2.3");
        File webappOut = new File(outputDir, appDir.getName());
        precompile(appDir, webappOut);
        verify(webappOut);
    }

    @Test
    public void precompileWebapp_2_4() throws IOException {
        File appDir = new File("test/webapp-2.4");
        File webappOut = new File(outputDir, appDir.getName());
        precompile(appDir, webappOut);
        verify(webappOut);
    }

    @Test
    public void precompileWebapp_2_5() throws IOException {
        File appDir = new File("test/webapp-2.5");
        File webappOut = new File(outputDir, appDir.getName());
        precompile(appDir, webappOut);
        verify(webappOut);
    }

    @Test
    public void precompileWebapp_3_0() throws IOException {
        File appDir = new File("test/webapp-3.0");
        File webappOut = new File(outputDir, appDir.getName());
        precompile(appDir, webappOut);
        verify(webappOut);
    }

    @Test
    public void precompileWebapp_3_1() throws IOException {
        File appDir = new File("test/webapp-3.1");
        File webappOut = new File(outputDir, appDir.getName());
        precompile(appDir, webappOut);
        verify(webappOut);
    }

    @Test
    public void precompileWebapp_4_0() throws IOException {
        File appDir = new File("test/webapp-4.0");
        File webappOut = new File(outputDir, appDir.getName());
        precompile(appDir, webappOut);
        verify(webappOut);
    }

    @Test
    public void precompileWebapp_5_0() throws IOException {
        File appDir = new File("test/webapp-5.0");
        File webappOut = new File(outputDir, appDir.getName());
        precompile(appDir, webappOut);
        verify(webappOut);
    }

    @Test
    public void precompileWebapp_6_0() throws IOException {
        File appDir = new File("test/webapp-6.0");
        File webappOut = new File(outputDir, appDir.getName());
        precompile(appDir, webappOut);
        verify(webappOut);
    }

    @Test
    public void precompileWebapp_6_1() throws IOException {
        File appDir = new File("test/webapp-6.1");
        File webappOut = new File(outputDir, appDir.getName());
        precompile(appDir, webappOut);
        verify(webappOut);
    }

    private void verify(File webappOut) {
        // for now, just check some expected files exist
        Assert.assertTrue(new File(webappOut, "generated_web.xml").exists());
        Assert.assertTrue(new File(webappOut,
                "org/apache/jsp/el_002das_002dliteral_jsp.java").exists());
        Assert.assertTrue(new File(webappOut,
                "org/apache/jsp/tld_002dversions_jsp.java").exists());
    }

    private void precompile(File appDir, File webappOut) throws IOException {
        remove(webappOut);
        Assert.assertTrue("Failed to create [" + webappOut + "]", webappOut.mkdirs());
        jspc.setUriroot(appDir.toString());
        jspc.setOutputDir(webappOut.toString());
        jspc.setValidateTld(false);
        jspc.setWebXml(new File(webappOut, "generated_web.xml").toString());
        jspc.execute();
    }


    private void remove(File base) throws IOException{
        if (!base.exists()) {
            return;
        }
        Files.walkFileTree(base.toPath(), new SimpleFileVisitor<Path>(){
            @Override
            public FileVisitResult visitFile(Path file,
                    BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir,
                    IOException exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}