|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.openide.filesystems; import org.openide.filesystems.*; import java.io.*; import java.io.File; import java.net.*; import java.util.*; /** * * @author rm111737 */ public class TestUtilHid { public static final File locationOfTempFolder (String name) throws IOException { String property = System.getProperty("workdir"); File workdir = (property == null) ? null: new File (property); File tmpdir = (workdir != null) ? workdir : new File(System.getProperty("java.io.tmpdir"), "fstests"); tmpdir.mkdirs(); if (!tmpdir.isDirectory()) throw new IOException("Could not make: " + tmpdir); File tmp = File.createTempFile(name,null, tmpdir); tmp.delete(); tmp = new File(tmp.getParent(),name); return tmp; } /** @return URL to folder where should be placed tested data */ public final static URL getResourceContext () { //System.out.println("getResourceContext: " + FileSystemFactoryHid.class.getResource("../data")); return FileSystemFactoryHid.class.getResource("../data/"); } /** It may be helpful to delete resursively Files */ public final static boolean deleteFolder (File file) throws IOException{ boolean ret = file.delete(); if (ret) { return true; } if (! file.exists()) { return false; } if (file.isDirectory()) { // first of all delete whole content File[] arr = file.listFiles(); for (int i = 0; i < arr.length; i++) { if (deleteFolder (arr[i]) != true) { throw new IOException ("Cannot delete: "+ arr[i]); //return false; } } } return (file.delete() ? true : false); } public final static FileSystem createLocalFileSystem(String testName, String[] resources) throws IOException { File mountPoint = locationOfLFSTempFolder(testName); return createLocalFileSystem(mountPoint, resources); } public static FileSystem createLocalFileSystem(File mountPoint, String[] resources) throws IOException { mountPoint.mkdir(); for (int i = 0; i < resources.length; i++) { File f = new File (mountPoint,resources[i]); if (f.isDirectory() || resources[i].endsWith("/")) { f.mkdirs(); } else { f.getParentFile().mkdirs(); try { f.createNewFile(); } catch (IOException iex) { throw new IOException ("While creating " + resources[i] + " in " + mountPoint.getAbsolutePath() + ": " + iex.toString() + ": " + f.getAbsolutePath() + " with resource list: " + Arrays.asList(resources)); } } } LocalFileSystem lfs = new StatusFileSystem(); try { lfs.setRootDirectory(mountPoint); } catch (Exception ex) {} return lfs; } public static File locationOfLFSTempFolder(String testName) throws IOException { File mountPoint = TestUtilHid.locationOfTempFolder ("lfstest"+testName); return mountPoint; } public final static void destroyLocalFileSystem (String testName) throws IOException { File mountPoint = TestUtilHid.locationOfTempFolder ("lfstest"+testName); if (mountPoint.exists()) { if (TestUtilHid.deleteFolder(mountPoint) == false) throw new IOException("Cannot delete test folder: " + mountPoint.toString()); } } public final static void destroyXMLFileSystem(String testName) throws IOException { File tempFile = TestUtilHid.locationOfTempFolder ("xfstest"+testName); File xmlFile = new File (tempFile,"xfstest.xml"); if (xmlFile.exists()) xmlFile.delete(); } public final static FileSystem createXMLFileSystem(String testName, String[] resources) throws IOException{ File xmlFile = createXMLLayer(testName, resources); XMLFileSystem xfs = new XMLFileSystem (); try { xfs.setXmlUrl(xmlFile.toURI().toURL()); } catch (Exception ex) {} return xfs; } public static File createXMLLayer(String testName, String[] resources) throws IOException { File tempFile = TestUtilHid.locationOfTempFolder ("xfstest"+testName); tempFile.mkdir(); File xmlFile = new File (tempFile,"xfstest.xml"); if (!xmlFile.exists()) { xmlFile.getParentFile().mkdirs(); xmlFile.createNewFile(); } FileOutputStream xos = new FileOutputStream (xmlFile); ResourceElement root = new ResourceElement (""); for (int i = 0; i < resources.length; i++) root.add (resources[i]); PrintWriter pw = new PrintWriter (xos); pw.println(" |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.