1 19 20 package org.netbeans.modules.j2ee.persistence.unit; 21 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.lang.reflect.Field ; 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 40 public abstract class PersistenceEditorTestBase extends PUDataObjectTestBase { 41 42 protected static final String PATH = "/persistence.xml"; 43 protected PUDataObject dataObject; 44 protected FileObject ddFile; 45 protected PersistenceToolBarMVElement mvElement; 46 47 48 public PersistenceEditorTestBase(String name) { 49 super(name); 50 } 51 52 protected void setUp() throws Exception { 53 super.setUp(); 54 initDataObject(); 55 56 } 57 58 protected void tearDown() throws Exception { 59 super.tearDown(); 60 ddFile.delete(); 61 } 62 63 private void initDataObject() throws IOException { 64 String persistenceFile = getDataDir().getAbsolutePath() + PATH; 65 FileObject original = FileUtil.toFileObject(new File (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 82 protected boolean fileObjectContains(FileObject fo, String str){ 83 return readFileObject(fo).indexOf(str) >= 0 ; 84 } 85 86 95 protected boolean dataCacheContains(String str) throws InterruptedException { 96 RequestProcessor.Task updateTask = getUpdateTask(); 97 98 assertNotNull("Could not get updateTask", updateTask); 100 updateTask.waitFinished(20000); 101 if (dataObject.getDataCache().getStringData().indexOf(str) > -1){ 102 return true; 103 } 104 return false; 105 } 106 107 113 private RequestProcessor.Task getUpdateTask(){ 114 RequestProcessor.Task updateTask = null; 115 try { 116 Field puSynchronizerField = dataObject.getClass().getDeclaredField("modelSynchronizer"); 118 puSynchronizerField.setAccessible(true); 119 XmlMultiViewDataSynchronizer puSynchronizer = 120 (XmlMultiViewDataSynchronizer) puSynchronizerField.get(dataObject); 121 Field updateTaskField = puSynchronizer.getClass().getSuperclass().getDeclaredField("updateTask"); 124 updateTaskField.setAccessible(true); 125 updateTask = (RequestProcessor.Task) updateTaskField.get(puSynchronizer); 126 } catch (IllegalArgumentException ex) { 127 throw new RuntimeException (ex); 128 } catch (IllegalAccessException ex) { 129 throw new RuntimeException (ex); 130 } catch (SecurityException ex) { 131 throw new RuntimeException (ex); 132 } catch (NoSuchFieldException ex) { 133 throw new RuntimeException (ex); 134 } 135 return updateTask; 136 } 137 138 protected String readFileObject(FileObject fo){ 139 StringBuffer sb = new StringBuffer (); 140 int i; 141 InputStream stream = null; 142 try { 143 stream = fo.getInputStream(); 144 while ((i = stream.read()) != -1) { 145 sb.append((char) i); 146 } 147 } catch (IOException ex) { 148 fail(ex.getMessage()); 149 } finally { 150 if (stream != null){ 151 try { 152 stream.close(); 153 } catch (IOException 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 |