Exceptions

tanzverfuehrung

Bekanntes Mitglied
meine methode getnode sagt aus, das es eine IllegalArgument Exception gibt ,wenn der Pfad name ungültig ist!
aber wie kann ich das richtig einbinden in meine methode???


Java:
@Override
	public boolean nodeExists(String pathName) throws IllegalArgumentException {

		try{
		if(isValidPath(pathName) == true)
			System.out.println("Wir haben einen gültigen PfadName!");
		}catch (IllegalArgumentException e) {
			System.out.println(e.getMessage());
		}
 
				
		if (pathName.isEmpty()) {
			return false;
		}

		int index = pathName.indexOf("/");

		if (index == 0) { // absoluter Pfad

			// zum relativen pfad machen zur weiter verarbeitung
			pathName = pathName.substring(index + 1);
		}

		SimplePreferences value = nodeMap.get(pathName);
		if (value != null)
			return true;

		index = pathName.indexOf("/");

		if (index < 0) { // Wenn der index < 0 ist wurde kein Slash mehr
							// gefunden, dann ist es der letzte Knoten
			value = nodeMap.get(pathName); // prüfen ob knoten
			// vorhanden ist
			if (value != null)
				return true;
			return false;
		}
		String[] split = pathName.split("/");
		for (int i = 0; i < split.length; i++) {
			value = nodeMap.get(split[i]); // prüfen ob knoten
			// vorhanden ist
			if (value != null)
				return true;
		}
		return false;
	}
	
	
	public boolean isValidPath(String pathnName){
		
		if(pathnName.endsWith("/")){
			return false;
		}
		int index =pathnName.indexOf("//");
		if(index > -1){
			return false;
		}
		return true;		
	}
 
Ich verstehe leider nicht, was Du meinst? Welche getNode-Methode? Was willst Du wo einbinden?

sorry ich meinte in meiner nodeExists methode
nodeExists
public abstract boolean nodeExists(String pathName)
throws BackingStoreExceptionReturns true if the named preference node exists in the same tree as this node. Relative path names (which do not begin with the slash character ('/')) are interpreted relative to this preference node.
If this node (or an ancestor) has already been removed with the removeNode() method, it is legal to invoke this method, but only with the path name ""; the invocation will return false. Thus, the idiom p.nodeExists("") may be used to test whether p has been removed.


Parameters:
pathName - the path name of the node whose existence is to be checked.
Returns:
true if the specified node exists.
Throws:
BackingStoreException - if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
IllegalArgumentException - if the path name is invalid (i.e., it contains multiple consecutive slash characters, or ends with a slash character and is more than one character long). NullPointerException - if path name is null. s * @throws IllegalStateException if this node (or an ancestor) has been removed with the removeNode() method and pathName is not the empty string ("").

--------------------------------------------------------------------------------


dazu habe ich jetzt ne mthode geschrieben(siehe code) isValidPath um zu ermitteln ob es ein gültiger pfad name ist!ich habe jetzt aber keine ehnung wie ich die exception in meine MEthode nodeExists kriege!
so das meine junit tests grün werden

Junittests
Java:
	@Test(expected=IllegalArgumentException.class)
	public void testNodeExistsWithSlashAtTheEnd() throws Exception {
		simple.nodeExists("bc/");
	}
	@Test(expected=IllegalArgumentException.class)
	public void testNodeExistsWithInvalidPath() throws Exception {
		simple.nodeExists("b//c");
	}
 
Java:
public boolean nodeExists(final String pathName) throws IllegalArgumentException {
    if (!isValidPath(pathName)) {
        throw new IllegalArgumentException("Der Pfadname ist ungültig: '" + pathName + "'");
    }
 
Java:
throw new IlligalArgumentException()
Damit wirfst Du in Deiner isValidPath-Methode eine Exception, die in Deiner nodeExists-Methode behandelt wird.
 
Java:
public boolean nodeExists(final String pathName) throws IllegalArgumentException {
    if (!isValidPath(pathName)) {
        throw new IllegalArgumentException("Der Pfadname ist ungültig: '" + pathName + "'");
    }
Ich habe ihn so verstanden, dass er die Exception in der isValidPath-Methode werfen möchte. Immerhin behandelt er diese schon in der nodeExists-Methode.
 
Und wieder ein weiterer glücklicher Mensch... 😀

ohhh ja das kann man so sagen!😳:toll:😀

aber habe noch ne kleine frage,
wollte jetzt noch für die methode isValidPAth junit test schreiben
aber es geht nicht!
er sagt immer
The method isValidPath(String) is undefined for the type SimplePreferencesTest

also ich kann die methode noicht richtig aufrufen!?
WHY????:L
ja undefiniert sagt er....aber :bahnhof:???:L
Java:
	@Test
	public void testIsValidPath() throws Exception {
	assertTrue(isValidPath("a/b/c"));
	}
	@Test
	public void testUnValidPath() throws Exception {
	assertFalse(isValidPath("a//b/c"));
	}
	@Test
	public void testUnValidPathSlashAtTheEnd() throws Exception {
	assertFalse(isValidPath("a//b/c/"));
	}
 
Du möchtest hier doch die Methoden einer anderen Klasse testen.

Also versuch es mal so:
Java:
@Test
public void testIsValidPath() throws Exception {
    assertTrue(new SimplePreferences().isValidPath("a/b/c"));
}

(Wenn Deine Klasse SimplePreferences ist)
 
Du möchtest hier doch die Methoden einer anderen Klasse testen.

Also versuch es mal so:
Java:
@Test
public void testIsValidPath() throws Exception {
    assertTrue(new SimplePreferences().isValidPath("a/b/c"));
}

(Wenn Deine Klasse SimplePreferences ist)

so glücklicher kann ich heut nciht mehr werden
DANKE:toll::applaus:
 

Zurück
Oben