Bonjour,

J'essaie de mettre en place un petit code de Servlet tout simple, qui prend un String et lui applique la méthode toUpperCase.

Le code fonctionne, c'est à dire que lorsque je lance mon serveur (mvn jetty:run) l'encodage sur la page web est correct même avec des accents (je teste "çàéü").
De même mon test d'intégration fonctionne si je l'exécute depuis Eclipse pendant que mon serveur (mvn jetty:run) fonctionne.

Par contre, si les tests d'intégrations sont démarrés pendant la phase de build (mvn install), le test plante sur un problème d'encodage.
J'obtiens au passage des WARN:oeju.UrlEncoded:qtp1960514242-22: org.eclipse.jetty.util.Utf8Appendable$NotUtf8Exception: Not valid UTF8! byte E0 in state 3

Ci dessous mon code, en espérant que vous puissiez m'aider.

Merci.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class ToUpperCaseServlet extends HttpServlet {
 
    private static final long serialVersionUID = 1L;
 
    public static final String STRING_PARAMETER = "str";
 
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String req = request.getParameter(STRING_PARAMETER);
        String resp = str == null ? "" : str.toUpperCase();
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(resp);
    }
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
public class ToUpperCaseServletIT {
 
    public static final String FULL_ROOT_CONTEXT = "http://localhost:8080/MonApp/";
 
    public static final String TO_UPPER_CASE_CONTEXT = "toUpperCase";
 
    public static String readLine(String http) throws IOException {
        StringBuilder sb = new StringBuilder();
        Scanner stream = new Scanner(new URL(http).openStream());
        while (stream.hasNext())
            sb.append(stream.next());
        stream.close();
        return sb.toString();
    }
 
    public static String toUpperCaseReadLine(String parameter) throws IOException {
        if (parameter == null)
            return readLine(FULL_ROOT_CONTEXT + TO_UPPER_CASE_CONTEXT);
        else
            return readLine(FULL_ROOT_CONTEXT + TO_UPPER_CASE_CONTEXT + "?" + ToUpperCaseServlet.STRING_PARAMETER + "=" + parameter);
    }
 
    @Test
    public void testToUpperCase() throws IOException {
        assertEquals("", toUpperCaseReadLine(null));
        assertEquals("TOTO", toUpperCaseReadLine("toto"));
        assertEquals("ÇÀÉÜ", toUpperCaseReadLine("çàéü")); // échec si lancé par mvn install
    }
}
Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
 
	<groupId>mon-group-id</groupId>
	<artifactId>mon-artifact-id</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
 
	<properties>
		<maven.compiler.source>1.7</maven.compiler.source>
		<maven.compiler.target>1.7</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
 
	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
 
	<build>
		<plugins>
			<plugin>
				<groupId>org.eclipse.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>9.2.11.v20150529</version>
				<configuration>
					<webAppConfig>
						<contextPath>/MonApp</contextPath>
					</webAppConfig>
					<scanIntervalSeconds>10</scanIntervalSeconds>
					<stopPort>8005</stopPort>
					<stopKey>STOP</stopKey>
				</configuration>
				<executions>
					<execution>
						<id>start-jetty</id>
						<phase>pre-integration-test</phase>
						<goals>
							<goal>start</goal>
						</goals>
						<configuration>
							<scanIntervalSeconds>0</scanIntervalSeconds>
							<daemon>true</daemon>
						</configuration>
					</execution>
					<execution>
						<id>stop-jetty</id>
						<phase>post-integration-test</phase>
						<goals>
							<goal>stop</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-failsafe-plugin</artifactId>
				<version>2.18.1</version>
				<executions>
					<execution>
						<goals>
							<goal>integration-test</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>
Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
	<servlet>
		<servlet-name>ToUpperCaseServlet</servlet-name>
		<servlet-class>servlet.ToUpperCaseServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>ToUpperCaseServlet</servlet-name>
		<url-pattern>/toUpperCase</url-pattern>
	</servlet-mapping>
 
</web-app>