package org.apache.jena.query.spatial;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.jena.atlas.lib.StrUtils;
import org.apache.jena.atlas.logging.Log;
import org.junit.Test;
import static org.junit.Assert.fail;

import com.spatial4j.core.exception.InvalidShapeException;

public class AbstractTestIndexingSpatialData extends
		AbstractTestDatasetWithSpatialIndex {

	@Test
	public void testIndexingStringLiteral() {
		final String turtle = StrUtils.strjoinNL(TURTLE_PROLOG, "<"
				+ RESOURCE_BASE + "testIndexingStringLiteral>",
				"   geo:lat '51.3827' ;", "   geo:long '-2.71909' ", ".");
		String queryString = StrUtils
				.strjoinNL(
						QUERY_PROLOG,
						"SELECT ?s",
						"WHERE {",
						" ?s spatial:nearby (51.3000 -2.71000 100.0 'miles' -1) .",
						"}");
		Set<String> expectedURIs = (new HashSet<String>());
		expectedURIs
				.addAll(Arrays
						.asList((new String[] { "http://example.org/data/resource/testIndexingStringLiteral" })));
		doTestSearch(turtle, queryString, expectedURIs);
	}

	@Test
	public void testIndexingWKTLiteral() {
		boolean jts_context_ready = false;
		try {
			SpatialIndex index = (SpatialIndex) dataset.getContext().get(
					SpatialQuery.spatialIndex);
			index.getDocDef().setSpatialContextFactory(
					SpatialQuery.JTS_SPATIAL_CONTEXT_FACTORY_CLASS);
			jts_context_ready = true;
		}catch (NoClassDefFoundError e){
			Log.warn(this, "JTS lib is not ready in classpath! An exception should be thrown later on!");
		}
		
		final String turtle = StrUtils
				.strjoinNL(
						TURTLE_PROLOG,
						"<" + RESOURCE_BASE + "testIndexingWKTLiteral>",
						"   wkt:asWKT 'POINT(-1.74803 52.4539)'^^wkt:wktLiteral  ",
						".");
		String queryString = StrUtils
				.strjoinNL(
						QUERY_PROLOG,
						"SELECT ?s",
						"WHERE {",
						" ?s spatial:nearby (51.3000 -2.71000 100.0 'miles' -1) .",
						"}");
		Set<String> expectedURIs = (new HashSet<String>());
		expectedURIs
				.addAll(Arrays
						.asList((new String[] { "http://example.org/data/resource/testIndexingWKTLiteral" })));
		
		try {
			doTestSearch(turtle, queryString, expectedURIs);
			if (!jts_context_ready){
				fail("An exception is supposed to be thrown for reading WKT shapes without JTS!");
			}
		}catch (InvalidShapeException e){
			if (jts_context_ready){
				fail("The exception is not supposed to be thrown: "+ e.getMessage());
			}
		}
	}

}

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

Add Apache header