Skip to content
This repository was archived by the owner on Jul 9, 2022. It is now read-only.

Commit 5766bc3

Browse files
committed
Tidy meta data and re-org test
1 parent 90bc79c commit 5766bc3

12 files changed

+115
-463
lines changed

.classpath

-14
This file was deleted.

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
/target
22
/bin
3+
.project
4+
.classpath
5+
.settings
6+

.project

-36
This file was deleted.

.settings/org.eclipse.jdt.core.prefs

-270
This file was deleted.

.settings/org.eclipse.jdt.ui.prefs

-57
This file was deleted.

.settings/org.eclipse.mylyn.tasks.ui.prefs

-4
This file was deleted.

.settings/org.eclipse.wst.common.component

-8
This file was deleted.

.settings/org.eclipse.wst.common.project.facet.core.xml

-5
This file was deleted.

pom.xml

+55-35
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,42 @@
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
44
<modelVersion>4.0.0</modelVersion>
5-
<groupId>org.springframework</groupId>
5+
<groupId>org.springframework.tenancy</groupId>
66
<artifactId>spring-tenancy</artifactId>
7-
<version>3.0.6-SNAPSHOT</version>
7+
<version>1.0.0.BUILD-SNAPSHOT</version>
88
<packaging>jar</packaging>
9-
<name>multi tenant framework</name>
10-
9+
<name>Spring Multi Tenant Framework</name>
10+
1111
<properties>
1212
<spring.framework.version>3.0.5.RELEASE</spring.framework.version>
1313
</properties>
14-
15-
<repositories>
14+
15+
<profiles>
16+
<profile>
17+
<id>bootstrap</id>
18+
<repositories>
19+
<repository>
20+
<id>spring-tenancy</id>
21+
<name>Spring Tenancy</name>
22+
<url>http://code2cloud.tasktop.com/alm/s/spring-tenancy/maven/</url>
23+
</repository>
24+
<repository>
25+
<id>EclipseLink Repo</id>
26+
<name>EclipseLink Repository</name>
27+
<url>http://mirror.csclub.uwaterloo.ca/eclipse/rt/eclipselink/maven.repo/</url>
28+
</repository>
29+
</repositories>
30+
</profile>
31+
</profiles>
32+
33+
<distributionManagement>
1634
<repository>
17-
<id>spring-tenancy</id>
18-
<name>Spring Tenancy</name>
19-
<url>http://code2cloud.tasktop.com/alm/s/spring-tenancy/maven/</url>
35+
<id>code2cloud-spring-tenacy</id>
36+
<name>Spring Tenancy Code2Cloud Repo</name>
37+
<url>dav:http://code2cloud.tasktop.com/alm/s/spring-tenancy/maven/</url>
2038
</repository>
21-
<repository>
22-
<id>EclipseLink Repo</id>
23-
<name>EclipseLink Repository</name>
24-
<url>http://mirror.csclub.uwaterloo.ca/eclipse/rt/eclipselink/maven.repo/</url>
25-
</repository>
26-
</repositories>
27-
28-
<distributionManagement>
29-
<repository>
30-
<id>code2cloud-spring-tenacy</id>
31-
<name>Spring Tenancy Code2Cloud Repo</name>
32-
<url>dav:http://code2cloud.tasktop.com/alm/s/spring-tenancy/maven/</url>
33-
</repository>
34-
</distributionManagement>
35-
39+
</distributionManagement>
40+
3641
<dependencies>
3742
<dependency>
3843
<groupId>org.springframework</groupId>
@@ -55,10 +60,16 @@
5560
<version>2.5</version>
5661
<scope>provided</scope>
5762
</dependency>
63+
<dependency>
64+
<groupId>log4j</groupId>
65+
<artifactId>log4j</artifactId>
66+
<version>1.2.14</version>
67+
<scope>test</scope>
68+
</dependency>
5869
<dependency>
5970
<groupId>junit</groupId>
6071
<artifactId>junit</artifactId>
61-
<version>4.7</version>
72+
<version>4.8.1</version>
6273
<scope>test</scope>
6374
</dependency>
6475
<dependency>
@@ -109,9 +120,18 @@
109120
<version>2.0.0</version>
110121
<scope>test</scope>
111122
</dependency>
112-
113123
</dependencies>
114-
124+
125+
<url>http://code2cloud.tasktop.com/alm/#projects/spring-tenancy</url>
126+
<issueManagement>
127+
<system>Code2Cloud</system>
128+
<url>http://code2cloud.tasktop.com/alm/#projects/spring-tenancy</url>
129+
</issueManagement>
130+
<scm>
131+
<developerConnection>[email protected]:SpringSource/spring-tenancy.git</developerConnection>
132+
<url>http://github.com/SpringSource/spring-tenancy</url>
133+
</scm>
134+
115135
<build>
116136
<plugins>
117137
<plugin>
@@ -123,13 +143,13 @@
123143
</configuration>
124144
</plugin>
125145
</plugins>
126-
<extensions>
127-
<extension>
128-
<groupId>org.apache.maven.wagon</groupId>
129-
<artifactId>wagon-webdav-jackrabbit</artifactId>
130-
<version>1.0-beta-6</version>
131-
</extension>
132-
</extensions>
146+
<extensions>
147+
<extension>
148+
<groupId>org.apache.maven.wagon</groupId>
149+
<artifactId>wagon-webdav-jackrabbit</artifactId>
150+
<version>1.0-beta-6</version>
151+
</extension>
152+
</extensions>
133153
</build>
134-
154+
135155
</project>

src/test/java/org/springframework/tenancy/datasource/DatabaseSwitchingDataSourceTest.java

+48-34
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.springframework.tenancy.datasource;
22

3-
import java.sql.SQLException;
4-
53
import javax.persistence.EntityManager;
64
import javax.persistence.PersistenceContext;
75

@@ -10,14 +8,20 @@
108
import org.junit.Test;
119
import org.junit.runner.RunWith;
1210
import org.springframework.beans.factory.annotation.Autowired;
13-
import org.springframework.test.annotation.Rollback;
11+
import org.springframework.test.annotation.DirtiesContext;
1412
import org.springframework.test.context.ContextConfiguration;
1513
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14+
import org.springframework.test.context.transaction.BeforeTransaction;
15+
import org.springframework.transaction.PlatformTransactionManager;
16+
import org.springframework.transaction.TransactionStatus;
1617
import org.springframework.transaction.annotation.Transactional;
18+
import org.springframework.transaction.support.TransactionCallback;
19+
import org.springframework.transaction.support.TransactionTemplate;
1720

1821
@RunWith(SpringJUnit4ClassRunner.class)
19-
@ContextConfiguration({ "/applicationContext-testDBSwitching.xml" })
20-
@Transactional()
22+
@ContextConfiguration
23+
@Transactional
24+
@DirtiesContext
2125
public class DatabaseSwitchingDataSourceTest {
2226

2327
@PersistenceContext
@@ -26,51 +30,48 @@ public class DatabaseSwitchingDataSourceTest {
2630
@Autowired
2731
private BasicDataSource dataSource;
2832

33+
@Autowired
34+
private PlatformTransactionManager transactionManager;
35+
2936
@Autowired
3037
private ThreadlocalDatabaseSwitchingDataSource switchingDataSource;
3138

39+
private static boolean initialized;
40+
3241
private final String DB1 = "DB1";
3342
private final String DB2 = "DB2";
3443

3544
private final String CREATE_TABLE_STMT = "CREATE TABLE DOMAINOBJECT (ID INTEGER PRIMARY KEY, STRING CHAR(50)) GRANT ALL ON DOMAINOBJECT TO SA;";
3645

37-
// I don't really want this to be a test, just a before that does not run under a transaction, however, I can't make
38-
// that work (@BeforeTransaction). This means that we must run all tests in the class.
39-
@Rollback(false)
40-
@Test
41-
public void setupDB1() throws SQLException {
42-
dataSource.getConnection().createStatement().execute("create schema " + DB1 + " " + CREATE_TABLE_STMT);
46+
@BeforeTransaction
47+
public void setUpDatabases() throws Exception {
4348

44-
switchingDataSource.setDatabaseName(DB1);
45-
DomainObject do1 = getMockDomainObject("db1");
46-
entityManager.persist(do1);
47-
DomainObject result = (DomainObject) entityManager.createQuery("select do from DomainObject do;")
48-
.getResultList().get(0);
49-
Assert.assertNotNull(result);
50-
Assert.assertEquals("db1", result.getString());
51-
entityManager.flush();
49+
if (!initialized) {
5250

53-
}
51+
initialized = true;
5452

55-
// I don't really want this to be a test, just a before that does not run under a transaction, however, I can't make
56-
// that work (@BeforeTransaction). This means that we must run all tests in the class.
57-
@Rollback(false)
58-
@Test
59-
public void setupDB2() throws SQLException {
60-
dataSource.getConnection().createStatement().execute("create schema " + DB2 + " " + CREATE_TABLE_STMT);
53+
dataSource.getConnection().createStatement().execute("create schema " + DB1 + " " + CREATE_TABLE_STMT);
54+
dataSource.getConnection().createStatement().execute("create schema " + DB2 + " " + CREATE_TABLE_STMT);
6155

62-
switchingDataSource.setDatabaseName(DB2);
63-
entityManager.persist(getMockDomainObject("db2"));
64-
DomainObject result = (DomainObject) entityManager.createQuery("select do from DomainObject do;")
65-
.getResultList().get(0);
66-
Assert.assertNotNull(result);
67-
Assert.assertEquals("db2", result.getString());
68-
entityManager.flush();
56+
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
57+
public Void doInTransaction(TransactionStatus status) {
58+
setUpData(DB1, "db1");
59+
return null;
60+
}
61+
});
62+
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
63+
public Void doInTransaction(TransactionStatus status) {
64+
setUpData(DB2, "db2");
65+
return null;
66+
}
67+
});
68+
69+
}
6970

7071
}
7172

7273
@Test
73-
public void testGetFromD2() {
74+
public void testGetFromDB2() {
7475
switchingDataSource.setDatabaseName(DB2);
7576
DomainObject result = (DomainObject) entityManager.createQuery("select do from DomainObject do;")
7677
.getResultList().get(0);
@@ -89,6 +90,19 @@ public void testGetFromDB1() {
8990

9091
private static long nextId = 1;
9192

93+
private void setUpData(String databaseName, String objectName) {
94+
95+
switchingDataSource.setDatabaseName(databaseName);
96+
DomainObject do1 = getMockDomainObject(objectName);
97+
entityManager.persist(do1);
98+
DomainObject result = (DomainObject) entityManager.createQuery("select do from DomainObject do;")
99+
.getResultList().get(0);
100+
Assert.assertNotNull(result);
101+
Assert.assertEquals(objectName, result.getString());
102+
entityManager.flush();
103+
104+
}
105+
92106
private DomainObject getMockDomainObject(String data) {
93107
DomainObject result = new DomainObject();
94108
result.setId(nextId++);

0 commit comments

Comments
 (0)