Skip to content

Commit

Permalink
OGM-717 Adding support for @type(type = "true_false")
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling authored and DavideD committed May 14, 2015
1 parent 2bfcf3f commit a2b310e
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 9 deletions.
@@ -0,0 +1,35 @@
/*
* 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.type.descriptor.impl;

import org.hibernate.ogm.model.spi.Tuple;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;

/**
* Maps a field to a {@link Character} value.
*
* @author Gunnar Morling
*/
public class CharMappedGridTypeDescriptor implements GridTypeDescriptor {
public static final CharMappedGridTypeDescriptor INSTANCE = new CharMappedGridTypeDescriptor();

@Override
public <X> GridValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
return new BasicGridBinder<X>(javaTypeDescriptor, this) {
@Override
protected void doBind(Tuple resultset, X value, String[] names, WrapperOptions options) {
resultset.put( names[0], javaTypeDescriptor.unwrap( value, Character.class, options ) );
}
};
}

@Override
public <X> GridValueExtractor<X> getExtractor(JavaTypeDescriptor<X> javaTypeDescriptor) {
return new BasicGridExtractor<X>( javaTypeDescriptor, true );
}
}
35 changes: 35 additions & 0 deletions core/src/main/java/org/hibernate/ogm/type/impl/TrueFalseType.java
@@ -0,0 +1,35 @@
/*
* 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.type.impl;

import org.hibernate.MappingException;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.ogm.type.descriptor.impl.CharMappedGridTypeDescriptor;

/**
* Maps {@link Boolean} to {@code T} or {@code F} characters.
*
* @author Gunnar Morling
*/
public class TrueFalseType extends AbstractGenericBasicType<Boolean> {

public static final TrueFalseType INSTANCE = new TrueFalseType();

public TrueFalseType() {
super( CharMappedGridTypeDescriptor.INSTANCE, org.hibernate.type.TrueFalseType.INSTANCE.getJavaTypeDescriptor() );
}

@Override
public int getColumnSpan(Mapping mapping) throws MappingException {
return 1;
}

@Override
public String getName() {
return "true_false";
}
}
Expand Up @@ -70,6 +70,7 @@ public TypeTranslatorImpl(GridDialect dialect) {
tmpMap.put( BigDecimalTypeDescriptor.INSTANCE, BigDecimalType.INSTANCE );
tmpMap.put( BigIntegerTypeDescriptor.INSTANCE, BigIntegerType.INSTANCE );
tmpMap.put( BooleanTypeDescriptor.INSTANCE, BooleanType.INSTANCE );
tmpMap.put( TrueFalseType.INSTANCE.getJavaTypeDescriptor(), TrueFalseType.INSTANCE );
tmpMap.put( ByteTypeDescriptor.INSTANCE, ByteType.INSTANCE );
tmpMap.put( JdbcDateTypeDescriptor.INSTANCE, DateType.INSTANCE );
tmpMap.put( JdbcTimestampTypeDescriptor.INSTANCE, TimestampType.INSTANCE );
Expand Down
13 changes: 13 additions & 0 deletions core/src/test/java/org/hibernate/ogm/backendtck/type/Bookmark.java
Expand Up @@ -12,6 +12,7 @@
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
Expand All @@ -22,6 +23,7 @@
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

/**
* Test entity containing the data types each data store needs to handle.
Expand Down Expand Up @@ -52,6 +54,9 @@ public enum Classifier {
private Boolean favourite;
private Byte displayMask;

@Type(type = "true_false")
private boolean isPrivate;

// byte arrays
@Lob
private byte[] lob;
Expand Down Expand Up @@ -154,6 +159,14 @@ public Boolean getFavourite() {
return favourite;
}

public void setPrivate(boolean isPrivate) {
this.isPrivate = isPrivate;
}

public boolean isPrivate() {
return isPrivate;
}

public Byte getDisplayMask() {
return displayMask;
}
Expand Down
Expand Up @@ -6,6 +6,9 @@
*/
package org.hibernate.ogm.backendtck.type;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
Expand All @@ -15,18 +18,14 @@
import java.util.TimeZone;
import java.util.UUID;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.ogm.backendtck.type.Bookmark.Classifier;
import org.hibernate.ogm.utils.OgmTestCase;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* @author Emmanuel Bernard &lt;emmanuel@hibernate.org&gt;
Expand Down Expand Up @@ -126,6 +125,14 @@ public void testBooleanSupport() throws Exception {
assertEquals( "Boolean value does not match", bookmark.getFavourite(), loadedBookmark.getFavourite() );
}

@Test
public void testTrueFalseMappedBooleanSupport() throws Exception {
bookmark.setPrivate( true );

Bookmark loadedBookmark = saveAndGet( bookmark );
assertEquals( "Boolean value does not match", bookmark.isPrivate(), loadedBookmark.isPrivate() );
}

@Test
public void testByteSupport() throws Exception {
bookmark.setDisplayMask( (byte) '8' );
Expand Down
@@ -0,0 +1,107 @@
/*
* 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.datastore.mongodb.test.mapping;

import static org.hibernate.ogm.datastore.mongodb.utils.MongoDBTestHelper.assertDbObject;

import org.hibernate.Transaction;
import org.hibernate.ogm.OgmSession;
import org.hibernate.ogm.backendtck.type.Bookmark;
import org.hibernate.ogm.utils.OgmTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* Tests for the mappings of built-in types into MongoDB.
*
* @author Gunnar Morling
*/
public class BuiltinTypeMappingTest extends OgmTestCase {

private String bookmarkId;

@Before
public void setUpTestData() {
OgmSession session = openSession();
Transaction transaction = session.beginTransaction();

Bookmark bookmark = new Bookmark();
bookmark.setFavourite( Boolean.TRUE );
bookmark.setPrivate( true );

session.persist( bookmark );
transaction.commit();
session.close();

this.bookmarkId = bookmark.getId();
}

@After
public void removeTestData() {
OgmSession session = openSession();
Transaction transaction = session.beginTransaction();

session.delete( session.get( Bookmark.class, bookmarkId ) );

transaction.commit();
session.close();
}

@Test
public void booleanMapping() {
OgmSession session = openSession();
Transaction transaction = session.beginTransaction();

assertDbObject(
session.getSessionFactory(),
// collection
"Bookmark",
// query
"{ '_id' : '" + bookmarkId + "' }",
// fields
"{ 'favourite' : '1' }",
// expected
"{ " +
"'_id' : '" + bookmarkId + "', " +
"'favourite' : true" +
"}"
);

transaction.commit();
session.close();
}

@Test
public void trueFalseTypeMapping() {
OgmSession session = openSession();
Transaction transaction = session.beginTransaction();

assertDbObject(
session.getSessionFactory(),
// collection
"Bookmark",
// query
"{ '_id' : '" + bookmarkId + "' }",
// fields
"{ 'isPrivate' : '1' }",
// expected
"{ " +
"'_id' : '" + bookmarkId + "', " +
"'isPrivate' : 'T'" +
"}"
);

transaction.commit();
session.close();
}

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Bookmark.class };
}
}
Expand Up @@ -267,11 +267,16 @@ public GridDialect getGridDialect(DatastoreProvider datastoreProvider) {
}

public static void assertDbObject(OgmSessionFactory sessionFactory, String collection, String queryDbObject, String expectedDbObject) {
assertDbObject( sessionFactory, collection, queryDbObject, null, expectedDbObject );
}

public static void assertDbObject(OgmSessionFactory sessionFactory, String collection, String queryDbObject, String projectionDbObject, String expectedDbObject) {
DBObject finder = (DBObject) JSON.parse( queryDbObject );
DBObject fields = projectionDbObject != null ? (DBObject) JSON.parse( projectionDbObject ) : null;
DBObject expected = (DBObject) JSON.parse( expectedDbObject );

MongoDBDatastoreProvider provider = MongoDBTestHelper.getProvider( sessionFactory );
DBObject actual = provider.getDatabase().getCollection( collection ).findOne( finder );
DBObject actual = provider.getDatabase().getCollection( collection ).findOne( finder, fields );

assertThat( isDBObjectAndContentEqual( actual, expected ) ).describedAs( "Expected: " + expected + " but was: " + actual ).isTrue();
}
Expand Down

0 comments on commit a2b310e

Please sign in to comment.