summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.persistence.adapter.jdbc/src/test/java/net/wotonomy/jdbcadaptor
diff options
context:
space:
mode:
authorBenjamin Culkin <scorpress@gmail.com>2024-07-01 17:27:48 -0400
committerBenjamin Culkin <scorpress@gmail.com>2024-07-01 17:27:48 -0400
commit02bc52037e9ccccca672d6156d9c325c74fe28b3 (patch)
treedb022b83c90562f461f4a27412caa9936a8dfd04 /projects/net.wotonomy.persistence.adapter.jdbc/src/test/java/net/wotonomy/jdbcadaptor
parentc75d7dbd613a47b217499f7a856c469a8bc59e2a (diff)
Update a whole bunch of things
Yeah... not a great commit message. t.b.h, I could maybe've split the commit into more parts; but that would be quite a lot off effort and would have a pretty decent chance of at least one of the commits leaving the repository in a non-working state. For the future, will want to try and commit more often so there aren't these mega-commits where it's just "a whole bunch of stuff changed"
Diffstat (limited to 'projects/net.wotonomy.persistence.adapter.jdbc/src/test/java/net/wotonomy/jdbcadaptor')
-rw-r--r--projects/net.wotonomy.persistence.adapter.jdbc/src/test/java/net/wotonomy/jdbcadaptor/JDBCDBTest.java89
-rw-r--r--projects/net.wotonomy.persistence.adapter.jdbc/src/test/java/net/wotonomy/jdbcadaptor/Person.java59
2 files changed, 148 insertions, 0 deletions
diff --git a/projects/net.wotonomy.persistence.adapter.jdbc/src/test/java/net/wotonomy/jdbcadaptor/JDBCDBTest.java b/projects/net.wotonomy.persistence.adapter.jdbc/src/test/java/net/wotonomy/jdbcadaptor/JDBCDBTest.java
new file mode 100644
index 0000000..5af3bff
--- /dev/null
+++ b/projects/net.wotonomy.persistence.adapter.jdbc/src/test/java/net/wotonomy/jdbcadaptor/JDBCDBTest.java
@@ -0,0 +1,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);
+ }
+} \ No newline at end of file
diff --git a/projects/net.wotonomy.persistence.adapter.jdbc/src/test/java/net/wotonomy/jdbcadaptor/Person.java b/projects/net.wotonomy.persistence.adapter.jdbc/src/test/java/net/wotonomy/jdbcadaptor/Person.java
new file mode 100644
index 0000000..d42c77c
--- /dev/null
+++ b/projects/net.wotonomy.persistence.adapter.jdbc/src/test/java/net/wotonomy/jdbcadaptor/Person.java
@@ -0,0 +1,59 @@
+package net.wotonomy.jdbcadaptor;
+
+import java.util.Objects;
+
+public class Person {
+ private String firstName;
+ private String lastName;
+
+ private int age;
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(age, firstName, lastName);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ Person other = (Person) obj;
+ return age == other.age && Objects.equals(firstName, other.firstName)
+ && Objects.equals(lastName, other.lastName);
+ }
+
+ @Override
+ public String toString() {
+ return "Person [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + "]";
+ }
+
+
+} \ No newline at end of file