<commit_before>/**
 * Copyright (c) 2002-2011 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.gis.spatial;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.neo4j.gis.spatial.query.SearchWithin;
import org.neo4j.gis.spatial.server.plugin.SpatialPlugin;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;

import com.vividsolutions.jts.geom.Envelope;

public class ServerPluginTest extends Neo4jTestCase {

	private static final String LAYER = "layer";
	private static final String LON = "lon";
	private static final String LAT = "lat";
	private SpatialPlugin plugin;

	@Before
	public void setUp() throws Exception {
		super.setUp();
		restartTx();
		plugin = new SpatialPlugin();

	}

	@After
	public void tearDown() throws Exception {
		super.tearDown();
	}

	@Test
	public void testCreateLayer() {
		SpatialDatabaseService spatialService = new SpatialDatabaseService(graphDb());
		assertNull(spatialService.getLayer(LAYER));
		plugin.addSimplePointLayer(graphDb(), LAYER, LAT, LON);

		assertNotNull(spatialService.getLayer(LAYER));
	}

	@Test
	public void testAddPointToLayerWithDefaults() {
		SpatialDatabaseService spatialService = new SpatialDatabaseService(graphDb());
		plugin.addSimplePointLayer(graphDb(), LAYER, LAT, LON);
		assertNotNull(spatialService.getLayer(LAYER));
		Layer layer2 = spatialService.getLayer(LAYER);
		SearchWithin withinQuery = new SearchWithin(layer2.getGeometryFactory().toGeometry(new Envelope(15.0, 16.0, 60.0, 61.0)));
		layer2.getIndex().executeSearch(withinQuery);
		List<SpatialDatabaseRecord> results = withinQuery.getResults();
		assertEquals(0, results.size());

		Transaction tx2 = graphDb().beginTx();
		Node point = graphDb().createNode();
		point.setProperty(LAT, 60.1);
		point.setProperty(LON, 15.2);
		point.setProperty("bbox", new double[] { 15.2, 60.1, 15.2, 60.1 });
		plugin.addNodeToLayer(graphDb(), point, LAYER);
		plugin.addGeometryWKTToLayer(graphDb(), "POINT(15.2 60.1)", LAYER);
		tx2.success();
		tx2.finish();
		layer2.getIndex().executeSearch(withinQuery);
		results = withinQuery.getResults();
		assertEquals(2, results.size());
	}
	
	@Test
	public void testDynamicLayer() {
		SpatialDatabaseService spatialService = new SpatialDatabaseService(graphDb());
		plugin.addEditableLayer(graphDb(), LAYER);
		Layer layer = spatialService.getLayer(LAYER);
		assertNotNull("Could not find layer '" + LAYER + "'", layer);
		checkResults(plugin.addGeometryWKTToLayer(graphDb(), "POINT(15.2 60.1)", LAYER), 1, layer);
		checkResults(plugin.addGeometryWKTToLayer(graphDb(), "LINESTRING(15.1 60.2, 15.3 60.1)", LAYER), 1, layer);
		checkResults(plugin.addGeometryWKTToLayer(graphDb(), "POLYGON((15.1 60.0, 15.1 60.2, 15.2 60.2, 15.2 60.0, 15.1 60.0))", LAYER), 1, layer);
		checkResults(plugin.addGeometryWKTToLayer(graphDb(), "POINT(15.15 60.15)", LAYER), 1, layer);
		checkResults(plugin.addCQLDynamicLayer(graphDb(), LAYER, "CQL1", "Geometry", "within(the_geom, POLYGON((15.1 60.0, 15.1 60.2, 15.2 60.2, 15.2 60.0, 15.1 60.0)))"), 1, layer);
		checkResults(plugin.addCQLDynamicLayer(graphDb(), LAYER, "CQL2", "Geometry", "within(the_geom, POLYGON((15.14 60.14, 15.14 60.16, 15.16 60.16, 15.16 60.14, 15.14 60.14)))"), 1, layer);
		assertNotNull(spatialService.getLayer("CQL1"));
		assertNotNull(spatialService.getLayer("CQL2"));
		checkResults(plugin.findGeometriesInLayer(graphDb(), 15.0, 15.3, 60.0, 60.2, LAYER), 4, layer);
		checkResults(plugin.findGeometriesInLayer(graphDb(), 15.1, 15.2, 60.0, 60.2, LAYER), 2, layer);
		checkResults(plugin.findGeometriesInLayer(graphDb(), 15.0, 15.3, 60.0, 60.2, "CQL1"), 2, layer);
		checkResults(plugin.findGeometriesInLayer(graphDb(), 15.0, 15.3, 60.0, 60.2, "CQL2"), 1, layer);
	}
	
	private int checkResults(Iterable<Node> results, int expected, Layer layer) {
		int count = 0;<commit_msg>adjusting test not to use TX around the plugin test<commit_after>/**
 * Copyright (c) 2002-2011 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.gis.spatial;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.neo4j.gis.spatial.query.SearchWithin;
import org.neo4j.gis.spatial.server.plugin.SpatialPlugin;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;

import com.vividsolutions.jts.geom.Envelope;

public class ServerPluginTest extends Neo4jTestCase {

	private static final String LAYER = "layer";
	private static final String LON = "lon";
	private static final String LAT = "lat";
	private SpatialPlugin plugin;

	@Before
	public void setUp() throws Exception {
		super.setUp();
		plugin = new SpatialPlugin();

	}

	@After
	public void tearDown() throws Exception {
		super.tearDown();
	}

	@Test
	public void testCreateLayer() {
		SpatialDatabaseService spatialService = new SpatialDatabaseService(graphDb());
		assertNull(spatialService.getLayer(LAYER));
		plugin.addSimplePointLayer(graphDb(), LAYER, LAT, LON);

		assertNotNull(spatialService.getLayer(LAYER));
	}

	@Test
	public void testAddPointToLayerWithDefaults() {
		SpatialDatabaseService spatialService = new SpatialDatabaseService(graphDb());
		plugin.addSimplePointLayer(graphDb(), LAYER, LAT, LON);
		assertNotNull(spatialService.getLayer(LAYER));
		Layer layer2 = spatialService.getLayer(LAYER);
		SearchWithin withinQuery = new SearchWithin(layer2.getGeometryFactory().toGeometry(new Envelope(15.0, 16.0, 60.0, 61.0)));
		layer2.getIndex().executeSearch(withinQuery);
		List<SpatialDatabaseRecord> results = withinQuery.getResults();
		assertEquals(0, results.size());

		Transaction tx2 = graphDb().beginTx();
		Node point = graphDb().createNode();
		point.setProperty(LAT, 60.1);
		point.setProperty(LON, 15.2);
		point.setProperty("bbox", new double[] { 15.2, 60.1, 15.2, 60.1 });
		tx2.success();
		tx2.finish();
		plugin.addNodeToLayer(graphDb(), point, LAYER);
		plugin.addGeometryWKTToLayer(graphDb(), "POINT(15.2 60.1)", LAYER);
		layer2.getIndex().executeSearch(withinQuery);
		results = withinQuery.getResults();
		assertEquals(2, results.size());
	}
	
	@Test
	public void testDynamicLayer() {
		SpatialDatabaseService spatialService = new SpatialDatabaseService(graphDb());
		plugin.addEditableLayer(graphDb(), LAYER);
		Layer layer = spatialService.getLayer(LAYER);
		assertNotNull("Could not find layer '" + LAYER + "'", layer);
		checkResults(plugin.addGeometryWKTToLayer(graphDb(), "POINT(15.2 60.1)", LAYER), 1, layer);
		checkResults(plugin.addGeometryWKTToLayer(graphDb(), "LINESTRING(15.1 60.2, 15.3 60.1)", LAYER), 1, layer);
		checkResults(plugin.addGeometryWKTToLayer(graphDb(), "POLYGON((15.1 60.0, 15.1 60.2, 15.2 60.2, 15.2 60.0, 15.1 60.0))", LAYER), 1, layer);
		checkResults(plugin.addGeometryWKTToLayer(graphDb(), "POINT(15.15 60.15)", LAYER), 1, layer);
		checkResults(plugin.addCQLDynamicLayer(graphDb(), LAYER, "CQL1", "Geometry", "within(the_geom, POLYGON((15.1 60.0, 15.1 60.2, 15.2 60.2, 15.2 60.0, 15.1 60.0)))"), 1, layer);
		checkResults(plugin.addCQLDynamicLayer(graphDb(), LAYER, "CQL2", "Geometry", "within(the_geom, POLYGON((15.14 60.14, 15.14 60.16, 15.16 60.16, 15.16 60.14, 15.14 60.14)))"), 1, layer);
		assertNotNull(spatialService.getLayer("CQL1"));
		assertNotNull(spatialService.getLayer("CQL2"));
		checkResults(plugin.findGeometriesInLayer(graphDb(), 15.0, 15.3, 60.0, 60.2, LAYER), 4, layer);
		checkResults(plugin.findGeometriesInLayer(graphDb(), 15.1, 15.2, 60.0, 60.2, LAYER), 2, layer);
		checkResults(plugin.findGeometriesInLayer(graphDb(), 15.0, 15.3, 60.0, 60.2, "CQL1"), 2, layer);
		checkResults(plugin.findGeometriesInLayer(graphDb(), 15.0, 15.3, 60.0, 60.2, "CQL2"), 1, layer);
	}
	
	private int checkResults(Iterable<Node> results, int expected, Layer layer) {
		int count = 0;
{'fraction_non_alphanumeric': 0.13968288210549024, 'fraction_numerical': 0.05457879408909503, 'mean_word_length': 5.1937207748831, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6437199', 'n_tokens_mistral': 3614, 'n_tokens_neox': 3115, 'n_words': 791}