EMMA Coverage Report (generated Tue May 16 15:34:38 CDT 2006)
[all classes][com.mysql.management.util]

COVERAGE SUMMARY FOR SOURCE FILE [Files.java]

nameclass, %method, %block, %line, %
Files.java100% (4/4)100% (22/22)98%  (274/279)100% (51.8/52)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class Files$1100% (1/1)100% (2/2)83%  (25/30)94%  (3.8/4)
inner (): String 100% (1/1)76%  (16/21)92%  (2.8/3)
Files$1 (Files, File): void 100% (1/1)100% (9/9)100% (1/1)
     
class Files100% (1/1)100% (16/16)100% (223/223)100% (44/44)
Files (): void 100% (1/1)100% (10/10)100% (2/2)
Files (Shell$Factory, char, Streams): void 100% (1/1)100% (12/12)100% (5/5)
addExecutableRights (File, PrintStream, PrintStream): void 100% (1/1)100% (40/40)100% (6/6)
asString (File): String 100% (1/1)100% (7/7)100% (1/1)
cleanTestDir (): boolean 100% (1/1)100% (5/5)100% (1/1)
cononical (File): File 100% (1/1)100% (15/15)100% (3/3)
deleteTree (File): boolean 100% (1/1)100% (23/23)100% (5/5)
getPath (File): String 100% (1/1)100% (8/8)100% (1/1)
isWindows (): boolean 100% (1/1)100% (8/8)100% (1/1)
newFile (Object): File 100% (1/1)100% (13/13)100% (3/3)
nullFile (): File 100% (1/1)100% (5/5)100% (1/1)
testDir (): File 100% (1/1)100% (7/7)100% (1/1)
tmp (): File 100% (1/1)100% (8/8)100% (1/1)
tmp (String): File 100% (1/1)100% (22/22)100% (4/4)
validCononicalDir (File): File 100% (1/1)100% (27/27)100% (6/6)
validCononicalDir (File, File): File 100% (1/1)100% (13/13)100% (3/3)
     
class Files$2100% (1/1)100% (2/2)100% (13/13)100% (2/2)
Files$2 (Files, File): void 100% (1/1)100% (9/9)100% (1/1)
inner (): Object 100% (1/1)100% (4/4)100% (1/1)
     
class Files$3100% (1/1)100% (2/2)100% (13/13)100% (2/2)
Files$3 (Files, File): void 100% (1/1)100% (9/9)100% (1/1)
inner (): Object 100% (1/1)100% (4/4)100% (1/1)

1/*
2 Copyright (C) 2004 MySQL AB
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 as 
6 published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 
17 */
18package com.mysql.management.util;
19 
20import java.io.File;
21import java.io.FileInputStream;
22import java.io.IOException;
23import java.io.PrintStream;
24 
25/**
26 * @author Eric Herman <eric@mysql.com>
27 * @version $Id: Files.java,v 1.2 2005/12/01 21:45:31 eherman Exp $
28 */
29public class Files {
30 
31    public static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
32 
33    public static final String USE_TEST_DIR = "c-mxj.files.use-test-dir";
34 
35    private Shell.Factory shellFactory;
36 
37    private char separatorChar;
38 
39    private Streams streams;
40 
41    public Files() {
42        this(new Shell.Factory(), File.separatorChar, new Streams());
43    }
44 
45    Files(Shell.Factory shellFactory, char separatorChar, Streams streams) {
46        this.shellFactory = shellFactory;
47        this.separatorChar = separatorChar;
48        this.streams = streams;
49    }
50 
51    public File testDir() {
52        return new File(tmp(), "test-c.mxj");
53    }
54 
55    public File tmp() {
56        return cononical(new File(System.getProperty(JAVA_IO_TMPDIR)));
57    }
58 
59    public File tmp(String subdir) {
60        String useTestDir = System.getProperty(USE_TEST_DIR);
61        if (Boolean.TRUE.toString().equalsIgnoreCase(useTestDir)) {
62            return new File(testDir(), subdir);
63        }
64        return new File(tmp(), subdir);
65    }
66 
67    /**
68     * Depth First traversal of the directory. Attempts to delete every file in
69     * the structure.
70     * 
71     * @return true if the file passed in is successfully deleted
72     */
73    public boolean deleteTree(File file) {
74        if (file.isDirectory()) {
75            File[] files = file.listFiles();
76            for (int i = 0; i < files.length; i++) {
77                deleteTree(files[i]);
78            }
79        }
80        return file.delete();
81    }
82 
83    /* TODO make this more platform independant */
84    /**
85     * On UNIX systems, in order for a file to be executable, it needs to have
86     * the execute bit set. This method executes a "chmod +x filename"
87     */
88    public void addExecutableRights(File executable, PrintStream out,
89            PrintStream err) {
90        if (isWindows()) {
91            return;
92        }
93        String[] args = { "chmod", "+x", executable.getPath() };
94        String tName = "make " + executable + " runable";
95        shellFactory.newShell(args, tName, out, err).run();
96    }
97 
98    public boolean isWindows() {
99        return separatorChar == '\\';
100    }
101 
102    public String asString(final File file) {
103        return new Exceptions.StringBlock() {
104            public String inner() throws IOException {
105                FileInputStream fis = new FileInputStream(file);
106                try {
107                    return streams.readString(fis);
108                } finally {
109                    fis.close();
110                }
111            }
112        }.exec();
113    }
114 
115    public File nullFile() {
116        return new File("");
117    }
118 
119    public File newFile(Object fileName) {
120        if (fileName == null) {
121            return nullFile();
122        }
123        return cononical(new File(fileName.toString()));
124    }
125 
126    public File cononical(final File file) {
127        if (file == null) {
128            throw new IllegalArgumentException("File may not be null");
129        }
130 
131        return (File) new Exceptions.Block() {
132            protected Object inner() throws Exception {
133                return file.getCanonicalFile();
134            }
135        }.exec();
136    }
137 
138    public String getPath(final File file) {
139        return (String) new Exceptions.Block() {
140            protected Object inner() throws Exception {
141                return file.getCanonicalPath();
142            }
143        }.exec();
144    }
145 
146    public boolean cleanTestDir() {
147        return deleteTree(testDir());
148    }
149 
150    public File validCononicalDir(File dir, File defaultDir) {
151        if (dir == null || dir.equals(nullFile())) {
152            dir = defaultDir;
153        }
154        return validCononicalDir(dir);
155    }
156 
157    public File validCononicalDir(final File dir) {
158        File cononical = cononical(dir);
159 
160        if (!cononical.exists()) {
161            cononical.mkdirs();
162        }
163        if (!cononical.isDirectory()) {
164            throw new IllegalArgumentException(cononical + " not a directory");
165        }
166        return cononical;
167    }
168}

[all classes][com.mysql.management.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov