PageRenderTime 53ms CodeModel.GetById 43ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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