Keepun
Keepun

Reputation: 87

Gradle 2.3 + JUnit 4.11 = broken Charset

Under normal conditions in the correct bytes written to the file, but in conjunction Gradle 2.3 + JUnit 4.11 unexpected result.

import org.junit.Test;
import java.io.FileOutputStream;
import static org.junit.Assert.*;

public class TestUTF16Test
{
   @Test
   public void testWrite() throws Exception
   {
      try (FileOutputStream bin = new FileOutputStream("testUTF16junit.txt")) {
         bin.write("Русский".getBytes("UTF-16LE"));
      }
   }
}

build.gradle

apply plugin: 'java'

sourceCompatibility = 1.7
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

$ gradle.bat :clean :test

:clean
:compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.7
1 warning

:processResources UP-TO-DATE
:classes
:compileTestJavawarning: [options] bootstrap class path not set in conjunction with -source 1.7
1 warning

:processTestResources UP-TO-DATE
:testClasses
:test

BUILD SUCCESSFUL

Total time: 4.999 secs

Result:

20 04 A0 00 21 04 53 04 21 04 03 04 21 04 03 04... 28 bytes

But it should be:

20 04 43 04 41 04 41 04 3A 04 38 04 39 04 = 14 bytes

OS: Microsoft Windows 7 [Version 6.1.7601]

Upvotes: 0

Views: 306

Answers (1)

Keepun
Keepun

Reputation: 87

Gradle fix

[ compileJava, compileTestJava ]*.options*.encoding = 'UTF-8'

2015, and javac can not recognize Unicode for BOM!

>javac.exe src\test\java\TestUTF16Test.java
src\test\java\TestUTF16Test.java:1: error: illegal character: '\u00bb'
я╗┐import org.junit.Test;
 ^
1 error

>javac.exe -version
javac 1.8.0_25

Now I understand why IDE doesn't add BOM...

Upvotes: 2

Related Questions