KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > favorites > templates > TemplatesPanelTest


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 package org.netbeans.modules.favorites.templates;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Arrays JavaDoc;
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 /**
34  * Tests creating/renaming/removing templates via TemplatesPanel.
35  *
36  * @author Jiri Rechtacek
37  */

38 public class TemplatesPanelTest extends NbTestCase {
39     File JavaDoc popural;
40     FileObject templateFolder;
41     DataFolder f;
42
43     /** Constructor required by JUnit.
44      * @param testName method name to be used as testcase
45      */

46     public TemplatesPanelTest(String JavaDoc s) {
47         super(s);
48     }
49     
50     protected void setUp () {
51         try {
52             templateFolder = Repository.getDefault ().getDefaultFileSystem ().getRoot ().createFolder ("TestTemplates");
53         } catch (IOException JavaDoc 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 JavaDoc 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 JavaDoc ioe) {
76             fail (ioe.getMessage ());
77         }
78     }
79     
80     public void testNewTemplateFromFile () throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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