|
| 1 | +/* |
| 2 | + * Copyright 2011 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
1 | 16 | package org.springframework.data.mongodb.crossstore;
|
2 | 17 |
|
3 | 18 | import javax.persistence.EntityManager;
|
|
27 | 42 | @ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext.xml")
|
28 | 43 | public class CrossStoreMongoTests {
|
29 | 44 |
|
30 |
| - @Autowired |
31 |
| - private MongoTemplate mongoTemplate; |
| 45 | + @Autowired |
| 46 | + private MongoTemplate mongoTemplate; |
32 | 47 |
|
33 |
| - private EntityManager entityManager; |
| 48 | + private EntityManager entityManager; |
34 | 49 |
|
35 |
| - @Autowired |
36 |
| - private PlatformTransactionManager transactionManager; |
37 |
| - |
38 |
| - @PersistenceContext |
39 |
| - public void setEntityManager(EntityManager entityManager) { |
40 |
| - this.entityManager = entityManager; |
41 |
| - } |
| 50 | + @Autowired |
| 51 | + private PlatformTransactionManager transactionManager; |
42 | 52 |
|
43 |
| - private void clearData(String collectionName) { |
44 |
| - DBCollection col = this.mongoTemplate.getCollection(collectionName); |
45 |
| - if (col != null) { |
46 |
| - this.mongoTemplate.dropCollection(collectionName); |
47 |
| - } |
48 |
| - } |
| 53 | + @PersistenceContext |
| 54 | + public void setEntityManager(EntityManager entityManager) { |
| 55 | + this.entityManager = entityManager; |
| 56 | + } |
49 | 57 |
|
50 |
| - @Test |
51 |
| - @Transactional |
52 |
| - @Rollback(false) |
53 |
| - public void testCreateJpaToMongoEntityRelationship() { |
54 |
| - clearData(Person.class.getName()); |
55 |
| - Person p = new Person("Thomas", 20); |
56 |
| - Address a = new Address(12, "MAin St.", "Boston", "MA", "02101"); |
57 |
| - p.setAddress(a); |
58 |
| - Resume r = new Resume(); |
59 |
| - r.addEducation("Skanstulls High School, 1975"); |
60 |
| - r.addEducation("Univ. of Stockholm, 1980"); |
61 |
| - r.addJob("DiMark, DBA, 1990-2000"); |
62 |
| - r.addJob("VMware, Developer, 2007-"); |
63 |
| - p.setResume(r); |
64 |
| - p.setId(1L); |
65 |
| - entityManager.persist(p); |
66 |
| - } |
| 58 | + private void clearData(String collectionName) { |
| 59 | + DBCollection col = this.mongoTemplate.getCollection(collectionName); |
| 60 | + if (col != null) { |
| 61 | + this.mongoTemplate.dropCollection(collectionName); |
| 62 | + } |
| 63 | + } |
67 | 64 |
|
68 |
| - @Test |
69 |
| - @Transactional |
70 |
| - @Rollback(false) |
71 |
| - public void testReadJpaToMongoEntityRelationship() { |
72 |
| - Person found = entityManager.find(Person.class, 1L); |
73 |
| - Assert.assertNotNull(found); |
74 |
| - Assert.assertEquals(Long.valueOf(1), found.getId()); |
75 |
| - Assert.assertNotNull(found); |
76 |
| - Assert.assertEquals(Long.valueOf(1), found.getId()); |
77 |
| - Assert.assertNotNull(found.getResume()); |
78 |
| - Assert.assertEquals("DiMark, DBA, 1990-2000" + "; " |
79 |
| - + "VMware, Developer, 2007-", found.getResume().getJobs()); |
80 |
| - found.getResume().addJob("SpringDeveloper.com, Consultant, 2005-2006"); |
81 |
| - found.setAge(44); |
82 |
| - } |
| 65 | + @Test |
| 66 | + @Transactional |
| 67 | + @Rollback(false) |
| 68 | + public void testCreateJpaToMongoEntityRelationship() { |
| 69 | + clearData(Person.class.getName()); |
| 70 | + Person p = new Person("Thomas", 20); |
| 71 | + Address a = new Address(12, "MAin St.", "Boston", "MA", "02101"); |
| 72 | + p.setAddress(a); |
| 73 | + Resume r = new Resume(); |
| 74 | + r.addEducation("Skanstulls High School, 1975"); |
| 75 | + r.addEducation("Univ. of Stockholm, 1980"); |
| 76 | + r.addJob("DiMark, DBA, 1990-2000"); |
| 77 | + r.addJob("VMware, Developer, 2007-"); |
| 78 | + p.setResume(r); |
| 79 | + p.setId(1L); |
| 80 | + entityManager.persist(p); |
| 81 | + } |
83 | 82 |
|
84 |
| - @Test |
85 |
| - @Transactional |
86 |
| - @Rollback(false) |
87 |
| - public void testUpdatedJpaToMongoEntityRelationship() { |
88 |
| - Person found = entityManager.find(Person.class, 1L); |
89 |
| - Assert.assertNotNull(found); |
90 |
| - Assert.assertEquals(Long.valueOf(1), found.getId()); |
91 |
| - Assert.assertNotNull(found); |
92 |
| - Assert.assertEquals(Long.valueOf(1), found.getId()); |
93 |
| - Assert.assertNotNull(found.getResume()); |
94 |
| - Assert.assertEquals("DiMark, DBA, 1990-2000" + "; " |
95 |
| - + "VMware, Developer, 2007-" + "; " |
96 |
| - + "SpringDeveloper.com, Consultant, 2005-2006", found.getResume().getJobs()); |
97 |
| - } |
98 |
| - |
99 |
| - @Test |
100 |
| - public void testMergeJpaEntityWithMongoDocument() { |
101 |
| - TransactionTemplate txTemplate = new TransactionTemplate(transactionManager); |
102 |
| - final Person detached = entityManager.find(Person.class, 1L); |
103 |
| - detached.getResume().addJob("TargetRx, Developer, 2000-2005"); |
104 |
| - Person merged = txTemplate.execute(new TransactionCallback<Person>() { |
105 |
| - public Person doInTransaction(TransactionStatus status) { |
106 |
| - return entityManager.merge(detached); |
107 |
| - } |
108 |
| - }); |
109 |
| - Assert.assertTrue(detached.getResume().getJobs().contains("TargetRx, Developer, 2000-2005")); |
110 |
| - Assert.assertTrue(merged.getResume().getJobs().contains("TargetRx, Developer, 2000-2005")); |
111 |
| - final Person updated = entityManager.find(Person.class, 1L); |
112 |
| - Assert.assertTrue(updated.getResume().getJobs().contains("TargetRx, Developer, 2000-2005")); |
113 |
| - } |
| 83 | + @Test |
| 84 | + @Transactional |
| 85 | + @Rollback(false) |
| 86 | + public void testReadJpaToMongoEntityRelationship() { |
| 87 | + Person found = entityManager.find(Person.class, 1L); |
| 88 | + Assert.assertNotNull(found); |
| 89 | + Assert.assertEquals(Long.valueOf(1), found.getId()); |
| 90 | + Assert.assertNotNull(found); |
| 91 | + Assert.assertEquals(Long.valueOf(1), found.getId()); |
| 92 | + Assert.assertNotNull(found.getResume()); |
| 93 | + Assert.assertEquals("DiMark, DBA, 1990-2000" + "; " |
| 94 | + + "VMware, Developer, 2007-", found.getResume().getJobs()); |
| 95 | + found.getResume().addJob("SpringDeveloper.com, Consultant, 2005-2006"); |
| 96 | + found.setAge(44); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + @Transactional |
| 101 | + @Rollback(false) |
| 102 | + public void testUpdatedJpaToMongoEntityRelationship() { |
| 103 | + Person found = entityManager.find(Person.class, 1L); |
| 104 | + Assert.assertNotNull(found); |
| 105 | + Assert.assertEquals(Long.valueOf(1), found.getId()); |
| 106 | + Assert.assertNotNull(found); |
| 107 | + Assert.assertEquals(Long.valueOf(1), found.getId()); |
| 108 | + Assert.assertNotNull(found.getResume()); |
| 109 | + Assert.assertEquals("DiMark, DBA, 1990-2000" + "; " |
| 110 | + + "VMware, Developer, 2007-" + "; " |
| 111 | + + "SpringDeveloper.com, Consultant, 2005-2006", found.getResume().getJobs()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testMergeJpaEntityWithMongoDocument() { |
| 116 | + TransactionTemplate txTemplate = new TransactionTemplate(transactionManager); |
| 117 | + final Person detached = entityManager.find(Person.class, 1L); |
| 118 | + detached.getResume().addJob("TargetRx, Developer, 2000-2005"); |
| 119 | + Person merged = txTemplate.execute(new TransactionCallback<Person>() { |
| 120 | + public Person doInTransaction(TransactionStatus status) { |
| 121 | + return entityManager.merge(detached); |
| 122 | + } |
| 123 | + }); |
| 124 | + Assert.assertTrue(detached.getResume().getJobs().contains("TargetRx, Developer, 2000-2005")); |
| 125 | + Assert.assertTrue(merged.getResume().getJobs().contains("TargetRx, Developer, 2000-2005")); |
| 126 | + final Person updated = entityManager.find(Person.class, 1L); |
| 127 | + Assert.assertTrue(updated.getResume().getJobs().contains("TargetRx, Developer, 2000-2005")); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testRemoveJpaEntityWithMongoDocument() { |
| 132 | + TransactionTemplate txTemplate = new TransactionTemplate(transactionManager); |
| 133 | + txTemplate.execute(new TransactionCallback<Person>() { |
| 134 | + public Person doInTransaction(TransactionStatus status) { |
| 135 | + Person p2 = new Person("Thomas", 20); |
| 136 | + Resume r2 = new Resume(); |
| 137 | + r2.addEducation("Skanstulls High School, 1975"); |
| 138 | + r2.addJob("DiMark, DBA, 1990-2000"); |
| 139 | + p2.setResume(r2); |
| 140 | + p2.setId(2L); |
| 141 | + entityManager.persist(p2); |
| 142 | + Person p3 = new Person("Thomas", 20); |
| 143 | + Resume r3 = new Resume(); |
| 144 | + r3.addEducation("Univ. of Stockholm, 1980"); |
| 145 | + r3.addJob("VMware, Developer, 2007-"); |
| 146 | + p3.setResume(r3); |
| 147 | + p3.setId(3L); |
| 148 | + entityManager.persist(p3); |
| 149 | + return null; |
| 150 | + } |
| 151 | + }); |
| 152 | + txTemplate.execute(new TransactionCallback<Person>() { |
| 153 | + public Person doInTransaction(TransactionStatus status) { |
| 154 | + final Person found2 = entityManager.find(Person.class, 2L); |
| 155 | + entityManager.remove(found2); |
| 156 | + return null; |
| 157 | + } |
| 158 | + }); |
| 159 | + boolean weFound3 = false; |
| 160 | + for (DBObject dbo : this.mongoTemplate.getCollection(Person.class.getName()).find()) { |
| 161 | + Assert.assertTrue(!dbo.get("_entity_id").equals(2L)); |
| 162 | + if (dbo.get("_entity_id").equals(3L)) { |
| 163 | + weFound3 = true; |
| 164 | + } |
| 165 | + } |
| 166 | + Assert.assertTrue(weFound3); |
| 167 | + } |
114 | 168 |
|
115 |
| - @Test |
116 |
| - public void testRemoveJpaEntityWithMongoDocument() { |
117 |
| - TransactionTemplate txTemplate = new TransactionTemplate(transactionManager); |
118 |
| - txTemplate.execute(new TransactionCallback<Person>() { |
119 |
| - public Person doInTransaction(TransactionStatus status) { |
120 |
| - Person p2 = new Person("Thomas", 20); |
121 |
| - Resume r2 = new Resume(); |
122 |
| - r2.addEducation("Skanstulls High School, 1975"); |
123 |
| - r2.addJob("DiMark, DBA, 1990-2000"); |
124 |
| - p2.setResume(r2); |
125 |
| - p2.setId(2L); |
126 |
| - entityManager.persist(p2); |
127 |
| - Person p3 = new Person("Thomas", 20); |
128 |
| - Resume r3 = new Resume(); |
129 |
| - r3.addEducation("Univ. of Stockholm, 1980"); |
130 |
| - r3.addJob("VMware, Developer, 2007-"); |
131 |
| - p3.setResume(r3); |
132 |
| - p3.setId(3L); |
133 |
| - entityManager.persist(p3); |
134 |
| - return null; |
135 |
| - } |
136 |
| - }); |
137 |
| - txTemplate.execute(new TransactionCallback<Person>() { |
138 |
| - public Person doInTransaction(TransactionStatus status) { |
139 |
| - final Person found2 = entityManager.find(Person.class, 2L); |
140 |
| - entityManager.remove(found2); |
141 |
| - return null; |
142 |
| - } |
143 |
| - }); |
144 |
| - boolean weFound3 = false; |
145 |
| - for (DBObject dbo : this.mongoTemplate.getCollection(Person.class.getName()).find()) { |
146 |
| - Assert.assertTrue(!dbo.get("_entity_id").equals(2L)); |
147 |
| - if (dbo.get("_entity_id").equals(3L)) { |
148 |
| - weFound3 = true; |
149 |
| - } |
150 |
| - } |
151 |
| - Assert.assertTrue(weFound3); |
152 |
| - } |
153 |
| - |
154 | 169 | }
|
0 commit comments