/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.yuanheng.cookjson;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;

import javax.json.*;
import javax.json.spi.JsonProvider;

import org.glassfish.json.JsonProviderImpl;
import org.junit.Assert;
import org.junit.Test;

/**
 * @author	Heng Yuan
 */
public class JsonReaderWriterTest
{
	private String getString (File file, JsonProvider provider) throws IOException
	{
		// create reader
		HashMap<String, Object> readConfig = new HashMap<String, Object> ();
		JsonReaderFactory rf = provider.createReaderFactory (readConfig);
		JsonReader reader = rf.createReader (new FileInputStream (file));
		JsonStructure obj = reader.read ();
		reader.close ();

		// write it out
		HashMap<String, Object> writeConfig = new HashMap<String, Object> ();
		JsonWriterFactory wf = provider.createWriterFactory (writeConfig);
		StringWriter sw = new StringWriter ();
		JsonWriter writer = wf.createWriter (sw);
		writer.write (obj);
		writer.close ();

		return sw.toString ();
	}

	@Test
	public void test () throws IOException
	{
		String f = "../tests/data/complex1.json";
		File file = new File (f.replace ('/', File.separatorChar));

		String str1 = getString (file, new CookJsonProvider ());
		String str2 = getString (file, new JsonProviderImpl ());

		// Because JsonObject's name ordering is somewhat random,
		// we cannot do string comparison.  But we can compare the
		// string length which should be equal.
		System.out.println (str1);
		System.out.println (str2);
		Assert.assertEquals (str1.length (), str2.length ());
	}
}

@@ -1285,16 +1285,30 @@
 provider
+, int readCase
 ) throws
@@ -1571,16 +1571,183 @@
 ructure 
+obj;%0D%0A%09%09switch (readCase)%0D%0A%09%09%7B%0D%0A%09%09%09case 1:%0D%0A%09%09%09%09obj = reader.readArray ();%0D%0A%09%09%09%09break;%0D%0A%09%09%09case 2:%0D%0A%09%09%09%09obj = reader.readObject ();%0D%0A%09%09%09%09break;%0D%0A%09%09%09default:%09// 0%0D%0A%09%09%09%09
 obj = re
@@ -1761,16 +1761,21 @@
 ad ();%0D%0A
+%09%09%7D%0D%0A
 %09%09reader
@@ -2119,103 +2119,84 @@
 %0A%0D%0A%09
-@Test%0D%0A%09public void test () throws IOException%0D%0A%09%7B%0D%0A%09%09String f = %22../tests/data/complex1.json%22;
+private void testFile (String fileName, int readCase) throws IOException%0D%0A%09%7B
 %0D%0A%09%09
@@ -2218,16 +2218,23 @@
  File (f
+ileName
 .replace
@@ -2280,16 +2280,43 @@
 g str1 =
+ %22%22;%0D%0A%09%09try%0D%0A%09%09%7B%0D%0A%09%09%09str1 =
  getStri
@@ -2352,70 +2352,195 @@
 r ()
-);%0D%0A%09%09String str2 = getString (file, new JsonProviderImpl ());
+, readCase);%0D%0A%09%09%7D%0D%0A%09%09catch (Exception ex)%0D%0A%09%09%7B%0D%0A%09%09%7D%0D%0A%09%09String str2 = %22%22;%0D%0A%09%09try%0D%0A%09%09%7B%0D%0A%09%09%09str2 = getString (file, new JsonProviderImpl (), readCase);%0D%0A%09%09%7D%0D%0A%09%09catch (Exception ex)%0D%0A%09%09%7B%0D%0A%09%09%7D
 %0D%0A%0D%0A
@@ -2705,16 +2705,18 @@
 equal.%0D%0A
+//
 %09%09System
@@ -2737,16 +2737,18 @@
 str1);%0D%0A
+//
 %09%09System
@@ -2829,12 +2829,230 @@
 ));%0D%0A%09%7D%0D
+%0A%0D%0A%09@Test%0D%0A%09public void test () throws IOException%0D%0A%09%7B%0D%0A%09%09for (int i = 0; i %3C 3; ++i)%0D%0A%09%09%09testFile (%22../tests/data/complex1.json%22, i);%0D%0A%09%09for (int i = 0; i %3C 3; ++i)%0D%0A%09%09%09testFile (%22../tests/data/string2.json%22, i);%0D%0A%09%7D%0D
 %0A%7D%0D%0A

Update JsonReaderWriter test.