From: <bo...@us...> - 2010-05-03 10:28:31
|
Revision: 377 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=377&view=rev Author: bodewig Date: 2010-05-03 10:28:25 +0000 (Mon, 03 May 2010) Log Message: ----------- make tests reusable in case there will ever be different implementations Modified Paths: -------------- trunk/xmlunit/build.xml trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/xpath/JAXPXPathEngineTest.java trunk/xmlunit/src/tests/net-core/xpath/XPathEngineTest.cs Added Paths: ----------- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/xpath/AbstractXPathEngineTest.java trunk/xmlunit/src/tests/net-core/xpath/AbstractXPathEngineTest.cs Modified: trunk/xmlunit/build.xml =================================================================== --- trunk/xmlunit/build.xml 2010-05-03 10:26:23 UTC (rev 376) +++ trunk/xmlunit/build.xml 2010-05-03 10:28:25 UTC (rev 377) @@ -157,6 +157,7 @@ </fileset> <fileset dir="${test.dir}/java-core"> <include name="**/*Test.java"/> + <exclude name="**/Abstract*.java"/> </fileset> </batchtest> </junit> Copied: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/xpath/AbstractXPathEngineTest.java (from rev 374, trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/xpath/JAXPXPathEngineTest.java) =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/xpath/AbstractXPathEngineTest.java (rev 0) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/xpath/AbstractXPathEngineTest.java 2010-05-03 10:28:25 UTC (rev 377) @@ -0,0 +1,137 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package net.sf.xmlunit.xpath; + +import java.util.HashMap; +import java.util.Iterator; +import javax.xml.transform.Source; +import net.sf.xmlunit.TestResources; +import net.sf.xmlunit.builder.Input; +import net.sf.xmlunit.exceptions.XMLUnitException; +import org.junit.Before; +import org.junit.Test; +import org.w3c.dom.Node; + +import static org.junit.Assert.*; + +public abstract class AbstractXPathEngineTest { + + protected abstract IXPathEngine getEngine(); + + private Source source; + + @Before public void readSource() throws Exception { + source = Input.fromFile(TestResources.BLAME_FILE).build(); + } + + @Test public void selectNodesWithNoMatches() { + Iterable<Node> i = getEngine().selectNodes("foo", source); + assertNotNull(i); + assertFalse(i.iterator().hasNext()); + } + + @Test public void selectNodesWithSingleMatch() { + Iterable<Node> i = getEngine().selectNodes("//ul", source); + assertNotNull(i); + Iterator<Node> it = i.iterator(); + assertTrue(it.hasNext()); + assertEquals("ul", it.next().getNodeName()); + assertFalse(it.hasNext()); + } + + @Test public void selectNodesWithMultipleMatchs() { + Iterable<Node> i = getEngine().selectNodes("//li", source); + assertNotNull(i); + int count = 0; + for (Iterator<Node> it = i.iterator(); it.hasNext(); ) { + count++; + assertEquals("li", it.next().getNodeName()); + } + assertEquals(4, count); + } + + @Test(expected=XMLUnitException.class) + public void selectNodesWithInvalidXPath() { + getEngine().selectNodes("//li[", source); + } + + @Test public void evaluateWithNoMatches() { + assertEquals("", getEngine().evaluate("foo", source)); + } + + @Test public void evaluateWithSingleMatch() { + assertEquals("Don't blame it on the...", + getEngine().evaluate("//title", source)); + } + + @Test public void evaluateWithSingleMatchTextSelector() { + assertEquals("Don't blame it on the...", + getEngine().evaluate("//title/text()", source)); + } + + @Test public void evaluateWithMultipleMatches() { + assertEquals("sunshine", + getEngine().evaluate("//li", source)); + } + + @Test(expected=XMLUnitException.class) + public void evaluateWithInvalidXPath() { + getEngine().evaluate("//li[", source); + } + + @Test public void selectNodesWithNS() { + IXPathEngine e = getEngine(); + source = Input.fromMemory("<n:d xmlns:n='urn:test:1'><n:e/></n:d>") + .build(); + HashMap<String, String> m = new HashMap<String, String>(); + m.put("x", "urn:test:1"); + e.setNamespaceContext(m); + Iterable<Node> it = e.selectNodes("/x:d/x:e", source); + assertTrue(it.iterator().hasNext()); + } + + @Test public void selectNodesWithDefaultNS() { + IXPathEngine e = getEngine(); + source = Input.fromMemory("<d xmlns='urn:test:1'><e/></d>") + .build(); + HashMap<String, String> m = new HashMap<String, String>(); + m.put("x", "urn:test:1"); + e.setNamespaceContext(m); + Iterable<Node> it = e.selectNodes("/x:d/x:e", source); + assertTrue(it.iterator().hasNext()); + } + + @Test public void selectNodesWithDefaultNSEmptyPrefix() { + IXPathEngine e = getEngine(); + source = Input.fromMemory("<d xmlns='urn:test:1'><e/></d>") + .build(); + HashMap<String, String> m = new HashMap<String, String>(); + m.put("", "urn:test:1"); + e.setNamespaceContext(m); + Iterable<Node> it = e.selectNodes("/:d/:e", source); + assertTrue(it.iterator().hasNext()); + } + + // doesn't match + public void selectNodesWithDefaultNSNoPrefix() { + IXPathEngine e = getEngine(); + source = Input.fromMemory("<d xmlns='urn:test:1'><e/></d>") + .build(); + HashMap<String, String> m = new HashMap<String, String>(); + m.put("", "urn:test:1"); + e.setNamespaceContext(m); + Iterable<Node> it = e.selectNodes("/d/e", source); + assertTrue(it.iterator().hasNext()); + } +} Modified: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/xpath/JAXPXPathEngineTest.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/xpath/JAXPXPathEngineTest.java 2010-05-03 10:26:23 UTC (rev 376) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/xpath/JAXPXPathEngineTest.java 2010-05-03 10:28:25 UTC (rev 377) @@ -13,123 +13,8 @@ */ package net.sf.xmlunit.xpath; -import java.util.HashMap; -import java.util.Iterator; -import javax.xml.transform.Source; -import net.sf.xmlunit.TestResources; -import net.sf.xmlunit.builder.Input; -import net.sf.xmlunit.exceptions.XMLUnitException; -import org.junit.Before; -import org.junit.Test; -import org.w3c.dom.Node; - -import static org.junit.Assert.*; - -public class JAXPXPathEngineTest { - - private Source source; - - @Before public void readSource() throws Exception { - source = Input.fromFile(TestResources.BLAME_FILE).build(); +public class JAXPXPathEngineTest extends AbstractXPathEngineTest { + @Override protected IXPathEngine getEngine() { + return new JAXPXPathEngine(); } - - @Test public void selectNodesWithNoMatches() { - Iterable<Node> i = new JAXPXPathEngine().selectNodes("foo", source); - assertNotNull(i); - assertFalse(i.iterator().hasNext()); - } - - @Test public void selectNodesWithSingleMatch() { - Iterable<Node> i = new JAXPXPathEngine().selectNodes("//ul", source); - assertNotNull(i); - Iterator<Node> it = i.iterator(); - assertTrue(it.hasNext()); - assertEquals("ul", it.next().getNodeName()); - assertFalse(it.hasNext()); - } - - @Test public void selectNodesWithMultipleMatchs() { - Iterable<Node> i = new JAXPXPathEngine().selectNodes("//li", source); - assertNotNull(i); - int count = 0; - for (Iterator<Node> it = i.iterator(); it.hasNext(); ) { - count++; - assertEquals("li", it.next().getNodeName()); - } - assertEquals(4, count); - } - - @Test(expected=XMLUnitException.class) - public void selectNodesWithInvalidXPath() { - new JAXPXPathEngine().selectNodes("//li[", source); - } - - @Test public void evaluateWithNoMatches() { - assertEquals("", new JAXPXPathEngine().evaluate("foo", source)); - } - - @Test public void evaluateWithSingleMatch() { - assertEquals("Don't blame it on the...", - new JAXPXPathEngine().evaluate("//title", source)); - } - - @Test public void evaluateWithSingleMatchTextSelector() { - assertEquals("Don't blame it on the...", - new JAXPXPathEngine().evaluate("//title/text()", source)); - } - - @Test public void evaluateWithMultipleMatches() { - assertEquals("sunshine", - new JAXPXPathEngine().evaluate("//li", source)); - } - - @Test(expected=XMLUnitException.class) - public void evaluateWithInvalidXPath() { - new JAXPXPathEngine().evaluate("//li[", source); - } - - @Test public void selectNodesWithNS() { - JAXPXPathEngine e = new JAXPXPathEngine(); - source = Input.fromMemory("<n:d xmlns:n='urn:test:1'><n:e/></n:d>") - .build(); - HashMap<String, String> m = new HashMap<String, String>(); - m.put("x", "urn:test:1"); - e.setNamespaceContext(m); - Iterable<Node> it = e.selectNodes("/x:d/x:e", source); - assertTrue(it.iterator().hasNext()); - } - - @Test public void selectNodesWithDefaultNS() { - JAXPXPathEngine e = new JAXPXPathEngine(); - source = Input.fromMemory("<d xmlns='urn:test:1'><e/></d>") - .build(); - HashMap<String, String> m = new HashMap<String, String>(); - m.put("x", "urn:test:1"); - e.setNamespaceContext(m); - Iterable<Node> it = e.selectNodes("/x:d/x:e", source); - assertTrue(it.iterator().hasNext()); - } - - @Test public void selectNodesWithDefaultNSEmptyPrefix() { - JAXPXPathEngine e = new JAXPXPathEngine(); - source = Input.fromMemory("<d xmlns='urn:test:1'><e/></d>") - .build(); - HashMap<String, String> m = new HashMap<String, String>(); - m.put("", "urn:test:1"); - e.setNamespaceContext(m); - Iterable<Node> it = e.selectNodes("/:d/:e", source); - assertTrue(it.iterator().hasNext()); - } - - // doesn't match - public void selectNodesWithDefaultNSNoPrefix() { - JAXPXPathEngine e = new JAXPXPathEngine(); - source = Input.fromMemory("<d xmlns='urn:test:1'><e/></d>") - .build(); - HashMap<String, String> m = new HashMap<String, String>(); - m.put("", "urn:test:1"); - e.setNamespaceContext(m); - Iterable<Node> it = e.selectNodes("/d/e", source); - assertTrue(it.iterator().hasNext()); - } } Copied: trunk/xmlunit/src/tests/net-core/xpath/AbstractXPathEngineTest.cs (from rev 375, trunk/xmlunit/src/tests/net-core/xpath/XPathEngineTest.cs) =================================================================== --- trunk/xmlunit/src/tests/net-core/xpath/AbstractXPathEngineTest.cs (rev 0) +++ trunk/xmlunit/src/tests/net-core/xpath/AbstractXPathEngineTest.cs 2010-05-03 10:28:25 UTC (rev 377) @@ -0,0 +1,145 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +using System.Collections.Generic; +using System.Xml; +using NUnit.Framework; +using net.sf.xmlunit.builder; +using net.sf.xmlunit.exceptions; + +namespace net.sf.xmlunit.xpath { + + public abstract class AbstractXPathEngineTest { + + private ISource source; + + protected abstract IXPathEngine Engine { get; } + + [SetUp] public void ReadSource() { + source = Input.FromFile(TestResources.BLAME_FILE).Build(); + } + + [Test] public void SelectNodesWithNoMatches() { + IEnumerable<XmlNode> i = Engine.SelectNodes("foo", + source); + Assert.IsNotNull(i); + Assert.IsFalse(i.GetEnumerator().MoveNext()); + } + + [Test] public void SelectNodesWithSingleMatch() { + IEnumerable<XmlNode> i = Engine.SelectNodes("//ul", source); + Assert.IsNotNull(i); + IEnumerator<XmlNode> it = i.GetEnumerator(); + Assert.IsTrue(it.MoveNext()); + Assert.AreEqual("ul", it.Current.Name); + Assert.IsFalse(it.MoveNext()); + } + + [Test] public void SelectNodesWithMultipleMatchs() { + IEnumerable<XmlNode> i = Engine.SelectNodes("//li", source); + Assert.IsNotNull(i); + int count = 0; + foreach (XmlNode n in i) { + count++; + Assert.AreEqual("li", n.Name); + } + Assert.AreEqual(4, count); + } + + [Test] + public void SelectNodesWithInvalidXPath() { + try { + Engine.SelectNodes("//li[", source); + Assert.Fail("expected an exception"); + } catch (XMLUnitException) { + // expected + } + } + + [Test] public void EvaluateWithNoMatches() { + Assert.AreEqual(string.Empty, Engine.Evaluate("foo", source)); + } + + [Test] public void EvaluateWithSingleMatch() { + Assert.AreEqual("Don't blame it on the...", + Engine.Evaluate("//title", source)); + } + + [Test] public void EvaluateWithSingleMatchTextSelector() { + Assert.AreEqual("Don't blame it on the...", + Engine.Evaluate("//title/text()", source)); + } + + [Test] public void EvaluateWithMultipleMatches() { + Assert.AreEqual("sunshine", + Engine.Evaluate("//li", source)); + } + + [Test] + public void EvaluateWithInvalidXPath() { + try { + Engine.Evaluate("//li[", source); + Assert.Fail("expected an exception"); + } catch (XMLUnitException) { + // expected + } + } + + [Test] public void SelectNodesWithNS() { + IXPathEngine e = Engine; + source = Input.FromMemory("<n:d xmlns:n='urn:test:1'><n:e/></n:d>") + .Build(); + Dictionary<string, string> m = new Dictionary<string, string>(); + m["x"] = "urn:test:1"; + e.NamespaceContext = m; + IEnumerable<XmlNode> it = e.SelectNodes("/x:d/x:e", source); + Assert.IsTrue(it.GetEnumerator().MoveNext()); + } + + [Test] public void SelectNodesWithDefaultNS() { + IXPathEngine e = Engine; + source = Input.FromMemory("<d xmlns='urn:test:1'><e/></d>") + .Build(); + Dictionary<string, string> m = new Dictionary<string, string>(); + m["x"] = "urn:test:1"; + e.NamespaceContext = m; + IEnumerable<XmlNode> it = e.SelectNodes("/x:d/x:e", source); + Assert.IsTrue(it.GetEnumerator().MoveNext()); + } + + // throws an exception "'/:d/:e' has an invalid token." + public void SelectNodesWithDefaultNSEmptyPrefix() { + IXPathEngine e = Engine; + source = Input.FromMemory("<d xmlns='urn:test:1'><e/></d>") + .Build(); + Dictionary<string, string> m = new Dictionary<string, string>(); + m[string.Empty] = "urn:test:1"; + e.NamespaceContext = m; + IEnumerable<XmlNode> it = e.SelectNodes("/:d/:e", source); + Assert.IsTrue(it.GetEnumerator().MoveNext()); + } + + // doesn't match + public void SelectNodesWithDefaultNSNoPrefix() { + IXPathEngine e = Engine; + source = Input.FromMemory("<d xmlns='urn:test:1'><e/></d>") + .Build(); + Dictionary<string, string> m = new Dictionary<string, string>(); + m[string.Empty] = "urn:test:1"; + e.NamespaceContext = m; + IEnumerable<XmlNode> it = e.SelectNodes("/d/e", source); + Assert.IsTrue(it.GetEnumerator().MoveNext()); + } + } +} Modified: trunk/xmlunit/src/tests/net-core/xpath/XPathEngineTest.cs =================================================================== --- trunk/xmlunit/src/tests/net-core/xpath/XPathEngineTest.cs 2010-05-03 10:26:23 UTC (rev 376) +++ trunk/xmlunit/src/tests/net-core/xpath/XPathEngineTest.cs 2010-05-03 10:28:25 UTC (rev 377) @@ -12,137 +12,16 @@ limitations under the License. */ -using System.Collections.Generic; -using System.Xml; using NUnit.Framework; -using net.sf.xmlunit.builder; -using net.sf.xmlunit.exceptions; namespace net.sf.xmlunit.xpath { [TestFixture] - public class XPathEngineTest { - - private ISource source; - - [SetUp] public void ReadSource() { - source = Input.FromFile(TestResources.BLAME_FILE).Build(); - } - - [Test] public void SelectNodesWithNoMatches() { - IEnumerable<XmlNode> i = new XPathEngine().SelectNodes("foo", - source); - Assert.IsNotNull(i); - Assert.IsFalse(i.GetEnumerator().MoveNext()); - } - - [Test] public void SelectNodesWithSingleMatch() { - IEnumerable<XmlNode> i = new XPathEngine().SelectNodes("//ul", - source); - Assert.IsNotNull(i); - IEnumerator<XmlNode> it = i.GetEnumerator(); - Assert.IsTrue(it.MoveNext()); - Assert.AreEqual("ul", it.Current.Name); - Assert.IsFalse(it.MoveNext()); - } - - [Test] public void SelectNodesWithMultipleMatchs() { - IEnumerable<XmlNode> i = new XPathEngine().SelectNodes("//li", - source); - Assert.IsNotNull(i); - int count = 0; - foreach (XmlNode n in i) { - count++; - Assert.AreEqual("li", n.Name); + public class XPathEngineTest : AbstractXPathEngineTest { + protected override IXPathEngine Engine { + get { + return new XPathEngine(); } - Assert.AreEqual(4, count); } - - [Test] - public void SelectNodesWithInvalidXPath() { - try { - new XPathEngine().SelectNodes("//li[", source); - Assert.Fail("expected an exception"); - } catch (XMLUnitException) { - // expected - } - } - - [Test] public void EvaluateWithNoMatches() { - Assert.AreEqual(string.Empty, new XPathEngine().Evaluate("foo", - source)); - } - - [Test] public void EvaluateWithSingleMatch() { - Assert.AreEqual("Don't blame it on the...", - new XPathEngine().Evaluate("//title", source)); - } - - [Test] public void EvaluateWithSingleMatchTextSelector() { - Assert.AreEqual("Don't blame it on the...", - new XPathEngine().Evaluate("//title/text()", - source)); - } - - [Test] public void EvaluateWithMultipleMatches() { - Assert.AreEqual("sunshine", - new XPathEngine().Evaluate("//li", source)); - } - - [Test] - public void EvaluateWithInvalidXPath() { - try { - new XPathEngine().Evaluate("//li[", source); - Assert.Fail("expected an exception"); - } catch (XMLUnitException) { - // expected - } - } - - [Test] public void SelectNodesWithNS() { - XPathEngine e = new XPathEngine(); - source = Input.FromMemory("<n:d xmlns:n='urn:test:1'><n:e/></n:d>") - .Build(); - Dictionary<string, string> m = new Dictionary<string, string>(); - m["x"] = "urn:test:1"; - e.NamespaceContext = m; - IEnumerable<XmlNode> it = e.SelectNodes("/x:d/x:e", source); - Assert.IsTrue(it.GetEnumerator().MoveNext()); - } - - [Test] public void SelectNodesWithDefaultNS() { - XPathEngine e = new XPathEngine(); - source = Input.FromMemory("<d xmlns='urn:test:1'><e/></d>") - .Build(); - Dictionary<string, string> m = new Dictionary<string, string>(); - m["x"] = "urn:test:1"; - e.NamespaceContext = m; - IEnumerable<XmlNode> it = e.SelectNodes("/x:d/x:e", source); - Assert.IsTrue(it.GetEnumerator().MoveNext()); - } - - // throws an exception "'/:d/:e' has an invalid token." - public void SelectNodesWithDefaultNSEmptyPrefix() { - XPathEngine e = new XPathEngine(); - source = Input.FromMemory("<d xmlns='urn:test:1'><e/></d>") - .Build(); - Dictionary<string, string> m = new Dictionary<string, string>(); - m[string.Empty] = "urn:test:1"; - e.NamespaceContext = m; - IEnumerable<XmlNode> it = e.SelectNodes("/:d/:e", source); - Assert.IsTrue(it.GetEnumerator().MoveNext()); - } - - // doesn't match - public void SelectNodesWithDefaultNSNoPrefix() { - XPathEngine e = new XPathEngine(); - source = Input.FromMemory("<d xmlns='urn:test:1'><e/></d>") - .Build(); - Dictionary<string, string> m = new Dictionary<string, string>(); - m[string.Empty] = "urn:test:1"; - e.NamespaceContext = m; - IEnumerable<XmlNode> it = e.SelectNodes("/d/e", source); - Assert.IsTrue(it.GetEnumerator().MoveNext()); - } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |