Skip to content

Commit

Permalink
Patch for Issue xetorthio#31: Cannot add non-JOhm annotations to @id
Browse files Browse the repository at this point in the history
…field.
  • Loading branch information
gsharma authored and Jonathan Leibiusky committed Jan 30, 2011
1 parent 8458442 commit b63f97a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/main/java/redis/clients/johm/JOhmUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package redis.clients.johm;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -319,9 +320,18 @@ static Long checkValidId(final Object model) {
}

static void checkValidIdType(final Field field) {
if (field.getAnnotations().length > 1) {
throw new JOhmException(
"Element annotated @Id cannot have any other annotations");
Annotation[] annotations = field.getAnnotations();
if (annotations.length > 1) {
for (Annotation annotation : annotations) {
Class<?> annotationType = annotation.annotationType();
if (annotationType.equals(Id.class)) {
continue;
}
if (JOHM_SUPPORTED_ANNOTATIONS.contains(annotationType)) {
throw new JOhmException(
"Element annotated @Id cannot have any other JOhm annotations");
}
}
}
Class<?> type = field.getType().getClass();
if (!type.isInstance(Long.class) || !type.isInstance(long.class)) {
Expand Down Expand Up @@ -448,6 +458,7 @@ public static boolean checkSupportedPrimitiveClazz(
}

private static final Set<Class<?>> JOHM_SUPPORTED_PRIMITIVES = new HashSet<Class<?>>();
private static final Set<Class<?>> JOHM_SUPPORTED_ANNOTATIONS = new HashSet<Class<?>>();
static {
JOHM_SUPPORTED_PRIMITIVES.add(String.class);
JOHM_SUPPORTED_PRIMITIVES.add(Byte.class);
Expand All @@ -468,5 +479,16 @@ public static boolean checkSupportedPrimitiveClazz(
JOHM_SUPPORTED_PRIMITIVES.add(boolean.class);
JOHM_SUPPORTED_PRIMITIVES.add(BigDecimal.class);
JOHM_SUPPORTED_PRIMITIVES.add(BigInteger.class);

JOHM_SUPPORTED_ANNOTATIONS.add(Array.class);
JOHM_SUPPORTED_ANNOTATIONS.add(Attribute.class);
JOHM_SUPPORTED_ANNOTATIONS.add(CollectionList.class);
JOHM_SUPPORTED_ANNOTATIONS.add(CollectionMap.class);
JOHM_SUPPORTED_ANNOTATIONS.add(CollectionSet.class);
JOHM_SUPPORTED_ANNOTATIONS.add(CollectionSortedSet.class);
JOHM_SUPPORTED_ANNOTATIONS.add(Id.class);
JOHM_SUPPORTED_ANNOTATIONS.add(Indexed.class);
JOHM_SUPPORTED_ANNOTATIONS.add(Model.class);
JOHM_SUPPORTED_ANNOTATIONS.add(Reference.class);
}
}
8 changes: 8 additions & 0 deletions src/test/java/redis/clients/johm/BasicPersistenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.Test;

import redis.clients.johm.models.Country;
import redis.clients.johm.models.FaultyModel;
import redis.clients.johm.models.Item;
import redis.clients.johm.models.User;

Expand Down Expand Up @@ -174,6 +175,13 @@ public void shouldNotPersistWithoutModel() {
JOhm.save(dummyNest);
}

@Test(expected = JOhmException.class)
public void shouldNotPersistModelWithOtherJOhmIdAnnotations() {
FaultyModel badModel = new FaultyModel();
badModel.setType("horribleId");
JOhm.save(badModel);
}

@Test
public void shouldHandleReferences() {
User user = new User();
Expand Down
1 change: 1 addition & 0 deletions src/test/java/redis/clients/johm/models/Country.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@Model
public class Country {
@Id
@TestAnnotation
private Long id;
@Attribute
private String name;
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/redis/clients/johm/models/FaultyModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package redis.clients.johm.models;

import redis.clients.johm.Array;
import redis.clients.johm.Attribute;
import redis.clients.johm.Id;
import redis.clients.johm.Model;

@Model
public class FaultyModel {
@Id
@Array(of = long.class, length = 1)
private long id;
@Attribute
private String type;

public long getId() {
return id;
}

public void setType(String type) {
this.type = type;
}

public String getType() {
return type;
}
}
11 changes: 11 additions & 0 deletions src/test/java/redis/clients/johm/models/TestAnnotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package redis.clients.johm.models;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TestAnnotation {
}

0 comments on commit b63f97a

Please sign in to comment.