001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.upgrade;
043:
044: import java.io.BufferedReader;
045: import java.io.File;
046: import java.io.FileOutputStream;
047: import java.io.FileReader;
048: import java.io.IOException;
049: import java.net.URL;
050: import java.util.*;
051: import java.util.regex.Matcher;
052: import java.util.regex.Pattern;
053:
054: import org.openide.filesystems.FileObject;
055:
056: import org.openide.filesystems.FileSystem;
057: import org.openide.filesystems.FileUtil;
058: import org.openide.filesystems.LocalFileSystem;
059: import org.openide.filesystems.MultiFileSystem;
060: import org.openide.filesystems.XMLFileSystem;
061:
062: /** Tests to check that copy of files works.
063: *
064: * @author Jaroslav Tulach
065: */
066: public final class CopyTest extends org.netbeans.junit.NbTestCase {
067: public CopyTest(String name) {
068: super (name);
069: }
070:
071: protected void setUp() throws java.lang.Exception {
072: super .setUp();
073:
074: clearWorkDir();
075: }
076:
077: public void testAppendSelectedLines() throws Exception {
078: //setup
079: List expectedLines = new ArrayList();
080: File wDir = getWorkDir();
081: File sFile = new File(wDir, this .getName() + ".file");
082: assertTrue(sFile.createNewFile());
083: File tFolder = new File(wDir, this .getName());
084: assertTrue(tFolder.mkdir());
085: File tFile = new File(tFolder, this .getName() + ".file");
086: assertTrue(tFile.createNewFile());
087: FileOutputStream fos = new FileOutputStream(tFile);
088: try {
089: String line = "nbplatform.default.harness.dir=${nbplatform.default.netbeans.dest.dir}/harness \n";
090: fos.write(line.getBytes());
091: expectedLines.add(line);
092: } finally {
093: fos.close();
094: }
095:
096: fos = new FileOutputStream(sFile);
097: try {
098: String line = "nbplatform.id.netbeans.dest.dir=/work/nball/nbbuild/netbeans \n";
099: fos.write(line.getBytes());
100: expectedLines.add(line);
101:
102: line = "nbplatform.id.netbeans.dest.dir=/work/nbide/netbeans \n";
103: fos.write(line.getBytes());
104: expectedLines.add(line);
105:
106: line = "nbplatform.default.netbeans.dest.dir=/work/nbide/netbeans \n";
107: fos.write(line.getBytes());
108: //lines.add(line); -- should be excluded
109: } finally {
110: fos.close();
111: }
112: String[] regexForSelection = new String[] { "^nbplatform[.](?![dD]efault).+[.](netbeans[.]dest[.]dir|label|harness[.]dir)=.+$"//NOI18N
113: };
114:
115: Copy.appendSelectedLines(sFile, tFolder, regexForSelection);
116: String line = null;
117: List resultLines = new ArrayList();
118: BufferedReader reader = new BufferedReader(
119: new FileReader(tFile));
120: try {
121: while ((line = reader.readLine()) != null) {
122: resultLines.add(line + "\n");
123: }
124: } finally {
125: reader.close();
126: }
127: assertEquals(expectedLines, resultLines);
128: }
129:
130: public void testCopy() throws Exception {
131: copyTest("path/X.txt");
132: }
133:
134: public void testDoesDeepCopy() throws Exception {
135: copyTest("path/dir/subdir/deepDir/X.txt");
136: }
137:
138: public void testCopyAttributes() throws Exception {
139: copyTest(true, "path/X.txt");
140: }
141:
142: public void testCopyFolderAttributes() throws Exception {
143: copyTest(true, new String[] { "path/folder/",
144: "path/folder/f.txt" });
145: }
146:
147: public void testDoNotCopyEmptyDirs() throws Exception {
148: copyTest("path/emptyDir/");
149: }
150:
151: public void testDoNotCopyEmptyDirs2() throws Exception {
152: copyTest(new String[] { "path/emptyDir/",
153: "path/emptyDir/emptyDir2/" });
154: }
155:
156: public void testDoesCopyHiddenFiles() throws Exception {
157: String[] res = { "root/Yes.txt", "root/X.txt_hidden", };
158: LocalFileSystem fs = createLocalFileSystem(res);
159: URL url = getClass().getResource("layer5.5.xml");
160: assertNotNull("found sample layer", url);
161: XMLFileSystem xfs = new XMLFileSystem(url);
162:
163: MultiFileSystem mfs = AutoUpgrade.createLayeredSystem(fs, xfs);
164:
165: FileObject fo = mfs.findResource("root");
166:
167: FileSystem original = FileUtil.createMemoryFileSystem();
168:
169: MultiFileSystem tgfs = new MultiFileSystem(new FileSystem[] {
170: fs, original });
171: FileObject tg = tgfs.getRoot().createFolder("target");
172: FileObject toBeHidden = FileUtil.createData(original.getRoot(),
173: "target/X.txt");
174:
175: assertEquals("One file is there", 1, tg.getChildren().length);
176: assertEquals("X.txt", tg.getChildren()[0].getNameExt());
177:
178: HashSet set = new HashSet();
179: set.add("root/Yes.txt");
180: set.add("root/X.txt_hidden");
181: Copy.copyDeep(fo, tg, set);
182:
183: assertEquals("After the copy there is still one file", 1, tg
184: .getFileObject("root").getChildren().length);
185: assertEquals(
186: "but the file is Yes.txt, as X.txt is hidden by txt_hidden",
187: "Yes.txt", tg.getFileObject("root").getChildren()[0]
188: .getNameExt());
189: }
190:
191: private static void writeTo(FileSystem fs, String res,
192: String content) throws java.io.IOException {
193: FileObject fo = org.openide.filesystems.FileUtil.createData(fs
194: .getRoot(), res);
195: org.openide.filesystems.FileLock lock = fo.lock();
196: java.io.OutputStream os = fo.getOutputStream(lock);
197: os.write(content.getBytes());
198: os.close();
199: lock.releaseLock();
200: }
201:
202: public LocalFileSystem createLocalFileSystem(String[] resources)
203: throws IOException {
204: File mountPoint = new File(getWorkDir(), "tmpfs");
205: mountPoint.mkdir();
206:
207: for (int i = 0; i < resources.length; i++) {
208: File f = new File(mountPoint, resources[i]);
209: if (f.isDirectory() || resources[i].endsWith("/")) {
210: FileUtil.createFolder(f);
211: } else {
212: FileUtil.createData(f);
213: }
214: }
215:
216: LocalFileSystem lfs = new LocalFileSystem();
217: try {
218: lfs.setRootDirectory(mountPoint);
219: } catch (Exception ex) {
220: }
221:
222: return lfs;
223: }
224:
225: private void copyTest(String... pathXtxt) throws IOException {
226: copyTest(false, pathXtxt);
227: }
228:
229: private void copyTest(boolean testAttribs, String... allPath)
230: throws IOException {
231: String atribName = "attribName";
232: String testPath = allPath[0];
233: ArrayList<String> fileList = new ArrayList<String>();
234: fileList.addAll(Arrays.asList(allPath));
235: fileList.addAll(Arrays.asList(new java.lang.String[] {
236: "path/Yes.txt", "path/No.txt", "path/Existing.txt" }));
237:
238: FileSystem fs = createLocalFileSystem(fileList
239: .toArray(new String[fileList.size()]));
240:
241: FileObject path = fs.findResource("path");
242: assertNotNull(path);
243: FileObject tg = fs.getRoot().createFolder("target");
244: assertNotNull(tg);
245: FileObject existing = FileUtil.createData(tg,
246: "path/Existing.txt");
247: assertNotNull(existing);
248: writeTo(fs, "target/path/Existing.txt", "existing-content");
249:
250: FileObject toCopyOne = fs.findResource(testPath);
251: boolean isFolder = toCopyOne.isFolder();
252: boolean isEmptyFolder = isFolder
253: && !toCopyOne.getData(true).hasMoreElements();
254: assertNotNull(toCopyOne);
255: if (testAttribs) {
256: toCopyOne.setAttribute(atribName, atribName);
257: }
258:
259: HashSet set = new HashSet();
260: for (String currentPath : allPath) {
261: currentPath = currentPath.endsWith("/") ? currentPath
262: .substring(0, currentPath.length() - 1)
263: : currentPath;
264: set.add(currentPath);
265: }
266:
267: set.add("path/Yes.txt");
268: set.add("path/Existing.txt");
269: org.netbeans.upgrade.Copy.copyDeep(path, tg, set);
270:
271: assertNotNull("file not copied: " + "path/Existing.txt", tg
272: .getFileObject("path/Existing.txt"));
273: assertNotNull("file not copied: " + "path/Yes.txt", tg
274: .getFileObject("path/Yes.txt"));
275: assertNull("file not copied: " + "path/No.txt", tg
276: .getFileObject("path/No.txt"));
277: org.openide.filesystems.FileObject copiedOne = tg
278: .getFileObject(testPath);
279:
280: FileObject copiedPath = tg.getFileObject("path");
281: assertNotNull("file copied: " + "path", copiedPath);
282: assertEquals("file copied: " + testPath, isEmptyFolder,
283: copiedOne == null);
284: if (!isEmptyFolder) {
285: String expected = testPath.endsWith("/") ? testPath
286: .substring(0, testPath.length() - 1) : testPath;
287: assertEquals("file copied: " + testPath, expected,
288: org.openide.filesystems.FileUtil.getRelativePath(
289: tg, copiedOne));
290: if (testAttribs) {
291: assertEquals("attribute copied", atribName, copiedOne
292: .getAttribute(atribName));
293: }
294: }
295:
296: byte[] arr = new byte[300];
297: int len = existing.getInputStream().read(arr);
298: String content = new String(arr, 0, len);
299:
300: //testDoNotOverwriteFiles
301: assertEquals("The content is kept from project", content,
302: "existing-content");
303:
304: }
305: }
|