package net.minidev.json.test;

import java.io.ByteArrayInputStream;
import java.io.StringReader;

import junit.framework.TestCase;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;

public class TestUtf8 extends TestCase {
	// Sinhalese language
	static String[] nonLatinTexts = new String[] { "සිංහල ජාතිය", "日本語", "Русский", "فارسی", "한국어", "Հայերեն", "हिन्दी", "עברית", "中文", "አማርኛ", "മലയാളം",
			"ܐܬܘܪܝܐ", "მარგალური" };

	public void testString() throws Exception {
		for (String nonLatinText : nonLatinTexts) {
			String s = "{\"key\":\"" + nonLatinText + "\"}";
			JSONObject obj = (JSONObject) JSONValue.parse(s);
			String v = (String) obj.get("key"); // result is incorrect
			System.out.println(v);
			assertEquals(v, nonLatinText);
		}
	}

	public void testReader() throws Exception {
		for (String nonLatinText : nonLatinTexts) {
			String s = "{\"key\":\"" + nonLatinText + "\"}";
			StringReader reader = new StringReader(s);
			JSONObject obj = (JSONObject) JSONValue.parse(reader);

			String v = (String) obj.get("key"); // result is incorrect
			System.out.println(v);
			assertEquals(v, nonLatinText);
		}
	}

	public void testInputStream() throws Exception {
		for (String nonLatinText : nonLatinTexts) {
			String s = "{\"key\":\"" + nonLatinText + "\"}";
			ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes("utf8"));
			JSONObject obj = (JSONObject) JSONValue.parse(bis);
			String v = (String) obj.get("key"); // result is incorrect
			System.out.println(v);
			assertEquals(v, nonLatinText);
		}
	}

	public void testBytes() throws Exception {
		for (String nonLatinText : nonLatinTexts) {
			String s = "{\"key\":\"" + nonLatinText + "\"}";
			byte[] bs = s.getBytes("utf8");
			JSONObject obj = (JSONObject) JSONValue.parse(bs);
			String v = (String) obj.get("key"); // result is incorrect
			System.out.println(v);
			assertEquals(v, nonLatinText);
		}
	}
}

@@ -703,34 +703,8 @@
 ect%0A
-%09%09%09System.out.println(v);%0A
 %09%09%09a
@@ -725,32 +725,32 @@
  nonLatinText);%0A
+
 %09%09%7D%0A%09%7D%0A%0A%09public 
@@ -1055,34 +1055,8 @@
 ect%0A
-%09%09%09System.out.println(v);%0A
 %09%09%09a
@@ -1438,34 +1438,8 @@
 ect%0A
-%09%09%09System.out.println(v);%0A
 %09%09%09a
@@ -1703,24 +1703,24 @@
 .parse(bs);%0A
+
 %09%09%09String v 
@@ -1773,34 +1773,8 @@
 ect%0A
-%09%09%09System.out.println(v);%0A
 %09%09%09a

remove System.out in test