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