KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > spi > support > PersistenceScopesHelperTest


1 package org.netbeans.modules.j2ee.persistence.spi.support;
2
3 import java.beans.PropertyChangeEvent JavaDoc;
4 import java.beans.PropertyChangeListener JavaDoc;
5 import java.io.File JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.Arrays JavaDoc;
8 import org.netbeans.api.java.classpath.ClassPath;
9 import org.netbeans.junit.NbTestCase;
10 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope;
11 import org.netbeans.modules.j2ee.persistence.api.PersistenceScopes;
12 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeFactory;
13 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeImplementation;
14 import org.openide.filesystems.FileObject;
15 import org.openide.filesystems.FileUtil;
16
17 /**
18  *
19  * @author Andrei Badea
20  */

21 public class PersistenceScopesHelperTest extends NbTestCase {
22
23     private FileObject dataDir;
24     private FileObject workDir;
25
26     private PersistenceScope persistenceScope;
27
28     public PersistenceScopesHelperTest(String JavaDoc testName) {
29         super(testName);
30     }
31
32     public void setUp() throws Exception JavaDoc {
33         clearWorkDir();
34         dataDir = FileUtil.toFileObject(getDataDir());
35         workDir = FileUtil.toFileObject(getWorkDir());
36
37         persistenceScope = PersistenceScopeFactory.createPersistenceScope(new PersistenceScopeImpl());
38     }
39
40     private static FileObject copyFile(FileObject source, FileObject destFolder) throws IOException JavaDoc {
41         return FileUtil.copyFile(source, destFolder, source.getName());
42     }
43
44     public void testBasic() throws Exception JavaDoc {
45         PersistenceScope persistenceScope2 = PersistenceScopeFactory.createPersistenceScope(new PersistenceScopeImpl());
46
47         FileObject parent = workDir.createFolder("parent");
48         File JavaDoc persistenceXmlFile = new File JavaDoc(FileUtil.toFile(parent), "persistence.xml");
49
50         PersistenceScopesHelper helper = new PersistenceScopesHelper();
51         PersistenceScopes persistenceScopes = helper.getPersistenceScopes();
52         PCL listener = new PCL();
53         persistenceScopes.addPropertyChangeListener(listener);
54
55         assertEquals(0, persistenceScopes.getPersistenceScopes().length);
56         assertEquals(0, listener.changeCount);
57
58         helper.changePersistenceScope(persistenceScope, persistenceXmlFile);
59
60         assertEquals(0, persistenceScopes.getPersistenceScopes().length);
61         assertEquals(0, listener.changeCount);
62
63         // changing the persistence scope to null while persistence.xml is not null is not supported...
64

65         try {
66             helper.changePersistenceScope(null, persistenceXmlFile);
67             fail();
68         } catch (IllegalArgumentException JavaDoc e) { }
69
70         // ... but changing the it to null while persistence.xml is null is
71

72         helper.changePersistenceScope(null, null);
73
74         assertEquals(0, persistenceScopes.getPersistenceScopes().length);
75         assertEquals(0, listener.changeCount);
76
77         // changing the persistence scope -- should not trigger any events, persistence.xml does not exist
78

79         helper.changePersistenceScope(persistenceScope2, persistenceXmlFile);
80
81         assertEquals(0, persistenceScopes.getPersistenceScopes().length);
82         assertEquals(0, listener.changeCount);
83
84         // setting a null persistence.xml -- should not trigger any events, persistence.xml did not exist before
85

86         helper.changePersistenceScope(persistenceScope2, null);
87
88         assertEquals(0, persistenceScopes.getPersistenceScopes().length);
89         assertEquals(0, listener.changeCount);
90
91         // setting back a non-null, but not existing persistence.xml -- should not trigger any events
92

93         helper.changePersistenceScope(persistenceScope2, persistenceXmlFile);
94
95         assertEquals(0, persistenceScopes.getPersistenceScopes().length);
96         assertEquals(0, listener.changeCount);
97
98         // creating the persistence.xml file -- should trigger an event
99

100         copyFile(dataDir.getFileObject("persistence.xml"), parent);
101
102         assertEquals(1, listener.changeCount);
103         assertSame(persistenceScope2, persistenceScopes.getPersistenceScopes()[0]);
104
105         // changing the persistence scope -- should trigger an event
106

107         helper.changePersistenceScope(persistenceScope, persistenceXmlFile);
108
109         assertEquals(2, listener.changeCount);
110         assertSame(persistenceScope, persistenceScopes.getPersistenceScopes()[0]);
111
112         // changing to the same persistence scope -- should not trigger an event, since nothing has changed from the client's point of view
113

114         helper.changePersistenceScope(persistenceScope, persistenceXmlFile);
115
116         assertEquals(2, listener.changeCount);
117         assertSame(persistenceScope, persistenceScopes.getPersistenceScopes()[0]);
118
119         // setting a null persistence.xml -- should trigger an event, persistence.xml existed before
120

121         helper.changePersistenceScope(persistenceScope, null);
122
123         assertEquals(3, listener.changeCount);
124         assertEquals(0, persistenceScopes.getPersistenceScopes().length);
125
126         // setting back a non-null and existing persistence.xml -- should trigger an event
127

128         helper.changePersistenceScope(persistenceScope, persistenceXmlFile);
129
130         assertEquals(4, listener.changeCount);
131         assertSame(persistenceScope, persistenceScopes.getPersistenceScopes()[0]);
132
133         // removing the persistence.xml file -- should trigger an event
134

135         parent.getFileObject("persistence.xml").delete();
136
137         assertEquals(5, listener.changeCount);
138         assertEquals(0, persistenceScopes.getPersistenceScopes().length);
139
140         // just making sure the helper is still returning the same instance of PersistenceScopes
141

142         assertSame(persistenceScopes, helper.getPersistenceScopes());
143     }
144
145     public void testPropertyChangeWhenFirstTimeSettingExistingPersistenceXml() throws Exception JavaDoc {
146         FileObject persistenceXml = copyFile(dataDir.getFileObject("persistence.xml"), workDir);
147
148         PersistenceScopesHelper helper = new PersistenceScopesHelper();
149         PersistenceScopes persistenceScopes = helper.getPersistenceScopes();
150         PCL listener = new PCL();
151         persistenceScopes.addPropertyChangeListener(listener);
152
153         helper.changePersistenceScope(persistenceScope, FileUtil.toFile(persistenceXml));
154
155         assertEquals(1, listener.changeCount);
156         assertSame(persistenceScope, helper.getPersistenceScopes().getPersistenceScopes()[0]);
157     }
158
159     private static final class PersistenceScopeImpl implements PersistenceScopeImplementation {
160
161         public FileObject getPersistenceXml() {
162             return null;
163         }
164
165         public ClassPath getClassPath() {
166             return null;
167         }
168     }
169
170     private static final class PCL implements PropertyChangeListener JavaDoc {
171
172         private int changeCount;
173
174         public void propertyChange(PropertyChangeEvent JavaDoc event) {
175             assertEquals(PersistenceScopes.PROP_PERSISTENCE_SCOPES, event.getPropertyName());
176             changeCount++;
177         }
178     }
179 }
180
Popular Tags