I have an encoding based unit test in my project. The test passes in Eclipse but fails with Maven.
All my files are UTF-8 encoded and I added the 2 following lines in my pom.xml but the test keeps failing:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
Here is the interesting part of the test. Credentials
is a simple bean storing the password as a String with getter/setter (no other code there).
AuthentificationHelper helper = new AuthentificationHelper();
// ...
String password = ":&=/?é$£";
String base64Hash = Base64.encodeString(login + ":" + password);
final String authHeader = "Basic " + base64Hash;
// ...
Credentials credentials = helper.credentialsWithBasicAuthentication(request);
assertEquals(password, credentials.getPassword());
The credentialsWithBasicAuthentication
does the reverse operation of what's being done here:
StringTokenizer st = new StringTokenizer(authHeader);
//...
String credentials = new String(Base64.decodeBase64(hashedCredentials), "UTF-8");
int p = credentials.indexOf(":");
//...
String password = credentials.substring(p + 1).trim();
return new Credentials(login, password);
Here is Maven's output:
Failed tests:
testCredentialsWithBasicAuthentication: expected:<:&=/?[?$?]> but was:<:&=/?[?$?]>
(not sure if this is relevent but my log4j appender is also configured to output data in UTF-8)
Any idea what's wrong? (the surprising part being that the console output is not displayed properly either)
SOLUTION (embarassing)
The Base64
class I was using was not the one provided by Apache Commons but a random one.
Replacing:
import random.bla.bla.Base64;
// ...
String base64Hash = Base64.encodeString(login + ":" + password);
by
import org.apache.commons.codec.binary.Base64;
// ...
String base64Hash = Base64.encodeBase64String(new String(login + ":" + password).getBytes("UTF-8"));
solved the problem.
Cough, cough, tumbleweed.