1 19 package org.netbeans.modules.favorites.templates; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Arrays ; 24 import org.netbeans.junit.*; 25 import org.openide.filesystems.FileLock; 26 import org.openide.filesystems.FileObject; 27 import org.openide.filesystems.FileUtil; 28 import org.openide.filesystems.Repository; 29 import org.openide.loaders.DataFolder; 30 import org.openide.loaders.DataObject; 31 import org.openide.nodes.Node; 32 33 38 public class TemplatesPanelTest extends NbTestCase { 39 File popural; 40 FileObject templateFolder; 41 DataFolder f; 42 43 46 public TemplatesPanelTest(String s) { 47 super(s); 48 } 49 50 protected void setUp () { 51 try { 52 templateFolder = Repository.getDefault ().getDefaultFileSystem ().getRoot ().createFolder ("TestTemplates"); 53 } catch (IOException ioe) { 54 fail (ioe.getMessage ()); 55 } 56 assertNotNull ("TestTemplates folder exists on SFS", templateFolder); 57 try { 58 popural = getWorkDir ().createTempFile ("popural", "java"); 59 } catch (IOException ioe) { 60 fail (ioe.getMessage ()); 61 } 62 assertTrue ("popural.tmp exists", popural.exists ()); 63 64 f = DataFolder.findFolder (templateFolder); 65 66 assertNotNull ("DataFolder found for FO " + templateFolder, f); 67 68 } 69 70 protected void tearDown() { 71 try { 72 FileLock l = templateFolder.lock (); 73 templateFolder.delete (l); 74 l.releaseLock (); 75 } catch (IOException ioe) { 76 fail (ioe.getMessage ()); 77 } 78 } 79 80 public void testNewTemplateFromFile () throws Exception { 81 DataObject dobj = TemplatesPanel.createTemplateFromFile (popural, f); 82 assertNotNull ("New DataObject found.", dobj); 83 assertTrue ("Is template.", dobj.isTemplate ()); 84 assertEquals ("Template is in the preffered folder.", f, dobj.getFolder ()); 85 } 86 87 public void testTwiceNewFromTemplate () throws Exception { 88 testNewTemplateFromFile (); 89 testNewTemplateFromFile (); 90 } 91 92 public void testDuplicateTemplate () { 93 DataObject dobj = TemplatesPanel.createTemplateFromFile (popural, f); 94 DataObject dupl = TemplatesPanel.createDuplicateFromNode (dobj.getNodeDelegate ()); 95 assertNotNull ("Duplicate DataObject found.", dobj); 96 assertTrue ("Duplicate is template.", dobj.isTemplate ()); 97 assertEquals ("Template is in same folder as original.", dobj.getFolder (), dupl.getFolder ()); 98 assertTrue ("Name is derived from original.", dupl.getNodeDelegate ().getName ().startsWith (dobj.getNodeDelegate ().getName ())); 99 } 100 public void testIgnoresSimplefolders() throws Exception { 101 FileObject root = Repository.getDefault ().getDefaultFileSystem ().getRoot (); 102 FileObject fo = FileUtil.createFolder(root, "Templates/SimpleFolder"); 103 fo.setAttribute("simple", Boolean.FALSE); 104 Node n = TemplatesPanel.getTemplateRootNode(); 105 Node[] arr = n.getChildren().getNodes(true); 106 assertEquals("Empty: " + Arrays.asList(arr), 0, arr.length); 107 } 108 public void testIgnoresSimpleNonFolders() throws Exception { 109 FileObject root = Repository.getDefault ().getDefaultFileSystem ().getRoot (); 110 FileObject fo = FileUtil.createData(root, "Templates/SimpleFolder.java"); 111 fo.setAttribute("simple", Boolean.FALSE); 112 fo.setAttribute("template", Boolean.TRUE); 113 Node n = TemplatesPanel.getTemplateRootNode(); 114 Node[] arr = n.getChildren().getNodes(true); 115 assertEquals("Empty: " + Arrays.asList(arr), 0, arr.length); 116 } 117 } 118 | Popular Tags |