1   /*
2    * $Header$
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    * Copyright 2000-2002 bob mcwhirter & James Strachan.
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or without
12   * modification, are permitted provided that the following conditions are
13   * met:
14   * 
15   *   * Redistributions of source code must retain the above copyright
16   *     notice, this list of conditions and the following disclaimer.
17   * 
18   *   * Redistributions in binary form must reproduce the above copyright
19   *     notice, this list of conditions and the following disclaimer in the
20   *     documentation and/or other materials provided with the distribution.
21   * 
22   *   * Neither the name of the Jaxen Project nor the names of its
23   *     contributors may be used to endorse or promote products derived 
24   *     from this software without specific prior written permission.
25   * 
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
30   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   *
38   * ====================================================================
39   * This software consists of voluntary contributions made by many 
40   * individuals on behalf of the Jaxen Project and was originally 
41   * created by bob mcwhirter <bob@werken.com> and 
42   * James Strachan <jstrachan@apache.org>.  For more information on the 
43   * Jaxen Project, please see <http://www.jaxen.org/>.
44   * 
45   * $Id$
46   */
47  
48  
49  package org.jaxen.test;
50  
51  import junit.framework.TestCase;
52  
53  import java.io.ByteArrayOutputStream;
54  import java.io.IOException;
55  import java.io.ObjectOutputStream;
56  import java.util.ArrayList;
57  import java.util.List;
58  
59  import javax.xml.parsers.DocumentBuilder;
60  import javax.xml.parsers.DocumentBuilderFactory;
61  import javax.xml.parsers.ParserConfigurationException;
62  
63  import org.jaxen.Context;
64  import org.jaxen.ContextSupport;
65  import org.jaxen.JaxenException;
66  import org.jaxen.XPath;
67  import org.jaxen.dom.DOMXPath;
68  import org.w3c.dom.Document;
69  import org.w3c.dom.Element;
70  import org.xml.sax.SAXException;
71  
72  public class ContextTest extends TestCase
73  {
74      private List           nodeSet;
75      private ContextSupport support;
76  
77      public ContextTest(String name)
78      {
79          super( name );
80      }
81  
82      public void setUp()
83      {
84          this.nodeSet = new ArrayList();
85  
86          this.nodeSet.add( "one" );
87          this.nodeSet.add( "two" );
88          this.nodeSet.add( "three" );
89          this.nodeSet.add( "four" );
90  
91          this.support = new ContextSupport();
92      }
93  
94      public void tearDown()
95      {
96          this.nodeSet = null;
97      }
98  
99      public void testSetNodeSet()
100     {
101         Context original = new Context( this.support );
102         assertEquals(0, original.getNodeSet().size() );
103         original.setNodeSet( this.nodeSet );
104         assertEquals(4, original.getNodeSet().size() );
105     }
106     
107     public void testShrinkNodeSet()
108     {
109         
110         Context original = new Context( this.support );
111         original.setNodeSet( this.nodeSet );
112         original.setPosition(3);
113         ArrayList list = new ArrayList();
114         list.add("1");
115         list.add("2");
116         list.add("3");
117         original.setNodeSet(list);
118         assertEquals(0, original.getPosition());
119         
120     }
121     
122     public void testDuplicate()
123     {
124         Context original = new Context( this.support );
125 
126         original.setNodeSet( this.nodeSet );
127 
128         original.setSize( 4 );
129         original.setPosition( 2 );
130 
131         Context dupe = original.duplicate();
132 
133         assertEquals(2, dupe.getPosition());
134         assertEquals(4, dupe.getSize());
135         
136         assertTrue( original != dupe );
137 
138         List dupeNodeSet = dupe.getNodeSet();
139 
140         assertTrue( original.getNodeSet() != dupe.getNodeSet() );
141 
142         dupeNodeSet.clear();
143 
144         assertSame( dupeNodeSet,
145                     dupe.getNodeSet() );
146 
147         assertEquals( 0,
148                       dupe.getNodeSet().size() );
149 
150 
151         assertEquals( 4,
152                       original.getNodeSet().size() );
153 
154         dupe.setSize( 0 );
155         dupe.setPosition( 0 );
156 
157         assertEquals( 0,
158                       dupe.getSize() );
159 
160         assertEquals( 0,
161                       dupe.getPosition() );
162 
163         assertEquals( 4,
164                       original.getSize() );
165 
166         assertEquals( 2,
167                       original.getPosition() );
168     }    
169     
170 
171     public void testXMLPrefixIsAlwaysBound() 
172       throws ParserConfigurationException, SAXException, IOException, JaxenException
173     {  
174 
175        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
176        factory.setNamespaceAware(true);
177        DocumentBuilder builder = factory.newDocumentBuilder();
178        Document doc = builder.parse( "xml/basic.xml" );
179        Element root = doc.getDocumentElement();
180        root.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "en");
181        XPath xpath = new DOMXPath( "/*/@xml:lang" );
182        List result = xpath.selectNodes( doc );
183        assertEquals(1, result.size());
184 
185     }    
186 
187     
188     public void testIsSerializable() throws IOException {
189 
190         ByteArrayOutputStream out = new ByteArrayOutputStream();
191         ObjectOutputStream oos = new ObjectOutputStream(out);
192         oos.writeObject(support);
193         oos.close();
194         assertTrue(out.toByteArray().length > 0);
195         
196     }    
197     
198     
199 }
200