Skip to content

Commit

Permalink
OGM-1458 Test querying on Byte type field
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever committed Sep 28, 2018
1 parent 56b5716 commit 9e6daee
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 5 deletions.
Expand Up @@ -35,7 +35,7 @@ public class QueryWithParametersTest extends OgmJpaTestCase {
public PackagingRule packaging = new PackagingRule( "persistencexml/ogm.xml", Movie.class );

@Test
@SkipByGridDialect(value = { INFINISPAN_REMOTE }, comment = "Query on Byte field are currently not supported")
@SkipByGridDialect(value = { INFINISPAN_REMOTE }, comment = "Due to ISPN-7863, fields starting with 'v' are currently not supported.")
public void canUseByteForSimpleComparison() {
EntityManager entityManager = getFactory().createEntityManager();
entityManager.getTransaction().begin();
Expand All @@ -50,7 +50,7 @@ public void canUseByteForSimpleComparison() {
}

@Test
@SkipByGridDialect(value = { INFINISPAN_REMOTE }, comment = "Query on Byte field are currently not supported")
@SkipByGridDialect(value = { INFINISPAN_REMOTE }, comment = "Due to ISPN-7863, fields starting with 'v' are currently not supported.")
public void canUseByteAsParameterForSimpleComparison() {
EntityManager entityManager = getFactory().createEntityManager();
entityManager.getTransaction().begin();
Expand All @@ -66,7 +66,7 @@ public void canUseByteAsParameterForSimpleComparison() {
}

@Test
@SkipByGridDialect(value = { INFINISPAN_REMOTE }, comment = "Query on Byte field are currently not supported")
@SkipByGridDialect(value = { INFINISPAN_REMOTE }, comment = "Due to ISPN-7863, fields starting with 'v' are currently not supported.")
public void canUseByteAsParameterForInComparison() {
EntityManager entityManager = getFactory().createEntityManager();
entityManager.getTransaction().begin();
Expand Down
Expand Up @@ -79,11 +79,13 @@ private void applyNamedParameters(QueryParameters queryParameters, Query query)
private Object getValue(Map.Entry<String, TypedGridValue> param) {
Object value = param.getValue().getValue();

// Protobuf does not support Character type
// convert to String type
// ProtoStream does not support natively Character or Byte types
if ( value instanceof Character ) {
return value.toString();
}
if ( value instanceof Byte ) {
return ( (Byte) value ).intValue();
}

return value;
}
Expand Down
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.ogm.datastore.infinispanremote.test.mapping;

import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.Id;

Expand All @@ -22,6 +23,14 @@ public class ByteEntity {

private Byte counter;

public ByteEntity() {
}

public ByteEntity(Integer id, Byte counter) {
this.id = id;
this.counter = counter;
}

public Integer getId() {
return id;
}
Expand All @@ -37,4 +46,31 @@ public Byte getCounter() {
public void setCounter(Byte counter) {
this.counter = counter;
}

@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
ByteEntity that = (ByteEntity) o;
return Objects.equals( id, that.id ) &&
Objects.equals( counter, that.counter );
}

@Override
public int hashCode() {
return Objects.hash( id, counter );
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder( "ByteEntity{" );
sb.append( "id=" ).append( id );
sb.append( ", counter=" ).append( counter );
sb.append( '}' );
return sb.toString();
}
}
@@ -0,0 +1,105 @@
/*
* 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.infinispanremote.test.query.parameter;

import static org.fest.assertions.Assertions.assertThat;

import java.util.Arrays;
import java.util.List;

import org.hibernate.ogm.backendtck.queries.parameters.Movie;
import org.hibernate.ogm.datastore.infinispanremote.test.mapping.ByteEntity;
import org.hibernate.ogm.datastore.infinispanremote.utils.InfinispanRemoteJpaServerRunner;
import org.hibernate.ogm.utils.TestForIssue;
import org.hibernate.ogm.utils.jpa.OgmJpaTestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* Alternative version of {@link org.hibernate.ogm.backendtck.queries.parameters.QueryWithParametersTest}
* to test querying on {@link Byte} field,
* because the original uses a {@link Byte} field with a starting with 'v' name: {@link Movie#viewerRating}.
*
* @see QueryWithParametersTest#canUseByteForSimpleComparison
* @see QueryWithParametersTest#canUseByteAsParameterForSimpleComparison
* @see QueryWithParametersTest#canUseByteAsParameterForInComparison
* @author Fabio Massimo Ercoli
*/
@RunWith(InfinispanRemoteJpaServerRunner.class)
@TestForIssue(jiraKey = "OGM-1458")
public class QueryWithParametersTest extends OgmJpaTestCase {

private ByteEntity entityA;
private ByteEntity entityB;
private ByteEntity entityC;

@Before
public void setup() {
entityA = new ByteEntity( 1, (byte) 7 );
entityB = new ByteEntity( 2, (byte) 3 );
entityC = new ByteEntity( 3, (byte) 9 );

inTransaction( em -> {
em.persist( entityA );
em.persist( entityB );
em.persist( entityC );
} );
}

@After
public void teardown() {
inTransaction( em -> {
em.refresh( entityA );
em.refresh( entityB );
em.refresh( entityC );

em.remove( entityA );
em.remove( entityB );
em.remove( entityC );
} );
}

@Test
public void canUseByteForSimpleComparison() {
inTransaction( em -> {
List<ByteEntity> entities = em.createQuery( "SELECT e FROM ByteEntity e WHERE e.counter = 3", ByteEntity.class )
.getResultList();

assertThat( entities ).containsExactly( entityB );
} );
}

@Test
public void canUseByteAsParameterForSimpleComparison() {
inTransaction( em -> {
List<ByteEntity> entities = em.createQuery( "SELECT e FROM ByteEntity e WHERE e.counter = :counter", ByteEntity.class )
.setParameter( "counter", (byte) 3 )
.getResultList();

assertThat( entities ).containsExactly( entityB );
} );
}

@Test
public void canUseByteAsParameterForInComparison() {
inTransaction( em -> {
List<ByteEntity> entities = em.createQuery( "SELECT e FROM ByteEntity e WHERE e.counter IN (:counters)", ByteEntity.class )
.setParameter( "counters", Arrays.asList( (byte) 7, (byte) 9 ) )
.getResultList();

assertThat( entities ).containsOnly( entityA, entityC );
} );
}

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { ByteEntity.class };
}
}

0 comments on commit 9e6daee

Please sign in to comment.