<commit_before>/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2011 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */

package org.geomajas.plugin.caching.step;

import org.geomajas.geometry.Crs;
import org.geomajas.global.GeomajasConstant;
import org.geomajas.layer.LayerException;
import org.geomajas.layer.VectorLayer;
import org.geomajas.layer.VectorLayerService;
import org.geomajas.layer.bean.BeanLayer;
import org.geomajas.layer.feature.InternalFeature;
import org.geomajas.plugin.caching.service.CacheCategory;
import org.geomajas.plugin.caching.service.CacheManagerServiceImpl;
import org.geomajas.plugin.caching.service.DummyCacheService;
import org.geomajas.service.FilterService;
import org.geomajas.service.GeoService;
import org.geomajas.service.TestRecorder;
import org.geomajas.spring.ThreadScopeContextHolder;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opengis.filter.Filter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

/**
 * Tests for the cached variant of the GetBounds pipeline.
 *
 * @author <NAME>
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/org/geomajas/spring/geomajasContext.xml",
		"/META-INF/geomajasContext.xml", "/org/geomajas/plugin/caching/DefaultCachedPipelines.xml",
		"/pipelineContext.xml", "/org/geomajas/testdata/layerBeans.xml", "/org/geomajas/testdata/layerCountries.xml",
		"/dummySecurity.xml", "/org/geomajas/spring/testRecorder.xml"})
public class GetFeaturesTest {

	private static final String LAYER_BEANS = "beans";
	private static final String LAYER_COUNTRIES = "countries";
	private static final double DELTA = 1e-10;


	@Autowired
	@Qualifier(LAYER_BEANS)
	private VectorLayer layerBeans;

	@Autowired
	@Qualifier(LAYER_COUNTRIES)
	private VectorLayer layerCountries;

	@Autowired
	private TestRecorder recorder;

	@Autowired
	private CacheManagerServiceImpl cacheManager;

	@Autowired
	private VectorLayerService vectorLayerService;

	@Autowired
	private GeoService geoService;

	@Autowired
	private org.geomajas.security.SecurityManager securityManager;

	@Autowired
	private FilterService filterService;

	private Crs wgs84;
	private Crs mercator;

	@Before
	public void init() throws LayerException {
		cacheManager.drop(layerBeans);
		securityManager.createSecurityContext(null); // assure a security context exists for this thread
		wgs84 = geoService.getCrs2("EPSG:4326");
		mercator = geoService.getCrs2("EPSG:900913");
	}

	@After
	public void clearSecurityContext() {
		cacheManager.drop(layerBeans);
		cacheManager.drop(layerCountries);
		recorder.clear();
		ThreadScopeContextHolder.clear();
	}

	@Test
	public void testFeatures() throws Exception {
		List<InternalFeature> features;

		// first run, this should put things in the cache
		recorder.clear();
		features = vectorLayerService.getFeatures(LAYER_BEANS, wgs84, null, null,
				GeomajasConstant.FEATURE_INCLUDE_NONE);
		Assert.assertNotNull(features);
		Assert.assertEquals(3, features.size());
		Assert.assertEquals("", recorder.matches(CacheCategory.FEATURE,
				"Put item in cache"));

		// verify that data is in the cache
		DummyCacheService cache = (DummyCacheService)cacheManager.getCacheForTesting(LAYER_BEANS, CacheCategory.FEATURE);
		Assert.assertEquals(1, cache.size());
		FeaturesCacheContainer bcc = (FeaturesCacheContainer) cache.getObject();
		bcc.getFeatures().remove(2);

		// get features again, the result should be different because we changed the cached value
		recorder.clear();
		features = vectorLayerService.getFeatures(LAYER_BEANS, wgs84, null, null,
				GeomajasConstant.FEATURE_INCLUDE_NONE);
		Assert.assertNotNull(features);
		Assert.assertEquals(2, features.size());
		Assert.assertEquals("", recorder.matches(CacheCategory.FEATURE,
				"Got item from cache"));

		// ask for different layer, should not be found in cache as context is different
		recorder.clear();
		features = vectorLayerService.getFeatures(LAYER_COUNTRIES, wgs84, null, null,
				GeomajasConstant.FEATURE_INCLUDE_NONE);
		Assert.assertNotNull(features);
		Assert.assertEquals(4, features.size());
		Assert.assertEquals("", recorder.matches(CacheCategory.FEATURE,
				"Put item in cache"));
	}

	@Test
	public void testFeaturesLazyConverted() throws Exception {
		((BeanLayer)layerBeans).setUseLazyFeatureConversion(true);

		List<InternalFeature> features;

		// first run, this should put things in the cache
		recorder.clear();
		features = vectorLayerService.getFeatures(LAYER_BEANS, wgs84, null, null,<commit_msg>GBE-175 fix use of DirtiesContext, should make tests run faster<commit_after>/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2011 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */

package org.geomajas.plugin.caching.step;

import org.geomajas.geometry.Crs;
import org.geomajas.global.GeomajasConstant;
import org.geomajas.layer.LayerException;
import org.geomajas.layer.VectorLayer;
import org.geomajas.layer.VectorLayerService;
import org.geomajas.layer.bean.BeanLayer;
import org.geomajas.layer.feature.InternalFeature;
import org.geomajas.plugin.caching.service.CacheCategory;
import org.geomajas.plugin.caching.service.CacheManagerServiceImpl;
import org.geomajas.plugin.caching.service.DummyCacheService;
import org.geomajas.service.FilterService;
import org.geomajas.service.GeoService;
import org.geomajas.service.TestRecorder;
import org.geomajas.spring.ThreadScopeContextHolder;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opengis.filter.Filter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

/**
 * Tests for the cached variant of the GetBounds pipeline.
 *
 * @author <NAME>
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/org/geomajas/spring/geomajasContext.xml",
		"/META-INF/geomajasContext.xml", "/org/geomajas/plugin/caching/DefaultCachedPipelines.xml",
		"/pipelineContext.xml", "/org/geomajas/testdata/layerBeans.xml", "/org/geomajas/testdata/layerCountries.xml",
		"/dummySecurity.xml", "/org/geomajas/spring/testRecorder.xml"})
public class GetFeaturesTest {

	private static final String LAYER_BEANS = "beans";
	private static final String LAYER_COUNTRIES = "countries";
	private static final double DELTA = 1e-10;


	@Autowired
	@Qualifier(LAYER_BEANS)
	private VectorLayer layerBeans;

	@Autowired
	@Qualifier(LAYER_COUNTRIES)
	private VectorLayer layerCountries;

	@Autowired
	private TestRecorder recorder;

	@Autowired
	private CacheManagerServiceImpl cacheManager;

	@Autowired
	private VectorLayerService vectorLayerService;

	@Autowired
	private GeoService geoService;

	@Autowired
	private org.geomajas.security.SecurityManager securityManager;

	@Autowired
	private FilterService filterService;

	private Crs wgs84;
	private Crs mercator;

	@Before
	public void init() throws LayerException {
		cacheManager.drop(layerBeans);
		securityManager.createSecurityContext(null); // assure a security context exists for this thread
		wgs84 = geoService.getCrs2("EPSG:4326");
		mercator = geoService.getCrs2("EPSG:900913");
	}

	@After
	public void clearSecurityContext() {
		cacheManager.drop(layerBeans);
		cacheManager.drop(layerCountries);
		recorder.clear();
		ThreadScopeContextHolder.clear();
	}

	@Test
	public void testFeatures() throws Exception {
		List<InternalFeature> features;

		// first run, this should put things in the cache
		recorder.clear();
		features = vectorLayerService.getFeatures(LAYER_BEANS, wgs84, null, null,
				GeomajasConstant.FEATURE_INCLUDE_NONE);
		Assert.assertNotNull(features);
		Assert.assertEquals(3, features.size());
		Assert.assertEquals("", recorder.matches(CacheCategory.FEATURE,
				"Put item in cache"));

		// verify that data is in the cache
		DummyCacheService cache = (DummyCacheService)cacheManager.getCacheForTesting(LAYER_BEANS, CacheCategory.FEATURE);
		Assert.assertEquals(1, cache.size());
		FeaturesCacheContainer bcc = (FeaturesCacheContainer) cache.getObject();
		bcc.getFeatures().remove(2);

		// get features again, the result should be different because we changed the cached value
		recorder.clear();
		features = vectorLayerService.getFeatures(LAYER_BEANS, wgs84, null, null,
				GeomajasConstant.FEATURE_INCLUDE_NONE);
		Assert.assertNotNull(features);
		Assert.assertEquals(2, features.size());
		Assert.assertEquals("", recorder.matches(CacheCategory.FEATURE,
				"Got item from cache"));

		// ask for different layer, should not be found in cache as context is different
		recorder.clear();
		features = vectorLayerService.getFeatures(LAYER_COUNTRIES, wgs84, null, null,
				GeomajasConstant.FEATURE_INCLUDE_NONE);
		Assert.assertNotNull(features);
		Assert.assertEquals(4, features.size());
		Assert.assertEquals("", recorder.matches(CacheCategory.FEATURE,
				"Put item in cache"));
	}

	@Test
	@DirtiesContext //changing lazy loading in layerBeans
	public void testFeaturesLazyConverted() throws Exception {
		((BeanLayer)layerBeans).setUseLazyFeatureConversion(true);

		List<InternalFeature> features;

		// first run, this should put things in the cache
		recorder.clear();
		features = vectorLayerService.getFeatures(LAYER_BEANS, wgs84, null, null,
{'fraction_non_alphanumeric': 0.10126582278481013, 'fraction_numerical': 0.008534714230916762, 'mean_word_length': 5.6342239185750635, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 16}, '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': '6445817', 'n_tokens_mistral': 3399, 'n_tokens_neox': 3131, 'n_words': 718}