Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OGM-952 Re-organizing test, asserting actual persistent representation
- Loading branch information
1 parent
7e102ce
commit f846d16
Showing
11 changed files
with
491 additions
and
212 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
.../src/test/java/org/hibernate/ogm/backendtck/type/converter/JpaAttributeConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Hibernate OGM, Domain model persistence for NoSQL datastores | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.ogm.backendtck.type.converter; | ||
|
||
import static org.fest.assertions.Assertions.assertThat; | ||
|
||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.ogm.OgmSessionFactory; | ||
import org.hibernate.ogm.cfg.OgmConfiguration; | ||
import org.hibernate.ogm.model.impl.DefaultEntityKeyMetadata; | ||
import org.hibernate.ogm.model.key.spi.EntityKey; | ||
import org.hibernate.ogm.utils.OgmTestCase; | ||
import org.hibernate.ogm.utils.TestHelper; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test the JPA @Convert logic in OGM | ||
* TODO: should it be in the TCK, that's core code but it's nice to see it tested across all backends | ||
* | ||
* @author Emmanuel Bernard emmanuel@hibernate.org | ||
*/ | ||
public class JpaAttributeConverterTest extends OgmTestCase { | ||
|
||
/** | ||
* String -> String | ||
*/ | ||
@Test | ||
public void jpaConverterIsApplied() throws Exception { | ||
Session session = openSession(); | ||
session.getTransaction().begin(); | ||
Printer printer = new Printer(); | ||
printer.name = "somefoo"; | ||
assertThat( printer.name ).isEqualTo( "somefoo" ); | ||
session.persist( printer ); | ||
session.getTransaction().commit(); | ||
session.clear(); | ||
|
||
session.getTransaction().begin(); | ||
// Make sure the converter has actually been applied | ||
Map<String, Object> persistedTuple = TestHelper.extractEntityTuple( | ||
sessions, | ||
getPrinterEntityKey( printer.id ) | ||
); | ||
String persistedPrinterName = (String) persistedTuple.get( "name" ); | ||
assertThat( persistedPrinterName ).isEqualTo( "SOMEFOO" ); | ||
session.getTransaction().commit(); | ||
session.clear(); | ||
|
||
session.getTransaction().begin(); | ||
printer = session.get( Printer.class, printer.id ); | ||
assertThat( printer ).isNotNull(); | ||
assertThat( printer.name ).isEqualTo( "somefoo" ); | ||
session.delete( printer ); | ||
session.getTransaction().commit(); | ||
|
||
session.close(); | ||
} | ||
|
||
/** | ||
* MyString -> String | ||
*/ | ||
@Test | ||
public void jpaConverterIsAppliedToCustomType() throws Exception { | ||
Session session = openSession(); | ||
session.getTransaction().begin(); | ||
Printer printer = new Printer(); | ||
printer.brand = new MyString( "printr inc." ); | ||
assertThat( printer.brand.toString() ).isEqualTo( "printr inc." ); | ||
session.persist( printer ); | ||
session.getTransaction().commit(); | ||
session.clear(); | ||
|
||
session.getTransaction().begin(); | ||
// Make sure the converter has actually been applied | ||
Map<String, Object> persistedTuple = TestHelper.extractEntityTuple( | ||
sessions, | ||
getPrinterEntityKey( printer.id ) | ||
); | ||
String persistedPrinterName = (String) persistedTuple.get( "brand" ); | ||
assertThat( persistedPrinterName ).isEqualTo( "PRINTR INC." ); | ||
session.getTransaction().commit(); | ||
session.clear(); | ||
|
||
session.getTransaction().begin(); | ||
printer = session.get( Printer.class, printer.id ); | ||
assertThat( printer ).isNotNull(); | ||
assertThat( printer.brand.toString() ).isEqualTo( "printr inc." ); | ||
session.delete( printer ); | ||
session.getTransaction().commit(); | ||
|
||
session.close(); | ||
} | ||
|
||
/** | ||
* String -> MyString: Exception expected | ||
*/ | ||
@Test | ||
public void testGridTypeForIntermediaryTypeNotSupported() throws Exception { | ||
OgmConfiguration cfg = new OgmConfiguration(); | ||
cfg.addAnnotatedClass( OtherPrinter.class ); | ||
|
||
try { | ||
OgmSessionFactory sf = cfg.buildSessionFactory(); | ||
sf.close(); | ||
assertThat( true == false ).as( "We should fail as the AttributeConverter is not supported" ); | ||
} | ||
catch (Exception e) { | ||
assertThat( e.getCause().getCause().getMessage() ).startsWith( "OGM000084" ); | ||
} | ||
} | ||
|
||
private EntityKey getPrinterEntityKey(UUID id) { | ||
return new EntityKey( | ||
new DefaultEntityKeyMetadata( "Printer", new String[] { "id" } ), | ||
new Object[]{ id.toString() } | ||
); | ||
} | ||
|
||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class<?>[] { Printer.class }; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
core/src/test/java/org/hibernate/ogm/backendtck/type/converter/MyString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Hibernate OGM, Domain model persistence for NoSQL datastores | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.ogm.backendtck.type.converter; | ||
|
||
/** | ||
* @author Emmanuel Bernard emmanuel@hibernate.org | ||
*/ | ||
public class MyString { | ||
|
||
private final String string; | ||
|
||
public MyString(String string) { | ||
// note that the string is explicitly not lower case sanitized | ||
// we let the converter do this job to see if it is called | ||
this.string = string; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return string; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ( ( string == null ) ? 0 : string.hashCode() ); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if ( this == obj ) { | ||
return true; | ||
} | ||
if ( obj == null ) { | ||
return false; | ||
} | ||
if ( getClass() != obj.getClass() ) { | ||
return false; | ||
} | ||
MyString other = (MyString) obj; | ||
if ( string == null ) { | ||
if ( other.string != null ) { | ||
return false; | ||
} | ||
} | ||
else if ( !string.equals( other.string ) ) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
.../java/org/hibernate/ogm/backendtck/type/converter/MyStringToUpperCaseStringConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Hibernate OGM, Domain model persistence for NoSQL datastores | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.ogm.backendtck.type.converter; | ||
|
||
import javax.persistence.AttributeConverter; | ||
|
||
/** | ||
* @author Gunnar Morling | ||
*/ | ||
public class MyStringToUpperCaseStringConverter implements AttributeConverter<MyString, String> { | ||
|
||
@Override | ||
public String convertToDatabaseColumn(MyString attribute) { | ||
return attribute != null ? attribute.toString().toUpperCase() : null; | ||
} | ||
|
||
@Override | ||
public MyString convertToEntityAttribute(String dbData) { | ||
return dbData != null ? new MyString( dbData.toLowerCase() ) : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...src/test/java/org/hibernate/ogm/backendtck/type/converter/StringToByteArrayConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Hibernate OGM, Domain model persistence for NoSQL datastores | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.ogm.backendtck.type.converter; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import javax.persistence.AttributeConverter; | ||
|
||
/** | ||
* @author Gunnar Morling | ||
*/ | ||
public class StringToByteArrayConverter implements AttributeConverter<String, byte[]> { | ||
|
||
@Override | ||
public byte[] convertToDatabaseColumn(String attribute) { | ||
return attribute != null ? attribute.getBytes( StandardCharsets.UTF_8 ) : null; | ||
} | ||
|
||
@Override | ||
public String convertToEntityAttribute(byte[] dbData) { | ||
return dbData != null ? new String( dbData, StandardCharsets.UTF_8 ) : null; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
.../src/test/java/org/hibernate/ogm/backendtck/type/converter/StringToMyStringConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Hibernate OGM, Domain model persistence for NoSQL datastores | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.ogm.backendtck.type.converter; | ||
|
||
import javax.persistence.AttributeConverter; | ||
|
||
/** | ||
* @author Gunnar Morling | ||
*/ | ||
public class StringToMyStringConverter implements AttributeConverter<String, MyString> { | ||
|
||
@Override | ||
public MyString convertToDatabaseColumn(String attribute) { | ||
return attribute != null ? new MyString( attribute.toUpperCase() ) : null; | ||
} | ||
|
||
@Override | ||
public String convertToEntityAttribute(MyString dbData) { | ||
return dbData != null ? dbData.toString().toLowerCase() : null; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...src/test/java/org/hibernate/ogm/backendtck/type/converter/StringToUpperCaseConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Hibernate OGM, Domain model persistence for NoSQL datastores | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.ogm.backendtck.type.converter; | ||
|
||
import javax.persistence.AttributeConverter; | ||
|
||
/** | ||
* @author Gunnar Morling | ||
*/ | ||
public class StringToUpperCaseConverter implements AttributeConverter<String, String> { | ||
|
||
@Override | ||
public String convertToDatabaseColumn(String attribute) { | ||
return attribute != null ? attribute.toUpperCase() : null; | ||
} | ||
|
||
@Override | ||
public String convertToEntityAttribute(String dbData) { | ||
return dbData != null ? dbData.toLowerCase() : null; | ||
} | ||
} |
Oops, something went wrong.