UnitTests funktionieren nicht

omamarthilde

Grünschnabel
Hallo,
ich habe nun seit einem monat java im studium, ich bin also noch blutiger anänger ;).
bis nächsten dienstag muss ich eine aufgabe abgeben, die von einem audiofile den pfadnamen bereinigt (also überflüssige slashes herrausfiltert und gegebenfalls von linux slash zu windows slash konvertiert) und dann aus dem pfadnamen den filenamen herrausfiltert, welcher dann in autor und titel geteilt wird.

der von mir geschriebene code funktioniert wunderbar mit meinen unittests. wenn ich aber die vorgegebenen unittests verwende bekomme ich fehler angezeigt. ich versuche nun schon seit einiger zeit diese fehler zu beseitigen, habe es bisjetzt aber noch nicht geschafft und da der dienstag immer näher rückt bin ich momentan ziemlich am verzweifeln.

hier mal mein code und die vorgegebenen testunits:

Code:
public class AudioFile {
	private String pathname;
	private String filename;
	char sepChar = java.io.File.separatorChar;
	private String autor;
	private String title;
	
	public AudioFile(){}
	
	public AudioFile(String path)
	{
		parsePathname(path);
		parseFilename(getFilename());
	}
	
	private boolean isWindows(){
		return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0;
	}

	public void translateDriveLetter(){
		if (isWindows() == true && pathname.length() >= 3
				&&pathname.charAt(0) == '/'
					&&pathname.charAt(2) == '/')
			
		{
			pathname = pathname.substring(1,2) + ":" + "\\" + pathname.substring(3);
		}
		if (isWindows() == false && pathname.length() >= 2
				&&pathname.charAt(1) == ':')
		{
			pathname = "/" + pathname.charAt(0) + "/" + pathname.substring(3);
		}

	}
	
	public void replaceSlash() {
		
		if (isWindows() == true && pathname.contains("/"))
		{
			pathname = pathname.replace("/", "\\");
		}
		if (isWindows() == false && pathname.contains("\\"))
		{
			pathname = pathname.replace("\\", "/");
		}
	}

	public void parsePathname(String path){
		pathname = path;
		
		
		replaceSlash();
		translateDriveLetter();
		
		pathname = pathname.replace("\\\\", "\\");
		filename = pathname.substring(pathname.lastIndexOf(sepChar) + 1, pathname.length());
		 
	}
	
	public String getPathname(){
		return pathname;
	}
	
	public String getFilename(){
		return filename;
	}
	
	public void parseFilename(String file) {
		
		filename = file; 
		
		if ("-".equals(filename))
		{
			autor = "";
			title = "-";
		}
		else if (" - ".equals(filename))
		{
			autor = "";
			title = "";
		}
		else if (filename.contains(" - "))
		{
			String[] splitString = filename.split(" - ");
			autor = splitString[0];
			title = splitString[1];
			
			title = title.substring(0, title.lastIndexOf('.'));
		}
		else
		{
			autor = "";
			title = filename.substring(0, filename.lastIndexOf('.'));
		}
		
		autor = autor.trim();
		title = title.trim();
	}
	
	public String getAuthor(){
		return autor;
	}
	
	public String getTitle(){
		return title;
	}
	
	public String toString(){
		if (autor.length() == 0)
		{
			return title;
		}
		else
		{
			return autor + " - " + title;
		}
	}
}

Code:
// We still use the Junit 3 framework since the APA server is not yet migrated to JUnit 4

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 {
            EmulateOtherOs.reset(); // no emulation
            // EmulateOtherOs.emulateWindows();
            // EmulateOtherOs.emulateLinux();
            // EmulateOtherOs.emulateMac();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        sep = java.io.File.separatorChar;
        String osname = System.getProperty("os.name");
        if (osname.toLowerCase().indexOf("win") >= 0)
            root = "C:" + sep;

        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
                "",
                " ",
                "//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.",
                "-",
                " -  "
        };

        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.",
                "-",
                " -  "
        };

        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.",
                "-",
                " -  "
        };

        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",
                "",
                ""
        };

        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",
                "-",
                ""
        };

        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) {
            // TODO Auto-generated catch block
            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
    public void test_TreatmentOfDriveLetters_01() {
        AudioFile af = new AudioFile();
        af.parsePathname("Z:\\part1\\\\file.mp3\\");

        char sepchar = java.io.File.separatorChar;

        if (isWindows()) {
            // On Windows we expect "Z:\part1\file.mp3\"
            assertEquals("Pathname stored incorrectly", "Z:" + sepchar
                    + "part1" + sepchar + "file.mp3" + sepchar,
                    af.getPathname());
        } else {
            // On Unix we expect "/Z/part1/file.mp3/"
            assertEquals("Pathname stored incorrectly", sepchar + "Z" + sepchar
                    + "part1" + sepchar + "file.mp3" + sepchar,
                    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 sepchar = java.io.File.separatorChar;

        if (isWindows()) {
            // On Windows we expect "Z:part1\file.mp3\"
            assertEquals("Pathname stored incorrectly", "Z:" + "part1"
                    + sepchar + "file.mp3" + sepchar, af.getPathname());
        } else {
            // On Unix we expect "/Z/part1/file.mp3/"
            assertEquals("Pathname stored incorrectly", sepchar + "Z" + sepchar
                    + "part1" + sepchar + "file.mp3" + sepchar,
                    af.getPathname());
        }
        assertEquals("Returned filename is incorrect", "", af.getFilename());
    }

    /*--------------------------------------------------------------------------
     * Auxiliary methods 
     */

    private boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0;
    }
}

die fehler treten auf bei

} catch (Exception e) {
fail("Fehler fuer pathname:" + current + ":" + e);


ich hoffe hier gibt es jemanden der mir helfen kann.
 
Du rufst in setUp und tearDown die super.setUp/tearDown Methode nicht auf.

(Ohne näher die Inhalte geprüft zu haben)
 
Du rufst in setUp und tearDown die super.setUp/tearDown Methode nicht auf.

(Ohne näher die Inhalte geprüft zu haben)

danke für deine antwort.
aber hat "super2 nicht etwas mit vererbung zu tun? uns wurde nämlich versichert das man bei dieser aufgabe nichts vererben muss.

sollte dies nicht der fall sein, wie kann ich diesen fehler beheben?
 
fail("Fehler fuer pathname:" + current + ":" + e);

Das bedeutet, dass eine Exception geworfen wird, der hier abgefangen wird, was aber keinen Sinn ergibt, da die Fehlermeldung so oder so von JUnit wiedergegeben werden würde. Du solltest dir daher den Stacktrace von e anschauen oder das try/catch entfernen, dann wird der Stacktrace von JUnit bereitgestellt.


Bei deinem JUnit-Test leitet deine Klasse von der JUnit-Klasse "TestCase" ab.

Von TestCase überschreibst du die Methoden setUp und tearDown. Da diese Methoden aber weiterhin aufgerufen werden müssen, musst du in deinen Methoden diese manuell mittels super.setUp bzw. super.tearDown entsprechend aufrufen.


Java:
public void setUp(){
     super.setUp();
     //Dein Code:
     //..
}
 
Zuletzt bearbeitet:
Zurück