Always use UTF-8

This commit is contained in:
Roland Praml
2022-01-11 10:11:13 +01:00
committed by FOCONIS\szemenyei
parent 0cf9f0bf58
commit 01847e44de
26 changed files with 189 additions and 117 deletions
@@ -18,7 +18,7 @@ import java.util.Locale;
* try {
* File f = new File("src/test/resources/test1.csv");
*
* FileReader reader = new FileReader(f);
* FileReader reader = new FileReader(f, encoding);
*
* CsvReader<Customer> csvReader = DB.createCsvReader(Customer.class);
*
@@ -0,0 +1,48 @@
package io.ebean.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
/**
* Utilities for IO. It uses UTF-8 as encoding when reading/writing and uses
* buffered IO for better performance.
*/
public class IOUtils {
/**
* Read from stream as UTF-8.
*/
public static BufferedReader newReader(InputStream is) {
return new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
}
/**
* Read from file as UTF-8.
*/
public static BufferedReader newReader(File file) throws FileNotFoundException {
return newReader(new FileInputStream(file));
}
/**
* Write to stream as UTF-8
*/
public static BufferedWriter newWriter(OutputStream os) {
return new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
}
/**
* Write to file as UTF-8
*/
public static BufferedWriter newWriter(File file) throws FileNotFoundException {
return newWriter(new FileOutputStream(file));
}
}
@@ -3,13 +3,13 @@ package io.ebeaninternal.server.core;
import io.ebean.ScriptRunner;
import io.ebean.ddlrunner.DdlRunner;
import io.ebean.ddlrunner.ScriptTransform;
import io.ebean.util.IOUtils;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.util.UrlHelper;
import javax.persistence.PersistenceException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.net.URL;
@@ -66,8 +66,9 @@ final class DScriptRunner implements ScriptRunner {
throw new IllegalArgumentException("resource is null?");
}
try (InputStream inputStream = UrlHelper.openNoCache(resource)) {
return readContent(new InputStreamReader(inputStream));
try (InputStream inputStream = UrlHelper.openNoCache(resource);
Reader reader = IOUtils.newReader(inputStream)) {
return readContent(reader);
} catch (IOException e) {
throw new PersistenceException("Failed to read script content", e);
@@ -4,11 +4,13 @@ import io.ebean.ProfileLocation;
import io.ebean.config.ProfilingConfig;
import io.ebean.plugin.Plugin;
import io.ebean.plugin.SpiServer;
import io.ebean.util.IOUtils;
import io.ebeaninternal.api.CoreLog;
import io.ebeaninternal.api.SpiProfileHandler;
import org.slf4j.Logger;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
@@ -123,7 +125,7 @@ public final class DefaultProfileHandler implements SpiProfileHandler, Plugin {
try {
String now = DTF.format(LocalDateTime.now());
File file = new File(dir, "txprofile-" + now + ".tprofile");
out = new BufferedWriter(new FileWriter(file));
out = IOUtils.newWriter(file);
} catch (IOException e) {
log.error("Not expected", e);
}
@@ -8,6 +8,7 @@ import io.ebean.core.type.DataReader;
import io.ebean.core.type.DocPropertyType;
import io.ebean.text.TextException;
import io.ebean.text.json.EJson;
import io.ebean.util.IOUtils;
import java.io.*;
import java.nio.charset.StandardCharsets;
@@ -75,14 +76,14 @@ abstract class ScalarTypeJsonMap extends ScalarTypeBase<Map> {
try {
if (keepSource) {
StringWriter jsonBuffer = new StringWriter();
try (InputStreamReader streamReader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
try (Reader streamReader = IOUtils.newReader(is)) {
transferTo(streamReader, jsonBuffer);
}
String rawJson = jsonBuffer.toString();
reader.pushJson(rawJson);
return parse(rawJson);
} else {
try (InputStreamReader streamReader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
try (Reader streamReader = IOUtils.newReader(is)) {
return parse(streamReader);
}
}
@@ -8,12 +8,12 @@ import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
import io.ebean.core.type.DocPropertyType;
import io.ebean.text.TextException;
import io.ebean.util.IOUtils;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
@@ -68,7 +68,7 @@ abstract class ScalarTypeJsonNode extends ScalarTypeBase<JsonNode> {
if (is == null) {
return null;
}
try (InputStreamReader reader = new InputStreamReader(is)) {
try (Reader reader = IOUtils.newReader(is)) {
return parse(reader);
} catch (IOException e) {
throw new SQLException("Error reading Blob stream from DB", e);
@@ -32,7 +32,7 @@ public final class SimpleAesEncryptor implements Encryptor {
}
private IvParameterSpec getIvParameterSpec(String initialVector) {
return new IvParameterSpec(initialVector.getBytes());
return new IvParameterSpec(initialVector.getBytes(StandardCharsets.UTF_8));
}
@Override
@@ -5,6 +5,7 @@ import io.ebean.config.DatabaseConfig;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.ddlrunner.DdlRunner;
import io.ebean.ddlrunner.ScriptTransform;
import io.ebean.util.IOUtils;
import io.ebean.util.JdbcClose;
import io.ebeaninternal.api.SpiDdlGenerator;
import io.ebeaninternal.api.SpiEbeanServer;
@@ -17,13 +18,11 @@ import org.slf4j.LoggerFactory;
import javax.persistence.PersistenceException;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.SQLException;
@@ -268,7 +267,7 @@ public class DdlGenerator implements SpiDdlGenerator {
if (is == null) {
log.warn("sql script {} was not found as a resource", sqlScript);
} else {
String content = readContent(new InputStreamReader(is));
String content = readContent(IOUtils.newReader(is)); // 'is' is closed
runScript(connection, false, content, sqlScript);
}
}
@@ -337,7 +336,7 @@ public class DdlGenerator implements SpiDdlGenerator {
protected void writeFile(String fileName, String fileContent) throws IOException {
File f = new File(baseDir, fileName);
try (FileWriter fw = new FileWriter(f)) {
try (Writer fw = IOUtils.newWriter(f)) {
fw.write(fileContent);
fw.flush();
}
@@ -348,7 +347,9 @@ public class DdlGenerator implements SpiDdlGenerator {
if (!f.exists()) {
return null;
}
return readContent(new FileReader(f));
try (Reader reader = IOUtils.newReader(f)) {
return readContent(reader);
}
}
protected String readContent(Reader reader) throws IOException {
@@ -1,8 +1,22 @@
package io.ebeaninternal.dbmigration;
import static io.ebeaninternal.api.PlatformMatch.matchPlatform;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringJoiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.ebean.DB;
import io.ebean.Database;
import io.ebean.annotation.Platform;
import io.ebean.config.ClassLoadConfig;
import io.ebean.config.DatabaseConfig;
import io.ebean.config.DbConstraintNaming;
import io.ebean.config.PlatformConfig;
@@ -27,6 +41,8 @@ import io.ebean.config.dbplatform.sqlite.SQLitePlatform;
import io.ebean.config.dbplatform.sqlserver.SqlServer16Platform;
import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform;
import io.ebean.dbmigration.DbMigration;
import io.ebean.util.IOUtils;
import io.ebean.util.StringHelper;
import io.ebeaninternal.api.DbOffline;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlOptions;
@@ -42,17 +58,6 @@ import io.ebeaninternal.dbmigration.model.PlatformDdlWriter;
import io.ebeaninternal.extraddl.model.DdlScript;
import io.ebeaninternal.extraddl.model.ExtraDdl;
import io.ebeaninternal.extraddl.model.ExtraDdlXmlReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import static io.ebeaninternal.api.PlatformMatch.matchPlatform;
/**
* Generates DB Migration xml and sql scripts.
@@ -417,7 +422,7 @@ public class DefaultDbMigration implements DbMigration {
String fullName = repeatableMigrationName(script.isInit(), script.getName());
logger.debug("writing repeatable script {}", fullName);
File file = new File(migrationDir, fullName);
try (FileWriter writer = new FileWriter(file)) {
try (Writer writer = IOUtils.newWriter(file)) {
writer.write(script.getValue());
writer.flush();
}
@@ -2,10 +2,11 @@ package io.ebeaninternal.dbmigration;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.migration.MigrationVersion;
import io.ebean.util.IOUtils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -50,17 +51,17 @@ class IndexMigration {
private void generateIndex() throws IOException {
Collections.sort(all);
FileWriter writer = new FileWriter(indexFile);
for (Entry entry : all) {
writeChecksumPadded(writer, entry.checksum);
writer.write(entry.fileName);
try (Writer writer = IOUtils.newWriter(indexFile)) {
for (Entry entry : all) {
writeChecksumPadded(writer, entry.checksum);
writer.write(entry.fileName);
writer.write(eol);
}
writer.write(eol);
}
writer.write(eol);
writer.close();
}
private void writeChecksumPadded(FileWriter writer, int checksum) throws IOException {
private void writeChecksumPadded(Writer writer, int checksum) throws IOException {
final String asStr = String.valueOf(checksum);
writer.write(asStr);
writer.write(',');
@@ -1,10 +1,11 @@
package io.ebeaninternal.dbmigration;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;
import io.ebean.util.IOUtils;
/**
* Calculates the checksum for the given file content.
*/
@@ -14,7 +15,7 @@ class MChecksum {
* Returns the checksum of the file. Agnostic of encoding and new line character.
*/
static int calculate(File file) {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")))) {
try (BufferedReader bufferedReader = IOUtils.newReader(file)) {
final CRC32 crc32 = new CRC32();
String line;
while ((line = bufferedReader.readLine()) != null) {
@@ -1,14 +1,15 @@
package io.ebeaninternal.dbmigration.migrationreader;
import io.ebean.util.IOUtils;
import io.ebeaninternal.dbmigration.migration.Migration;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* Simple writer for output of the Migration/ChangeSet as an XML document.
@@ -26,7 +27,7 @@ public class MigrationXmlWriter {
*/
public void write(Migration migration, File file) {
try (FileWriter writer = new FileWriter(file)) {
try (Writer writer = IOUtils.newWriter(file)) {
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
if (comment != null) {
@@ -2,6 +2,7 @@ package io.ebeaninternal.dbmigration.model;
import io.ebean.config.DatabaseConfig;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.util.IOUtils;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlHandler;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
@@ -14,7 +15,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
@@ -70,16 +70,16 @@ public class PlatformDdlWriter {
*/
protected void writePlatformDdl(DdlWrite write, File resourcePath, String fullVersion) throws IOException {
if (!write.isApplyEmpty()) {
try (FileWriter applyWriter = createWriter(resourcePath, fullVersion, ".sql")) {
try (Writer applyWriter = createWriter(resourcePath, fullVersion, ".sql")) {
writeApplyDdl(applyWriter, write);
applyWriter.flush();
}
}
}
protected FileWriter createWriter(File path, String fullVersion, String suffix) throws IOException {
protected Writer createWriter(File path, String fullVersion, String suffix) throws IOException {
File applyFile = new File(path, fullVersion + suffix);
return new FileWriter(applyFile);
return IOUtils.newWriter(applyFile);
}
/**
@@ -1,5 +1,6 @@
package io.ebeaninternal.dbmigration.ddlgeneration;
import io.ebean.util.IOUtils;
import io.ebeaninternal.dbmigration.migration.AddColumn;
import io.ebeaninternal.dbmigration.migration.ChangeSet;
import io.ebeaninternal.dbmigration.migration.CreateTable;
@@ -9,8 +10,8 @@ import io.ebeaninternal.dbmigration.migrationreader.MigrationXmlReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.util.List;
/**
@@ -89,8 +90,7 @@ public class Helper {
public static String asText(InputStream in) throws IOException {
try {
InputStreamReader reader = new InputStreamReader(in);
try (Reader reader = IOUtils.newReader(in)) {
LineNumberReader lineNumberReader = new LineNumberReader(reader);
@@ -4,24 +4,22 @@ import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import io.ebean.ModifyAwareType;
import io.ebean.text.json.EJson;
import io.ebean.util.IOUtils;
import io.ebeaninternal.json.ModifyAwareMap;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;
import java.nio.file.Files;
import java.util.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
public class EJsonTests {
private static final Logger log = LoggerFactory.getLogger(EJsonTests.class);
@Test
public void test_map_simple() throws IOException {
@@ -46,15 +44,14 @@ public class EJsonTests {
public void write_withWriter_expect_writerNotClosed() throws IOException {
File temp = Files.createTempFile("some", ".json").toFile();
FileWriter writer = new FileWriter(temp);
Map<String,Object> map = new LinkedHashMap<>();
map.put("foo", "bar");
EJson.write(map, writer);
writer.write("The end.");
writer.flush();
writer.close();
try (Writer writer = IOUtils.newWriter(temp)) {
log.info("write to file {}", temp.getAbsolutePath());
Map<String, Object> map = new LinkedHashMap<>();
map.put("foo", "bar");
EJson.write(map, writer);
writer.write("The end.");
}
assertThat(temp).hasContent("{\"foo\":\"bar\"}The end.");
}
@Test
@@ -11,6 +11,7 @@ import org.tests.model.basic.ResetBasicData;
import org.tests.model.basic.TBytesOnly;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
@@ -94,7 +95,7 @@ public class CachedBeanDataSerializeTest extends BaseTestCase {
TBytesOnly bean = new TBytesOnly();
bean.setId(42);
bean.setContent(stringContent.getBytes("UTF-8"));
bean.setContent(stringContent.getBytes(StandardCharsets.UTF_8));
BeanDescriptor<TBytesOnly> desc = getBeanDescriptor(TBytesOnly.class);
CachedBeanData extract = CachedBeanDataFromBean.extract(desc, (EntityBean) bean);
@@ -5,6 +5,7 @@ import io.ebean.config.EncryptKey;
import org.junit.jupiter.api.Test;
import org.tests.basic.encrypt.BasicEncryptKey;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import static org.assertj.core.api.Assertions.assertThat;
@@ -19,7 +20,7 @@ public class TestSimpleEncryptor extends BaseTestCase {
EncryptKey key = new BasicEncryptKey("hello");
byte[] data = "test123".getBytes();
byte[] data = "test123".getBytes(StandardCharsets.UTF_8);
byte[] ecData = e.encrypt(data, key);
@@ -8,12 +8,14 @@ import org.tests.model.basic.PFileContent;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.nio.charset.StandardCharsets;
public class TestDeleteImportedPartial extends BaseTestCase {
@Test
public void test() {
PFile persistentFile = new PFile("test.txt", new PFileContent("test".getBytes()));
PFile persistentFile = new PFile("test.txt", new PFileContent("test".getBytes(StandardCharsets.UTF_8)));
DB.save(persistentFile);
Integer id = persistentFile.getId();
@@ -8,13 +8,15 @@ import org.tests.model.basic.PersistentFileContent;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.nio.charset.StandardCharsets;
public class TestDeleteOneToOne extends BaseTestCase {
@Test
public void testCreateDeletePersistentFile() {
PersistentFile persistentFile = new PersistentFile("test.txt", new PersistentFileContent(
"test".getBytes()));
"test".getBytes(StandardCharsets.UTF_8)));
DB.save(persistentFile);
Integer id = persistentFile.getId();
@@ -8,16 +8,18 @@ import org.tests.model.basic.PFileContent;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.nio.charset.StandardCharsets;
public class TestDeleteOneToOneMultiple extends BaseTestCase {
@Test
public void testCreateDeletePersistentFile() {
PFile persistentFile = new PFile("test.txt", new PFileContent("test".getBytes()));
PFile persistentFile = new PFile("test.txt", new PFileContent("test".getBytes(StandardCharsets.UTF_8)));
// PFile persistentFile = new PFile();
// persistentFile.setName("test.txt");
// PFileContent content = new PFileContent();
// content.setContent("test".getBytes());
// content.setContent("test".getBytes(StandardCharsets.UTF_8));
// persistentFile.setFileContent(content);
DB.save(persistentFile);
@@ -8,12 +8,14 @@ import org.tests.model.basic.PersistentFileContent;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.nio.charset.StandardCharsets;
public class TestSaveDeleteOneToOne extends BaseTestCase {
@Test
public void testCreateDeletePersistentFile() {
PersistentFile persistentFile = new PersistentFile("test.txt",
new PersistentFileContent("test".getBytes()));
new PersistentFileContent("test".getBytes(StandardCharsets.UTF_8)));
DB.save(persistentFile);
DB.delete(persistentFile);
@@ -22,7 +24,7 @@ public class TestSaveDeleteOneToOne extends BaseTestCase {
@Test
public void testCreateLoadDeletePersistentFile() {
PersistentFile persistentFile = new PersistentFile("test.txt",
new PersistentFileContent("test".getBytes()));
new PersistentFileContent("test".getBytes(StandardCharsets.UTF_8)));
DB.save(persistentFile);
@@ -8,11 +8,13 @@ import org.tests.model.basic.PFileContent;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.nio.charset.StandardCharsets;
public class TestSaveDeleteOneToOneMultiple extends BaseTestCase {
// public void testCreateDeletePFile() {
// PFile persistentFile = new PFile("test.txt",
// new PFileContent("test".getBytes()));
// new PFileContent("test".getBytes(StandardCharsets.UTF_8)));
//
// DB.save(persistentFile);
// DB.delete(persistentFile);
@@ -21,7 +23,7 @@ public class TestSaveDeleteOneToOneMultiple extends BaseTestCase {
@Test
public void testCreateLoadDeletePFile() {
PFile persistentFile = new PFile("test.txt",
new PFileContent("test".getBytes()));
new PFileContent("test".getBytes(StandardCharsets.UTF_8)));
DB.save(persistentFile);
@@ -9,12 +9,14 @@ import org.tests.model.basic.PFileContent;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.nio.charset.StandardCharsets;
public class TestConstructorPutfieldReplacement extends BaseTestCase {
@Test
public void test() {
PFile persistentFile = new PFile("test.txt", new PFileContent("test".getBytes()));
PFile persistentFile = new PFile("test.txt", new PFileContent("test".getBytes(StandardCharsets.UTF_8)));
EntityBean eb = (EntityBean) persistentFile;
EntityBeanIntercept ebi = eb._ebean_getIntercept();
@@ -3,27 +3,25 @@ package org.tests.text.csv;
import io.ebean.DB;
import io.ebean.TransactionalTestCase;
import io.ebean.text.csv.CsvReader;
import io.ebean.util.IOUtils;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.Customer;
import org.tests.model.basic.ResetBasicData;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.net.URL;
import java.util.Locale;
public class TestCsvReader extends TransactionalTestCase {
@Test
public void test() {
public void test() throws Exception {
ResetBasicData.reset();
try {
URL resource = TestCsvReaderWithCallback.class.getResource("/test1.csv");
File f = new File(resource.getFile());
FileReader reader = new FileReader(f);
URL resource = TestCsvReaderWithCallback.class.getResource("/test1.csv");
try (Reader reader = IOUtils.newReader(resource.openStream())){
CsvReader<Customer> csvReader = DB.getDefault().createCsvReader(Customer.class);
@@ -39,9 +37,7 @@ public class TestCsvReader extends TransactionalTestCase {
csvReader.addProperty("billingAddress.country.code");
csvReader.process(reader);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@@ -4,11 +4,12 @@ import io.ebean.DB;
import io.ebean.TransactionalTestCase;
import io.ebean.text.csv.CsvReader;
import io.ebean.text.csv.DefaultCsvCallback;
import io.ebean.util.IOUtils;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.Customer;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.net.URL;
import java.util.Locale;
@@ -20,41 +21,40 @@ public class TestCsvReaderWithCallback extends TransactionalTestCase {
public void test() throws Throwable {
URL resource = TestCsvReaderWithCallback.class.getResource("/test1.csv");
File f = new File(resource.getFile());
try (Reader reader = IOUtils.newReader(resource.openStream())) {
FileReader reader = new FileReader(f);
CsvReader<Customer> csvReader = DB.getDefault().createCsvReader(Customer.class);
CsvReader<Customer> csvReader = DB.getDefault().createCsvReader(Customer.class);
csvReader.setPersistBatchSize(2);
csvReader.setLogInfoFrequency(3);
csvReader.setPersistBatchSize(2);
csvReader.setLogInfoFrequency(3);
csvReader.addIgnore();
// csvReader.addProperty("id");
csvReader.addProperty("status");
csvReader.addProperty("name");
csvReader.addDateTime("anniversary", "dd-MMM-yyyy", Locale.ENGLISH);
csvReader.addProperty("billingAddress.line1");
csvReader.addProperty("billingAddress.city");
// processor.addReference("billingAddress.country.code");
csvReader.addProperty("billingAddress.country.code");
csvReader.addIgnore();
// csvReader.addProperty("id");
csvReader.addProperty("status");
csvReader.addProperty("name");
csvReader.addDateTime("anniversary", "dd-MMM-yyyy", Locale.ENGLISH);
csvReader.addProperty("billingAddress.line1");
csvReader.addProperty("billingAddress.city");
// processor.addReference("billingAddress.country.code");
csvReader.addProperty("billingAddress.country.code");
int before = DB.find(Customer.class).findCount();
int before = DB.find(Customer.class).findCount();
csvReader.process(reader, new DefaultCsvCallback<Customer>() {
csvReader.process(reader, new DefaultCsvCallback<Customer>() {
@Override
public void processBean(int row, String[] lineContent, Customer cust) {
@Override
public void processBean(int row, String[] lineContent, Customer cust) {
server.save(cust.getBillingAddress(), transaction);
server.save(cust, transaction);
server.save(cust.getBillingAddress(), transaction);
server.save(cust, transaction);
}
}
});
});
int after = DB.find(Customer.class).findCount();
assertThat(after).isEqualTo(before + 9);
int after = DB.find(Customer.class).findCount();
assertThat(after).isEqualTo(before + 9);
}
}
}
@@ -4,9 +4,12 @@ import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.text.json.EJson;
import io.ebean.text.json.JsonContext;
import io.ebean.util.IOUtils;
import org.junit.jupiter.api.Test;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -20,19 +23,20 @@ public class TestJsonSimple extends BaseTestCase {
public void test() throws IOException {
InputStream is = this.getClass().getResourceAsStream("/example1.json");
String jsonText;
try (final Reader reader = IOUtils.newReader(is)) {
LineNumberReader lineReader = new LineNumberReader(reader);
final Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
LineNumberReader lineReader = new LineNumberReader(reader);
String readLine;
String readLine;
StringBuilder sb = new StringBuilder();
while ((readLine = lineReader.readLine()) != null) {
sb.append(readLine);
}
StringBuilder sb = new StringBuilder();
while ((readLine = lineReader.readLine()) != null) {
sb.append(readLine);
jsonText = sb.toString();
}
String jsonText = sb.toString();
Object el = EJson.parse(jsonText);
assertThat(el).isNotNull();