/tests/com/google/appengine/datanucleus/test/Flight.java

http://datanucleus-appengine.googlecode.com/ · Java · 222 lines · 164 code · 31 blank · 27 comment · 33 complexity · 01ddc07497aa48a19a3195a21db99b48 MD5 · raw file

  1. /**********************************************************************
  2. Copyright (c) 2009 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. **********************************************************************/
  13. package com.google.appengine.datanucleus.test;
  14. import com.google.appengine.api.datastore.Entity;
  15. import com.google.appengine.api.datastore.Key;
  16. import java.io.Serializable;
  17. import javax.jdo.annotations.Column;
  18. import javax.jdo.annotations.Extension;
  19. import javax.jdo.annotations.IdGeneratorStrategy;
  20. import javax.jdo.annotations.PersistenceCapable;
  21. import javax.jdo.annotations.Persistent;
  22. import javax.jdo.annotations.PrimaryKey;
  23. import javax.jdo.annotations.Version;
  24. import javax.jdo.annotations.VersionStrategy;
  25. /**
  26. * @author Max Ross <maxr@google.com>
  27. */
  28. @PersistenceCapable(detachable = "true")
  29. @Version(strategy = VersionStrategy.VERSION_NUMBER)
  30. public class Flight implements Serializable {
  31. private String id;
  32. @Persistent
  33. private String origin;
  34. @Persistent
  35. private String dest;
  36. @Persistent
  37. private String name;
  38. @Persistent
  39. int you;
  40. @Persistent
  41. int me;
  42. @Persistent
  43. @Column(name="flight_number")
  44. int flightNumber;
  45. public Flight() {
  46. }
  47. public Flight(String origin, String dest, String name, int you, int me) {
  48. this.origin = origin;
  49. this.dest = dest;
  50. this.name = name;
  51. this.you = you;
  52. this.me = me;
  53. }
  54. public String getOrigin() {
  55. return origin;
  56. }
  57. public void setOrigin(String origin) {
  58. this.origin = origin;
  59. }
  60. public String getDest() {
  61. return dest;
  62. }
  63. public void setDest(String dest) {
  64. this.dest = dest;
  65. }
  66. public String getName() {
  67. return name;
  68. }
  69. public void setName(String name) {
  70. this.name = name;
  71. }
  72. public int getYou() {
  73. return you;
  74. }
  75. public void setYou(int you) {
  76. this.you = you;
  77. }
  78. public int getMe() {
  79. return me;
  80. }
  81. public void setMe(int me) {
  82. this.me = me;
  83. }
  84. @PrimaryKey
  85. @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value="true")
  86. @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  87. public String getId() {
  88. return id;
  89. }
  90. /**
  91. * You really shouldn't call this unless you're looking for trouble.
  92. * Useful for tests that verify the trouble.
  93. */
  94. public void setId(String id) {
  95. this.id = id;
  96. }
  97. public int getFlightNumber() {
  98. return flightNumber;
  99. }
  100. public void setFlightNumber(int flightNumber) {
  101. this.flightNumber = flightNumber;
  102. }
  103. @Override
  104. public String toString() {
  105. return "\n\nid: " + id + "\norigin: " + origin + "\ndest: " + dest
  106. + "\nname: " + name + "\nyou: " + you + "\nme: " + me;
  107. }
  108. public static Entity newFlightEntity(
  109. String keyName, String name, String origin, String dest, int you, int me) {
  110. return newFlightEntity(keyName, name, origin, dest, you, me, 300);
  111. }
  112. public static void addData(Entity e, String name, String origin, String dest, int you, int me, int flightNumber) {
  113. e.setProperty("name", name);
  114. e.setProperty("origin", origin);
  115. e.setProperty("dest", dest);
  116. e.setProperty("you", you);
  117. e.setProperty("me", me);
  118. e.setProperty("VERSION", 1L);
  119. e.setProperty("flight_number", flightNumber);
  120. }
  121. public static Entity newFlightEntity(Key parent, String keyName, String name, String origin,
  122. String dest, int you, int me, int flightNumber) {
  123. Entity e;
  124. if (keyName == null) {
  125. if (parent == null) {
  126. e = new Entity(Flight.class.getSimpleName());
  127. } else {
  128. e = new Entity(Flight.class.getSimpleName(), parent);
  129. }
  130. } else {
  131. if (parent == null) {
  132. e = new Entity(Flight.class.getSimpleName(), keyName);
  133. } else {
  134. e = new Entity(Flight.class.getSimpleName(), keyName, parent);
  135. }
  136. }
  137. addData(e, name, origin, dest, you, me, flightNumber);
  138. return e;
  139. }
  140. public static Entity newFlightEntity(
  141. String keyName, String name, String origin, String dest, int you, int me, int flightNumber) {
  142. return newFlightEntity(null, keyName, name, origin, dest, you, me, flightNumber);
  143. }
  144. public static Entity newFlightEntity(
  145. String name, String origin, String dest, int you, int me, int flightNumber) {
  146. return newFlightEntity(null, name, origin, dest, you, me, flightNumber);
  147. }
  148. public static Entity newFlightEntity(String name, String origin, String dest,
  149. int you, int me) {
  150. return newFlightEntity(null, name, origin, dest, you, me);
  151. }
  152. /**
  153. * We get weird test failures if we give Flight an equals() method due to
  154. * attempts to read fields of deleted objects.
  155. * TODO(maxr) Straighten this out.
  156. */
  157. public boolean customEquals(Object o) {
  158. if (this == o) {
  159. return true;
  160. }
  161. if (o == null || getClass() != o.getClass()) {
  162. return false;
  163. }
  164. Flight flight = (Flight) o;
  165. if (flightNumber != flight.flightNumber) {
  166. return false;
  167. }
  168. if (me != flight.me) {
  169. return false;
  170. }
  171. if (you != flight.you) {
  172. return false;
  173. }
  174. if (dest != null ? !dest.equals(flight.dest) : flight.dest != null) {
  175. return false;
  176. }
  177. if (id != null ? !id.equals(flight.id) : flight.id != null) {
  178. return false;
  179. }
  180. if (name != null ? !name.equals(flight.name) : flight.name != null) {
  181. return false;
  182. }
  183. if (origin != null ? !origin.equals(flight.origin) : flight.origin != null) {
  184. return false;
  185. }
  186. return true;
  187. }
  188. }