|
|
@@ -11,8 +11,10 @@ import java.io.IOException;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import javax.xml.transform.TransformerConfigurationException;
|
|
|
+import net.ranides.assira.TestFiles;
|
|
|
import net.ranides.assira.collection.maps.MultiMap;
|
|
|
import static net.ranides.assira.junit.NewAssert.*;
|
|
|
+import static net.ranides.assira.xml.XMLQuery.$;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
/**
|
|
|
@@ -24,55 +26,61 @@ public class XMLQueryTest {
|
|
|
|
|
|
@Test
|
|
|
public void testRoot() throws IOException {
|
|
|
- XMLElement doc = XMLQuery.$(new File("src/test/resources/eclipse/artifacts.xml"));
|
|
|
+ XMLElement doc = $(TestFiles.XML_INPUT);
|
|
|
|
|
|
- List<String> root = Arrays.asList("artifactRepository", "configuration", "repository");
|
|
|
- List<String> body = Arrays.asList("#text", "properties", "#text", "mappings", "#text", "artifacts", "#text");
|
|
|
- List<String> items = Arrays.asList("properties", "mappings", "artifacts");
|
|
|
+ List<String> root = Arrays.asList("info", "info", "params", "#comment", "ccms");
|
|
|
+ List<String> body = Arrays.asList("#text", "users", "#text", "data", "#text", "configuration", "#text");
|
|
|
+ List<String> items = Arrays.asList("users", "data", "configuration");
|
|
|
|
|
|
- assertEquals(root.subList(0, 3), doc.children().names());
|
|
|
- assertEquals(root.subList(0, 2), doc.children(XMLType.PROCESSING).names());
|
|
|
- assertEquals(root.subList(2, 3), doc.children(XMLType.ELEMENT).names());
|
|
|
+ assertEquals(root.subList(0, 5), doc.children().names());
|
|
|
+ assertEquals(root.subList(0, 3), doc.children(XMLType.PROCESSING).names());
|
|
|
+ assertEquals(root.subList(4, 5), doc.children(XMLType.ELEMENT).names());
|
|
|
+ assertEquals(" @author Ranides Atterwim <ranides@gmail.com> ", doc.children(XMLType.COMMENT).text());
|
|
|
|
|
|
assertEquals(body, doc.root().children().names());
|
|
|
assertEquals(items, doc.root().children(XMLType.ELEMENT).names());
|
|
|
+ assertEquals(items, doc.normalize().root().children().names());
|
|
|
|
|
|
- assertEquals("repository", doc.root().name());
|
|
|
+ assertEquals("ccms", doc.root().name());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testFindStar() throws IOException {
|
|
|
- XMLElement doc = XMLQuery.$(new File("src/test/resources/eclipse/artifacts.xml"));
|
|
|
+ XMLElement doc = $(TestFiles.XML_INPUT);
|
|
|
|
|
|
- assertEquals(3395, doc.find("*").size());
|
|
|
- assertEquals(3394, doc.root().find("*").size());
|
|
|
+ assertEquals(39, doc.find("*").size());
|
|
|
+ assertEquals(38, doc.root().find("*").size());
|
|
|
|
|
|
- assertEquals(2378, doc.find("artifact *").size());
|
|
|
- assertEquals(2378, doc.find("artifact").find("*").size());
|
|
|
- assertEquals(4, doc.find("? //artifact[1]").find("*").size());
|
|
|
+ assertEquals(27, doc.find("users *").size());
|
|
|
+ assertEquals(27, doc.find("users").find("*").size());
|
|
|
+ assertEquals(Arrays.asList("login", "password", "name", "mail", "mail"), doc.find("? //user[1]").find("*").names());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testXPath() throws IOException {
|
|
|
- XMLElement doc = XMLQuery.$(new File("src/test/resources/eclipse/artifacts.xml"));
|
|
|
+ XMLElement doc = $(TestFiles.XML_INPUT);
|
|
|
|
|
|
- List<String> exp1 = Arrays.asList("plugins.jar", "binary.jar", "features.jar");
|
|
|
-
|
|
|
- assertEquals(exp1, doc.find("? //mappings/rule/@output").texts());
|
|
|
- assertEquals(exp1, doc.find("? /repository/mappings/rule/@output").texts());
|
|
|
+ List<String> exp1 = Arrays.asList("101", "102", "103", "104", "105");
|
|
|
+ List<String> exp2 = Arrays.asList();
|
|
|
+
|
|
|
+ assertEquals(exp1, doc.find("? //users/user/@id").texts());
|
|
|
+ assertEquals(exp2, doc.find("? /users/user/@id").texts());
|
|
|
+ assertEquals(exp2, doc.find("? users/user/@id").texts());
|
|
|
+ assertEquals(exp1, doc.find("? ccms/users/user/@id").texts());
|
|
|
+ assertEquals(exp1, doc.find("? /ccms/users/user/@id").texts());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testCSS() throws IOException {
|
|
|
- XMLElement doc = XMLQuery.$(new File("src/test/resources/eclipse/artifacts.xml"));
|
|
|
+ XMLElement doc = $(TestFiles.XML_INPUT);
|
|
|
|
|
|
- XMLElements css = doc.find("artifact property[name='download.size']").attrs("value");
|
|
|
-
|
|
|
- assertEquals(1008, css.size());
|
|
|
+ XMLElements css = doc.find("users user[active='true']").attrs("id");
|
|
|
+
|
|
|
+ assertEquals(Arrays.asList("101", "102", "105"), css.texts());
|
|
|
|
|
|
- List<Integer> numbers = Arrays.asList(26817, 159306, 48552, 48832, 731290, 12166, 18032, 150535);
|
|
|
+ List<Integer> numbers = Arrays.asList(101, 105);
|
|
|
|
|
|
- assertEquals(numbers, css.stream(this::$int).filter(v -> 0 == v%7).limit(8).list());
|
|
|
+ assertEquals(numbers, css.stream(this::$int).filter(v -> 1 == v%2).list());
|
|
|
}
|
|
|
|
|
|
private int $int(XMLElement n) throws NumberFormatException {
|
|
|
@@ -81,38 +89,52 @@ public class XMLQueryTest {
|
|
|
|
|
|
@Test
|
|
|
public void testChildren() throws IOException {
|
|
|
- XMLElement doc = XMLQuery.$(new File("src/test/resources/eclipse/artifacts.xml"));
|
|
|
+ XMLElement doc = $(TestFiles.XML_INPUT);
|
|
|
|
|
|
assertFalse(doc.children("xxx").matches());
|
|
|
- assertFalse(doc.children("mappings").matches());
|
|
|
- assertFalse(doc.children(" mappings").matches());
|
|
|
- assertFalse(doc.children("mappings rule").matches());
|
|
|
+ assertFalse(doc.children("users").matches());
|
|
|
+ assertFalse(doc.children(" users").matches());
|
|
|
+ assertFalse(doc.children("users user").matches());
|
|
|
+
|
|
|
+ assertFalse(doc.root().children("xxx").matches());
|
|
|
+ assertTrue(doc.root().children("users").matches());
|
|
|
+ assertTrue(doc.root().children(" users").matches());
|
|
|
+ assertFalse(doc.root().children("users user").matches());
|
|
|
+
|
|
|
+ assertEquals(0, doc.root().children("xxx").size());
|
|
|
+ assertEquals(1, doc.root().children("users").size());
|
|
|
+ assertEquals(1, doc.root().children(" users").size());
|
|
|
+ assertEquals(0, doc.root().children("users user").size());
|
|
|
|
|
|
assertEquals(0, doc.find("xxx").size());
|
|
|
- assertEquals(1, doc.find("mappings").size());
|
|
|
- assertEquals(1, doc.find(" mappings").size());
|
|
|
- assertEquals(3, doc.find("mappings rule").size());
|
|
|
+ assertEquals(1, doc.find("users").size());
|
|
|
+ assertEquals(1, doc.find(" users").size());
|
|
|
+ assertEquals(5, doc.find("users user").size());
|
|
|
}
|
|
|
|
|
|
|
|
|
@Test
|
|
|
public void testAttrMap() throws IOException {
|
|
|
- XMLElement doc = XMLQuery.$(new File("src/test/resources/eclipse/artifacts.xml"));
|
|
|
-
|
|
|
- assertTrue(doc.children("mappings rule").attrs().map().isEmpty());
|
|
|
+ XMLElement doc = $(TestFiles.XML_INPUT);
|
|
|
|
|
|
- MultiMap<String, String> attr = doc.find("mappings rule").attrs().map();
|
|
|
+ assertTrue(doc.children("xxx").attrs().map().isEmpty());
|
|
|
+ assertTrue(doc.find("users item").attrs().map().isEmpty());
|
|
|
+ assertTrue(doc.find("users user login").attrs().map().isEmpty());
|
|
|
|
|
|
- List<String> exp1 = Arrays.asList("A","B","C");
|
|
|
- List<String> exp2 = Arrays.asList("plugins.jar", "binary.jar", "features.jar");
|
|
|
-
|
|
|
- assertEquivalent(exp1, attr.getAll("filter"));
|
|
|
- assertEquivalent(exp2, attr.getAll("output"));
|
|
|
+// assertTrue(doc.children("mappings rule").attrs().map().isEmpty());
|
|
|
+//
|
|
|
+// MultiMap<String, String> attr = doc.find("mappings rule").attrs().map();
|
|
|
+//
|
|
|
+// List<String> exp1 = Arrays.asList("A","B","C");
|
|
|
+// List<String> exp2 = Arrays.asList("plugins.jar", "binary.jar", "features.jar");
|
|
|
+//
|
|
|
+// assertEquivalent(exp1, attr.getAll("filter"));
|
|
|
+// assertEquivalent(exp2, attr.getAll("output"));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testAttributes() throws IOException, TransformerConfigurationException {
|
|
|
- XMLElement doc = XMLQuery.$(new File("src/test/resources/eclipse/artifacts.xml"));
|
|
|
+ XMLElement doc = $(new File("src/test/resources/eclipse/artifacts.xml"));
|
|
|
|
|
|
XMLElement attr1 = doc.find("repository mappings rule").attrs("output").first();
|
|
|
XMLElement attr2 = doc.find("repository mappings rule").attrs().first("output");
|