⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileutilstest.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *  Licensed to the Apache Software Foundation (ASF) under one or more *  contributor license agreements.  See the NOTICE file distributed with *  this work for additional information regarding copyright ownership. *  The ASF licenses this file to You under the Apache License, Version 2.0 *  (the "License"); you may not use this file except in compliance with *  the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * *  Unless required by applicable law or agreed to in writing, software *  distributed under the License is distributed on an "AS IS" BASIS, *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *  See the License for the specific language governing permissions and *  limitations under the License. * */package org.apache.tools.ant.util;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import junit.framework.TestCase;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.taskdefs.condition.Os;/** * Tests for org.apache.tools.ant.util.FileUtils. * */public class FileUtilsTest extends TestCase {    private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();    private File removeThis;    private String root;    public FileUtilsTest(String name) {        super(name);    }    public void setUp() {        // Windows adds the drive letter in uppercase, unless you run Cygwin        root = new File(File.separator).getAbsolutePath().toUpperCase();    }    public void tearDown() {        if (removeThis != null && removeThis.exists()) {        	if (!removeThis.delete()) {                removeThis.deleteOnExit();            }        }    }    /**     * test modification.     * Since Ant1.7, the method being tested no longer uses     * reflection to provide backwards support to Java1.1, so this     * test is not so critical. But it does explore file system     * behaviour and will help catch any regression in Java itself,     * so is worth retaining.     * @see FileUtils#setFileLastModified(java.io.File, long)     * @throws IOException     */    public void testSetLastModified() throws IOException {        removeThis = new File("dummy");        FileOutputStream fos = new FileOutputStream(removeThis);        fos.write(new byte[0]);        fos.close();        long modTime = removeThis.lastModified();        assertTrue(modTime != 0);        /*         * Sleep for some time to make sure a touched file would get a         * more recent timestamp according to the file system's         * granularity (should be > 2s to account for Windows FAT).         */        try {            Thread.sleep(5000);        } catch (InterruptedException ie) {            fail(ie.getMessage());        }        FILE_UTILS.setFileLastModified(removeThis, -1);        long secondModTime = removeThis.lastModified();        assertTrue(secondModTime > modTime);        // number of milliseconds in a day        final int millisperday=24 * 3600 * 1000;        // in a previous version, the date of the file was set to 123456        // milliseconds since 01.01.1970        // it did not work on a computer running JDK 1.4.1_02 + Windows 2000        FILE_UTILS.setFileLastModified(removeThis, secondModTime + millisperday);        long thirdModTime = removeThis.lastModified();        /*         * I would love to compare this with 123456, but depending on         * the filesystems granularity it can take an arbitrary value.         *         * Just assert the time has changed.         */        assertTrue(thirdModTime != secondModTime);    }    public void testResolveFile() {        if (!(Os.isFamily("dos") || Os.isFamily("netware"))) {            /*             * Start with simple absolute file names.             */            assertEquals(File.separator,                         FILE_UTILS.resolveFile(null, "/").getPath());            assertEquals(File.separator,                         FILE_UTILS.resolveFile(null, "\\").getPath());        } else {            assertEqualsIgnoreDriveCase(localize(File.separator),                FILE_UTILS.resolveFile(null, "/").getPath());            assertEqualsIgnoreDriveCase(localize(File.separator),                FILE_UTILS.resolveFile(null, "\\").getPath());            /*             * throw in drive letters             */            String driveSpec = "C:";            assertEquals(driveSpec + "\\",                         FILE_UTILS.resolveFile(null, driveSpec + "/").getPath());            assertEquals(driveSpec + "\\",                         FILE_UTILS.resolveFile(null, driveSpec + "\\").getPath());            String driveSpecLower = "c:";            assertEquals(driveSpecLower + "\\",                         FILE_UTILS.resolveFile(null, driveSpecLower + "/").getPath());            assertEquals(driveSpecLower + "\\",                         FILE_UTILS.resolveFile(null, driveSpecLower + "\\").getPath());            /*             * promised to eliminate consecutive slashes after drive letter.             */            assertEquals(driveSpec + "\\",                         FILE_UTILS.resolveFile(null, driveSpec + "/////").getPath());            assertEquals(driveSpec + "\\",                         FILE_UTILS.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath());        }        if (Os.isFamily("netware")) {            /*             * throw in NetWare volume names             */            String driveSpec = "SYS:";            assertEquals(driveSpec,                         FILE_UTILS.resolveFile(null, driveSpec + "/").getPath());            assertEquals(driveSpec,                         FILE_UTILS.resolveFile(null, driveSpec + "\\").getPath());            String driveSpecLower = "sys:";            assertEquals(driveSpec,                         FILE_UTILS.resolveFile(null, driveSpecLower + "/").getPath());            assertEquals(driveSpec,                         FILE_UTILS.resolveFile(null, driveSpecLower + "\\").getPath());            /*             * promised to eliminate consecutive slashes after drive letter.             */            assertEquals(driveSpec,                         FILE_UTILS.resolveFile(null, driveSpec + "/////").getPath());            assertEquals(driveSpec,                         FILE_UTILS.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath());        } else if (!(Os.isFamily("dos"))) {            /*             * drive letters must be considered just normal filenames.             */            String driveSpec = "C:";            String udir = System.getProperty("user.dir");            assertEquals(udir + File.separator + driveSpec,                         FILE_UTILS.resolveFile(null, driveSpec + "/").getPath());            assertEquals(udir + File.separator + driveSpec,                         FILE_UTILS.resolveFile(null, driveSpec + "\\").getPath());            String driveSpecLower = "c:";            assertEquals(udir + File.separator + driveSpecLower,                         FILE_UTILS.resolveFile(null, driveSpecLower + "/").getPath());            assertEquals(udir + File.separator + driveSpecLower,                         FILE_UTILS.resolveFile(null, driveSpecLower + "\\").getPath());        }        /*         * Now test some relative file name magic.         */        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "4").getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "./4").getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.resolveFile(new File(localize("/1/2/3")), ".\\4").getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "./.\\4").getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "../3/4").getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "..\\3\\4").getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "../../5/.././2/./3/6/../4").getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "..\\../5/..\\./2/./3/6\\../4").getPath());        assertEquals("meaningless result but no exception",                new File(localize("/1/../../b")),                FILE_UTILS.resolveFile(new File(localize("/1")), "../../b"));    }    public void testNormalize() {        if (!(Os.isFamily("dos") || Os.isFamily("netware"))) {            /*             * Start with simple absolute file names.             */            assertEquals(File.separator,                         FILE_UTILS.normalize("/").getPath());            assertEquals(File.separator,                         FILE_UTILS.normalize("\\").getPath());        } else {            try {                 FILE_UTILS.normalize("/").getPath();                 fail("normalized \"/\" on dos or netware");            } catch (Exception e) {            }            try {                 FILE_UTILS.normalize("\\").getPath();                 fail("normalized \"\\\" on dos or netware");            } catch (Exception e) {            }        }        if (Os.isFamily("dos")) {            /*             * throw in drive letters             */            String driveSpec = "C:";            try {                 FILE_UTILS.normalize(driveSpec).getPath();                 fail(driveSpec + " is not an absolute path");            } catch (Exception e) {            }            assertEquals(driveSpec + "\\",                         FILE_UTILS.normalize(driveSpec + "/").getPath());            assertEquals(driveSpec + "\\",                         FILE_UTILS.normalize(driveSpec + "\\").getPath());            String driveSpecLower = "c:";            assertEquals(driveSpecLower + "\\",                         FILE_UTILS.normalize(driveSpecLower + "/").getPath());            assertEquals(driveSpecLower + "\\",                         FILE_UTILS.normalize(driveSpecLower + "\\").getPath());            /*             * promised to eliminate consecutive slashes after drive letter.             */            assertEquals(driveSpec + "\\",                         FILE_UTILS.normalize(driveSpec + "/////").getPath());            assertEquals(driveSpec + "\\",                         FILE_UTILS.normalize(driveSpec + "\\\\\\\\\\\\").getPath());        } else if (Os.isFamily("netware")) {            /*             * throw in NetWare volume names             */            String driveSpec = "SYS:";            assertEquals(driveSpec,                         FILE_UTILS.normalize(driveSpec).getPath());            assertEquals(driveSpec,                         FILE_UTILS.normalize(driveSpec + "/").getPath());            assertEquals(driveSpec,                         FILE_UTILS.normalize(driveSpec + "\\").getPath());            String driveSpecLower = "sys:";            assertEquals(driveSpec,                         FILE_UTILS.normalize(driveSpecLower).getPath());            assertEquals(driveSpec,                         FILE_UTILS.normalize(driveSpecLower + "/").getPath());            assertEquals(driveSpec,                         FILE_UTILS.normalize(driveSpecLower + "\\").getPath());            assertEquals(driveSpec + "\\junk",                         FILE_UTILS.normalize(driveSpecLower + "\\junk").getPath());            /*             * promised to eliminate consecutive slashes after drive letter.             */            assertEquals(driveSpec,                         FILE_UTILS.normalize(driveSpec + "/////").getPath());            assertEquals(driveSpec,                         FILE_UTILS.normalize(driveSpec + "\\\\\\\\\\\\").getPath());        } else {            try {                String driveSpec = "C:";                assertEquals(driveSpec,                             FILE_UTILS.normalize(driveSpec).getPath());                fail("Expected failure, C: isn't an absolute path on other os's");            } catch (BuildException e) {                // Passed test             }        }        /*         * Now test some relative file name magic.         */        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.normalize(localize("/1/2/3/4")).getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.normalize(localize("/1/2/3/./4")).getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.normalize(localize("/1/2/3/.\\4")).getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.normalize(localize("/1/2/3/./.\\4")).getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.normalize(localize("/1/2/3/../3/4")).getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.normalize(localize("/1/2/3/..\\3\\4")).getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.normalize(localize("/1/2/3/../../5/.././2/./3/6/../4")).getPath());        assertEquals(localize("/1/2/3/4"),                     FILE_UTILS.normalize(localize("/1/2/3/..\\../5/..\\./2/./3/6\\../4")).getPath());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -