1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/*
* Jaudiotagger Copyright (C)2004,2005
*
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
* General Public License as published by the Free Software Foundation; either version 2.1 of the License,
* or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this library; if not,
* you can getFields a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.jaudiotagger;
import junit.framework.TestCase;
import org.jaudiotagger.logging.ErrorMessage;
import org.jaudiotagger.tag.TagOptionSingleton;
import java.io.*;
import java.util.EnumMap;
import java.util.regex.Pattern;
/**
*
*/
public abstract class AbstractTestCase extends TestCase {
@Override
public void setUp()
{
TagOptionSingleton.getInstance().setToDefault();
}
/**
* Stores a {@link Pattern} for each {@link ErrorMessage}.<br>
* Place holders like "{<number>}" will be replaced with
* ".*".<br>
*/
private final static EnumMap<ErrorMessage, Pattern> ERROR_PATTERNS;
static {
ERROR_PATTERNS = new EnumMap<ErrorMessage, Pattern>(ErrorMessage.class);
for (ErrorMessage curr : ErrorMessage.values()) {
final String regex = curr.getMsg().replaceAll("\\{\\d+\\}", ".*");
ERROR_PATTERNS.put(curr, Pattern.compile(regex,
Pattern.CASE_INSENSITIVE | Pattern.DOTALL));
}
}
private static boolean append(File fromFile1, File fromFile2, File toFile) {
try {
FileInputStream in = new FileInputStream(fromFile1);
FileInputStream in2 = new FileInputStream(fromFile2);
FileOutputStream out = new FileOutputStream(toFile);
BufferedInputStream inBuffer = new BufferedInputStream(in);
BufferedInputStream inBuffer2 = new BufferedInputStream(in2);
BufferedOutputStream outBuffer = new BufferedOutputStream(out);
int theByte;
while ((theByte = inBuffer.read()) > -1) {
outBuffer.write(theByte);
}
while ((theByte = inBuffer2.read()) > -1) {
outBuffer.write(theByte);
}
outBuffer.close();
inBuffer.close();
inBuffer2.close();
out.close();
in.close();
in2.close();
// cleanupif files are not the same length
if ((fromFile1.length() + fromFile2.length()) != toFile.length()) {
toFile.delete();
return false;
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* Copy a File
*
* @param fromFile
* The existing File
* @param toFile
* The new File
* @return <code>true</code> if and only if the renaming succeeded;
* <code>false</code> otherwise
*/
public static boolean copy(File fromFile, File toFile) {
try {
FileInputStream in = new FileInputStream(fromFile);
FileOutputStream out = new FileOutputStream(toFile);
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > -1) {
out.write(buf, 0, len);
}
in.close();
out.close();
// cleanupif files are not the same length
if (fromFile.length() != toFile.length()) {
toFile.delete();
return false;
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* Copy audiofile to processing dir ready for use in test
*
* @param fileName
* @return
*/
public static File copyAudioToTmp(String fileName) {
File inputFile = new File("testdata", fileName);
File outputFile = new File("testdatatmp", fileName);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
boolean result = copy(inputFile, outputFile);
assertTrue(result);
return outputFile;
}
/**
* Copy audiofile to processing dir ready for use in test, use this if using
* same file in multiple tests because with junit multithreading can have
* problems otherwise
*
* @param fileName
* @return
*/
public static File copyAudioToTmp(String fileName, File newFileName) {
File inputFile = new File("testdata", fileName);
File outputFile = new File("testdatatmp", newFileName.getName());
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
boolean result = copy(inputFile, outputFile);
assertTrue(result);
return outputFile;
}
/**
* Prepends file with tag file in order to create an mp3 with a valid id3
*
* @param tagfile
* @param fileName
* @return
*/
public static File copyAudioToTmp(String tagfile, String fileName) {
File inputTagFile = new File("testtagdata", tagfile);
File inputFile = new File("testdata", fileName);
File outputFile = new File("testdatatmp", fileName);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
boolean result = append(inputTagFile, inputFile, outputFile);
assertTrue(result);
return outputFile;
}
/**
* This method asserts that the given <code>actual</code> message is
* constructed with the <code>expected</code> message string.<br>
* <br>
*
* @param expected
* the expected message source.
* @param actual
* the message to compare against.
*/
public void assertErrorMessage(final ErrorMessage expected,
final String actual) {
assertTrue("Message not correctly constructed.", ERROR_PATTERNS.get(
expected).matcher(actual).matches());
}
}
|