1 28 package net.sf.jguard.ext.util; 29 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 33 import junit.framework.TestCase; 34 35 import org.dom4j.Document; 36 import org.dom4j.Element; 37 38 public class XMLUtilsTest extends TestCase { 39 40 43 public void testRead() { 44 45 Document doc = null; 46 URL url = getClass().getResource("/jGuardUsersPrincipals.xml"); 47 String strUrl = url.toString(); 48 Element root = null; 49 50 51 try { 52 53 doc = XMLUtils.read(strUrl); 54 root = doc.getRootElement(); 55 assertTrue(" there is no elements in the document ",root.elements().size()>0); 56 } catch (Throwable e) { 57 TestCase.fail(" testRead fail "); 58 } 59 60 try { 61 String str1= strUrl.replaceFirst("file:///","file:/"); 62 System.out.println("str1="+str1); 63 doc = XMLUtils.read(str1); 64 root = doc.getRootElement(); 65 assertTrue(" there is no elements in the document ",root.elements().size()>0); 66 } catch (Throwable e) { 67 TestCase.fail(" testRead fail "); 68 } 69 70 try { 71 String str2= strUrl.replaceFirst("file:///","file://"); 72 System.out.println("str2="+ str2); 73 doc = XMLUtils.read(str2); 74 root = doc.getRootElement(); 75 assertTrue(" there is no elements in the document ",root.elements().size()>0); 76 } catch (Throwable e) { 77 TestCase.fail(" testRead fail "); 78 } 79 try { 80 String str3= strUrl.replaceFirst("file:///","file://///////////"); 81 System.out.println("str3="+str3); 82 doc = XMLUtils.read(str3); 83 root = doc.getRootElement(); 84 assertTrue(" there is no elements in the document ",root.elements().size()>0); 85 } catch (Throwable e) { 86 TestCase.fail(" testRead fail "); 87 } 88 89 try { 90 String str4= strUrl.replaceFirst("file:///","file:///////qsdfsdf"); 91 System.out.println("str4="+str4); 92 doc = XMLUtils.read(str4); 93 root = doc.getRootElement(); 94 assertTrue(" there is no elements in the document ",root.elements().size()>0); 95 } catch (Throwable e) { 96 TestCase.fail(" testRead fail "); 97 } 98 99 100 } 101 102 public void testResolveLocation(){ 103 String [] filePatterns = {"toto","file:/toto","file://toto","file:///toto", 104 "file:////toto","file://///toto","file://///toto", 105 "file:////////toto"}; 106 107 String [] blankPatterns = {"file:////to to","file:////to to ", 108 "file:////toto%20toto ","to to","to %20to"}; 109 testPatterns(filePatterns); 110 testPatterns(blankPatterns); 111 112 } 113 114 private void testPatterns(String [] patterns) { 115 for(int i = 0;i<patterns.length;i++){ 116 String resolvedPattern = XMLUtils.resolveLocation(patterns[i]); 117 System.out.println("*"+resolvedPattern+"*"); 118 try { 119 URL url = new URL (resolvedPattern); 120 } catch (MalformedURLException e) { 121 TestCase.fail(e.getLocalizedMessage()); 122 } 123 } 124 } 125 126 127 } 128 | Popular Tags |