001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tests.shared;
034:
035: import com.flexive.shared.XPathElement;
036: import com.flexive.shared.content.FxPK;
037: import com.flexive.shared.exceptions.FxInvalidParameterException;
038: import org.testng.Assert;
039: import org.testng.annotations.Test;
040:
041: import java.util.List;
042:
043: /**
044: * XPathElement test
045: *
046: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
047: */
048: @Test(groups={"shared"})
049: public class XPathElementTest {
050:
051: @Test
052: public void toElement() throws FxInvalidParameterException {
053: XPathElement xpe = XPathElement.toElement("/Test/A/B", "B[3]");
054: assert ("B".equals(xpe.getAlias()) && xpe.getIndex() == 3) : "expected B[3]";
055: xpe = XPathElement.toElement("/Test/A/B", "B");
056: assert ("B".equals(xpe.getAlias()) && xpe.getIndex() == 1) : "expected B[1]";
057: }
058:
059: @Test
060: public void lastElement() throws FxInvalidParameterException {
061: assert "C[1]".equals(XPathElement.lastElement("/A/B/C")
062: .toString());
063: assert "C[123]".equals(XPathElement.lastElement("/A/B/C[123]")
064: .toString());
065: assert "C[2]".equals(XPathElement.lastElement("/C[2]")
066: .toString());
067: try {
068: XPathElement.lastElement("/A/B/C[123]s");
069: assert false : "expected exception";
070: } catch (FxInvalidParameterException e) {
071: //ok
072: }
073: try {
074: XPathElement.lastElement("A/B/C[123]");
075: assert false : "expected exception";
076: } catch (FxInvalidParameterException e) {
077: //ok
078: }
079: try {
080: XPathElement.lastElement("/A/B/C[a]");
081: assert false : "expected exception";
082: } catch (FxInvalidParameterException e) {
083: //ok
084: }
085: }
086:
087: @Test
088: public void isValid() {
089: String[] validPatterns = { "/A", "/A[1]", "/A[1]/B[1]/C[1]",
090: "/AA[1]/B", "A[@pk=123.46]/A/BC", "A[@pk=NEW]/A",
091: "A[@pk=5.LIVE]/A", "A[@pk=7.MAX]/A", };
092: String[] invalidPatterns = { "/A/", "/A[a]", "A/B[1]",
093: "/AA[1]/B/", "A[@pk=NEW.42]/A", "A[@pk=LIVE]/A",
094: "A[@pk=]/A", "A[@pk]/A", "A[]/A", };
095: for (String valid : validPatterns)
096: assert XPathElement.isValidXPath(valid) : "Pattern "
097: + valid + " was expected to be valid!";
098: for (String valid : invalidPatterns)
099: assert !XPathElement.isValidXPath(valid) : "Pattern "
100: + valid + " was expected to be invalid!";
101: }
102:
103: @Test
104: public void split() throws FxInvalidParameterException {
105: String[][] validPatterns = {
106: { "/A[1]/B[2]/C[1]", "/A/B[2]/C" },
107: { "/A[1]/B[1]", "/A/B" },
108: { "/A[1]/B[1]/C[3]", "/A/B/C[3]" }, { "/A[1]", "/A" } };
109: for (String[] pattern : validPatterns)
110: assert pattern[0].equals(XPathElement.toXPath(XPathElement
111: .split(pattern[1])));
112: }
113:
114: @Test
115: public void cardinalities() throws FxInvalidParameterException {
116: int[] card = XPathElement.getIndices("/A/B[2]/C[3]/D");
117: assert card.length == 4 && card[0] == 1 && card[1] == 2
118: && card[2] == 3 && card[3] == 1 : "multiplicity didn't match!";
119: assert "/A[1]/B[2]/C[1]".equals(XPathElement
120: .toXPathMult("/A/B[2]/C"));
121: assert "/A/B/C".equals(XPathElement
122: .toXPathNoMult("/A[2]/B/C[1234]"));
123: assert "XY/A/B/C".equals(XPathElement
124: .toXPathNoMult("XY[@pk=NEW]/A[2]/B/C[1234]")) : "Expected XY/A/B/C but got "
125: + XPathElement
126: .toXPathNoMult("XY[@pk=NEW]/A[2]/B/C[1234]");
127: List<XPathElement> xp = XPathElement.split("/A/B[1]/C/D[2]");
128: assert !xp.get(0).isIndexDefined();
129: assert xp.get(1).isIndexDefined();
130: assert !xp.get(2).isIndexDefined();
131: assert xp.get(3).isIndexDefined();
132: }
133:
134: @Test
135: public void buildXPath() {
136: String[][][] testsNoSlash = {
137: { { "12/23" }, { null, "12", "23" } },
138: { { "ROOT/A/B/C" }, { "/ROOT", "/A", "B/", "//C/" } },
139: { { "A/B/C" }, { null, "A", "B", "/C" } } };
140: String[][][] testsSlash = {
141: { { "/12/23" }, { null, "12", "23" } },
142: { { "/ROOT/A/B/C" }, { "/ROOT", "/A", "B/", "//C/" } },
143: { { "/A/B/C" }, { null, "A", null, "B", "/C" } } };
144: for (String[][] test : testsNoSlash)
145: assert test[0][0].equals(XPathElement.buildXPath(false,
146: test[1]));
147: for (String[][] test : testsSlash)
148: assert test[0][0].equals(XPathElement.buildXPath(true,
149: test[1]));
150: }
151:
152: @Test
153: public void xpathPk() {
154: Assert.assertEquals(XPathElement
155: .getPK("TYPE[@pk=5.1]/property"), new FxPK(5, 1));
156: Assert.assertEquals(XPathElement
157: .getPK("TYPE[@pk=NEW]/property"), new FxPK());
158: Assert.assertEquals(XPathElement
159: .getPK("TYPE[@pk=5.LIVE]/property"), new FxPK(5,
160: FxPK.LIVE));
161: Assert.assertEquals(XPathElement
162: .getPK("TYPE[@pk=5.MAX]/property"), new FxPK(5,
163: FxPK.MAX));
164: }
165:
166: }
|