001: /*
002: jGuard is a security framework based on top of jaas (java authentication and authorization security).
003: it is written for web applications, to resolve simply, access control problems.
004: version $Name: $
005: http://sourceforge.net/projects/jguard/
006:
007: Copyright (C) 2004 Charles GAY
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU Lesser General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018:
019: You should have received a copy of the GNU Lesser General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022:
023:
024: jGuard project home page:
025: http://sourceforge.net/projects/jguard/
026:
027: */
028: package net.sf.jguard.ext.util;
029:
030: import java.net.MalformedURLException;
031: import java.net.URL;
032:
033: import junit.framework.TestCase;
034:
035: import org.dom4j.Document;
036: import org.dom4j.Element;
037:
038: public class XMLUtilsTest extends TestCase {
039:
040: /*
041: * Test method for 'net.sf.jguard.ext.util.XMLUtils.read(String)'
042: */
043: public void testRead() {
044:
045: Document doc = null;
046: URL url = getClass().getResource("/jGuardUsersPrincipals.xml");
047: String strUrl = url.toString();
048: Element root = null;
049:
050: try {
051:
052: doc = XMLUtils.read(strUrl);
053: root = doc.getRootElement();
054: assertTrue(" there is no elements in the document ", root
055: .elements().size() > 0);
056: } catch (Throwable e) {
057: TestCase.fail(" testRead fail ");
058: }
059:
060: try {
061: String str1 = strUrl.replaceFirst("file:///", "file:/");
062: System.out.println("str1=" + str1);
063: doc = XMLUtils.read(str1);
064: root = doc.getRootElement();
065: assertTrue(" there is no elements in the document ", root
066: .elements().size() > 0);
067: } catch (Throwable e) {
068: TestCase.fail(" testRead fail ");
069: }
070:
071: try {
072: String str2 = strUrl.replaceFirst("file:///", "file://");
073: System.out.println("str2=" + str2);
074: doc = XMLUtils.read(str2);
075: root = doc.getRootElement();
076: assertTrue(" there is no elements in the document ", root
077: .elements().size() > 0);
078: } catch (Throwable e) {
079: TestCase.fail(" testRead fail ");
080: }
081: try {
082: String str3 = strUrl.replaceFirst("file:///",
083: "file://///////////");
084: System.out.println("str3=" + str3);
085: doc = XMLUtils.read(str3);
086: root = doc.getRootElement();
087: assertTrue(" there is no elements in the document ", root
088: .elements().size() > 0);
089: } catch (Throwable e) {
090: TestCase.fail(" testRead fail ");
091: }
092:
093: try {
094: String str4 = strUrl.replaceFirst("file:///",
095: "file:///////qsdfsdf");
096: System.out.println("str4=" + str4);
097: doc = XMLUtils.read(str4);
098: root = doc.getRootElement();
099: assertTrue(" there is no elements in the document ", root
100: .elements().size() > 0);
101: } catch (Throwable e) {
102: TestCase.fail(" testRead fail ");
103: }
104:
105: }
106:
107: public void testResolveLocation() {
108: String[] filePatterns = { "toto", "file:/toto", "file://toto",
109: "file:///toto", "file:////toto", "file://///toto",
110: "file://///toto", "file:////////toto" };
111:
112: String[] blankPatterns = { "file:////to to", "file:////to to ",
113: "file:////toto%20toto ", "to to", "to %20to" };
114: testPatterns(filePatterns);
115: testPatterns(blankPatterns);
116:
117: }
118:
119: private void testPatterns(String[] patterns) {
120: for (int i = 0; i < patterns.length; i++) {
121: String resolvedPattern = XMLUtils
122: .resolveLocation(patterns[i]);
123: System.out.println("*" + resolvedPattern + "*");
124: try {
125: URL url = new URL(resolvedPattern);
126: } catch (MalformedURLException e) {
127: TestCase.fail(e.getLocalizedMessage());
128: }
129: }
130: }
131:
132: }
|