JsonEqualityTest.java

/***************************************************************************
   Copyright 2014 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.BufferedReader;
import java.io.FileReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class JsonEqualityTest {
	private static final Logger logger = LoggerFactory.getLogger(JsonEqualityTest.class);

	public static void main(String[] args) throws Exception {
		final BufferedReader tests = new BufferedReader(new FileReader(args[0]));
		String type = tests.readLine();
		while(type!=null) {
			logger.debug("Type: {}", type);
			final String jsonA = tests.readLine();
			logger.debug(jsonA);
			final String jsonB = tests.readLine();
			logger.debug(jsonB);
			final boolean expected = "true".equals(tests.readLine());
			boolean result;
			if("O".equals(type)) {
				final JsonObject a = JsonObject.read(jsonA);
				final JsonObject b = JsonObject.read(jsonB);
				result = a.equals(b);
				if(b.equals(a) != result) { throw new AssertionError("Equality not transitive."); }
			} else {
				final JsonArray a = JsonArray.read(jsonA);
				final JsonArray b = JsonArray.read(jsonB);
				result = a.equals(b);
				if(b.equals(a) != result) { throw new AssertionError("Equality not transitive."); }
			}
			logger.debug("Result: {}, Expected: {}", result, expected);
			if(result != expected) { throw new AssertionError("JSON comparison produced the wrong result."); }
			tests.readLine();
			type = tests.readLine();
		}
	}
}