KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > unit > PersistenceEditorTestBase


1 /**
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.unit;
21
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.lang.reflect.Field JavaDoc;
27 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Persistence;
28 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
29 import org.netbeans.modules.xml.multiview.XmlMultiViewDataSynchronizer;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileUtil;
32 import org.openide.loaders.DataObject;
33 import org.openide.util.RequestProcessor;
34
35 /**
36  * Base class for persistence multiview editor tests.
37  *
38  * @author Erno Mononen
39  */

40 public abstract class PersistenceEditorTestBase extends PUDataObjectTestBase {
41     
42     protected static final String JavaDoc PATH = "/persistence.xml";
43     protected PUDataObject dataObject;
44     protected FileObject ddFile;
45     protected PersistenceToolBarMVElement mvElement;
46     
47     
48     public PersistenceEditorTestBase(String JavaDoc name) {
49         super(name);
50     }
51     
52     protected void setUp() throws Exception JavaDoc {
53         super.setUp();
54         initDataObject();
55         
56     }
57     
58     protected void tearDown() throws Exception JavaDoc {
59         super.tearDown();
60         ddFile.delete();
61     }
62     
63     private void initDataObject() throws IOException JavaDoc{
64         String JavaDoc persistenceFile = getDataDir().getAbsolutePath() + PATH;
65         FileObject original = FileUtil.toFileObject(new File JavaDoc(persistenceFile));
66         
67         FileObject workDirFO = FileUtil.toFileObject(getWorkDir());
68         this.ddFile = FileUtil.copyFile(original, workDirFO, "persistence_copy");
69         this.dataObject = (PUDataObject) DataObject.find(ddFile);
70         this.mvElement = new PersistenceToolBarMVElement(dataObject);
71         
72         Persistence persistence = dataObject.getPersistence();
73         assertSame(2, persistence.getPersistenceUnit().length);
74         assertEquals("em", persistence.getPersistenceUnit(0).getName());
75         assertEquals("em2", persistence.getPersistenceUnit(1).getName());
76     }
77     
78     /**
79      * @return true if given FileObject contains given String, false
80      * otherwise.
81      */

82     protected boolean fileObjectContains(FileObject fo, String JavaDoc str){
83         return readFileObject(fo).indexOf(str) >= 0 ;
84     }
85     
86     /**
87      * Waits for the model synchronizer to update the data and
88      * then checks whether data object's data cache contains
89      * given string.
90      * @return true if data object's data cache contains given str,
91      * false otherwise.
92      * @throws InterruptedException if waiting has been interrupted or if
93      * the wait cannot succeed due to possible deadlock collision
94      */

95     protected boolean dataCacheContains(String JavaDoc str) throws InterruptedException JavaDoc{
96         RequestProcessor.Task updateTask = getUpdateTask();
97         
98         assertNotNull("Could not get updateTask", updateTask); //NOI18N
99

100         updateTask.waitFinished(20000);
101         if (dataObject.getDataCache().getStringData().indexOf(str) > -1){
102             return true;
103         }
104         return false;
105     }
106     
107     /**
108      * Gets the task that takes care of updating the data from
109      * the data object's model synchronizer. Relies heavily on reflection and
110      * will break down when there are changes in class hierarchy or field names.
111      * Needed for now since the current API doesn't give access to the task.
112      */

113     private RequestProcessor.Task getUpdateTask(){
114         RequestProcessor.Task updateTask = null;
115         try {
116             // get PUDataObject's model synchronizer (PUDataObject.ModelSynchronizer)
117
Field JavaDoc puSynchronizerField = dataObject.getClass().getDeclaredField("modelSynchronizer");
118             puSynchronizerField.setAccessible(true);
119             XmlMultiViewDataSynchronizer puSynchronizer =
120                     (XmlMultiViewDataSynchronizer) puSynchronizerField.get(dataObject);
121             // get the update task from the XmlMultiViewDataSynchronizer that is
122
// puSynchronizer's super class
123
Field JavaDoc updateTaskField = puSynchronizer.getClass().getSuperclass().getDeclaredField("updateTask");
124             updateTaskField.setAccessible(true);
125             updateTask = (RequestProcessor.Task) updateTaskField.get(puSynchronizer);
126         } catch (IllegalArgumentException JavaDoc ex) {
127             throw new RuntimeException JavaDoc(ex);
128         } catch (IllegalAccessException JavaDoc ex) {
129             throw new RuntimeException JavaDoc(ex);
130         } catch (SecurityException JavaDoc ex) {
131             throw new RuntimeException JavaDoc(ex);
132         } catch (NoSuchFieldException JavaDoc ex) {
133             throw new RuntimeException JavaDoc(ex);
134         }
135         return updateTask;
136     }
137     
138     protected String JavaDoc readFileObject(FileObject fo){
139         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
140         int i;
141         InputStream JavaDoc stream = null;
142         try {
143             stream = fo.getInputStream();
144             while ((i = stream.read()) != -1) {
145                 sb.append((char) i);
146             }
147         } catch (IOException JavaDoc ex) {
148             fail(ex.getMessage());
149         } finally {
150             if (stream != null){
151                 try {
152                     stream.close();
153                 } catch (IOException JavaDoc ex) {
154                     fail(ex.getMessage());
155                 }
156             }
157         }
158         return sb.toString();
159         
160     }
161     
162     
163     protected boolean containsUnit(PersistenceUnit persistenceUnit){
164         for (int i = 0; i < dataObject.getPersistence().getPersistenceUnit().length; i++) {
165             if (dataObject.getPersistence().getPersistenceUnit()[i].equals(persistenceUnit)){
166                 return true;
167             }
168         }
169         return false;
170     }
171     
172 }
173
Popular Tags