diff --git a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/FailingIndexTests.java b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/FailingIndexTests.java new file mode 100644 index 000000000..b86904ebd --- /dev/null +++ b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/FailingIndexTests.java @@ -0,0 +1,103 @@ +/** + * Copyright 2011 the original author or authors. + * + * 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 org.springframework.data.neo4j.aspects.support; + +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.neo4j.graphdb.Node; +import org.neo4j.graphdb.index.Index; +import org.springframework.data.neo4j.annotation.Indexed; +import org.springframework.data.neo4j.annotation.NodeEntity; +import org.springframework.data.neo4j.aspects.Friendship; +import org.springframework.data.neo4j.aspects.Group; +import org.springframework.data.neo4j.aspects.Person; +import org.springframework.data.neo4j.aspects.SubGroup; +import org.springframework.data.neo4j.repository.GraphRepository; +import org.springframework.data.neo4j.support.index.IndexType; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.CleanContextCacheTestExecutionListener; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; +import org.springframework.test.context.transaction.TransactionalTestExecutionListener; +import org.springframework.transaction.annotation.Transactional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.springframework.data.neo4j.aspects.Person.persistedPerson; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath:org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml"}) +@TestExecutionListeners({CleanContextCacheTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD, hierarchyMode = DirtiesContext.HierarchyMode.EXHAUSTIVE) + +public class FailingIndexTests extends EntityTestBase { + + private static final String NAME_VALUE = "aName"; + private static final String NAME_VALUE2 = "aSecondName"; + + @Test + @Transactional + public void testFindGroupByInstanceIndex() { + Group group = persist(new SubGroup()); + group.setIndexLevelName("indexLevelNameValue"); + Index subGroupIndex = neo4jTemplate.getIndex(SubGroup.class); + final Node found = subGroupIndex.get("indexLevelName", "indexLevelNameValue").getSingle(); + final SubGroup foundEntity = neo4jTemplate.createEntityFromState(found, SubGroup.class, neo4jTemplate.getMappingPolicy(SubGroup.class)); + assertEquals(group, foundEntity); + } + + @NodeEntity + static class InvalidIndexed { + + @Indexed(indexType=IndexType.FULLTEXT) + String fulltextNoIndexName; + + public void setFulltextNoIndexName(String fulltextNoIndexName) { + this.fulltextNoIndexName = fulltextNoIndexName; + } + } + + @Test(expected = IllegalStateException.class) + @Transactional + public void indexAccessWithFullAndNoIndexNameShouldFail() { + InvalidIndexed invalidIndexed = persist(new InvalidIndexed()); + invalidIndexed.setFulltextNoIndexName(NAME_VALUE); + } + + @Test + @Transactional + public void testDontFindGroupByNonIndexedFieldWithAnnotation() { + Group group = persist(new Group()); + group.setUnindexedName("value-unindexedName"); + final Group found = this.groupRepository.findByPropertyValue("unindexedName", "value-unindexedName"); + assertNull(found); + } + + @Test + @Transactional + public void testCanIndexIntFieldsOnRelationshipEntities() { + Person p = persistedPerson(NAME_VALUE, 35); + Person p2 = persistedPerson(NAME_VALUE2, 25); + Friendship friendship = p.knows(p2); + friendship.setYears(1); + GraphRepository friendshipFinder = neo4jTemplate.repositoryFor(Friendship.class); + assertEquals(friendship, friendshipFinder.findByPropertyValue("Friendship.years", 1)); + } +} diff --git a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/GraphRepositoryTests.java b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/GraphRepositoryTests.java index 0ec065d6c..e38e141da 100644 --- a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/GraphRepositoryTests.java +++ b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/GraphRepositoryTests.java @@ -25,6 +25,7 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.neo4j.aspects.Group; import org.springframework.data.neo4j.aspects.Person; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.CleanContextCacheTestExecutionListener; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; diff --git a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/IndexTests.java b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/IndexTests.java index 468275b9d..f1350d92c 100644 --- a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/IndexTests.java +++ b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/IndexTests.java @@ -17,6 +17,7 @@ package org.springframework.data.neo4j.aspects.support; import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.neo4j.graphdb.DynamicRelationshipType; @@ -55,8 +56,9 @@ import static org.springframework.data.neo4j.aspects.Person.persistedPerson; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml"}) -@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) -//@TestExecutionListeners({CleanContextCacheTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) +@TestExecutionListeners({CleanContextCacheTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD, hierarchyMode = DirtiesContext.HierarchyMode.EXHAUSTIVE) + public class IndexTests extends EntityTestBase { private static final String NAME = "name"; @@ -66,6 +68,7 @@ public class IndexTests extends EntityTestBase { @Test @Transactional + @Ignore public void testCanIndexIntFieldsOnRelationshipEntities() { Person p = persistedPerson(NAME_VALUE, 35); Person p2 = persistedPerson(NAME_VALUE2, 25); @@ -132,6 +135,7 @@ public class IndexTests extends EntityTestBase { @Test @Transactional + @Ignore public void testFindGroupByInstanceIndex() { Group group = persist(new SubGroup()); group.setIndexLevelName("indexLevelNameValue"); @@ -226,6 +230,7 @@ public class IndexTests extends EntityTestBase { @Test(expected = IllegalStateException.class) @Transactional + @Ignore public void indexAccessWithFullAndNoIndexNameShouldFail() { InvalidIndexed invalidIndexed = persist(new InvalidIndexed()); invalidIndexed.setFulltextNoIndexName(NAME_VALUE); @@ -241,6 +246,7 @@ public class IndexTests extends EntityTestBase { @Test @Transactional + @Ignore public void testDontFindGroupByNonIndexedFieldWithAnnotation() { Group group = persist(new Group()); group.setUnindexedName("value-unindexedName"); diff --git a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/NodeEntityQueryTests.java b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/NodeEntityQueryTests.java index 5fefbe12b..bbde2c53e 100644 --- a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/NodeEntityQueryTests.java +++ b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/NodeEntityQueryTests.java @@ -22,8 +22,12 @@ import org.junit.runner.RunWith; import org.neo4j.helpers.collection.IteratorUtil; import org.springframework.data.neo4j.aspects.Group; import org.springframework.data.neo4j.aspects.Person; +import org.springframework.test.context.CleanContextCacheTestExecutionListener; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; +import org.springframework.test.context.transaction.TransactionalTestExecutionListener; import org.springframework.transaction.annotation.Transactional; import java.util.Collection; @@ -41,6 +45,7 @@ import static org.hamcrest.Matchers.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml"}) @Transactional +@TestExecutionListeners({CleanContextCacheTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) public class NodeEntityQueryTests extends EntityTestBase { private TestTeam testTeam; private Person michael; diff --git a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/NodeEntityRelationshipTests.java b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/NodeEntityRelationshipTests.java index 9e7923d50..9e683db85 100644 --- a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/NodeEntityRelationshipTests.java +++ b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/NodeEntityRelationshipTests.java @@ -26,8 +26,12 @@ import org.springframework.data.neo4j.aspects.Friendship; import org.springframework.data.neo4j.aspects.Group; import org.springframework.data.neo4j.aspects.Mentorship; import org.springframework.data.neo4j.aspects.Person; +import org.springframework.test.context.CleanContextCacheTestExecutionListener; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; +import org.springframework.test.context.transaction.TransactionalTestExecutionListener; import org.springframework.transaction.annotation.Transactional; import java.util.Arrays; @@ -46,6 +50,7 @@ import static org.springframework.data.neo4j.aspects.Person.persistedPerson; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml"}) +@TestExecutionListeners({CleanContextCacheTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) public class NodeEntityRelationshipTests extends EntityTestBase { diff --git a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/NodeEntityTests.java b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/NodeEntityTests.java index d70202391..b8b4752a8 100644 --- a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/NodeEntityTests.java +++ b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/NodeEntityTests.java @@ -25,8 +25,12 @@ import org.springframework.dao.DataRetrievalFailureException; import org.springframework.data.neo4j.aspects.Attribute; import org.springframework.data.neo4j.aspects.Group; import org.springframework.data.neo4j.aspects.Person; +import org.springframework.test.context.CleanContextCacheTestExecutionListener; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; +import org.springframework.test.context.transaction.TransactionalTestExecutionListener; import org.springframework.transaction.annotation.Transactional; import javax.validation.ValidationException; @@ -41,6 +45,7 @@ import static org.springframework.data.neo4j.aspects.Person.persistedPerson; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml"}) +@TestExecutionListeners({CleanContextCacheTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) public class NodeEntityTests extends EntityTestBase { diff --git a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/query/QueryEngineTests.java b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/query/QueryEngineTests.java index 179641cbc..1cdf61834 100644 --- a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/query/QueryEngineTests.java +++ b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/query/QueryEngineTests.java @@ -38,8 +38,12 @@ import org.springframework.data.neo4j.support.DelegatingGraphDatabase; import org.springframework.data.neo4j.support.Neo4jTemplate; import org.springframework.data.neo4j.support.conversion.EntityResultConverter; import org.springframework.data.neo4j.support.query.QueryEngine; +import org.springframework.test.context.CleanContextCacheTestExecutionListener; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; +import org.springframework.test.context.transaction.TransactionalTestExecutionListener; import org.springframework.transaction.annotation.Transactional; import java.util.Collection; @@ -58,6 +62,7 @@ import static org.neo4j.helpers.collection.MapUtil.map; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml"}) @Transactional +@TestExecutionListeners({CleanContextCacheTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) public class QueryEngineTests extends EntityTestBase { @Autowired protected ConversionService conversionService; diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GraphRepositoryTests.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GraphRepositoryTests.java index 33ed4f8c6..d036e9cb7 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GraphRepositoryTests.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GraphRepositoryTests.java @@ -34,6 +34,7 @@ import org.springframework.data.neo4j.model.*; import org.springframework.data.neo4j.support.Neo4jTemplate; import org.springframework.data.neo4j.support.conversion.NoSuchColumnFoundException; import org.springframework.data.neo4j.support.node.Neo4jHelper; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.CleanContextCacheTestExecutionListener; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; diff --git a/spring-data-neo4j/src/test/java/org/springframework/test/context/CleanContextCacheTestExecutionListener.java b/spring-data-neo4j/src/test/java/org/springframework/test/context/CleanContextCacheTestExecutionListener.java index 182aab9bc..f14a19a41 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/test/context/CleanContextCacheTestExecutionListener.java +++ b/spring-data-neo4j/src/test/java/org/springframework/test/context/CleanContextCacheTestExecutionListener.java @@ -16,28 +16,26 @@ package org.springframework.test.context; -import java.lang.reflect.Field; -import java.util.Map; - -import org.springframework.context.ApplicationContext; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.support.AbstractTestExecutionListener; public class CleanContextCacheTestExecutionListener extends AbstractTestExecutionListener { - @Override + @Override + public void beforeTestClass(TestContext testContext) throws Exception { + super.beforeTestClass(testContext); + testContext.markApplicationContextDirty(DirtiesContext.HierarchyMode.EXHAUSTIVE); + } + +// @Override +// public void afterTestMethod(TestContext testContext) throws Exception { +// testContext.markApplicationContextDirty(DirtiesContext.HierarchyMode.EXHAUSTIVE); +// super.afterTestMethod(testContext); +// } + + @Override public void afterTestClass(TestContext testContext) throws Exception { - - Field cacheField = TestContext.class.getDeclaredField("contextCache"); - cacheField.setAccessible(true); - ContextCache cache = (ContextCache) cacheField.get(testContext); - Field cacheMapField = ContextCache.class.getDeclaredField("contextMap"); - cacheMapField.setAccessible(true); - @SuppressWarnings("unchecked") - Map cacheMap = (Map) cacheMapField - .get(cache); - - for (MergedContextConfiguration key : cacheMap.keySet()) { - cache.setDirty(key); - } + testContext.markApplicationContextDirty(DirtiesContext.HierarchyMode.EXHAUSTIVE); + super.afterTestClass(testContext); } }