-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13743 from evanchooly/issue12514
MongoDB Panache: Update field access with appropriate get/set methods
- Loading branch information
Showing
16 changed files
with
242 additions
and
19 deletions.
There are no files selected for viewing
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
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
5 changes: 4 additions & 1 deletion
5
...db/panache/person/PersonEntityResource.kt → .../person/resources/PersonEntityResource.kt
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
7 changes: 6 additions & 1 deletion
7
...anache/person/PersonRepositoryResource.kt → ...son/resources/PersonRepositoryResource.kt
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
6 changes: 2 additions & 4 deletions
6
...ve/person/ReactivePersonEntityResource.kt → ...resources/ReactivePersonEntityResource.kt
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
6 changes: 2 additions & 4 deletions
6
...erson/ReactivePersonRepositoryResource.kt → ...urces/ReactivePersonRepositoryResource.kt
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
56 changes: 56 additions & 0 deletions
56
...ion-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/AccessorEntity.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,56 @@ | ||
package io.quarkus.it.mongodb.panache; | ||
|
||
import org.bson.codecs.pojo.annotations.BsonIgnore; | ||
|
||
public class AccessorEntity extends GenericEntity { | ||
|
||
public byte b; | ||
public boolean bool; | ||
public char c; | ||
public double d; | ||
public float f; | ||
public int i; | ||
public long l; | ||
public short s; | ||
public String string; | ||
@BsonIgnore | ||
public Object trans; | ||
@BsonIgnore | ||
public Object trans2; | ||
|
||
transient int getBCalls = 0; | ||
transient int getTransCalls = 0; | ||
transient int setICalls = 0; | ||
transient int setTransCalls = 0; | ||
|
||
public byte getB() { | ||
getBCalls++; | ||
return b; | ||
} | ||
|
||
// explicit getter or setter | ||
|
||
public Object getTrans() { | ||
getTransCalls++; | ||
return trans; | ||
} | ||
|
||
public void setTrans(Object trans) { | ||
setTransCalls++; | ||
this.trans = trans; | ||
} | ||
|
||
public void method() { | ||
// touch some fields | ||
@SuppressWarnings("unused") | ||
byte b2 = b; | ||
i = 2; | ||
t = 1; | ||
t2 = 2; | ||
} | ||
|
||
public void setI(int i) { | ||
setICalls++; | ||
this.i = i; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...tion-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/GenericEntity.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,16 @@ | ||
package io.quarkus.it.mongodb.panache; | ||
|
||
import io.quarkus.mongodb.panache.PanacheMongoEntity; | ||
|
||
public abstract class GenericEntity<T> extends PanacheMongoEntity { | ||
public T t; | ||
public T t2; | ||
|
||
public T getT2() { | ||
return t2; | ||
} | ||
|
||
public void setT2(T t2) { | ||
this.t2 = t2; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...ation-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/TestEndpoint.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,74 @@ | ||
package io.quarkus.it.mongodb.panache; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
|
||
@Path("/accessors") | ||
public class TestEndpoint { | ||
|
||
@GET | ||
public String testAccessors() throws NoSuchMethodException, SecurityException { | ||
checkMethod(AccessorEntity.class, "getString", String.class); | ||
checkMethod(AccessorEntity.class, "isBool", boolean.class); | ||
checkMethod(AccessorEntity.class, "getC", char.class); | ||
checkMethod(AccessorEntity.class, "getS", short.class); | ||
checkMethod(AccessorEntity.class, "getI", int.class); | ||
checkMethod(AccessorEntity.class, "getL", long.class); | ||
checkMethod(AccessorEntity.class, "getF", float.class); | ||
checkMethod(AccessorEntity.class, "getD", double.class); | ||
checkMethod(AccessorEntity.class, "getT", Object.class); | ||
checkMethod(AccessorEntity.class, "getT2", Object.class); | ||
|
||
checkMethod(AccessorEntity.class, "setString", void.class, String.class); | ||
checkMethod(AccessorEntity.class, "setBool", void.class, boolean.class); | ||
checkMethod(AccessorEntity.class, "setC", void.class, char.class); | ||
checkMethod(AccessorEntity.class, "setS", void.class, short.class); | ||
checkMethod(AccessorEntity.class, "setI", void.class, int.class); | ||
checkMethod(AccessorEntity.class, "setL", void.class, long.class); | ||
checkMethod(AccessorEntity.class, "setF", void.class, float.class); | ||
checkMethod(AccessorEntity.class, "setD", void.class, double.class); | ||
checkMethod(AccessorEntity.class, "setT", void.class, Object.class); | ||
checkMethod(AccessorEntity.class, "setT2", void.class, Object.class); | ||
|
||
try { | ||
checkMethod(AccessorEntity.class, "getTrans2", Object.class); | ||
Assertions.fail("transient field should have no getter: trans2"); | ||
} catch (NoSuchMethodException x) { | ||
} | ||
|
||
try { | ||
checkMethod(AccessorEntity.class, "setTrans2", void.class, Object.class); | ||
Assertions.fail("transient field should have no setter: trans2"); | ||
} catch (NoSuchMethodException x) { | ||
} | ||
|
||
// Now check that accessors are called | ||
AccessorEntity entity = new AccessorEntity(); | ||
@SuppressWarnings("unused") | ||
byte b = entity.b; | ||
Assertions.assertEquals(1, entity.getBCalls); | ||
entity.i = 2; | ||
Assertions.assertEquals(1, entity.setICalls); | ||
Object trans = entity.trans; | ||
Assertions.assertEquals(0, entity.getTransCalls); | ||
entity.trans = trans; | ||
Assertions.assertEquals(0, entity.setTransCalls); | ||
|
||
// accessors inside the entity itself | ||
entity.method(); | ||
Assertions.assertEquals(2, entity.getBCalls); | ||
Assertions.assertEquals(2, entity.setICalls); | ||
|
||
return "OK"; | ||
} | ||
|
||
private void checkMethod(Class<?> klass, String name, Class<?> returnType, Class<?>... params) | ||
throws NoSuchMethodException, SecurityException { | ||
Method method = klass.getMethod(name, params); | ||
Assertions.assertEquals(returnType, method.getReturnType()); | ||
} | ||
} |
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
Oops, something went wrong.