summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.persistence.adapter.jdbc/src/test/java/net/wotonomy/jdbcadaptor/JDBCDBTest.java
blob: 5af3bff9e684c3c64c15611d3d6ee88a6206d6ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package net.wotonomy.jdbcadaptor;

import java.sql.SQLException;

import junit.framework.TestCase;
import net.wotonomy.access.EODatabase;
import net.wotonomy.access.EODatabaseContext;
import net.wotonomy.control.EOClassDescription;
import net.wotonomy.control.EOEditingContext;
import net.wotonomy.control.EOTemporaryGlobalID;
import net.wotonomy.foundation.NSDictionary;

public class JDBCDBTest extends TestCase {
	public void testJDBCContextH2() throws SQLException, ClassNotFoundException {
		String jdbcURL = "jdbc:h2:mem:test";
		
		// Feels a bit weird not to put anything here, but I don't think we need to
		NSDictionary<String, String> jdbcInfo = new NSDictionary<>();
		
		NSDictionary<String, Object> connDict = new NSDictionary<>();
		connDict.put("driver", "org.h2.Driver");
		connDict.put("jdbc2Info", jdbcInfo);
		connDict.put("URL", jdbcURL);
		JDBCAdaptor adaptor = new JDBCAdaptor("JDBC");
		adaptor.setConnectionDictionary(connDict);
		adaptor.assertConnectionDictionaryIsValid();
		
		EODatabase db = new EODatabase(adaptor);
		EODatabaseContext dbCtx = new EODatabaseContext(db);
		((JDBCContext)dbCtx.adaptorContext()).connect();
		
		EOEditingContext editCtx = new EOEditingContext(dbCtx);
		
		EOClassDescription<Person> personDesc = EOClassDescription.classDescriptionForClass(Person.class);
		EOTemporaryGlobalID tempGID = new EOTemporaryGlobalID();
		Person person = personDesc.createInstanceWithEditingContext(editCtx, tempGID);
		
		person.setFirstName("John");
		person.setLastName("Doe");
		person.setAge(25);
		
		editCtx.saveChanges();
		
		Person newPerson = (Person) editCtx.objectForGlobalID(tempGID);
		assertEquals(person, newPerson);
	}
	
	public void testJDBCContextPostgres() throws SQLException, ClassNotFoundException {
		String jdbcURL = "jdbc:postgresql:wotonomy";
		
		// Feels a bit weird not to put anything here, but I don't think we need to
		NSDictionary<String, String> jdbcInfo = new NSDictionary<>();
		
		NSDictionary<String, Object> connDict = new NSDictionary<>();
		connDict.put("driver", "org.postgresql.Driver");
		connDict.put("jdbc2Info", jdbcInfo);
		connDict.put("URL", jdbcURL);
		connDict.put("username", "wotonomy");
		connDict.put("password", "wotonomy");
		
		JDBCAdaptor adaptor = new JDBCAdaptor("JDBC");
		adaptor.setConnectionDictionary(connDict);
		adaptor.assertConnectionDictionaryIsValid();
		
		EODatabase db = new EODatabase(adaptor);
		EODatabaseContext dbCtx = new EODatabaseContext(db);
		((JDBCContext)dbCtx.adaptorContext()).connect();
		
		EOEditingContext editCtx = new EOEditingContext(dbCtx);
		
		EOClassDescription<Person> personDesc = EOClassDescription.classDescriptionForClass(Person.class);
		
		EOTemporaryGlobalID tempGID = new EOTemporaryGlobalID();
		Person person = personDesc.createInstanceWithEditingContext(editCtx, tempGID);
		
		person.setFirstName("John");
		person.setLastName("Doe");
		person.setAge(25);
		
		editCtx.saveChanges();
		editCtx.invalidateAllObjects();

		((JDBCContext)dbCtx.adaptorContext()).commitTransaction();
		//editCtx.reset();
		
		Person newPerson = (Person) editCtx.objectForGlobalID(tempGID);
		assertEquals(person, newPerson);
	}
}