JsonTest.java

/***************************************************************************
   Copyright 2015 Emily Estes

   Licensed 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 net.metanotion.json;


import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.Reader;
import java.util.HashSet;
import java.util.Iterator;

import net.metanotion.util.RecordInputStream;

public final class JsonTest {
	public static void assertTrue(final boolean result) {
		if(!result) { throw new RuntimeException("Expected true"); }
	}

	public static void main(final String[] args) throws Exception {
		try (final RecordInputStream in = new RecordInputStream(new FileInputStream(args[0]))) {
			do {
				final Reader r = new InputStreamReader(in, "UTF-8");
				final StreamingParser parser = new StreamingParser();
				final Object o = parser.parse(r, new JsonReader());
				System.out.println(o);
				System.out.println("next");
				if(in.read() != -1) { throw new AssertionError("Expected next record/EOF"); }
			} while(in.nextRecord());
		}
		try (final RecordInputStream in = new RecordInputStream(new FileInputStream(args[1]))) {
			do {
				final Reader r = new InputStreamReader(in, "UTF-8");
				final StreamingParser parser = new StreamingParser();
				try {
					final Object o = parser.parse(r, new JsonReader());
					throw new AssertionError("Expected a JSON parsing error");
				} catch (final ParserException pe) {
					// Parser errors expected
				}
				if(in.read() != -1) { throw new AssertionError("Expected next record/EOF"); }
				System.out.println("next failure");
			} while(in.nextRecord());
		}


		final HashSet<Json> h = new HashSet<>();

		final JsonArray ja = new JsonArray();
		ja.add(null);
		ja.add(new JsonObject());
		ja.add(new JsonArray());
		ja.add(5L);
		ja.add(1);
		ja.add(3.0f);
		ja.add(2.0);
		ja.add(false);
		ja.add("test");
		try {
			ja.add(new Object());
			throw new AssertionError("Can't add a bad type.");
		} catch (final IllegalArgumentException iae) {
			// Expected.
		}
		ja.put(0, null);
		ja.put(0, new JsonObject());
		ja.put(0, new JsonArray());
		ja.put(0, 5L);
		ja.put(0, 1);
		ja.put(0, 3.0f);
		ja.put(0, 2.0);
		ja.put(0, false);
		ja.put(0, "test");
		try {
			ja.put(0, new Object());
			throw new AssertionError("Can't put a bad type.");
		} catch (final IllegalArgumentException iae) {
			// Expected.
		}
		assertTrue(ja.size() == 9);
		final Iterator<Object> it = ja.iterator();
		assertTrue(it.hasNext());
		assertTrue("test".equals(it.next()));
		assertTrue("test".equals(ja.get(0)));
		assertTrue("test".equals(ja.remove(0)));
		try {
			it.remove();
			throw new AssertionError("JsonArray iterators are immutable");
		} catch (final UnsupportedOperationException uoe) {
			// Expected.
		}

		h.add(ja);
		assertTrue(h.contains(ja));


		final JsonObject jo = new JsonObject();
		jo.put("0", null);
		jo.put("1", new JsonObject());
		jo.put("2", new JsonArray());
		jo.put("3", 5L);
		jo.put("4", 1);
		jo.put("5", 3.0f);
		jo.put("6", 2.0);
		jo.put("7", false);
		jo.put("8", "test");
		try {
			jo.put("9", new Object());
			throw new AssertionError("Can't put a bad type.");
		} catch (final IllegalArgumentException iae) {
			// Expected.
		}
		assertTrue(jo.size() == 9);
		assertTrue("test".equals(jo.remove("8")));

		h.add(jo);
		assertTrue(h.contains(jo));
	}
}