Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

eclipse - Encoding issue with data.sql in Spring Boot with Maven

I'm trying to test a data access library which contains Spring Data JPA entites and repositories.

My SpringBootTests create a database and populates it automatically with schema-test.sql and data-test.sql

In my application.properties:

spring.datasource.initialization-mode=always
spring.datasource.platform=test
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.hibernate.ddl-auto=none

In my data-test.sql, I have some latin characters. Something like:

INSERT INTO TABLE
(ID, NAME)
VALUES
(1, 'Donnée');

My data-test.sql file is UTF-8 encoded. I doubled checked with Vim :se fenc and in Eclipse: File properties in Eclipse

When I launch my JUnit test from Eclipse, everything works, my tests pass. But when I launch a maven package goal from Eclipse, my tests fail with something like:

Expecting:
  <Optional[TableEntity [id=1, name=Donn??e]]>
to contain:
  <TableEntity [id=1, name=Donnée]>
but did not.

It seems that maven does not interpret my data-test.sql file as an UTF-8 encoded file, but as a CP-1252 encoded file.

I added the following in my pom.xml:

<properties>
    <java.version>11</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

and also:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
        <encoding>UTF-8</encoding>
        <!-- Latin1 is still the default for .properties files -->
        <propertiesEncoding>ISO-8859-1</propertiesEncoding>
    </configuration>
</plugin>

In the Eclipse "Run configuration", I added the following VM arguments :

-Dfile.encoding=UTF-8

and I also force the encoding in the "Common" tab: encoding on common tab

In the console, I find:

[DEBUG] Copying file application.properties
[DEBUG] file application.properties has a filtered file extension
[DEBUG] Using 'ISO-8859-1' encoding to copy filtered resource 'application.properties'.
[DEBUG] copy (...)srcest
esourcesapplication.properties to (...)argetest-classesapplication.properties
[DEBUG] Copying file data-test.sql
[DEBUG] file data-test.sql has a filtered file extension
[DEBUG] Using 'UTF-8' encoding to copy filtered resource 'data-test.sql'.
[DEBUG] copy (...)srcest
esourcesdata-test.sql to (...)argetest-classesdata-test.sql
[DEBUG] Copying file schema-test.sql
[DEBUG] file schema-test.sql has a filtered file extension
[DEBUG] Using 'UTF-8' encoding to copy filtered resource 'schema-test.sql'.
[DEBUG] copy (...)srcest
esourcesschema-test.sql to (...)argetest-classesschema-test.sql

Is there a hidden option somewhere forcing Maven, or my SpringBootTest class to interpret the SQL file as an old CP-1252 or ISO-8859-1 encoding?

A full example is available on GitHub.

question from:https://stackoverflow.com/questions/65885856/encoding-issue-with-data-sql-in-spring-boot-with-maven

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Show your eclipse properties. Should be "UTF-8" as well.

enter image description here

Encoding in DB

Check the encoding for the H2 database. So set hibernate.hbm2ddl.charset_name = "UTF-8" As you use spring boot you could also set as follow:

spring.datasource.connectionProperties=useUnicode=true;characterEncoding=utf-8;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...