1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software 
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License.
16   */
17  
18  /*
19   * InstanceLifecycleEventTest.java
20   *
21   */
22  
23  package javax.jdo.listener;
24  
25  import javax.jdo.util.AbstractTest;
26  import javax.jdo.util.BatchTestRunner;
27  
28  /**
29   * Tests that instances of InstanceLifecycleEvent can be created and
30   * that the source, type, and target instances are correct.
31   */
32  public class InstanceLifecycleEventTest extends AbstractTest {
33      
34      Object created = new Object();
35      Object loaded = new Object();
36      Object stored = new Object();
37      Object cleared = new Object();
38      Object deleted = new Object();
39      Object dirtied = new Object();
40      Object attached = new Object();
41      Object attachTarget = new Object();
42      Object detached = new Object();
43      Object detachTarget = new Object();
44      
45      /** Creates a new instance of SingleFieldIdentityTest */
46      public InstanceLifecycleEventTest() {
47      }
48      
49      /**
50       * @param args the command line arguments
51       */
52      public static void main(String[] args) {
53          BatchTestRunner.run(InstanceLifecycleEventTest.class);
54      }
55      
56      public void testConstructorCreateEvent() {
57          InstanceLifecycleEvent e = new InstanceLifecycleEvent 
58                  (created, InstanceLifecycleEvent.CREATE);
59          assertSame ("Create source differs.", created, e.getSource());
60          assertEquals ("Create type differs.", InstanceLifecycleEvent.CREATE, e.getEventType());
61      }
62      
63      public void testConstructorLoadEvent() {
64          InstanceLifecycleEvent e = new InstanceLifecycleEvent 
65                  (loaded, InstanceLifecycleEvent.LOAD);
66          assertSame ("Load source differs.", loaded, e.getSource());
67          assertEquals ("Load type differs.", InstanceLifecycleEvent.LOAD, e.getEventType());
68      }
69      
70      public void testConstructorStoreEvent() {
71          InstanceLifecycleEvent e = new InstanceLifecycleEvent 
72                  (stored, InstanceLifecycleEvent.STORE);
73          assertSame ("Store source differs.", stored, e.getSource());
74          assertEquals ("Store type differs.", InstanceLifecycleEvent.STORE, e.getEventType());
75      }
76      
77      public void testConstructorClearEvent() {
78          InstanceLifecycleEvent e = new InstanceLifecycleEvent 
79                  (cleared, InstanceLifecycleEvent.CLEAR);
80          assertSame ("Clear source differs.", cleared, e.getSource());
81          assertEquals ("Clear type differs.", InstanceLifecycleEvent.CLEAR, e.getEventType());
82      }
83      
84      public void testConstructorDeleteEvent() {
85          InstanceLifecycleEvent e = new InstanceLifecycleEvent 
86                  (deleted, InstanceLifecycleEvent.DELETE);
87          assertSame ("Delete source differs.", deleted, e.getSource());
88          assertEquals ("Delete type differs.", InstanceLifecycleEvent.DELETE, e.getEventType());
89      }
90      
91      public void testConstructorDirtyEvent() {
92          InstanceLifecycleEvent e = new InstanceLifecycleEvent 
93                  (dirtied, InstanceLifecycleEvent.DIRTY);
94          assertSame ("Dirty source differs.", dirtied, e.getSource());
95          assertEquals ("Dirty type differs.", InstanceLifecycleEvent.DIRTY, e.getEventType());
96      }
97      
98      public void testConstructorDetachEvent() {
99          InstanceLifecycleEvent e = new InstanceLifecycleEvent 
100                 (detached, InstanceLifecycleEvent.DETACH, detachTarget);
101         assertSame ("Detach source differs.", detached, e.getSource());
102         assertEquals ("Detach type differs.", InstanceLifecycleEvent.DETACH, e.getEventType());
103         assertSame ("Detach target differs.", detachTarget, e.getTarget());
104     }
105     
106     public void testConstructorAttachEvent() {
107         InstanceLifecycleEvent e = new InstanceLifecycleEvent 
108                 (attached, InstanceLifecycleEvent.ATTACH, attachTarget);
109         assertSame ("Attach source differs.", attached, e.getSource());
110         assertEquals ("Attach type differs.", InstanceLifecycleEvent.ATTACH, e.getEventType());
111         assertSame ("Attach target differs.", attachTarget, e.getTarget());
112     }
113     
114     public void testIllegalConstructorTooSmall() {
115         try {
116             InstanceLifecycleEvent e = new InstanceLifecycleEvent (new Object(), -1);
117         } catch (IllegalArgumentException e) {
118             return; // good catch
119         } 
120         fail ("Invalid event did not throw IllegalArgumentException.");
121     }
122     
123     public void testIllegalConstructorTooBig() {
124         try {
125             InstanceLifecycleEvent e = new InstanceLifecycleEvent (new Object(), 8);
126         } catch (IllegalArgumentException e) {
127             return; // good catch
128         }
129         fail ("Invalid event did not throw IllegalArgumentException.");
130     }
131 }