001    package com.oxygenxml.validate.nvdl;
002    
003    import junit.framework.TestCase;
004    
005    public class NamespaceSpecificationTest extends TestCase {
006      
007      
008      /**
009       * Tests for the covers method.
010       *
011       */
012      public void testCovers() {
013        doCovers("*");
014        doCovers("\\");
015        doCovers("#");
016      }
017      
018      /**
019       * Test covers with different wildcards.
020       * @param w The wildcard.
021       */
022      public void doCovers(String w) {
023        // any namespace
024        NamespaceSpecification nss = 
025          new NamespaceSpecification(
026              NamespaceSpecification.ANY_NAMESPACE, 
027              NamespaceSpecification.DEFAULT_WILDCARD);
028        assertTrue(nss.covers(NamespaceSpecification.ANY_NAMESPACE));
029        assertFalse(nss.covers("otherNamespace"));
030        
031        // specified namespace, no wildcards.
032        nss = new NamespaceSpecification("http://www.oxygenxml.com/test",w);
033        assertFalse(nss.covers(NamespaceSpecification.ANY_NAMESPACE));
034        assertTrue(nss.covers("http://www.oxygenxml.com/test"));
035        assertFalse(nss.covers("http://www.oxygenxml.com/test1"));
036        
037        // one wildcard at the end.
038        nss = new NamespaceSpecification("http://www.oxygenxml.com/test" + w, w);
039        assertFalse(nss.covers(NamespaceSpecification.ANY_NAMESPACE));
040        assertTrue(nss.covers("http://www.oxygenxml.com/test"));
041        assertTrue(nss.covers("http://www.oxygenxml.com/test/test"));
042        assertFalse(nss.covers("http://www.oxygenxml.com/xtest"));
043        
044        // one wildcard in the middle.
045        nss = new NamespaceSpecification("http://www.oxygenxml.com/" + w + "test",w);    
046        assertFalse(nss.covers(NamespaceSpecification.ANY_NAMESPACE));
047        assertTrue(nss.covers("http://www.oxygenxml.com/test"));
048        assertTrue(nss.covers("http://www.oxygenxml.com/test/test"));
049        assertTrue(nss.covers("http://www.oxygenxml.com/xtest"));
050        assertFalse(nss.covers("http://www.oxygenxml.com/testx"));    
051        
052        // one wildcard at the beginning
053        nss = new NamespaceSpecification(w + "http://www.oxygenxml.com/test", w);
054        assertFalse(nss.covers(NamespaceSpecification.ANY_NAMESPACE));
055        assertTrue(nss.covers("http://www.oxygenxml.com/test"));
056        assertFalse(nss.covers("http://www.oxygenxml.com/test/test"));
057        assertTrue(nss.covers("xhttp://www.oxygenxml.com/test"));
058        assertFalse(nss.covers("xhttp://www.oxygenxml.com/testy"));
059        
060        // two wildcards
061        nss = new NamespaceSpecification(w + "http://www.oxygenxml.com/test" + w, w);
062        assertFalse(nss.covers(NamespaceSpecification.ANY_NAMESPACE));
063        assertTrue(nss.covers("http://www.oxygenxml.com/test"));
064        assertTrue(nss.covers("XXXhttp://www.oxygenxml.com/testXXX"));
065        assertTrue(nss.covers("http://www.oxygenxml.com/testXXX"));
066        assertTrue(nss.covers("XXXhttp://www.oxygenxml.com/test"));
067        assertFalse(nss.covers("http://www.oxygenxmlX.com/test"));
068        
069        nss = new NamespaceSpecification("http://www.oxygenxml.com/" + w + "test" + w, w);
070        assertFalse(nss.covers(NamespaceSpecification.ANY_NAMESPACE));
071        assertTrue(nss.covers("http://www.oxygenxml.com/XXXtest"));
072        assertTrue(nss.covers("http://www.oxygenxml.com/testXXX"));
073        assertTrue(nss.covers("http://www.oxygenxml.com/XXXtestXXXXX"));
074        assertFalse(nss.covers("http://www.oxygenxml.comX/XXXtestXXXXX"));
075        
076        nss = new NamespaceSpecification("http://www.oxygenxml."+ w +"/" + w + "test", w);
077        assertFalse(nss.covers(NamespaceSpecification.ANY_NAMESPACE));
078        assertTrue(nss.covers("http://www.oxygenxml.com/XXXtest"));
079        assertTrue(nss.covers("http://www.oxygenxml.XXX/XXXtest"));
080        assertFalse(nss.covers("http://www.oxygenxml.com/XXXtestX"));
081        
082        // no wildcard, spefified
083        nss = new NamespaceSpecification("http://www.oxygenxml."+ w +"/" + w + "test", "");
084        assertTrue(nss.covers("http://www.oxygenxml." + w + "/" + w + "test"));
085        assertFalse(nss.covers("http://www.oxygenxml." + "XXX" + "/" + w + "test"));
086      }
087    
088      /**
089       * Test for the compete method.
090       */
091      public void testCompete() {
092        doCompete("*", "*");
093        doCompete("#", "#");
094        doCompete("\\", "\\");
095        doCompete("*", "#");
096        doCompete("*", "\\");
097        doCompete("#", "\\");
098      }
099    
100      /**
101       * Test compete with different wildcards.
102       * @param w1 First namespace specification wildcard.
103       * @param w2 Second namespace specification wildcard.
104       */
105      private void doCompete(String w1, String w2) {
106        NamespaceSpecification nss1 = new NamespaceSpecification("", w1);
107        NamespaceSpecification nss2 = new NamespaceSpecification("", w2);    
108        assertTrue(nss1.compete(nss2));
109        
110        nss1 = new NamespaceSpecification("http://www.oxygenxml.com", w1);
111        nss2 = new NamespaceSpecification("http://www.oxygenxml.com", w2);    
112        assertTrue(nss1.compete(nss2));
113    
114        nss1 = new NamespaceSpecification("http://www.oxygenxml.com/test", w1);
115        nss2 = new NamespaceSpecification("http://www.oxygenxml.com", w2);    
116        assertFalse(nss1.compete(nss2));
117    
118        nss1 = new NamespaceSpecification("http://www.oxygenxml.com" + w1, w1);
119        nss2 = new NamespaceSpecification("http://www.oxygenxml.com", w2);    
120        assertTrue(nss1.compete(nss2));
121    
122        nss1 = new NamespaceSpecification("http://www.oxygenxml.com" + w1, w1);
123        nss2 = new NamespaceSpecification("http://www.oxygenxml.com/test", w2);    
124        assertTrue(nss1.compete(nss2));
125        
126        nss1 = new NamespaceSpecification("http://www.oxygenxml2.com" + w1, w1);
127        nss2 = new NamespaceSpecification("http://www.oxygenxml.com", w2);    
128        assertFalse(nss1.compete(nss2));
129        
130        nss1 = new NamespaceSpecification("http://www." + w1 + ".com", w1);
131        nss2 = new NamespaceSpecification("http://www.oxygenxml." + w2, w2);    
132        assertTrue(nss1.compete(nss2));
133        
134        nss1 = new NamespaceSpecification("http" + w1 + "://www." + w1 + ".com", w1);
135        nss2 = new NamespaceSpecification("http://www.oxyg" + w2 + "enxml." + w2, w2);    
136        assertTrue(nss1.compete(nss2));
137        
138        nss1 = new NamespaceSpecification("http://www." + w1 + ".com", w1);
139        nss2 = new NamespaceSpecification(
140            NamespaceSpecification.ANY_NAMESPACE, 
141            NamespaceSpecification.DEFAULT_WILDCARD);    
142        assertFalse(nss1.compete(nss2));
143        
144        nss1 = new NamespaceSpecification(
145            NamespaceSpecification.ANY_NAMESPACE, 
146            NamespaceSpecification.DEFAULT_WILDCARD);
147        nss2 = new NamespaceSpecification(
148            NamespaceSpecification.ANY_NAMESPACE, 
149            NamespaceSpecification.DEFAULT_WILDCARD);    
150        assertTrue(nss1.compete(nss2));
151      
152        nss1 = new NamespaceSpecification("http://www.oxygenxml.com/test", "");
153        nss2 = new NamespaceSpecification("http://www.oxyg" + w2 + "enxml." + w2, w2);    
154        assertTrue(nss1.compete(nss2));
155        
156        nss1 = new NamespaceSpecification("http://www.oxygenxml.com/test", "");
157        nss2 = new NamespaceSpecification("http://www.oxygenxml.com/test", "");    
158        assertTrue(nss1.compete(nss2));
159        
160        nss1 = new NamespaceSpecification("http://www.oxygenxml.com/test", "");
161        nss2 = new NamespaceSpecification("http://www.oxygenxml.com/test/x", "");    
162        assertFalse(nss1.compete(nss2));
163      }
164    }