KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > ext > util > XMLUtilsTest


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name: $
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.ext.util;
29
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URL JavaDoc;
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     /*
41      * Test method for 'net.sf.jguard.ext.util.XMLUtils.read(String)'
42      */

43     public void testRead() {
44
45             Document doc = null;
46             URL JavaDoc url = getClass().getResource("/jGuardUsersPrincipals.xml");
47             String JavaDoc 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 JavaDoc e) {
57                     TestCase.fail(" testRead fail ");
58                 }
59
60              try {
61                  String JavaDoc 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 JavaDoc e) {
67                   TestCase.fail(" testRead fail ");
68              }
69
70              try {
71                  String JavaDoc 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 JavaDoc e) {
77                   TestCase.fail(" testRead fail ");
78              }
79              try {
80                  String JavaDoc 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 JavaDoc e) {
86                   TestCase.fail(" testRead fail ");
87              }
88
89              try {
90                  String JavaDoc 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 JavaDoc e) {
96                   TestCase.fail(" testRead fail ");
97              }
98
99
100     }
101     
102     public void testResolveLocation(){
103         String JavaDoc[] filePatterns = {"toto","file:/toto","file://toto","file:///toto",
104                               "file:////toto","file://///toto","file://///toto",
105                               "file:////////toto"};
106         
107         String JavaDoc[] 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 JavaDoc[] patterns) {
115         for(int i = 0;i<patterns.length;i++){
116             String JavaDoc resolvedPattern = XMLUtils.resolveLocation(patterns[i]);
117             System.out.println("*"+resolvedPattern+"*");
118             try {
119                 URL JavaDoc url = new URL JavaDoc(resolvedPattern);
120             } catch (MalformedURLException JavaDoc e) {
121                 TestCase.fail(e.getLocalizedMessage());
122             }
123         }
124     }
125
126
127 }
128
Popular Tags