|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AntTestSetGenUtil.java | 75% | 91,4% | 100% | 89,1% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Copyright (C) 2003, Marco Jansen / Contrado Technologies (http://www.contrado.nl)
|
|
| 3 |
*
|
|
| 4 |
* This program is free software; you can redistribute it and/or
|
|
| 5 |
* modify it under the terms of the GNU General Public License as
|
|
| 6 |
* published by the Free Software Foundation; either version 2 of the
|
|
| 7 |
* License, or (at your option) any later version.
|
|
| 8 |
*
|
|
| 9 |
* This program is distributed in the hope that it will be useful, but
|
|
| 10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
| 12 |
* General Public License for more details.
|
|
| 13 |
*/
|
|
| 14 |
|
|
| 15 |
package net.sourceforge.anttestsetgen;
|
|
| 16 |
|
|
| 17 |
import org.apache.tools.ant.BuildException;
|
|
| 18 |
|
|
| 19 |
import java.io.BufferedReader;
|
|
| 20 |
import java.io.File;
|
|
| 21 |
import java.io.FileNotFoundException;
|
|
| 22 |
import java.io.FileReader;
|
|
| 23 |
import java.io.IOException;
|
|
| 24 |
import java.net.URL;
|
|
| 25 |
|
|
| 26 |
/**
|
|
| 27 |
* The AntTestSetGenUtil class contains a number of simple utility functions.
|
|
| 28 |
*
|
|
| 29 |
* @author <a href="mailto:m.c.jansen.spam at planet.nl">Marco Jansen (Contrado Technologies)</a>
|
|
| 30 |
* @version $Id: AntTestSetGenUtil.java,v 1.1 2004/01/04 15:00:18 mcjansen Exp $
|
|
| 31 |
*/
|
|
| 32 |
|
|
| 33 |
public class AntTestSetGenUtil { |
|
| 34 |
|
|
| 35 |
/**
|
|
| 36 |
* Read a file from disk and place the content in a String.
|
|
| 37 |
*
|
|
| 38 |
* @param fileName The name of the file (should be in the classpath)
|
|
| 39 |
* @return a String with the content of the file.
|
|
| 40 |
* @throws RuntimeException In case there is an error loading the file.
|
|
| 41 |
*/
|
|
| 42 | 2 |
public static String readFile(String fileName) { |
| 43 | 2 |
URL url = Thread.currentThread().getContextClassLoader().getResource(fileName); |
| 44 | 2 |
if (url == null) { |
| 45 | 0 |
throw new RuntimeException("File '" + fileName + "' not found in classpath."); |
| 46 |
} |
|
| 47 |
|
|
| 48 | 2 |
BufferedReader in = null;
|
| 49 | 2 |
StringBuffer message = new StringBuffer();
|
| 50 | 2 |
try {
|
| 51 | 2 |
in = new BufferedReader(new FileReader(url.getFile())); |
| 52 | 2 |
String line = null;
|
| 53 | ? |
while ((line = in.readLine()) != null) { |
| 54 | 28 |
message.append(line); |
| 55 |
} |
|
| 56 | 2 |
in.close(); |
| 57 |
} catch (FileNotFoundException e) {
|
|
| 58 | 0 |
throw new RuntimeException("File '" + url.getFile() + "' not found."); |
| 59 |
} catch (IOException e) {
|
|
| 60 | 0 |
throw new RuntimeException("Error reading file '" + url.getFile() + "'."); |
| 61 |
} |
|
| 62 | 2 |
return message.toString();
|
| 63 |
} |
|
| 64 |
|
|
| 65 |
/**
|
|
| 66 |
* Create one or more directories.
|
|
| 67 |
*
|
|
| 68 |
* Either '/' or '\' should be used as a path seperator.<BR>
|
|
| 69 |
* <BR>
|
|
| 70 |
* Example: foo/test/bar
|
|
| 71 |
*
|
|
| 72 |
* @param path The name of the path to be created
|
|
| 73 |
* @return true in case of success
|
|
| 74 |
*/
|
|
| 75 | 16 |
public static boolean mkdirs(String path) { |
| 76 | 16 |
File dir = new File(path);
|
| 77 | 16 |
return dir.mkdirs();
|
| 78 |
} |
|
| 79 |
|
|
| 80 |
/**
|
|
| 81 |
* Remove one or more directories
|
|
| 82 |
*
|
|
| 83 |
* Either '/' or '\' should be used as a path seperator.<BR>
|
|
| 84 |
* <BR>
|
|
| 85 |
* Example: foo/test/bar
|
|
| 86 |
*
|
|
| 87 |
* @param path The name of the path to be created
|
|
| 88 |
* @return true in case of success
|
|
| 89 |
*/
|
|
| 90 | 4 |
public static boolean removeDirectory(String path) { |
| 91 | 4 |
File dir = new File(path);
|
| 92 | 4 |
return dir.delete();
|
| 93 |
} |
|
| 94 |
|
|
| 95 |
/**
|
|
| 96 |
* Check if a path exists
|
|
| 97 |
*
|
|
| 98 |
* Either '/' or '\' should be used as a path seperator.<BR>
|
|
| 99 |
* <BR>
|
|
| 100 |
* Example: foo/test/bar
|
|
| 101 |
*
|
|
| 102 |
* @param path The name of the path to be created
|
|
| 103 |
* @return true in case of success
|
|
| 104 |
*/
|
|
| 105 | 6 |
public static boolean directoryExists(String path) { |
| 106 | 6 |
File dir = new File(path);
|
| 107 | 6 |
return dir.isDirectory() && dir.exists();
|
| 108 |
} |
|
| 109 |
|
|
| 110 |
/**
|
|
| 111 |
* Convert a Java package name to a full path name.
|
|
| 112 |
*
|
|
| 113 |
* The full path is the destination + the transformed package name.<BR>
|
|
| 114 |
* <BR>
|
|
| 115 |
* Example: destination = src/java, packageName=net.sourceforge.anttestsetgen<BR>
|
|
| 116 |
* The result will then be src/java/net/sourceforge/anttestsetgen
|
|
| 117 |
*
|
|
| 118 |
* @param destination The orginating destination
|
|
| 119 |
* @param packageName The Java package name
|
|
| 120 |
* @return the full path
|
|
| 121 |
*/
|
|
| 122 | 20 |
public static String convertPackageToPath(String destination, String packageName) { |
| 123 | 20 |
String directory = "";
|
| 124 | 20 |
if (packageName != null && !packageName.equals("")) { |
| 125 | 18 |
directory = packageName.replace('.', '/');
|
| 126 |
} |
|
| 127 |
|
|
| 128 | 20 |
if (destination != null && !destination.equals("")) { |
| 129 | 16 |
if (destination.charAt(destination.length() - 1) == '/') {
|
| 130 | 2 |
return destination + directory;
|
| 131 |
} else {
|
|
| 132 | 14 |
return destination + "/" + directory; |
| 133 |
} |
|
| 134 |
} else {
|
|
| 135 | 4 |
return directory;
|
| 136 |
} |
|
| 137 |
} |
|
| 138 |
|
|
| 139 |
/**
|
|
| 140 |
* Remove a file from the file system.
|
|
| 141 |
*
|
|
| 142 |
* No status info is given about the success (or failure) of the operation.
|
|
| 143 |
*
|
|
| 144 |
* @param fileName The name of the file to be removed.
|
|
| 145 |
*/
|
|
| 146 | 6 |
public static void removeFile(String fileName) { |
| 147 | 6 |
File file = new File(fileName);
|
| 148 | 6 |
file.delete(); |
| 149 |
} |
|
| 150 |
|
|
| 151 |
/**
|
|
| 152 |
* Check if a file exists in the file system.
|
|
| 153 |
*
|
|
| 154 |
* @param fileName The name of the file to be removed.
|
|
| 155 |
* @return True in case the file exists.
|
|
| 156 |
*/
|
|
| 157 | 12 |
public static boolean fileExists(String fileName) { |
| 158 | 12 |
File file = new File(fileName);
|
| 159 | 12 |
return file.exists();
|
| 160 |
} |
|
| 161 |
|
|
| 162 |
/**
|
|
| 163 |
* Check if a required property is set.
|
|
| 164 |
*
|
|
| 165 |
* @param name The name of the required property.
|
|
| 166 |
* @param value The current value of the required property
|
|
| 167 |
* @return The current value of the required property
|
|
| 168 |
* @throws org.apache.tools.ant.BuildException
|
|
| 169 |
* In case the value is null
|
|
| 170 |
*/
|
|
| 171 | 1884 |
public static String requiredCheck(String name, String value) { |
| 172 | 1884 |
if (value != null) { |
| 173 | 1864 |
return value;
|
| 174 |
} else {
|
|
| 175 | 20 |
throw new BuildException("'" + name + "' attribute must be set!"); |
| 176 |
} |
|
| 177 |
} |
|
| 178 |
|
|
| 179 |
|
|
| 180 |
} |
|
| 181 |
|
|
||||||||||