Tr3x
Lieutenant
- Registriert
- Feb. 2007
- Beiträge
- 640
Hallo,
ich versuche gerade einen MediaPlayer zu basteln. Bisher lief alles recht gut. Nur jetzt häng ich einfach an den einfachen JUnitTests.
Und zwar habe ich ein AudioFile.java das eine methode parsePathname und parseFilename hat. In der ersten werden alle Slashes für das jeweilige OS richtig gestellt, und die doppelten entfernt. Bei meinen eigenen UnitTests gibts da noch kein Problem. In der zweiten Methode werden führende und endende Leerzeichen entfernt und Interpret und Titel jeweils getrennt gespeichert.
Das eigentliche Problem ist das bei dem UnitTest in Zeile 249 gemeckert wird mit
java.lang.StringIndexOutofBoundsException: String index out of range: -1
Ich versuch die ganze Zeit alles auszutesten und rumzubasteln aber ich kann den Fehler einfach nicht finden. Ich hoffe es kann mir da jemand behilflich sein.
VIELEN DANK
Am besten ich füg hier einmal den AudioFile.java code hoch
und das hier ist der zubestehende JUnitTest code
ich versuche gerade einen MediaPlayer zu basteln. Bisher lief alles recht gut. Nur jetzt häng ich einfach an den einfachen JUnitTests.
Und zwar habe ich ein AudioFile.java das eine methode parsePathname und parseFilename hat. In der ersten werden alle Slashes für das jeweilige OS richtig gestellt, und die doppelten entfernt. Bei meinen eigenen UnitTests gibts da noch kein Problem. In der zweiten Methode werden führende und endende Leerzeichen entfernt und Interpret und Titel jeweils getrennt gespeichert.
Das eigentliche Problem ist das bei dem UnitTest in Zeile 249 gemeckert wird mit
java.lang.StringIndexOutofBoundsException: String index out of range: -1
Ich versuch die ganze Zeit alles auszutesten und rumzubasteln aber ich kann den Fehler einfach nicht finden. Ich hoffe es kann mir da jemand behilflich sein.
VIELEN DANK
Am besten ich füg hier einmal den AudioFile.java code hoch
Code:
public class AudioFile {
private String pathname, filename, author, title;
public AudioFile(String pathname) {
parsePathname(pathname);
parseFilename(getFilename());
}
public AudioFile() {
}
public void parsePathname(String pname) {
pathname = pname;
filename = "";
int slash, mem = 0;
char os = java.io.File.separatorChar;
String pname2 = "";
//slashes drehen
if(os == '\\'){
//windows
pname = pname.replace('/', '\\');
}
else {
//unix
pname = pname.replace('\\', '/');
}
//doppeltenslashes entfernen
if(os == '\\') {
//windows
for(int i=0; i<pname.length();i++) {
if(pname.charAt(i) == '\\') {
if(mem == 0) {
pname2 += pname.charAt(i);
mem = 1;
}
else { mem = 1;
}
}
else {
pname2 += pname.charAt(i);
mem = 0;
}
}
if(pname2.contains("\\")) {
slash = pname2.lastIndexOf('\\');
slash +=1;
this.filename = pname2.substring(pname2.lastIndexOf(java.io.File.separator)+1);
}
else {
this.filename = pname2;
}
}
else {
//unix
for(int i=0; i<pname.length();i++) {
if(pname.charAt(i) == '/') {
if(mem == 0) {
pname2 += pname.charAt(i);
mem = 1;
}
else { mem = 1;
}
}
else {
pname2 += pname.charAt(i);
mem = 0;
}
}
if(pname2.contains("/")) {
slash = pname2.lastIndexOf('/');
slash +=1;
this.filename = pname2.substring(pname2.lastIndexOf(java.io.File.separator)+1);
}
else {
this.filename = pname2;
}
}
System.out.println(filename);
this.pathname = pname2;
return;
}
public String getPathname() {
return pathname;
}
public String getFilename() {
return filename;
}
public void parseFilename(String filename) {
int i = filename.length();
if(i>0) {
if(filename.contains(" - ")){
String[] split = filename.split(" - ");
author = split[0];
author = author.trim();
title = split[1];
title = split[1].substring(0, split[1].lastIndexOf("."));
title = title.trim();
}
else {
this.author = "";
if(filename.contains(" - ")){
this.author = "";
this.title = "";
}
else
if(filename.contains(".")) {
title = filename.substring(0, filename.lastIndexOf(".")).trim();
}
else {
title = filename.trim();
}
}
}
else {
title = "";
author = "";
}
return;
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public String toString() {
if (author.equals("")) {
return getTitle();
}
else {
return getAuthor() + " - " + getTitle();
}
}
}
und das hier ist der zubestehende JUnitTest code
Code:
// We still use the Junit 3 framework since the APA server is not yet migrated to JUnit 4
// This is the version that runs on the PCs.
//
// It provides the facility to emulate a different OS and enables the developer
// to check the behavior of the test code on platforms different from the
// developers platform.
// The facility is convenient to check the normalization of pathnames with respect to
// '/' and '\'
//
// The behavior is defined within the method setUp() below.
//
// Note: at the APA server we are running the linux emulation!
//
import junit.framework.TestCase;
public class AudioFileTest extends TestCase {
private static char sep;
private static String root = "/";
private static String pathNames[];
private static String expectedPathNames[];
private static String expectedFileNames[];
private static String authors[];
private static String titles[];
private static String toStrings[];
public void setUp() {
try {
// Activate exactly one the following lines
// in order to define the emulation you like
// Usually, the first line (no emulation) is active
//
EmulateOtherOs.reset(); // no emulation
// EmulateOtherOs.emulateWindows();
// EmulateOtherOs.emulateLinux();
// EmulateOtherOs.emulateMac();
} catch (Exception e) {
e.printStackTrace();
}
sep = java.io.File.separatorChar;
String osname = System.getProperty("os.name");
if (osname.toLowerCase().indexOf("win") >= 0)
root = "C:" + sep;
// This array contains the arguments we feed to method parsePathname()
pathNames = new String[] {
root + "home" + sep + "meier" + sep + "Musik" + sep + "Falco - Rock Me Amadeus.mp3",
root + "home" + sep + "db-admin" + sep + "Frankie Goes To Hollywood - The Power Of Love.ogg",
root + "tmp" + sep + "Deep Purple - Smoke On The Water.wav",
root + "my-tmp" + sep + "file.mp3",
"Falco - Rock Me Amadeus.mp3",
"file.mp3",
".." + sep + "music" + sep + "audiofile.au",
" A.U.T.O.R - T.I.T.E.L .EXTENSION",
"Hans-Georg Sonstwas - Blue-eyed boy-friend.mp3",
// Some more ugly test cases.
// Note that arbitrary combinations of / and \ are provided.
// Consecutive occurrences of these are to be squeezed and
// replaced by a single separator that corresponds to
// the platform running the application (use java.io.File.separatorChar).
// Further note that spaces and tabs (white space) are not
// altered by this normalization.
"",
" ",
"//your-tmp/part1//file.mp3/",
"../your-tmp/..//part1//file.mp3/",
"\\file.mp3",
"\\part1\\\\file.mp3\\",
"\\part1///file.mp3",
"/MP3-Archiv/.nox",
"/MP3-Archiv/Falco - Rock me Amadeus.",
"-",
" - "
};
// Array of the results expected from method getPathname()
// We expect normalization with respect to consecutive occurrences of / and \
// and replacement by a single java.io.File.separatorChar
// Spaces and tabs (white space) are not altered.
expectedPathNames = new String[] {
root + "home" + sep + "meier" + sep + "Musik" + sep + "Falco - Rock Me Amadeus.mp3",
root + "home" + sep + "db-admin" + sep + "Frankie Goes To Hollywood - The Power Of Love.ogg",
root + "tmp" + sep + "Deep Purple - Smoke On The Water.wav",
root + "my-tmp" + sep + "file.mp3",
"Falco - Rock Me Amadeus.mp3",
"file.mp3",
".." + sep + "music" + sep + "audiofile.au",
" A.U.T.O.R - T.I.T.E.L .EXTENSION",
"Hans-Georg Sonstwas - Blue-eyed boy-friend.mp3",
// Some more ugly test cases
"",
" ",
sep + "your-tmp" + sep + "part1" + sep + "file.mp3" + sep,
".." + sep + "your-tmp" + sep + ".." + sep + "part1" + sep + "file.mp3" + sep,
sep + "file.mp3",
sep + "part1" + sep + "file.mp3" + sep,
sep + "part1" + sep + "file.mp3",
sep + "MP3-Archiv" + sep + ".nox",
sep + "MP3-Archiv" + sep + "Falco - Rock me Amadeus.",
"-",
" - "
};
// Array of the results expected from method getFilename()
// Spaces and tabs (white space) are not altered.
expectedFileNames = new String[] {
"Falco - Rock Me Amadeus.mp3",
"Frankie Goes To Hollywood - The Power Of Love.ogg",
"Deep Purple - Smoke On The Water.wav",
"file.mp3",
"Falco - Rock Me Amadeus.mp3",
"file.mp3",
"audiofile.au",
" A.U.T.O.R - T.I.T.E.L .EXTENSION",
"Hans-Georg Sonstwas - Blue-eyed boy-friend.mp3",
// Some more ugly test cases
"",
" ",
"",
"",
"file.mp3",
"",
"file.mp3",
".nox",
"Falco - Rock me Amadeus.",
"-",
" - "
};
// Array of the results expected from method getAuthor()
// Leading and trailing spaces and tabs (white space) are trimmed.
authors = new String[] {
"Falco",
"Frankie Goes To Hollywood",
"Deep Purple",
"",
"Falco",
"",
"",
"A.U.T.O.R",
"Hans-Georg Sonstwas",
// Some more ugly test cases
"",
"",
"",
"",
"",
"",
"",
"",
"Falco",
"",
""
};
// Array of the results expected from method getTitle()
// Leading and trailing spaces and tabs (white space) are trimmed.
titles = new String[] {
"Rock Me Amadeus",
"The Power Of Love",
"Smoke On The Water",
"file",
"Rock Me Amadeus",
"file",
"audiofile",
"T.I.T.E.L",
"Blue-eyed boy-friend",
// Some more ugly test cases
"",
"",
"",
"",
"file",
"",
"file",
"",
"Rock me Amadeus",
"-",
""
};
// Array of the results expected from method toString()
toStrings = new String[] {
"Falco - Rock Me Amadeus",
"Frankie Goes To Hollywood - The Power Of Love",
"Deep Purple - Smoke On The Water",
"file",
"Falco - Rock Me Amadeus",
"file",
"audiofile",
"A.U.T.O.R - T.I.T.E.L",
"Hans-Georg Sonstwas - Blue-eyed boy-friend",
// Some more ugly test cases
"",
"",
"",
"",
"file",
"",
"file",
"",
"Falco - Rock me Amadeus",
"-",
""
};
}
public void tearDown() {
try {
EmulateOtherOs.reset();
} catch (Exception e) {
e.printStackTrace();
}
}
public void testSettersAndGetters() {
// Use the constructor without arguments
String current = null;
try {
for (int i = 0; i < pathNames.length; i++) {
String p = pathNames[i];
current = p;
AudioFile af = new AudioFile();
af.parsePathname(p);
af.parseFilename(af.getFilename());
assertEquals("getPathname() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", expectedPathNames[i],
af.getPathname());
assertEquals("getFilename() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", expectedFileNames[i],
af.getFilename());
assertEquals("getAuthor() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", authors[i], af.getAuthor());
assertEquals("getTitle() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", titles[i], af.getTitle());
assertEquals("toString() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", toStrings[i], af.toString());
}
} catch (Exception e) {
fail("Fehler fuer pathname:" + current + ":" + e);
}
}
public void testCtor() {
// Perform the same getter and setter tests by using the ctor with one
// argument for construction
String current = null;
try {
for (int i = 0; i < pathNames.length; i++) {
String p = pathNames[i];
current = p;
AudioFile af = new AudioFile(p);
assertEquals("getPathname() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", expectedPathNames[i],
af.getPathname());
assertEquals("getFilename() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", expectedFileNames[i],
af.getFilename());
assertEquals("getAuthor() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", authors[i], af.getAuthor());
assertEquals("getTitle() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", titles[i], af.getTitle());
assertEquals("toString() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", toStrings[i], af.toString());
}
} catch (Exception e) {
fail("Fehler fuer pathname:" + current + ":" + e);
}
}
// A test case for the treatment of drive letters.
// If a drive specifier (a single letter followed by :)
// is provided it is to be replaced according to the platform.
// On windows: d: --> d: (no transformation of drive specifier)
// On other platform: d: --> /d/ or more precisely sep + "d" + sep
public void test_TreatmentOfDriveLetters_01() {
AudioFile af = new AudioFile();
af.parsePathname("Z:\\part1\\\\file.mp3\\");
char sep = java.io.File.separatorChar;
if (isWindows()) {
// On Windows we expect "Z:\part1\file.mp3\"
assertEquals("Pathname stored incorrectly",
"Z:" + sep + "part1" + sep + "file.mp3" + sep,
af.getPathname());
} else {
// On other platforms we expect "/Z/part1/file.mp3/"
assertEquals("Pathname stored incorrectly",
sep + "Z" + sep + "part1" + sep + "file.mp3" + sep,
af.getPathname());
}
assertEquals("Returned filename is incorrect", "", af.getFilename());
}
// A test case for the treatment of drive letters
public void test_TreatmentOfDriveLetters_02() {
AudioFile af = new AudioFile();
af.parsePathname("Z:///part1//file.mp3");
char sep = java.io.File.separatorChar;
if (isWindows()) {
// On Windows we expect "Z:\part1\file.mp3"
assertEquals("Pathname stored incorrectly",
"Z:" + sep + "part1" + sep + "file.mp3",
af.getPathname());
} else {
// On other platforms we expect "/Z/part1/file.mp3/"
assertEquals("Pathname stored incorrectly",
sep + "Z" + sep + "part1" + sep + "file.mp3",
af.getPathname());
}
assertEquals("Returned filename is incorrect", "file.mp3", af.getFilename());
}
// A test case for the treatment of drive letters
public void test_TreatmentOfDriveLetters_03() {
AudioFile af = new AudioFile();
af.parsePathname("Z:///file.mp3");
char sep = java.io.File.separatorChar;
if (isWindows()) {
// On Windows we expect "Z:\file.mp3"
assertEquals("Pathname stored incorrectly",
"Z:" + sep + "file.mp3",
af.getPathname());
} else {
// On other platforms we expect "/Z/file.mp3"
assertEquals("Pathname stored incorrectly",
sep + "Z" + sep + "file.mp3",
af.getPathname());
}
assertEquals("Returned filename is incorrect", "file.mp3", af.getFilename());
}
/*--------------------------------------------------------------------------
* Auxiliary methods
*/
private boolean isWindows() {
return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0;
}
}