TestUtf8.java

package net.minidev.json.test;

import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.util.stream.Stream;

import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class TestUtf8 {
	public static Stream<Arguments> languages() {
		return Stream.of(Arguments.of("Sinhala", "��������������� ���������������"), Arguments.of("Japanese", "���������"),
				Arguments.of("Russian", "��������������"), Arguments.of("Farsi", "����������"), Arguments.of("Korean", "���������"),
				Arguments.of("Armenian", "��������������"), Arguments.of("Hindi", "������������������"), Arguments.of("Hebrew", "����������"),
				Arguments.of("Chinese", "������"), Arguments.of("Amharic", "������������"), Arguments.of("Malayalam", "������������������"),
				Arguments.of("Assyrian Neo-Aramaic", "������������"), Arguments.of("Georgian", "���������������������������"),
				Arguments.of("Emojis", "����������������������������������������������������������������������������������������������������������������������������������������������������������"));
	};

	@ParameterizedTest
	@MethodSource("languages")
	public void supportI18nString(String language, String nonLatinText) throws Exception {
		String json = "{\"key\":\"" + nonLatinText + "\"}";
		JSONObject obj = (JSONObject) JSONValue.parse(json);
		String actual = (String) obj.get("key");
		assertEquals(nonLatinText, actual, "Parsing String " + language + " text");
	}

	@ParameterizedTest
	@MethodSource("languages")
	public void supportI18nStringReader(String language, String nonLatinText) throws Exception {
		String json = "{\"key\":\"" + nonLatinText + "\"}";
		StringReader reader = new StringReader(json);
		JSONObject obj = (JSONObject) JSONValue.parse(reader);

		String actual = (String) obj.get("key");
		assertEquals(nonLatinText, actual, "Parsing StringReader " + language + " text");
	}

	@ParameterizedTest
	@MethodSource("languages")
	public void supportI18nByteArrayInputStream(String language, String nonLatinText) throws Exception {
		String json = "{\"key\":\"" + nonLatinText + "\"}";
		ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes("utf8"));
		JSONObject obj = (JSONObject) JSONValue.parse(bis);
		String actual = (String) obj.get("key");
		assertEquals(nonLatinText, actual, "Parsing ByteArrayInputStream " + language + " text");
	}

	@ParameterizedTest
	@MethodSource("languages")
	public void supportI18nBytes(String language, String nonLatinText) throws Exception {
		String json = "{\"key\":\"" + nonLatinText + "\"}";
		byte[] bs = json.getBytes("utf8");
		JSONObject obj = JSONValue.parse(bs, JSONObject.class);
		String actual = (String) obj.get("key");
		assertEquals(nonLatinText, actual, "Parsing bytes[] " + language + " text");
	}
}