#1640 - For Postgres support mapping InetAddress to INET db column (#1641)

* #1640 - For Postgres support mapping InetAddress to INET db column

* #1640 - For Postgres support mapping InetAddress to INET db column

Support for Inet and Cdir types + offlineMigrationGeneration + PlatformConfig.databaseInetAddressVarchar for turning off InetAddress mapping on Postgres
This commit is contained in:
Rob Bygrave
2019-02-21 22:14:21 +13:00
committed by GitHub
parent 03a42f7f6e
commit 5975694538
15 changed files with 374 additions and 27 deletions
@@ -1,34 +1,46 @@
package io.ebeaninternal.server.type;
import org.junit.Assert;
import org.junit.Test;
import java.net.InetAddress;
import static org.junit.Assert.assertEquals;
public class ConvertInetAddressTest {
@Test
public void forString() {
InetAddress addr = ConvertInetAddresses.forString("128.1.10.23");
Assert.assertNotNull(addr);
Assert.assertEquals("128.1.10.23", addr.getHostAddress());
assertEquals("128.1.10.23", addr.getHostAddress());
String ip6addr = "2001:db8:85a3:0:0:8a2e:370:7334";
InetAddress addr6 = ConvertInetAddresses.forString(ip6addr);
String uriAddr6 = ConvertInetAddresses.toUriString(addr6);
Assert.assertEquals("[" + ip6addr + "]", uriAddr6);
Assert.assertEquals(ip6addr, addr6.getHostAddress());
assertEquals("[" + ip6addr + "]", uriAddr6);
assertEquals(ip6addr, addr6.getHostAddress());
}
@Test
public void ipv6_fromHost_getHostAddress() {
InetAddress addr2 = ConvertInetAddresses.fromHost("2001:4f8:3:ba:2e0:81ff:fe22:d1f1");
assertEquals("2001:4f8:3:ba:2e0:81ff:fe22:d1f1", addr2.getHostAddress());
}
@Test
public void ipv6_fromHost_getHostAddress_2() {
InetAddress addr2 = ConvertInetAddresses.fromHost("2001:db8:85a3:0:0:8a2e:370:7334");
assertEquals("2001:db8:85a3:0:0:8a2e:370:7334", addr2.getHostAddress());
}
@Test
public void toUriString() {
InetAddress addr = ConvertInetAddresses.forString("128.1.10.23");
Assert.assertEquals("128.1.10.23", addr.getHostAddress());
assertEquals("128.1.10.23", addr.getHostAddress());
String uriAddr = ConvertInetAddresses.toUriString(addr);
Assert.assertEquals("128.1.10.23", uriAddr);
assertEquals("128.1.10.23", uriAddr);
}
}
@@ -1,61 +1,100 @@
package org.tests.basic.type;
import io.ebean.Ebean;
import io.ebean.DB;
import io.ebean.TransactionalTestCase;
import org.tests.model.basic.EWithInetAddr;
import org.junit.Assert;
import io.ebean.types.Cdir;
import io.ebean.types.Inet;
import org.junit.Test;
import org.tests.model.basic.EWithInetAddr;
import java.net.InetAddress;
import java.net.UnknownHostException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
public class TestInetAddressType extends TransactionalTestCase {
@Test
public void testIp4() throws UnknownHostException {
insertUpdateDeleteFind("120.12.12.56");
insertUpdateDeleteFind("120.12.12.56", "120.12.12.56");
}
@Test
public void testIp6() throws UnknownHostException {
insertUpdateDeleteFind("2001:db8:85a3:0:0:8a2e:370:7334");
if (isPostgres()) {
insertUpdateDeleteFind("2001:db8:85a3:0:0:8a2e:370:7334", "2001:db8:85a3::8a2e:370:7334");
} else {
insertUpdateDeleteFind("2001:db8:85a3:0:0:8a2e:370:7334", "2001:db8:85a3:0:0:8a2e:370:7334");
}
}
private void insertUpdateDeleteFind(String ipAddress) throws UnknownHostException {
private void insertUpdateDeleteFind(String ipAddress, String expected) throws UnknownHostException {
EWithInetAddr bean1 = new EWithInetAddr();
bean1.setName("jim");
InetAddress address1 = InetAddress.getByName(ipAddress);
bean1.setInetAddress(address1);
bean1.setInet2(new Inet(ipAddress));
bean1.setCdir(new Cdir(ipAddress));
Ebean.save(bean1);
EWithInetAddr bean2 = Ebean.find(EWithInetAddr.class, bean1.getId());
DB.save(bean1);
EWithInetAddr bean2 = DB.find(EWithInetAddr.class, bean1.getId());
InetAddress address2 = bean2.getInetAddress();
Assert.assertNotNull(address2.getHostAddress());
Assert.assertEquals(address1.getHostAddress(), address2.getHostAddress());
assertNotNull(address2.getHostAddress());
assertThat(address1.getHostAddress()).isEqualTo(address2.getHostAddress());
assertThat(bean2.getInet2().getAddress()).isEqualTo(expected);
assertThat(bean2.getCdir().getAddress()).isEqualTo(expected);
bean2.setName("modJim");
bean2.setInetAddress(InetAddress.getByName("120.12.20.80"));
Ebean.save(bean2);
Ebean.delete(bean2);
bean1.setInet2(new Inet("120.12.20.80"));
bean1.setCdir(new Cdir("120.12.20.80"));
DB.save(bean2);
DB.delete(bean2);
}
@Test
public void use_null() throws UnknownHostException {
public void testIp6_ranges() {
insertFindDeleteRange("2001:4f8:3:ba::/64");
insertFindDeleteRange("2001:4f8:3:ba:2e0:81ff:fe22:d1f1/64");
}
private void insertFindDeleteRange(String ipAddressRange) {
EWithInetAddr bean1 = new EWithInetAddr();
bean1.setName("withRange");
bean1.setInet2(new Inet(ipAddressRange));
bean1.setCdir(new Cdir(ipAddressRange));
DB.save(bean1);
EWithInetAddr bean2 = DB.find(EWithInetAddr.class, bean1.getId());
assertNotNull(bean2.getInet2());
assertThat(bean2.getInet2().getAddress()).isEqualTo(ipAddressRange);
assertThat(bean2.getCdir().getAddress()).isEqualTo(ipAddressRange);
DB.delete(bean2);
}
@Test
public void use_null() {
EWithInetAddr bean1 = new EWithInetAddr();
bean1.setName("jim");
Ebean.save(bean1);
DB.save(bean1);
EWithInetAddr bean2 = Ebean.find(EWithInetAddr.class, bean1.getId());
InetAddress address2 = bean2.getInetAddress();
Assert.assertNull(address2);
EWithInetAddr bean2 = DB.find(EWithInetAddr.class, bean1.getId());
assertNull(bean2.getInetAddress());
assertNull(bean2.getInet2());
}
}
@@ -1,5 +1,8 @@
package org.tests.model.basic;
import io.ebean.types.Cdir;
import io.ebean.types.Inet;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@@ -20,6 +23,10 @@ public class EWithInetAddr {
InetAddress inetAddress;
Inet inet2;
Cdir cdir;
public Long getId() {
return id;
}
@@ -52,4 +59,19 @@ public class EWithInetAddr {
this.inetAddress = inetAddress;
}
public Inet getInet2() {
return inet2;
}
public void setInet2(Inet inet2) {
this.inet2 = inet2;
}
public Cdir getCdir() {
return cdir;
}
public void setCdir(Cdir cdir) {
this.cdir = cdir;
}
}