PageRenderTime 45ms CodeModel.GetById 1ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/jdo/JDOMetaDataValidatorTest.java

http://datanucleus-appengine.googlecode.com/
Java | 289 lines | 217 code | 41 blank | 31 comment | 4 complexity | 915546fc4f08000bbf33e7857dbc5bf3 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.jdo;
 17
 18import com.google.appengine.datanucleus.test.jdo.IgnorableMappingsJDO.HasUniqueConstraint;
 19import com.google.appengine.datanucleus.test.jdo.IgnorableMappingsJDO.HasUniqueConstraints;
 20import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.EncodedPkOnNonPrimaryKeyField;
 21import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.EncodedPkOnNonStringPrimaryKeyField;
 22import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.HasLongPkWithKeyAncestor;
 23import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.HasLongPkWithStringAncestor;
 24import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.HasMultiplePkIdFields;
 25import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.HasMultiplePkNameFields;
 26import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.HasPkIdSortOnOneToMany;
 27import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.HasPkNameSortOnOneToMany;
 28import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.HasTwoOneToOnesWithSharedBaseClass;
 29import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.HasUnencodedStringPkWithKeyAncestor;
 30import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.HasUnencodedStringPkWithStringAncestor;
 31import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.LongParent;
 32import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.ManyToMany1;
 33import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.ManyToMany2;
 34import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.MultipleAncestors;
 35import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.OneToManyParentWithRootOnlyLongBiChild;
 36import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.OneToManyParentWithRootOnlyLongUniChild;
 37import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.OneToManyParentWithRootOnlyStringBiChild;
 38import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.OneToManyParentWithRootOnlyStringUniChild;
 39import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.OneToOneParentWithRootOnlyLongBiChild;
 40import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.OneToOneParentWithRootOnlyLongUniChild;
 41import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.OneToOneParentWithRootOnlyStringBiChild;
 42import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.OneToOneParentWithRootOnlyStringUniChild;
 43import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.PkIdOnNonLongField;
 44import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.PkIdWithUnencodedStringPrimaryKey;
 45import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.PkMarkedAsAncestor;
 46import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.PkMarkedAsPkId;
 47import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.PkMarkedAsPkName;
 48import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.PkNameOnNonStringField;
 49import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.PkNameWithUnencodedStringPrimaryKey;
 50import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.SequenceOnEncodedStringPk;
 51import com.google.appengine.datanucleus.test.jdo.IllegalMappingsJDO.SequenceOnKeyPk;
 52
 53import javax.jdo.JDOFatalUserException;
 54import javax.jdo.Query;
 55
 56import org.datanucleus.metadata.InvalidMetaDataException;
 57
 58/**
 59 * @author Max Ross <maxr@google.com>
 60 */
 61public class JDOMetaDataValidatorTest extends JDOTestCase {
 62
 63  @Override
 64  protected void tearDown() throws Exception {
 65    // force a new pmf for each test
 66    if (pm != null && !pm.isClosed() && pm.currentTransaction().isActive()) {
 67      rollbackTxn();
 68    }
 69    pmf.close();
 70    super.tearDown();
 71  }
 72
 73  public void testKeyAncestorPlusNameOnlyPK() {
 74    HasUnencodedStringPkWithKeyAncestor pojo = new HasUnencodedStringPkWithKeyAncestor();
 75    pojo.id = "yar";
 76    assertMetaDataException(pojo);
 77  }
 78
 79  public void testStringAncestorPlusNameOnlyPK() {
 80    HasUnencodedStringPkWithStringAncestor pojo = new HasUnencodedStringPkWithStringAncestor();
 81    pojo.id = "yar";
 82    assertMetaDataException(pojo);
 83  }
 84
 85  public void testKeyAncestorPlusLongPK() {
 86    HasLongPkWithKeyAncestor pojo = new HasLongPkWithKeyAncestor();
 87    assertMetaDataException(pojo);
 88  }
 89
 90  public void testStringAncestorPlusLongPK() {
 91    HasLongPkWithStringAncestor pojo = new HasLongPkWithStringAncestor();
 92    assertMetaDataException(pojo);
 93  }
 94
 95  public void testMultiplePKNameFields() {
 96    HasMultiplePkNameFields pojo = new HasMultiplePkNameFields();
 97    assertMetaDataException(pojo);
 98  }
 99
100  public void testMultiplePKIdFields() {
101    HasMultiplePkIdFields pojo = new HasMultiplePkIdFields();
102    assertMetaDataException(pojo);
103  }
104
105  public void testMultipleAncestors() {
106    MultipleAncestors pojo = new MultipleAncestors();
107    assertMetaDataException(pojo);
108  }
109
110  public void testEncodedPkOnNonPrimaryKeyField() {
111    EncodedPkOnNonPrimaryKeyField pojo = new EncodedPkOnNonPrimaryKeyField();
112    pojo.id = "yar";
113    assertMetaDataException(pojo);
114  }
115
116  public void testEncodedPkOnNonStringPrimaryKeyField() {
117    EncodedPkOnNonStringPrimaryKeyField pojo = new EncodedPkOnNonStringPrimaryKeyField();
118    assertMetaDataException(pojo);
119  }
120
121  public void testPkNameOnNonStringField() {
122    PkNameOnNonStringField pojo = new PkNameOnNonStringField();
123    assertMetaDataException(pojo);
124  }
125
126  public void testPkIdOnNonLongField() {
127    PkIdOnNonLongField pojo = new PkIdOnNonLongField();
128    assertMetaDataException(pojo);
129  }
130
131  public void testPkMarkedAsAncestor() {
132    PkMarkedAsAncestor pojo = new PkMarkedAsAncestor();
133    assertMetaDataException(pojo);
134  }
135
136  public void testPkMarkedAsPkId() {
137    PkMarkedAsPkId pojo = new PkMarkedAsPkId();
138    assertMetaDataException(pojo);
139  }
140
141  public void testPkMarkedAsPkName() {
142    PkMarkedAsPkName pojo = new PkMarkedAsPkName();
143    assertMetaDataException(pojo);
144  }
145
146  public void testPkIdWithUnencodedStringPrimaryKey() {
147    PkIdWithUnencodedStringPrimaryKey pojo = new PkIdWithUnencodedStringPrimaryKey();
148    pojo.id = "yar";
149    assertMetaDataException(pojo);
150  }
151
152  public void testPkNameWithUnencodedStringPrimaryKey() {
153    PkNameWithUnencodedStringPrimaryKey pojo = new PkNameWithUnencodedStringPrimaryKey();
154    pojo.id = "yar";
155    assertMetaDataException(pojo);
156  }
157
158  public void testLongPkWithUnidirectionalOneToManyChild() {
159    OneToManyParentWithRootOnlyLongUniChild pojo = new OneToManyParentWithRootOnlyLongUniChild();
160    pojo.id = "yar";
161    assertMetaDataException(pojo);
162  }
163
164  public void testLongPkWithBidirectionalOneToManyChild() {
165    OneToManyParentWithRootOnlyLongBiChild pojo = new OneToManyParentWithRootOnlyLongBiChild();
166    pojo.id = "yar";
167    assertMetaDataException(pojo);
168  }
169
170  public void testStringPkWithUnidirectionalOneToManyChild() {
171    OneToManyParentWithRootOnlyStringUniChild pojo = new OneToManyParentWithRootOnlyStringUniChild();
172    pojo.id = "yar";
173    assertMetaDataException(pojo);
174  }
175
176  public void testStringPkWithBidirectionalOneToManyChild() {
177    OneToManyParentWithRootOnlyStringBiChild pojo = new OneToManyParentWithRootOnlyStringBiChild();
178    pojo.id = "yar";
179    assertMetaDataException(pojo);
180  }
181
182  public void testLongPkWithUnidirectionalOneToOneChild() {
183    OneToOneParentWithRootOnlyLongUniChild pojo = new OneToOneParentWithRootOnlyLongUniChild();
184    pojo.id = "yar";
185    assertMetaDataException(pojo);
186  }
187
188  public void testLongPkWithUnidirectionalOneToOneChild_Fetch() {
189    beginTxn();
190    try {
191      pm.getObjectById(OneToOneParentWithRootOnlyLongUniChild.class, "yar");
192      fail("expected exception");
193    } catch (JDOFatalUserException e) {
194      // good
195      assertTrue(e.getCause() instanceof InvalidMetaDataException);
196      rollbackTxn();
197    }
198  }
199
200  public void testLongPkWithUnidirectionalOneToOneChild_Query() {
201    beginTxn();
202    try {
203      Query q = pm.newQuery(OneToOneParentWithRootOnlyLongUniChild.class);
204      q.execute();
205      fail("expected exception");
206    } catch (JDOFatalUserException e) {
207      // good
208      assertTrue(e.getCause() instanceof InvalidMetaDataException);
209      rollbackTxn();
210    }
211  }
212
213  public void testLongPkWithBidirectionalOneToOneChild() {
214    OneToOneParentWithRootOnlyLongBiChild pojo = new OneToOneParentWithRootOnlyLongBiChild();
215    pojo.id = "yar";
216    assertMetaDataException(pojo);
217  }
218
219  public void testStringPkWithUnidirectionalOneToOneChild() {
220    OneToOneParentWithRootOnlyStringUniChild pojo = new OneToOneParentWithRootOnlyStringUniChild();
221    pojo.id = "yar";
222    assertMetaDataException(pojo);
223  }
224
225  public void testStringPkWithBidirectionalOneToOneChild() {
226    OneToOneParentWithRootOnlyStringBiChild pojo = new OneToOneParentWithRootOnlyStringBiChild();
227    pojo.id = "yar";
228    assertMetaDataException(pojo);
229  }
230
231  public void testAncestorOfIllegalType() {
232    assertMetaDataException(new LongParent());
233  }
234
235  public void testUniqueConstraint() {
236    assertMetaDataException(new HasUniqueConstraint());
237  }
238
239  public void testUniqueConstraints() {
240    assertMetaDataException(new HasUniqueConstraints());
241  }
242
243  public void testManyToMany() {
244    assertMetaDataException(new ManyToMany1());
245    assertMetaDataException(new ManyToMany2());
246  }
247
248  public void testEncodedStringPkWithSequence() {
249    assertMetaDataException(new SequenceOnEncodedStringPk());
250  }
251
252  public void testKeyPkWithSequence() {
253    assertMetaDataException(new SequenceOnKeyPk());
254  }
255
256  // Only applicable to earlier storage versions
257/*  public void testHasMultipleRelationshipFieldsOfSameType() {
258    assertMetaDataException(new Has2CollectionsOfSameType());
259    assertMetaDataException(new Has2OneToOnesOfSameType());
260    assertMetaDataException(new HasOneToOneAndOneToManyOfSameType());
261    assertMetaDataException(new Has2CollectionsOfSameTypeChild());
262    assertMetaDataException(new Has2CollectionsOfAssignableType());
263    assertMetaDataException(new Has2CollectionsOfAssignableTypeSub());
264  }*/
265
266  public void testHasKeySubComponentSortOnOneToMany() {
267    assertMetaDataException(new HasPkIdSortOnOneToMany());
268    assertMetaDataException(new HasPkNameSortOnOneToMany());
269  }
270
271  public void testHasTwoOneToOnesWithSharedBaseClass() throws Exception {
272    beginTxn();
273    pm.makePersistent(new HasTwoOneToOnesWithSharedBaseClass());
274    commitTxn();
275  }
276
277  private void assertMetaDataException(Object pojo) {
278    beginTxn();
279    try {
280      pm.makePersistent(pojo);
281      fail("expected exception");
282    } catch (JDOFatalUserException e) {
283      // good
284      assertTrue(e.getCause().getClass().getName(),
285                 e.getCause() instanceof InvalidMetaDataException);
286      rollbackTxn();
287    }
288  }
289}