Path: blob/master/test/jdk/java/io/File/Unicode.java
41149 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 501693825* @summary Test file operations with Unicode filenames26* @author Martin Buchholz27*/2829import java.io.*;3031public class Unicode32{33static int fail = 0;34static void fail(String msg) {35fail++;36System.err.println(msg);37}3839static boolean creat(File f) throws Exception {40try {41FileOutputStream out = new FileOutputStream(f);42out.write(new byte[]{'a', 'b', 'c'});43out.close();44// Check that the file we tried to create has the expected name45return find(f);46} catch (Exception e) {47return false;48}49}5051static boolean find(File f) throws Exception {52String fn = f.getPath();53String[] fns = new File(".").list();54for (int i = 0; i < fns.length; i++)55if (fns[i].equals(fn))56return true;57return false;58}5960static void sanityCheck(File f) throws Exception {61if (! f.exists()) fail("! f.exists()");62if ( f.length() != 3) fail(" f.length() != 3");63if ( f.isAbsolute()) fail(" f.isAbsolute()");64if (! f.canRead()) fail("! f.canRead()");65if (! f.canWrite()) fail("! f.canWrite()");66if ( f.isHidden()) fail(" f.isHidden()");67if (! f.isFile()) fail("! f.isFile()");68if ( f.isDirectory()) fail(" f.isDirectory()");69}7071public static void main(String [] args) throws Exception {72final File f1 = new File("\u0411.tst");73final File f2 = new File("\u0412.tst");7475try {76if (! creat(f1))77// Couldn't create file with Unicode filename?78return;7980System.out.println("This system supports Unicode filenames!");81sanityCheck(f1);8283f1.renameTo(f2);84sanityCheck(f2);85if (! f2.delete()) fail("! f2.delete()");86if ( f2.exists()) fail(" f2.exists()");87if ( f1.exists()) fail(" f1.exists()");88if ( f1.delete()) fail(" f1.delete()");8990if (fail != 0) throw new Exception(fail + " failures");91} finally {92f1.delete();93f2.delete();94}95}96}979899