KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > wizards > shortcut > ShortcutWizardTestBase


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.apache.tools.ant.module.wizards.shortcut;
21
22 import java.io.File JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import org.apache.tools.ant.module.api.AntProjectCookie;
26 import org.apache.tools.ant.module.xml.AntProjectSupport;
27 import org.netbeans.junit.MockServices;
28 import org.netbeans.junit.NbTestCase;
29 import org.openide.filesystems.FileLock;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileSystem;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.filesystems.LocalFileSystem;
34 import org.openide.filesystems.Repository;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38
39 /**
40  * Base class for tests in the package.
41  * @author Jesse Glick
42  */

43 public abstract class ShortcutWizardTestBase extends NbTestCase {
44     
45     protected ShortcutWizardTestBase(String JavaDoc name) {
46         super(name);
47     }
48
49     private File JavaDoc scratchF;
50     protected ShortcutWizard wiz;
51     private AntProjectCookie project;
52     private Element JavaDoc target1;
53     protected ShortcutIterator iter;
54     
55     private void mkdir(String JavaDoc path) {
56         new File JavaDoc(scratchF, path.replace('/', File.separatorChar)).mkdirs();
57     }
58     
59     @Override JavaDoc
60     protected void setUp() throws Exception JavaDoc {
61         super.setUp();
62         clearWorkDir();
63         scratchF = getWorkDir();
64         mkdir("system/Menu/&File");
65         mkdir("system/Menu/&Edit");
66         mkdir("system/Menu/&Build");
67         mkdir("system/Menu/&Build/Other");
68         mkdir("system/Menu/Help");
69         mkdir("system/Toolbars/Build");
70         mkdir("system/Toolbars/Help");
71         mkdir("system/Shortcuts");
72         System.setProperty("SYSTEMDIR", new File JavaDoc(scratchF, "system").getAbsolutePath());
73         FileObject scratch = FileUtil.toFileObject(scratchF);
74         assertNotNull("have a scratch dir", scratch);
75         MockServices.setServices(Repo.class);
76         FileObject sfs = Repository.getDefault().getDefaultFileSystem().getRoot();
77         FileObject menuFolder = sfs.getFileObject("Menu");
78         assertNotNull("have Menu", menuFolder);
79         menuFolder.setAttribute("&File/&Edit", Boolean.TRUE);
80         menuFolder.setAttribute("&Edit/&Build", Boolean.TRUE);
81         menuFolder.setAttribute("&Build/Help", Boolean.TRUE);
82         FileObject toolbarsFolder = sfs.getFileObject("Toolbars");
83         assertNotNull("have Toolbars", toolbarsFolder);
84         toolbarsFolder.setAttribute("Build/Help", Boolean.TRUE);
85         assertNotNull("have Shortcuts", sfs.getFileObject("Shortcuts"));
86         FileObject buildXml = scratch.createData("build.xml");
87         FileLock lock = buildXml.lock();
88         OutputStream JavaDoc os = buildXml.getOutputStream(lock);
89         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(os);
90         pw.println("<project name='my proj' default='whatever' basedir='.'>");
91         pw.println(" <target name='targ1'>");
92         pw.println(" <echo>hello #1</echo>");
93         pw.println(" </target>");
94         pw.println(" <target name='targ2'>");
95         pw.println(" <echo>hello #2</echo>");
96         pw.println(" </target>");
97         pw.println("</project>");
98         pw.flush();
99         os.close();
100         lock.releaseLock();
101         project = new AntProjectSupport(buildXml);
102         Document JavaDoc doc = project.getDocument();
103         assertNotNull("parsed " + buildXml, doc);
104         Element JavaDoc docEl = doc.getDocumentElement();
105         NodeList JavaDoc nl = docEl.getElementsByTagName("target");
106         target1 = (Element JavaDoc)nl.item(0);
107         assertEquals("target #1", "targ1", target1.getAttribute("name"));
108         iter = new ShortcutIterator();
109         wiz = new ShortcutWizard(project, target1, iter);
110     }
111     
112     @Override JavaDoc
113     protected boolean runInEQ() {
114         return true;
115     }
116     
117     public static final class Repo extends Repository {
118         
119         public Repo() throws Exception JavaDoc {
120             super(mksystem());
121         }
122         
123         private static FileSystem mksystem() throws Exception JavaDoc {
124             LocalFileSystem lfs = new LocalFileSystem();
125             lfs.setRootDirectory(new File JavaDoc(System.getProperty("SYSTEMDIR")));
126             return lfs;
127         }
128         
129     }
130     
131 }
132
Popular Tags