|
20 | 20 | */ |
21 | 21 | package org.exist.xquery; |
22 | 22 |
|
| 23 | +import org.exist.xquery.functions.array.ArrayType; |
| 24 | +import org.exist.xquery.value.ArrayWrapper; |
| 25 | +import org.exist.xquery.value.Item; |
23 | 26 | import org.exist.xquery.value.Sequence; |
24 | 27 | import org.exist.xquery.value.Type; |
25 | 28 | import org.junit.jupiter.api.Test; |
26 | 29 |
|
27 | 30 | import java.util.Arrays; |
28 | 31 | import java.util.List; |
29 | 32 |
|
| 33 | +import static org.easymock.EasyMock.expect; |
30 | 34 | import static org.easymock.EasyMock.mock; |
31 | 35 | import static org.easymock.EasyMock.replay; |
32 | 36 | import static org.easymock.EasyMock.verify; |
| 37 | +import static org.junit.Assert.assertTrue; |
33 | 38 | import static org.junit.jupiter.api.Assertions.assertEquals; |
34 | 39 |
|
35 | 40 | public class XPathUtilTest { |
@@ -108,4 +113,95 @@ public void arrayArrayStringsToSequenceStrings() throws XPathException { |
108 | 113 | } |
109 | 114 | verify(mockContext); |
110 | 115 | } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void arrayWrapperStringsToArrayStrings() throws XPathException { |
| 119 | + final XQueryContext mockContext = mock(XQueryContext.class); |
| 120 | + expect(mockContext.nextExpressionId()).andReturn(1).anyTimes(); |
| 121 | + |
| 122 | + final ArrayWrapper strings = new ArrayWrapper(new String[] { "hello", "goodbye" }); |
| 123 | + |
| 124 | + replay(mockContext); |
| 125 | + final Sequence result = XPathUtil.javaObjectToXPath(strings, mockContext); |
| 126 | + assertEquals(1, result.getItemCount()); |
| 127 | + final Item resultItem = result.itemAt(0); |
| 128 | + assertEquals(Type.ARRAY_ITEM, resultItem.getType()); |
| 129 | + final ArrayType resultArray = (ArrayType) resultItem; |
| 130 | + for (int i = 0; i < strings.array.length; i++) { |
| 131 | + final Sequence arrayItem = resultArray.get(i); |
| 132 | + assertTrue(arrayItem.hasOne()); |
| 133 | + assertEquals(Type.STRING, arrayItem.itemAt(0).getType()); |
| 134 | + assertEquals(strings.array[i], arrayItem.itemAt(0).getStringValue()); |
| 135 | + } |
| 136 | + verify(mockContext); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void arrayArrayWrapperStringsToArrayStrings() throws XPathException { |
| 141 | + final XQueryContext mockContext = mock(XQueryContext.class); |
| 142 | + expect(mockContext.nextExpressionId()).andReturn(1).anyTimes(); |
| 143 | + |
| 144 | + final ArrayWrapper[] strings = { |
| 145 | + new ArrayWrapper(new String[] { "hello" }), |
| 146 | + new ArrayWrapper(new String[] { "goodbye", "see you again soon" }), |
| 147 | + }; |
| 148 | + |
| 149 | + replay(mockContext); |
| 150 | + final Sequence result = XPathUtil.javaObjectToXPath(strings, mockContext); |
| 151 | + assertEquals(strings.length, result.getItemCount()); |
| 152 | + for (int i = 0; i < strings.length; i++) { |
| 153 | + final Item resultItem = result.itemAt(i); |
| 154 | + assertEquals(Type.ARRAY_ITEM, resultItem.getType()); |
| 155 | + |
| 156 | + final ArrayType resultArray = (ArrayType) resultItem; |
| 157 | + for (int j = 0; j < strings[i].array.length; j++) { |
| 158 | + final Sequence arrayItem = resultArray.get(j); |
| 159 | + assertTrue(arrayItem.hasOne()); |
| 160 | + assertEquals(Type.STRING, arrayItem.itemAt(0).getType()); |
| 161 | + assertEquals(strings[i].array[j], arrayItem.itemAt(0).getStringValue()); |
| 162 | + } |
| 163 | + } |
| 164 | + verify(mockContext); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void arrayArrayWrapperStringSequenceToArrayStrings() throws XPathException { |
| 169 | + final XQueryContext mockContext = mock(XQueryContext.class); |
| 170 | + expect(mockContext.nextExpressionId()).andReturn(1).anyTimes(); |
| 171 | + |
| 172 | + final ArrayWrapper[] strings = { |
| 173 | + new ArrayWrapper(new String[] { "hello" }), |
| 174 | + new ArrayWrapper(new Object[] { "goodbye", new String[] { "see you again soon", "42" } }), |
| 175 | + }; |
| 176 | + |
| 177 | + replay(mockContext); |
| 178 | + |
| 179 | + final Sequence result = XPathUtil.javaObjectToXPath(strings, mockContext); |
| 180 | + assertEquals(strings.length, result.getItemCount()); |
| 181 | + Item resultItem = result.itemAt(0); |
| 182 | + assertEquals(Type.ARRAY_ITEM, resultItem.getType()); |
| 183 | + ArrayType resultArray = (ArrayType) resultItem; |
| 184 | + assertEquals(1, resultArray.getSize()); |
| 185 | + Sequence arrayEntry = resultArray.get(0); |
| 186 | + assertEquals(1, arrayEntry.getItemCount()); |
| 187 | + assertEquals(Type.STRING, arrayEntry.itemAt(0).getType()); |
| 188 | + assertEquals("hello", arrayEntry.itemAt(0).getStringValue()); |
| 189 | + |
| 190 | + resultItem = result.itemAt(1); |
| 191 | + assertEquals(Type.ARRAY_ITEM, resultItem.getType()); |
| 192 | + resultArray = (ArrayType) resultItem; |
| 193 | + assertEquals(2, resultArray.getSize()); |
| 194 | + arrayEntry = resultArray.get(0); |
| 195 | + assertEquals(1, arrayEntry.getItemCount()); |
| 196 | + assertEquals(Type.STRING, arrayEntry.itemAt(0).getType()); |
| 197 | + assertEquals("goodbye", arrayEntry.itemAt(0).getStringValue()); |
| 198 | + arrayEntry = resultArray.get(1); |
| 199 | + assertEquals(2, arrayEntry.getItemCount()); |
| 200 | + assertEquals(Type.STRING, arrayEntry.itemAt(0).getType()); |
| 201 | + assertEquals("see you again soon", arrayEntry.itemAt(0).getStringValue()); |
| 202 | + assertEquals(Type.STRING, arrayEntry.itemAt(1).getType()); |
| 203 | + assertEquals("42", arrayEntry.itemAt(1).getStringValue()); |
| 204 | + |
| 205 | + verify(mockContext); |
| 206 | + } |
111 | 207 | } |
0 commit comments