1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
|
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QAbstractMessageHandler>
#include <QFileInfo>
#include <QNetworkReply>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <QXmlFormatter>
#include <QXmlItem>
#include <QXmlName>
#include <QXmlQuery>
#include <QXmlResultItems>
#include <QXmlSerializer>
#include "MessageSilencer.h"
#include "MessageValidator.h"
#include "NetworkOverrider.h"
#include "PushBaseliner.h"
#include "../qabstracturiresolver/TestURIResolver.h"
#include "../qsimplexmlnodemodel/TestSimpleNodeModel.h"
#include "TestFundament.h"
#include "../network-settings.h"
/*!
\class tst_QXmlQuery
\internal
\since 4.4
\brief Tests class QXmlQuery.
This test is not intended for testing the engine, but the functionality specific
to the QXmlQuery class.
In other words, if you have an engine bug; don't add it here because it won't be
tested properly. Instead add it to the test suite.
*/
class tst_QXmlQuery : public QObject
, private TestFundament
{
Q_OBJECT
public:
inline tst_QXmlQuery() : m_generatedBaselines(0)
, m_pushTestsCount(0)
, m_testNetwork(true)
{
}
private Q_SLOTS:
void initTestCase();
void defaultConstructor() const;
void copyConstructor() const;
void constructorQXmlNamePool() const;
void constructorQXmlNamePoolQueryLanguage() const;
void constructorQXmlNamePoolWithinQSimpleXmlNodeModel() const;
void assignmentOperator() const;
void isValid() const;
void sequentialExecution() const;
void bindVariableQString() const;
void bindVariableQStringNoExternalDeclaration() const;
void bindVariableQXmlName() const;
void bindVariableQXmlNameTriggerWarnings() const;
void bindVariableQStringQIODevice() const;
void bindVariableQStringQIODeviceWithByteArray() const;
void bindVariableQStringQIODeviceWithString() const;
void bindVariableQStringQIODeviceWithQFile() const;
void bindVariableQXmlNameQIODevice() const;
void bindVariableQXmlNameQIODeviceTriggerWarnings() const;
void bindVariableXSLTSuccess() const;
void bindVariableTemporaryNode() const;
void setMessageHandler() const;
void messageHandler() const;
void evaluateToQAbstractXmlReceiverTriggerWarnings() const;
void evaluateToQXmlResultItems() const;
void evaluateToQXmlResultItemsTriggerWarnings() const;
void evaluateToQXmlResultItemsErrorAtEnd() const;
void evaluateToReceiver();
void evaluateToReceiver_data() const;
void evaluateToReceiverOnInvalidQuery() const;
void evaluateToQStringTriggerError() const;
void evaluateToQString() const;
void evaluateToQString_data() const;
void evaluateToQStringSignature() const;
void checkGeneratedBaselines() const;
void basicXQueryToQtTypeCheck() const;
void basicQtToXQueryTypeCheck() const;
void bindNode() const;
void relativeBaseURI() const;
void emptyBaseURI() const;
void roundTripDateWithinQXmlItem() const;
void bindingMissing() const;
void bindDefaultConstructedItem() const;
void bindDefaultConstructedItem_data() const;
void bindEmptyNullString() const;
void bindEmptyString() const;
void rebindVariableSameType() const;
void rebindVariableDifferentType() const;
void rebindVariableWithNullItem() const;
void eraseQXmlItemBinding() const;
void eraseDeviceBinding() const;
void constCorrectness() const;
void objectSize() const;
void setUriResolver() const;
void uriResolver() const;
void messageXML() const;
void resultItemsDeallocatedQuery() const;
void copyCheckMessageHandler() const;
void shadowedVariables() const;
void setFocusQXmlItem() const;
void setFocusQUrl() const;
void setFocusQIODevice() const;
void setFocusQIODeviceAvoidVariableClash() const;
void setFocusQIODeviceFailure() const;
void setFocusQIODeviceTriggerWarnings() const;
void setFocusQString() const;
void setFocusQStringFailure() const;
void setFocusQStringSignature() const;
void recompilationWithEvaluateToResultFailing() const;
void secondEvaluationWithEvaluateToResultFailing() const;
void recompilationWithEvaluateToReceiver() const;
void fnDocOnQIODeviceTimeout() const;
void evaluateToQStringListOnInvalidQuery() const;
void evaluateToQStringList() const;
void evaluateToQStringListTriggerWarnings() const;
void evaluateToQStringList_data() const;
void evaluateToQStringListNoConversion() const;
void evaluateToQIODevice() const;
void evaluateToQIODeviceTriggerWarnings() const;
void evaluateToQIODeviceSignature() const;
void evaluateToQIODeviceOnInvalidQuery() const;
void setQueryQIODeviceQUrl() const;
void setQueryQIODeviceQUrlTriggerWarnings() const;
void setQueryQString() const;
void setQueryQUrlSuccess() const;
void setQueryQUrlSuccess_data() const;
void setQueryQUrlFailSucceed() const;
void setQueryQUrlFailure() const;
void setQueryQUrlFailure_data() const;
void setQueryQUrlBaseURI() const;
void setQueryQUrlBaseURI_data() const;
void setQueryWithNonExistentQUrlOnValidQuery() const;
void setQueryWithInvalidQueryFromQUrlOnValidQuery() const;
void retrieveNameFromQuery() const;
void retrieveNameFromQuery_data() const;
void cleanupTestCase() const;
void declareUnavailableExternal() const;
void msvcCacheIssue() const;
void unavailableExternalVariable() const;
void useUriResolver() const;
void queryWithFocusAndVariable() const;
void undefinedFocus() const;
void basicFocusUsage() const;
void queryLanguage() const;
void queryLanguageSignature() const;
void enumQueryLanguage() const;
void setNetworkAccessManager() const;
void networkAccessManagerSignature() const;
void networkAccessManagerDefaultValue() const;
void networkAccessManager() const;
void setInitialTemplateNameQXmlName() const;
void setInitialTemplateNameQXmlNameSignature() const;
void setInitialTemplateNameQString() const;
void setInitialTemplateNameQStringSignature() const;
void initialTemplateName() const;
void initialTemplateNameSignature() const;
void fnDocNetworkAccessSuccess() const;
void fnDocNetworkAccessSuccess_data() const;
void fnDocNetworkAccessFailure() const;
void fnDocNetworkAccessFailure_data() const;
void multipleDocsAndFocus() const;
void multipleEvaluationsWithDifferentFocus() const;
void bindVariableQXmlQuery() const;
void bindVariableQXmlQuery_data() const;
void bindVariableQStringQXmlQuerySignature() const;
void bindVariableQXmlNameQXmlQuerySignature() const;
void bindVariableQXmlNameQXmlQuery() const;
void bindVariableQXmlQueryInvalidate() const;
void unknownSourceLocation() const;
void identityConstraintSuccess() const;
void identityConstraintFailure() const;
void identityConstraintFailure_data() const;
// TODO call all URI resolving functions where 1) the URI resolver return a null QUrl(); 2) resolves into valid, existing URI, 3) invalid, non-existing URI.
// TODO bind stringlists, variant lists, both ways.
// TODO trigger serialization error, or any error in evaluateToushCallback().
// TODO let items travle between two queries, as seen in the SDK
// TODO what happens if the query declares local variable and external ones are provided?
private:
enum
{
/**
* One excluded, since we skip static-base-uri.xq.
*/
ExpectedQueryCount = 29
};
static void checkBaseURI(const QUrl &baseURI, const QString &candidate);
static QStringList queries();
static const QString m_xmlPatternsDir;
int m_generatedBaselines;
int m_pushTestsCount;
const bool m_testNetwork;
};
const QString tst_QXmlQuery::m_xmlPatternsDir = QFINDTESTDATA("../xmlpatterns");
void tst_QXmlQuery::initTestCase()
{
QVERIFY2(!m_xmlPatternsDir.isEmpty(), qPrintable(QString::fromLatin1("Cannot locate '../xmlpatterns' starting from %1").arg(QDir::currentPath())));
QVERIFY(QtNetworkSettings::verifyTestNetworkSettings());
}
void tst_QXmlQuery::checkBaseURI(const QUrl &baseURI, const QString &candidate)
{
/* The use of QFileInfo::canonicalFilePath() takes into account that drive letters
* on Windows may have different cases. */
QVERIFY(QDir(baseURI.toLocalFile()).relativeFilePath(QFileInfo(candidate).canonicalFilePath()).startsWith("../"));
}
QStringList tst_QXmlQuery::queries()
{
QDir dir;
dir.cd(inputFile(m_xmlPatternsDir + QLatin1String("/queries/")));
return dir.entryList(QStringList(QLatin1String("*.xq")));
}
void tst_QXmlQuery::defaultConstructor() const
{
/* Allocate instance in different orders. */
{
QXmlQuery query;
}
{
QXmlQuery query1;
QXmlQuery query2;
}
{
QXmlQuery query1;
QXmlQuery query2;
QXmlQuery query3;
}
}
void tst_QXmlQuery::copyConstructor() const
{
/* Verify that we can take a const reference, and simply do a copy of a default constructed object. */
{
const QXmlQuery query1;
QXmlQuery query2(query1);
}
/* Copy twice. */
{
const QXmlQuery query1;
QXmlQuery query2(query1);
QXmlQuery query3(query2);
}
/* Verify that copying default values works. */
{
const QXmlQuery query1;
const QXmlQuery query2(query1);
QCOMPARE(query2.messageHandler(), query1.messageHandler());
QCOMPARE(query2.uriResolver(), query1.uriResolver());
QCOMPARE(query2.queryLanguage(), query1.queryLanguage());
QCOMPARE(query2.initialTemplateName(), query1.initialTemplateName());
QCOMPARE(query2.networkAccessManager(), query1.networkAccessManager());
}
/* Check that the
*
* - name pool
* - URI resolver
* - message handler
* - query language
* - initial template name
*
* sticks with the copy. */
{
MessageSilencer silencer;
TestURIResolver resolver;
QNetworkAccessManager networkManager;
QXmlQuery query1(QXmlQuery::XSLT20);
QXmlNamePool np1(query1.namePool());
query1.setMessageHandler(&silencer);
query1.setUriResolver(&resolver);
query1.setNetworkAccessManager(&networkManager);
const QXmlName name(np1, QLatin1String("localName"),
QLatin1String("http://example.com/"),
QLatin1String("prefix"));
query1.setInitialTemplateName(name);
const QXmlQuery query2(query1);
QCOMPARE(query2.messageHandler(), static_cast<QAbstractMessageHandler *>(&silencer));
QCOMPARE(query2.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver));
QCOMPARE(query2.queryLanguage(), QXmlQuery::XSLT20);
QCOMPARE(query2.initialTemplateName(), name);
QCOMPARE(query2.networkAccessManager(), &networkManager);
QXmlNamePool np2(query2.namePool());
QCOMPARE(name.namespaceUri(np2), QString::fromLatin1("http://example.com/"));
QCOMPARE(name.localName(np2), QString::fromLatin1("localName"));
QCOMPARE(name.prefix(np2), QString::fromLatin1("prefix"));
}
{
QXmlQuery original;
original.setFocus(QXmlItem(4));
original.setQuery(QLatin1String("."));
QVERIFY(original.isValid());
const QXmlQuery copy(original);
QXmlResultItems result;
copy.evaluateTo(&result);
QCOMPARE(result.next().toAtomicValue(), QVariant(4));
QVERIFY(result.next().isNull());
QVERIFY(!result.hasError());
}
/* Copy, set, compare. Check that copies are independent. */
{
// TODO all members except queryLanguage().
}
}
void tst_QXmlQuery::constructorQXmlNamePool() const
{
/* Check that the namepool we are passed, is actually used. */
QXmlNamePool np;
QXmlQuery query(np);
const QXmlName name(np, QLatin1String("localName"),
QLatin1String("http://example.com/"),
QLatin1String("prefix"));
QXmlNamePool np2(query.namePool());
QCOMPARE(name.namespaceUri(np2), QString::fromLatin1("http://example.com/"));
QCOMPARE(name.localName(np2), QString::fromLatin1("localName"));
QCOMPARE(name.prefix(np2), QString::fromLatin1("prefix"));
}
/*!
Ensure that the internal variable loading mechanisms uses the user-supplied
name pool.
If that is not the case, different name pools are used and the code crashes.
\since 4.5
*/
void tst_QXmlQuery::constructorQXmlNamePoolQueryLanguage() const
{
QXmlNamePool np;
QXmlName name(np, QLatin1String("arbitraryName"));
QXmlQuery query(QXmlQuery::XQuery10, np);
QBuffer input;
input.setData("<yall/>");
QVERIFY(input.open(QIODevice::ReadOnly));
query.bindVariable(name, &input);
query.setQuery("string(doc($arbitraryName))");
QStringList result;
query.evaluateTo(&result);
}
void tst_QXmlQuery::constructorQXmlNamePoolWithinQSimpleXmlNodeModel() const
{
class TestCTOR : public TestSimpleNodeModel
{
public:
TestCTOR(const QXmlNamePool &np) : TestSimpleNodeModel(np)
{
}
void checkCTOR() const
{
/* If this fails to compile, the constructor has trouble with taking
* a mutable reference.
*
* The reason we use the this pointer explicitly, is to avoid a compiler
* warnings with MSVC 2005. */
QXmlQuery(this->namePool());
}
};
QXmlNamePool np;
TestCTOR ctor(np);
ctor.checkCTOR();
}
void tst_QXmlQuery::assignmentOperator() const
{
class ReturnURI : public QAbstractUriResolver
{
public:
ReturnURI() {}
virtual QUrl resolve(const QUrl &relative,
const QUrl &baseURI) const
{
return baseURI.resolved(relative);
}
};
/* Assign this to this. */
{
QXmlQuery query;
query = query;
query = query;
query = query;
/* Just call a couple of functions to give valgrind
* something to check. */
QVERIFY(!query.isValid());
query.messageHandler();
}
/* Assign null instances a couple of times. */
{
QXmlQuery query1;
QXmlQuery query2;
query1 = query2;
query1 = query2;
query1 = query2;
/* Just call a couple of functions to give valgrind
* something to check. */
QVERIFY(!query1.isValid());
query1.messageHandler();
/* Just call a couple of functions to give valgrind
* something to check. */
QVERIFY(!query2.isValid());
query2.messageHandler();
}
/* Create a query, set all the things it stores, and ensure it
* travels over to the new instance. */
{
MessageSilencer silencer;
const ReturnURI returnURI;
QXmlNamePool namePool;
QBuffer documentDevice;
documentDevice.setData(QByteArray("<e>a</e>"));
QVERIFY(documentDevice.open(QIODevice::ReadOnly));
QXmlQuery original(namePool);
QXmlName testName(namePool, QLatin1String("somethingToCheck"));
original.setMessageHandler(&silencer);
original.bindVariable(QLatin1String("var"), QXmlItem(1));
original.bindVariable(QLatin1String("device"), &documentDevice);
original.setUriResolver(&returnURI);
original.setFocus(QXmlItem(3));
original.setQuery(QLatin1String("$var, 1 + 1, ., string(doc($device))"));
/* Do a copy, and check that everything followed on into the copy. No modification
* of the copy. */
{
QXmlQuery copy;
/* We use assignment operator, not copy constructor. */
copy = original;
QVERIFY(copy.isValid());
QCOMPARE(copy.uriResolver(), static_cast<const QAbstractUriResolver *>(&returnURI));
QCOMPARE(copy.messageHandler(), static_cast<QAbstractMessageHandler *>(&silencer));
QCOMPARE(testName.localName(copy.namePool()), QString::fromLatin1("somethingToCheck"));
QXmlResultItems result;
copy.evaluateTo(&result);
QCOMPARE(result.next().toAtomicValue(), QVariant(1));
QCOMPARE(result.next().toAtomicValue(), QVariant(2));
QCOMPARE(result.next().toAtomicValue(), QVariant(3));
QCOMPARE(result.next().toAtomicValue(), QVariant(QString::fromLatin1("a")));
QVERIFY(result.next().isNull());
QVERIFY(!result.hasError());
}
/* Copy, and change values. Things should detach. */
{
/* Evaluate the copy. */
{
MessageSilencer secondSilencer;
const ReturnURI secondUriResolver;
QBuffer documentDeviceCopy;
documentDeviceCopy.setData(QByteArray("<e>b</e>"));
QVERIFY(documentDeviceCopy.open(QIODevice::ReadOnly));
QXmlQuery copy;
copy = original;
copy.setMessageHandler(&secondSilencer);
/* Here we rebind variable values. */
copy.bindVariable(QLatin1String("var"), QXmlItem(4));
copy.bindVariable(QLatin1String("device"), &documentDeviceCopy);
copy.setUriResolver(&secondUriResolver);
copy.setFocus(QXmlItem(6));
copy.setQuery(QLatin1String("$var, 1 + 1, ., string(doc($device))"));
/* Check that the copy picked up the new things. */
QVERIFY(copy.isValid());
QCOMPARE(copy.uriResolver(), static_cast<const QAbstractUriResolver *>(&secondUriResolver));
QCOMPARE(copy.messageHandler(), static_cast<QAbstractMessageHandler *>(&secondSilencer));
QXmlResultItems resultCopy;
copy.evaluateTo(&resultCopy);
QCOMPARE(resultCopy.next().toAtomicValue(), QVariant(4));
QCOMPARE(resultCopy.next().toAtomicValue(), QVariant(2));
QCOMPARE(resultCopy.next().toAtomicValue(), QVariant(6));
const QString stringedDevice(resultCopy.next().toAtomicValue().toString());
QCOMPARE(stringedDevice, QString::fromLatin1("b"));
QVERIFY(resultCopy.next().isNull());
QVERIFY(!resultCopy.hasError());
}
/* Evaluate the original. */
{
/* Check that the original is unchanged. */
QVERIFY(original.isValid());
QCOMPARE(original.uriResolver(), static_cast<const QAbstractUriResolver *>(&returnURI));
QCOMPARE(original.messageHandler(), static_cast<QAbstractMessageHandler *>(&silencer));
QXmlResultItems resultOriginal;
original.evaluateTo(&resultOriginal);
QCOMPARE(resultOriginal.next().toAtomicValue(), QVariant(1));
QCOMPARE(resultOriginal.next().toAtomicValue(), QVariant(2));
QCOMPARE(resultOriginal.next().toAtomicValue(), QVariant(3));
QCOMPARE(resultOriginal.next().toAtomicValue(), QVariant(QString::fromLatin1("a")));
QVERIFY(resultOriginal.next().isNull());
QVERIFY(!resultOriginal.hasError());
}
}
}
}
/*!
Since QXmlQuery doesn't seek devices to position 0, this code triggers a bug
where document caching doesn't work. Since the document caching doesn't work,
the device will be read twice, and the second time the device is at the end,
hence premature end of document.
*/
void tst_QXmlQuery::sequentialExecution() const
{
QBuffer inBuffer;
inBuffer.setData(QByteArray("<input/>"));
QVERIFY(inBuffer.open(QIODevice::ReadOnly));
QXmlQuery query;
query.bindVariable("inputDocument", &inBuffer);
QByteArray outArray;
QBuffer outBuffer(&outArray);
outBuffer.open(QIODevice::WriteOnly);
const QString queryString(QLatin1String("doc($inputDocument)"));
query.setQuery(queryString);
QXmlFormatter formatter(query, &outBuffer);
QVERIFY(query.evaluateTo(&formatter));
/* If this line is removed, the bug isn't triggered. */
query.setQuery(queryString);
QVERIFY(query.evaluateTo(&formatter));
}
void tst_QXmlQuery::isValid() const
{
/* Check default value. */
QXmlQuery query;
QVERIFY(!query.isValid());
}
void tst_QXmlQuery::bindVariableQString() const
{
{
QXmlQuery query;
/* Bind with a null QXmlItem. */
query.bindVariable(QLatin1String("name"), QXmlItem());
}
{
QXmlQuery query;
/* Bind with a null QVariant. */
query.bindVariable(QLatin1String("name"), QXmlItem(QVariant()));
}
{
QXmlQuery query;
/* Bind with a null QXmlNodeModelIndex. */
query.bindVariable(QLatin1String("name"), QXmlItem(QXmlNodeModelIndex()));
}
}
void tst_QXmlQuery::bindVariableQStringNoExternalDeclaration() const
{
QXmlQuery query;
query.bindVariable(QLatin1String("foo"), QXmlItem(QLatin1String("Variable Value")));
query.setQuery(QLatin1String("$foo"));
QVERIFY(query.isValid());
QStringList result;
QVERIFY(query.evaluateTo(&result));
QCOMPARE(result, QStringList() << QLatin1String("Variable Value"));
}
void tst_QXmlQuery::bindVariableQXmlName() const
{
// TODO
}
void tst_QXmlQuery::bindVariableQXmlNameTriggerWarnings() const
{
QXmlQuery query;
QTest::ignoreMessage(QtWarningMsg, "The variable name cannot be null.");
query.bindVariable(QXmlName(), QVariant());
}
void tst_QXmlQuery::bindVariableQStringQIODeviceWithByteArray() const
{
QXmlQuery query;
QByteArray in("<e/>");
QBuffer device(&in);
QVERIFY(device.open(QIODevice::ReadOnly));
query.bindVariable("doc", &device);
query.setQuery(QLatin1String("declare variable $doc external; $doc"));
QVERIFY(query.isValid());
/* Check the URI corresponding to the variable. */
{
QXmlResultItems items;
query.evaluateTo(&items);
QCOMPARE(items.next().toAtomicValue().toString(), QString::fromLatin1("tag:trolltech.com,2007:QtXmlPatterns:QIODeviceVariable:doc"));
}
/* Now, actually load the document. We use the same QXmlQuery just to stress recompilation a bit. */
{
query.setQuery(QLatin1String("declare variable $doc external; doc($doc)"));
QByteArray out;
QBuffer outBuffer(&out);
QVERIFY(outBuffer.open(QIODevice::WriteOnly));
QXmlSerializer serializer(query, &outBuffer);
QVERIFY(query.evaluateTo(&serializer));
QCOMPARE(out, in);
}
}
void tst_QXmlQuery::bindVariableQStringQIODeviceWithString() const
{
QXmlQuery query;
QString in("<qstring/>");
QByteArray inUtf8(in.toUtf8());
QBuffer inDevice(&inUtf8);
QVERIFY(inDevice.open(QIODevice::ReadOnly));
query.bindVariable("doc", &inDevice);
query.setQuery(QLatin1String("declare variable $doc external; doc($doc)"));
QByteArray out;
QBuffer outBuffer(&out);
QVERIFY(outBuffer.open(QIODevice::WriteOnly));
QXmlSerializer serializer(query, &outBuffer);
QVERIFY(query.evaluateTo(&serializer));
QCOMPARE(out, inUtf8);
}
void tst_QXmlQuery::bindVariableQStringQIODeviceWithQFile() const
{
QXmlQuery query;
QFile inDevice(QFINDTESTDATA("input.xml"));
QVERIFY(inDevice.open(QIODevice::ReadOnly));
query.bindVariable("doc", &inDevice);
query.setQuery(QLatin1String("declare variable $doc external; doc($doc)"));
QByteArray out;
QBuffer outBuffer(&out);
QVERIFY(outBuffer.open(QIODevice::WriteOnly));
QXmlSerializer serializer(query, &outBuffer);
QVERIFY(query.evaluateTo(&serializer));
outBuffer.close();
QCOMPARE(out, QByteArray("<!-- This is just a file for testing. --><input/>"));
}
void tst_QXmlQuery::bindVariableQStringQIODevice() const
{
QXmlQuery query;
/* Rebind the variable. */
{
/* First evaluation. */
{
QByteArray in1("<e1/>");
QBuffer inDevice1(&in1);
QVERIFY(inDevice1.open(QIODevice::ReadOnly));
query.bindVariable("in", &inDevice1);
query.setQuery(QLatin1String("doc($in)"));
QByteArray out1;
QBuffer outDevice1(&out1);
QVERIFY(outDevice1.open(QIODevice::WriteOnly));
QXmlSerializer serializer(query, &outDevice1);
query.evaluateTo(&serializer);
QCOMPARE(out1, in1);
}
/* Second evaluation, rebind variable. */
{
QByteArray in2("<e2/>");
QBuffer inDevice2(&in2);
QVERIFY(inDevice2.open(QIODevice::ReadOnly));
query.bindVariable(QLatin1String("in"), &inDevice2);
QByteArray out2;
QBuffer outDevice2(&out2);
QVERIFY(outDevice2.open(QIODevice::WriteOnly));
QXmlSerializer serializer(query, &outDevice2);
QVERIFY(query.evaluateTo(&serializer));
QCOMPARE(out2, in2);
}
}
// TODO trigger recompilation when setting qiodevices., and qiodevice overwritten by other type, etc.
}
void tst_QXmlQuery::bindVariableQXmlNameQIODevice() const
{
// TODO
}
void tst_QXmlQuery::bindVariableQXmlNameQIODeviceTriggerWarnings() const
{
QXmlNamePool np;
QXmlQuery query(np);
QBuffer buffer;
QTest::ignoreMessage(QtWarningMsg, "A null, or readable QIODevice must be passed.");
query.bindVariable(QXmlName(np, QLatin1String("foo")), &buffer);
QTest::ignoreMessage(QtWarningMsg, "The variable name cannot be null.");
query.bindVariable(QXmlName(), 0);
}
void tst_QXmlQuery::bindVariableXSLTSuccess() const
{
QXmlQuery stylesheet(QXmlQuery::XSLT20);
stylesheet.setInitialTemplateName(QLatin1String("main"));
stylesheet.bindVariable(QLatin1String("variableNoSelectNoBodyBoundWithBindVariable"),
QVariant(QLatin1String("MUST NOT SHOW 1")));
stylesheet.bindVariable(QLatin1String("variableSelectBoundWithBindVariable"),
QVariant(QLatin1String("MUST NOT SHOW 2")));
stylesheet.bindVariable(QLatin1String("variableSelectWithTypeIntBoundWithBindVariable"),
QVariant(QLatin1String("MUST NOT SHOW 3")));
stylesheet.bindVariable(QLatin1String("paramNoSelectNoBodyBoundWithBindVariable"),
QVariant(QLatin1String("param1")));
stylesheet.bindVariable(QLatin1String("paramNoSelectNoBodyBoundWithBindVariableRequired"),
QVariant(QLatin1String("param1")));
stylesheet.bindVariable(QLatin1String("paramSelectBoundWithBindVariable"),
QVariant(QLatin1String("param2")));
stylesheet.bindVariable(QLatin1String("paramSelectBoundWithBindVariableRequired"),
QVariant(QLatin1String("param3")));
stylesheet.bindVariable(QLatin1String("paramSelectWithTypeIntBoundWithBindVariable"),
QVariant(4));
stylesheet.bindVariable(QLatin1String("paramSelectWithTypeIntBoundWithBindVariableRequired"),
QVariant(QLatin1String("param5")));
stylesheet.setQuery(QUrl(inputFileAsURI(m_xmlPatternsDir + QLatin1String("/stylesheets/parameters.xsl"))));
QVERIFY(stylesheet.isValid());
QBuffer deviceOut;
QVERIFY(deviceOut.open(QIODevice::ReadWrite));
QVERIFY(stylesheet.evaluateTo(&deviceOut));
const QString result(QString::fromUtf8(deviceOut.data().constData()));
QCOMPARE(result,
QString::fromLatin1("Variables: variableSelectsDefaultValue variableSelectsDefaultValue2 3 4 "
"Parameters: param1 param1 param2 param3 4 param5"));
}
void tst_QXmlQuery::bindVariableTemporaryNode() const
{
/* First we do it with QXmlResultItems staying in scope. */;
{
QXmlQuery query1;
query1.setQuery("<anElement/>");
QXmlResultItems result1;
query1.evaluateTo(&result1);
QXmlQuery query2(query1);
query2.bindVariable("fromQuery1", result1.next());
query2.setQuery("$fromQuery1");
QString output;
QVERIFY(query2.evaluateTo(&output));
QCOMPARE(output, QString::fromLatin1("<anElement/>\n"));
}
/* And now with it deallocating, so its internal DynamicContext pointer is
* released. This doesn't work in Qt 4.5 and is ok. */
{
QXmlQuery query1;
query1.setQuery("<anElement/>");
QXmlQuery query2;
{
QXmlResultItems result1;
query1.evaluateTo(&result1);
query2.bindVariable("fromQuery1", result1.next());
query2.setQuery("$fromQuery1");
}
QString output;
return; // See comment above.
QVERIFY(query2.evaluateTo(&output));
QCOMPARE(output, QString::fromLatin1("<anElement/>\n"));
}
}
void tst_QXmlQuery::messageHandler() const
{
{
/* Check default value. */
QXmlQuery query;
QCOMPARE(query.messageHandler(), static_cast<QAbstractMessageHandler *>(0));
}
}
void tst_QXmlQuery::setMessageHandler() const
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
QCOMPARE(static_cast<QAbstractMessageHandler *>(&silencer), query.messageHandler());
}
void tst_QXmlQuery::evaluateToReceiver()
{
QFETCH(QString, inputQuery);
/* This query prints a URI specific to the local system. */
if(inputQuery == QLatin1String("static-base-uri.xq"))
return;
++m_pushTestsCount;
const QString queryURI(inputFile(m_xmlPatternsDir + QLatin1String("/queries/") + inputQuery));
QFile queryFile(queryURI);
QVERIFY(queryFile.exists());
QVERIFY(queryFile.open(QIODevice::ReadOnly));
QXmlQuery query;
MessageSilencer receiver;
query.setMessageHandler(&receiver);
query.setQuery(&queryFile, QUrl::fromLocalFile(queryURI));
/* We read all the queries, and some of them are invalid. However, we
* only want those that compile. */
if(!query.isValid())
return;
QString produced;
QTextStream stream(&produced, QIODevice::WriteOnly);
PushBaseliner push(stream, query.namePool());
QVERIFY(push.isValid());
query.evaluateTo(&push);
const QString baselineName(QFINDTESTDATA("pushBaselines/") + inputQuery.left(inputQuery.length() - 2) + QString::fromLatin1("ref"));
QFile baseline(baselineName);
if(baseline.exists())
{
QVERIFY(baseline.open(QIODevice::ReadOnly | QIODevice::Text));
const QString stringedBaseline(QString::fromUtf8(baseline.readAll()));
QCOMPARE(produced, stringedBaseline);
}
else
{
QVERIFY(baseline.open(QIODevice::WriteOnly));
/* This is intentionally a warning, don't remove it. Update the baselines instead. */
qWarning() << "Generated baseline for:" << baselineName;
++m_generatedBaselines;
baseline.write(produced.toUtf8());
}
}
void tst_QXmlQuery::evaluateToReceiver_data() const
{
QTest::addColumn<QString>("inputQuery");
foreach (QString const& query, queries())
{
/* This outputs a URI specific to the environment, so we can't use it for this
* particular test. */
if (query != QLatin1String("staticBaseURI.xq"))
QTest::newRow(query.toUtf8().constData()) << query;
}
}
void tst_QXmlQuery::evaluateToReceiverOnInvalidQuery() const
{
/* Invoke on a default constructed object. */
{
QByteArray out;
QBuffer buffer(&out);
buffer.open(QIODevice::WriteOnly);
QXmlQuery query;
QXmlSerializer serializer(query, &buffer);
QVERIFY(!query.evaluateTo(&serializer));
}
/* Invoke on an invalid query; compile time error. */
{
QByteArray out;
QBuffer buffer(&out);
buffer.open(QIODevice::WriteOnly);
MessageSilencer silencer;
QXmlQuery query;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("1 + "));
QXmlSerializer serializer(query, &buffer);
QVERIFY(!query.evaluateTo(&serializer));
}
/* Invoke on an invalid query; runtime error. */
{
QByteArray out;
QBuffer buffer(&out);
buffer.open(QIODevice::WriteOnly);
MessageSilencer silencer;
QXmlQuery query;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("error()"));
QXmlSerializer serializer(query, &buffer);
QVERIFY(!query.evaluateTo(&serializer));
}
}
void tst_QXmlQuery::evaluateToQStringTriggerError() const
{
/* Invoke on a default constructed object. */
{
QXmlQuery query;
QString out;
QVERIFY(!query.evaluateTo(&out));
}
/* Invoke on an invalid query; compile time error. */
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("1 + "));
QString out;
QVERIFY(!query.evaluateTo(&out));
}
/* Invoke on an invalid query; runtime error. */
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("error()"));
QString out;
QVERIFY(!query.evaluateTo(&out));
}
}
void tst_QXmlQuery::evaluateToQString() const
{
QFETCH(QString, query);
QFETCH(QString, expectedOutput);
QXmlQuery queryInstance;
queryInstance.setQuery(query);
QVERIFY(queryInstance.isValid());
QString result;
QVERIFY(queryInstance.evaluateTo(&result));
QCOMPARE(result, expectedOutput);
}
void tst_QXmlQuery::evaluateToQString_data() const
{
QTest::addColumn<QString>("query");
QTest::addColumn<QString>("expectedOutput");
QTest::newRow("Two atomics")
<< QString::fromLatin1("1, 'two'")
<< QString::fromLatin1("1 two\n");
QTest::newRow("An element")
<< QString::fromLatin1("<e>{1}</e>")
<< QString::fromLatin1("<e>1</e>\n");
}
void tst_QXmlQuery::evaluateToQStringSignature() const
{
const QXmlQuery query;
QString output;
/* evaluateTo(QString *) should be a const function. */
query.evaluateTo(&output);
}
void tst_QXmlQuery::evaluateToQAbstractXmlReceiverTriggerWarnings() const
{
QXmlQuery query;
/* We check the return value as well as warning message here. */
QTest::ignoreMessage(QtWarningMsg, "A non-null callback must be passed.");
QCOMPARE(query.evaluateTo(static_cast<QAbstractXmlReceiver *>(0)),
false);
}
void tst_QXmlQuery::evaluateToQXmlResultItems() const
{
/* Invoke on a default constructed object. */
{
QXmlQuery query;
QXmlResultItems result;
query.evaluateTo(&result);
QVERIFY(result.next().isNull());
}
}
void tst_QXmlQuery::evaluateToQXmlResultItemsTriggerWarnings() const
{
QTest::ignoreMessage(QtWarningMsg, "A null pointer cannot be passed.");
QXmlQuery query;
query.evaluateTo(static_cast<QXmlResultItems *>(0));
}
void tst_QXmlQuery::evaluateToQXmlResultItemsErrorAtEnd() const
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("1 to 100, fn:error()"));
QVERIFY(query.isValid());
QXmlResultItems it;
query.evaluateTo(&it);
while(!it.next().isNull())
{
}
}
/*!
If baselines were generated, we flag it as a failure such that it gets
attention, and that they are adjusted accordingly.
*/
void tst_QXmlQuery::checkGeneratedBaselines() const
{
QCOMPARE(m_generatedBaselines, 0);
/* If this check fails, the auto test setup is misconfigured, or files have
* been added/removed without this number being updated. */
QCOMPARE(m_pushTestsCount, int(ExpectedQueryCount));
}
void tst_QXmlQuery::basicXQueryToQtTypeCheck() const
{
QFile queryFile(m_xmlPatternsDir + QLatin1String("/queries/") + QString::fromLatin1("allAtomics.xq"));
QVERIFY(queryFile.open(QIODevice::ReadOnly));
QXmlQuery query;
query.setQuery(&queryFile);
QVERIFY(query.isValid());
QXmlResultItems it;
query.evaluateTo(&it);
QVariantList expectedValues;
expectedValues.append(QString::fromLatin1("xs:untypedAtomic"));
expectedValues.append(QDateTime(QDate(2002, 10, 10), QTime(23, 2, 11), Qt::UTC));
expectedValues.append(QDate(2002, 10, 10));
expectedValues.append(QVariant()); /* We currently doesn't support xs:time through the API. */
expectedValues.append(QVariant()); /* xs:duration */
expectedValues.append(QVariant()); /* xs:dayTimeDuration */
expectedValues.append(QVariant()); /* xs:yearMonthDuration */
if(sizeof(qreal) == sizeof(float)) {//ARM casts to Float not to double
expectedValues.append(QVariant(float(3e3))); /* xs:float */
expectedValues.append(QVariant(float(4e4))); /* xs:double */
expectedValues.append(QVariant(float(2))); /* xs:decimal */
} else {
expectedValues.append(QVariant(double(3e3))); /* xs:float */
expectedValues.append(QVariant(double(4e4))); /* xs:double */
expectedValues.append(QVariant(double(2))); /* xs:decimal */
}
/* xs:integer and its sub-types. */
expectedValues.append(QVariant(qlonglong(16)));
expectedValues.append(QVariant(qlonglong(-6)));
expectedValues.append(QVariant(qlonglong(-4)));
expectedValues.append(QVariant(qlonglong(5)));
expectedValues.append(QVariant(qlonglong(6)));
expectedValues.append(QVariant(qlonglong(7)));
expectedValues.append(QVariant(qlonglong(8)));
expectedValues.append(QVariant(qlonglong(9)));
expectedValues.append(QVariant(qulonglong(10)));
expectedValues.append(QVariant(qlonglong(11)));
expectedValues.append(QVariant(qlonglong(12)));
expectedValues.append(QVariant(qlonglong(13)));
expectedValues.append(QVariant(qlonglong(14)));
expectedValues.append(QVariant()); /* xs:gYearMonth("1976-02"), */
expectedValues.append(QVariant()); /* xs:gYear("2005-12:00"), */
expectedValues.append(QVariant()); /* xs:gMonthDay("--12-25-14:00"), */
expectedValues.append(QVariant()); /* xs:gDay("---25-14:00"), */
expectedValues.append(QVariant()); /* xs:gMonth("--12-14:00"), */
expectedValues.append(true); /* xs:boolean("true"), */
expectedValues.append(QVariant(QByteArray::fromBase64(QByteArray("aaaa")))); /* xs:base64Binary("aaaa"), */
expectedValues.append(QVariant(QByteArray::fromHex(QByteArray("FFFF")))); /* xs:hexBinary("FFFF"), */
expectedValues.append(QVariant(QString::fromLatin1("http://example.com/"))); /* xs:anyURI("http://example.com/"), */
QXmlNamePool np(query.namePool());
expectedValues.append(QVariant(qVariantFromValue(QXmlName(np, QLatin1String("localName"),
QLatin1String("http://example.com/2"),
QLatin1String("prefix")))));
expectedValues.append(QVariant(QString::fromLatin1("An xs:string")));
expectedValues.append(QVariant(QString::fromLatin1("normalizedString")));
expectedValues.append(QVariant(QString::fromLatin1("token")));
expectedValues.append(QVariant(QString::fromLatin1("language")));
expectedValues.append(QVariant(QString::fromLatin1("NMTOKEN")));
expectedValues.append(QVariant(QString::fromLatin1("Name")));
expectedValues.append(QVariant(QString::fromLatin1("NCName")));
expectedValues.append(QVariant(QString::fromLatin1("ID")));
expectedValues.append(QVariant(QString::fromLatin1("IDREF")));
expectedValues.append(QVariant(QString::fromLatin1("ENTITY")));
int i = 0;
QXmlItem item(it.next());
while(!item.isNull())
{
QVERIFY(item.isAtomicValue());
const QVariant produced(item.toAtomicValue());
const QVariant &expected = expectedValues.at(i);
/* For the cases where we can't represent a value in the XDM with Qt,
* we return an invalid QVariant. */
QCOMPARE(expected.isValid(), produced.isValid());
QCOMPARE(produced.type(), expected.type());
if(expected.isValid())
{
/* This is only needed for xs:decimal though, for some reason. Probably
* just artifacts created somewhere. */
if(produced.type() == QVariant::Double)
QVERIFY(qFuzzyCompare(produced.toDouble(), expected.toDouble()));
else if(qVariantCanConvert<QXmlName>(produced))
{
/* QVariant::operator==() does identity comparison, it doesn't delegate to operator==() of
* the contained type, unless it's hardcoded into QVariant. */
const QXmlName n1 = qVariantValue<QXmlName>(produced);
const QXmlName n2 = qVariantValue<QXmlName>(expected);
QCOMPARE(n1, n2);
}
else
QCOMPARE(produced, expected);
}
++i;
item = it.next();
}
QCOMPARE(i, expectedValues.count());
}
/*!
Send values from Qt into XQuery.
*/
void tst_QXmlQuery::basicQtToXQueryTypeCheck() const
{
QFile queryFile(m_xmlPatternsDir + QLatin1String("/queries/") + QLatin1String("allAtomicsExternally.xq"));
QVERIFY(queryFile.exists());
QVERIFY(queryFile.open(QIODevice::ReadOnly));
QCOMPARE(QVariant(QDate(1999, 9, 10)).type(), QVariant::Date);
QXmlQuery query;
QXmlNamePool np(query.namePool());
const QXmlName name(np, QLatin1String("localname"),
QLatin1String("http://example.com"),
QLatin1String("prefix"));
query.bindVariable(QLatin1String("fromQUrl"), QXmlItem(QUrl(QString::fromLatin1("http://example.com/"))));
query.bindVariable(QLatin1String("fromQByteArray"), QXmlItem(QByteArray("AAAA")));
query.bindVariable(QLatin1String("fromBool"), QXmlItem(bool(true)));
query.bindVariable(QLatin1String("fromQDate"), QXmlItem(QDate(2000, 10, 11)));
// TODO Do with different QDateTime time specs
query.bindVariable(QLatin1String("fromQDateTime"), QXmlItem(QDateTime(QDate(2001, 9, 10), QTime(1, 2, 3))));
query.bindVariable(QLatin1String("fromDouble"), QXmlItem(double(3)));
query.bindVariable(QLatin1String("fromFloat"), QXmlItem(float(4)));
query.bindVariable(QLatin1String("integer"), QXmlItem(5));
query.bindVariable(QLatin1String("fromQString"), QXmlItem(QString::fromLatin1("A QString")));
query.bindVariable(QLatin1String("fromQChar"), QXmlItem(QChar::fromLatin1('C')));
query.bindVariable(QLatin1String("fromIntLiteral"), QXmlItem(QVariant(654)));
{
QVariant ui(uint(5));
QCOMPARE(ui.type(), QVariant::UInt);
query.bindVariable(QLatin1String("fromUInt"), ui);
}
{
QVariant ulnglng(qulonglong(6));
QCOMPARE(ulnglng.type(), QVariant::ULongLong);
query.bindVariable(QLatin1String("fromULongLong"), ulnglng);
}
{
QVariant qlnglng(qlonglong(7));
QCOMPARE(qlnglng.type(), QVariant::LongLong);
query.bindVariable(QLatin1String("fromLongLong"), qlnglng);
}
query.setQuery(&queryFile);
// TODO do queries which declares external variables with types. Tons of combos here.
// TODO ensure that binding with QXmlItem() doesn't make a binding available.
// TODO test rebinding a variable.
QVERIFY(query.isValid());
QXmlResultItems it;
query.evaluateTo(&it);
QXmlItem item(it.next());
QVERIFY(!item.isNull());
QVERIFY(item.isAtomicValue());
if(sizeof(qreal) == sizeof(float)) //ARM casts to Float not to double
QCOMPARE(item.toAtomicValue().toString(),
QLatin1String("4 true 3 654 7 41414141 C 2000-10-11Z 2001-09-10T01:02:03 "
"A QString http://example.com/ 5 6 true false false true true true true true true true "
"true true true"));
else
QCOMPARE(item.toAtomicValue().toString(),
QLatin1String("4 true 3 654 7 41414141 C 2000-10-11Z 2001-09-10T01:02:03 "
"A QString http://example.com/ 5 6 true true true true true true true true true true "
"true true true"));
}
void tst_QXmlQuery::bindNode() const
{
QXmlQuery query;
TestSimpleNodeModel nodeModel(query.namePool());
query.bindVariable(QLatin1String("node"), nodeModel.root());
QByteArray out;
QBuffer buff(&out);
QVERIFY(buff.open(QIODevice::WriteOnly));
query.setQuery(QLatin1String("declare variable $node external; $node"));
QXmlSerializer serializer(query, &buff);
QVERIFY(query.evaluateTo(&serializer));
QCOMPARE(out, QByteArray("<nodeName/>"));
}
/*!
Pass in a relative URI, and make sure it is resolved against the current application directory.
*/
void tst_QXmlQuery::relativeBaseURI() const
{
QXmlQuery query;
query.setQuery(QLatin1String("fn:static-base-uri()"), QUrl(QLatin1String("a/relative/uri.weirdExtension")));
QVERIFY(query.isValid());
QByteArray result;
QBuffer buffer(&result);
QVERIFY(buffer.open(QIODevice::ReadWrite));
QXmlSerializer serializer(query, &buffer);
QVERIFY(query.evaluateTo(&serializer));
const QUrl loaded(QUrl::fromEncoded(result));
QUrl appPath(QUrl::fromLocalFile(QCoreApplication::applicationFilePath()));
QVERIFY(loaded.isValid());
QVERIFY(appPath.isValid());
QVERIFY(!loaded.isRelative());
QVERIFY(!appPath.isRelative());
QFileInfo dir(appPath.toLocalFile());
dir.setFile(QString());
/* We can't use QUrl::isParentOf() because it doesn't do what we want it to */
if(!loaded.toLocalFile().startsWith(dir.absoluteFilePath()))
QTextStream(stderr) << "dir.absoluteFilePath():" << dir.absoluteFilePath() << "loaded.toLocalFile():" << loaded.toLocalFile();
checkBaseURI(loaded, dir.absoluteFilePath());
}
void tst_QXmlQuery::emptyBaseURI() const
{
QXmlQuery query;
query.setQuery(QLatin1String("fn:static-base-uri()"), QUrl());
QVERIFY(query.isValid());
QByteArray result;
QBuffer buffer(&result);
QVERIFY(buffer.open(QIODevice::ReadWrite));
QXmlSerializer serializer(query, &buffer);
QVERIFY(query.evaluateTo(&serializer));
const QUrl loaded(QUrl::fromEncoded(result));
QUrl appPath(QUrl::fromLocalFile(QCoreApplication::applicationFilePath()));
QVERIFY(loaded.isValid());
QVERIFY(appPath.isValid());
QVERIFY(!loaded.isRelative());
QVERIFY(!appPath.isRelative());
QFileInfo dir(appPath.toLocalFile());
dir.setFile(QString());
QCOMPARE(loaded, appPath);
}
/*!
Ensure that QDate comes out as QDateTime.
*/
void tst_QXmlQuery::roundTripDateWithinQXmlItem() const
{
const QDate date(1999, 9, 10);
QVERIFY(date.isValid());
const QVariant variant(date);
QVERIFY(variant.isValid());
QCOMPARE(variant.type(), QVariant::Date);
const QXmlItem item(variant);
QVERIFY(!item.isNull());
QVERIFY(item.isAtomicValue());
const QVariant out(item.toAtomicValue());
QVERIFY(out.isValid());
QCOMPARE(out.type(), QVariant::Date);
QCOMPARE(out.toDate(), date);
}
/*!
Check whether a query is valid, which uses an unbound variable.
*/
void tst_QXmlQuery::bindingMissing() const
{
QXmlQuery query;
MessageSilencer messageHandler;
query.setMessageHandler(&messageHandler);
QFile queryFile(m_xmlPatternsDir + QLatin1String("/queries/") + QString::fromLatin1("externalVariable.xq"));
QVERIFY(queryFile.open(QIODevice::ReadOnly));
query.setQuery(&queryFile);
QVERIFY(!query.isValid());
}
void tst_QXmlQuery::bindDefaultConstructedItem() const
{
QFETCH(QXmlItem, item);
QXmlQuery query;
MessageSilencer messageHandler;
query.setMessageHandler(&messageHandler);
QFile queryFile(m_xmlPatternsDir + QLatin1String("/queries/") + QString::fromLatin1("externalVariable.xq"));
QVERIFY(queryFile.open(QIODevice::ReadOnly));
query.setQuery(&queryFile);
query.bindVariable(QLatin1String("externalVariable"), item);
QVERIFY(!query.isValid());
}
void tst_QXmlQuery::bindDefaultConstructedItem_data() const
{
QTest::addColumn<QXmlItem>("item");
QTest::newRow("QXmlItem()") << QXmlItem();
QTest::newRow("QXmlItem(QVariant())") << QXmlItem(QVariant());
QTest::newRow("QXmlItem(QXmlNodeModelIndex())") << QXmlItem(QXmlNodeModelIndex());
}
/*!
Remove a binding by binding QXmlItem() with the same name.
*/
void tst_QXmlQuery::eraseQXmlItemBinding() const
{
QXmlQuery query;
MessageSilencer messageHandler;
query.setMessageHandler(&messageHandler);
QFile queryFile(m_xmlPatternsDir + QLatin1String("/queries/") + QString::fromLatin1("externalVariable.xq"));
QVERIFY(queryFile.open(QIODevice::ReadOnly));
query.bindVariable(QLatin1String("externalVariable"), QXmlItem(3));
query.setQuery(&queryFile);
QVERIFY(query.isValid());
QByteArray result;
QBuffer buffer(&result);
QVERIFY(buffer.open(QIODevice::ReadWrite));
QXmlSerializer serializer(query, &buffer);
QVERIFY(query.evaluateTo(&serializer));
QCOMPARE(result, QByteArray("3 6<e>3</e>false"));
query.bindVariable(QLatin1String("externalVariable"), QXmlItem());
QVERIFY(!query.isValid());
}
/*!
Erase a variable binding
*/
void tst_QXmlQuery::eraseDeviceBinding() const
{
/* Erase an existing QIODevice binding with another QIODevice binding. */
{
QXmlQuery query;
QByteArray doc("<e/>");
QBuffer buffer(&doc);
QVERIFY(buffer.open(QIODevice::ReadOnly));
query.bindVariable(QLatin1String("in"), &buffer);
query.setQuery(QLatin1String("$in"));
QVERIFY(query.isValid());
query.bindVariable(QLatin1String("in"), 0);
QVERIFY(!query.isValid());
}
/* Erase an existing QXmlItem binding with another QIODevice binding. */
{
QXmlQuery query;
query.bindVariable(QLatin1String("in"), QXmlItem(5));
query.setQuery(QLatin1String("$in"));
QVERIFY(query.isValid());
query.bindVariable(QLatin1String("in"), 0);
QVERIFY(!query.isValid());
}
}
/*!
Bind a variable, evaluate, bind with a different value but same type, and evaluate again.
*/
void tst_QXmlQuery::rebindVariableSameType() const
{
QXmlQuery query;
MessageSilencer messageHandler;
query.setMessageHandler(&messageHandler);
query.bindVariable(QLatin1String("externalVariable"), QXmlItem(3));
{
QFile queryFile(m_xmlPatternsDir + QLatin1String("/queries/") + QString::fromLatin1("externalVariable.xq"));
QVERIFY(queryFile.open(QIODevice::ReadOnly));
query.setQuery(&queryFile);
}
QVERIFY(query.isValid());
{
QByteArray result;
QBuffer buffer(&result);
QVERIFY(buffer.open(QIODevice::ReadWrite));
QXmlSerializer serializer(query, &buffer);
QVERIFY(query.evaluateTo(&serializer));
QCOMPARE(result, QByteArray("3 6<e>3</e>false"));
}
{
query.bindVariable(QLatin1String("externalVariable"), QXmlItem(5));
QByteArray result;
QBuffer buffer(&result);
QVERIFY(buffer.open(QIODevice::ReadWrite));
QXmlSerializer serializer(query, &buffer);
QVERIFY(query.evaluateTo(&serializer));
QCOMPARE(result, QByteArray("5 8<e>5</e>false"));
}
}
void tst_QXmlQuery::rebindVariableDifferentType() const
{
/* Rebind QXmlItem variable with QXmlItem variable. */
{
QXmlQuery query;
query.bindVariable(QLatin1String("in"), QXmlItem(3));
query.setQuery(QLatin1String("$in"));
QVERIFY(query.isValid());
query.bindVariable(QLatin1String("in"), QXmlItem("A string"));
QVERIFY(!query.isValid());
}
/* Rebind QIODevice variable with QXmlItem variable. */
{
QXmlQuery query;
QBuffer buffer;
buffer.setData(QByteArray("<e/>"));
QVERIFY(buffer.open(QIODevice::ReadOnly));
query.bindVariable(QLatin1String("in"), &buffer);
query.setQuery(QLatin1String("$in"));
QVERIFY(query.isValid());
query.bindVariable(QLatin1String("in"), QXmlItem("A string"));
QVERIFY(!query.isValid());
}
/* Rebind QXmlItem variable with QIODevice variable. The type of the
* variable changes, so a recompile is necessary. */
{
QXmlQuery query;
query.bindVariable(QLatin1String("in"), QXmlItem(QLatin1String("A string")));
query.setQuery(QLatin1String("$in"));
QVERIFY(query.isValid());
QBuffer buffer;
buffer.setData(QByteArray("<e/>"));
QVERIFY(buffer.open(QIODevice::ReadOnly));
query.bindVariable(QLatin1String("in"), &buffer);
QVERIFY(!query.isValid());
}
}
void tst_QXmlQuery::rebindVariableWithNullItem() const
{
QXmlQuery query;
query.bindVariable(QLatin1String("name"), QXmlItem(5));
query.bindVariable(QLatin1String("name"), QXmlItem());
}
void tst_QXmlQuery::constCorrectness() const
{
QXmlResultItems result;
QXmlQuery tmp;
tmp.setQuery(QLatin1String("1")); /* Just so we have a valid query. */
const QXmlQuery query(tmp);
/* These functions should be const. */
query.isValid();
query.evaluateTo(&result);
query.namePool();
query.uriResolver();
query.messageHandler();
{
QString dummyString;
QTextStream dummyStream(&dummyString);
PushBaseliner dummy(dummyStream, query.namePool());
QVERIFY(dummy.isValid());
query.evaluateTo(&dummy);
}
}
void tst_QXmlQuery::objectSize() const
{
/* We have a d pointer. */
QCOMPARE(sizeof(QXmlQuery), sizeof(void *));
}
void tst_QXmlQuery::setUriResolver() const
{
/* Set a null resolver, and make sure it can take a const pointer. */
{
QXmlQuery query;
query.setUriResolver(static_cast<const QAbstractUriResolver *>(0));
QCOMPARE(query.uriResolver(), static_cast<const QAbstractUriResolver *>(0));
}
{
TestURIResolver resolver;
QXmlQuery query;
query.setUriResolver(&resolver);
QCOMPARE(query.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver));
}
}
void tst_QXmlQuery::uriResolver() const
{
/* Check default value. */
{
QXmlQuery query;
QCOMPARE(query.uriResolver(), static_cast<const QAbstractUriResolver *>(0));
}
}
void tst_QXmlQuery::messageXML() const
{
QXmlQuery query;
MessageValidator messageValidator;
query.setMessageHandler(&messageValidator);
query.setQuery(QLatin1String("1basicSyntaxError"));
QRegExp removeFilename(QLatin1String("Location: file:.*\\#"));
QVERIFY(removeFilename.isValid());
QVERIFY(messageValidator.success());
QCOMPARE(messageValidator.received().remove(removeFilename),
QString::fromLatin1("Type:3\n"
"Description: <html xmlns='http://www.w3.org/1999/xhtml/'><body><p>syntax error, unexpected unknown keyword</p></body></html>\n"
"Identifier: http://www.w3.org/2005/xqt-errors#XPST0003\n"
"1,1"));
}
/*!
1. Allocate QXmlResultItems
2. Allocate QXmlQuery
3. evaluate to the QXmlResultItems instance
4. Dellocate the QXmlQuery instance
5. Ensure QXmlResultItems works
*/
void tst_QXmlQuery::resultItemsDeallocatedQuery() const
{
QXmlResultItems result;
{
QXmlQuery query;
query.setQuery(QLatin1String("1, 2, xs:integer(<e>3</e>)"));
query.evaluateTo(&result);
}
QCOMPARE(result.next().toAtomicValue(), QVariant(1));
QCOMPARE(result.next().toAtomicValue(), QVariant(2));
QCOMPARE(result.next().toAtomicValue(), QVariant(3));
QVERIFY(result.next().isNull());
QVERIFY(!result.hasError());
}
/*!
1. Bind variable with bindVariable()
2. setQuery that has 'declare variable' with same name.
3. Ensure the value inside the query is used. We don't guarantee this behavior
but that's what we lock.
*/
void tst_QXmlQuery::shadowedVariables() const
{
QXmlQuery query;
query.bindVariable("varName", QXmlItem(3));
query.setQuery(QLatin1String("declare variable $varName := 5; $varName"));
QXmlResultItems result;
query.evaluateTo(&result);
QCOMPARE(result.next().toAtomicValue(), QVariant(5));
}
void tst_QXmlQuery::setFocusQXmlItem() const
{
/* Make sure we can take a const reference. */
{
QXmlQuery query;
const QXmlItem item;
query.setFocus(item);
}
// TODO evaluate with atomic value, check type
// TODO evaluate with node, check type
// TODO ensure that setFocus() triggers query recompilation, as appropriate.
// TODO let the focus be undefined, call isvalid, call evaluate anyway
// TODO let the focus be undefined, call evaluate directly
}
void tst_QXmlQuery::setFocusQUrl() const
{
/* Load a focus which isn't well-formed. */
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
QVERIFY(!query.setFocus(QUrl(QLatin1String("data/notWellformed.xml"))));
}
/* Ensure the same URI resolver is used. */
{
QXmlQuery query(QXmlQuery::XSLT20);
const TestURIResolver resolver(QUrl(inputFileAsURI(m_xmlPatternsDir + QLatin1String("/stylesheets/documentElement.xml"))));
query.setUriResolver(&resolver);
QVERIFY(query.setFocus(QUrl(QLatin1String("arbitraryURI"))));
query.setQuery(QUrl(inputFileAsURI(m_xmlPatternsDir + QLatin1String("/stylesheets/copyWholeDocument.xsl"))));
QVERIFY(query.isValid());
QBuffer result;
QVERIFY(result.open(QIODevice::ReadWrite));
QXmlSerializer serializer(query, &result);
query.evaluateTo(&serializer);
QCOMPARE(result.data(), QByteArray("<doc/>"));
}
// TODO ensure that the focus type doesn't change from XSLT20 on the main instance.
}
/*!
This code poses a challenge wrt. to internal caching.
*/
void tst_QXmlQuery::setFocusQIODevice() const
{
QXmlQuery query;
{
QBuffer focus;
focus.setData(QByteArray("<e>abc</e>"));
QVERIFY(focus.open(QIODevice::ReadOnly));
query.setFocus(&focus);
query.setQuery(QLatin1String("string()"));
QVERIFY(query.isValid());
QString output;
query.evaluateTo(&output);
QCOMPARE(output, QString::fromLatin1("abc\n"));
}
/* Set a new focus, make sure it changes & works. */
{
QBuffer focus2;
focus2.setData(QByteArray("<e>abc2</e>"));
QVERIFY(focus2.open(QIODevice::ReadOnly));
query.setFocus(&focus2);
QVERIFY(query.isValid());
QString output;
query.evaluateTo(&output);
QCOMPARE(output, QString::fromLatin1("abc2\n"));
}
}
/*!
Since we internally use variable bindings for implementing the focus, we need
to make sure we don't clash in this area.
*/
void tst_QXmlQuery::setFocusQIODeviceAvoidVariableClash() const
{
QBuffer buffer;
buffer.setData("<e>focus</e>");
QVERIFY(buffer.open(QIODevice::ReadOnly));
/* First we bind the variable name, then the focus. */
{
QXmlQuery query;
query.bindVariable(QString(QLatin1Char('u')), QVariant(1));
query.setFocus(&buffer);
query.setQuery(QLatin1String("string()"));
QString out;
query.evaluateTo(&out);
QCOMPARE(out, QString::fromLatin1("focus\n"));
}
/* First we bind the focus, then the variable name. */
{
QXmlQuery query;
QVERIFY(buffer.open(QIODevice::ReadOnly));
query.setFocus(&buffer);
query.bindVariable(QString(QLatin1Char('u')), QVariant(1));
query.setQuery(QLatin1String("string()"));
QString out;
query.evaluateTo(&out);
QCOMPARE(out, QString::fromLatin1("focus\n"));
}
}
void tst_QXmlQuery::setFocusQIODeviceFailure() const
{
/* A not well-formed input document. */
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
QBuffer input;
input.setData("<e");
QVERIFY(input.open(QIODevice::ReadOnly));
QCOMPARE(query.setFocus(&input), false);
}
}
void tst_QXmlQuery::setFocusQString() const
{
QXmlQuery query;
/* Basic use of focus. */
{
QVERIFY(query.setFocus(QLatin1String("<e>textNode</e>")));
query.setQuery(QLatin1String("string()"));
QVERIFY(query.isValid());
QString out;
query.evaluateTo(&out);
QCOMPARE(out, QString::fromLatin1("textNode\n"));
}
/* Set to a new focus, make sure it changes and works. */
{
QVERIFY(query.setFocus(QLatin1String("<e>newFocus</e>")));
QString out;
query.evaluateTo(&out);
QCOMPARE(out, QString::fromLatin1("newFocus\n"));
}
}
void tst_QXmlQuery::setFocusQStringFailure() const
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
QVERIFY(!query.setFocus(QLatin1String("<notWellformed")));
/* Let's try the slight special case of a null string. */
QVERIFY(!query.setFocus(QString()));
}
void tst_QXmlQuery::setFocusQStringSignature() const
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
const QString argument;
/* We should take a const ref. */
query.setFocus(argument);
/* We should return a bool. */
static_cast<bool>(query.setFocus(QString()));
}
void tst_QXmlQuery::setFocusQIODeviceTriggerWarnings() const
{
/* A null pointer. */
{
QXmlQuery query;
QTest::ignoreMessage(QtWarningMsg, "A null QIODevice pointer cannot be passed.");
QCOMPARE(query.setFocus(static_cast<QIODevice *>(0)), false);
}
/* A non opened-device. */
{
QXmlQuery query;
QBuffer notReadable;
QVERIFY(!notReadable.isReadable());
QTest::ignoreMessage(QtWarningMsg, "The device must be readable.");
QCOMPARE(query.setFocus(¬Readable), false);
}
}
void tst_QXmlQuery::fnDocNetworkAccessSuccess() const
{
#if defined(Q_OS_WINCE) && !defined(_X86_)
QStringList testsToSkip;
testsToSkip << "http scheme" << "ftp scheme";
if (testsToSkip.contains(QTest::currentDataTag()))
QSKIP("Network tests are currently unsupported on Windows CE.");
#endif
QFETCH(QUrl, uriToOpen);
QFETCH(QByteArray, expectedOutput);
if(!uriToOpen.isValid())
qDebug() << "uriToOpen:" << uriToOpen;
QVERIFY(uriToOpen.isValid());
QXmlQuery query;
query.bindVariable(QLatin1String("uri"), QVariant(uriToOpen));
query.setQuery(QLatin1String("declare variable $uri external;\ndoc($uri)"));
QVERIFY(query.isValid());
QByteArray result;
QBuffer buffer(&result);
QVERIFY(buffer.open(QIODevice::WriteOnly));
QXmlSerializer serializer(query, &buffer);
QVERIFY(query.evaluateTo(&serializer));
QCOMPARE(result, expectedOutput);
}
void tst_QXmlQuery::fnDocNetworkAccessSuccess_data() const
{
QTest::addColumn<QUrl>("uriToOpen");
QTest::addColumn<QByteArray>("expectedOutput");
QTest::newRow("file scheme")
<< inputFileAsURI(QFINDTESTDATA("input.xml"))
<< QByteArray("<!-- This is just a file for testing. --><input/>");
QTest::newRow("data scheme with ASCII")
/* QUrl::toPercentEncoding(QLatin1String("<e/>")) yields "%3Ce%2F%3E". */
<< QUrl::fromEncoded("data:application/xml,%3Ce%2F%3E")
<< QByteArray("<e/>");
QTest::newRow("data scheme with ASCII no MIME type")
<< QUrl::fromEncoded("data:,%3Ce%2F%3E")
<< QByteArray("<e/>");
QTest::newRow("data scheme with base 64")
<< QUrl::fromEncoded("data:application/xml;base64,PGUvPg==")
<< QByteArray("<e/>");
QTest::newRow("qrc scheme")
<< QUrl::fromEncoded("qrc:/QXmlQueryTestData/data/oneElement.xml")
<< QByteArray("<oneElement/>");
if(!m_testNetwork)
return;
QTest::newRow("http scheme")
<< QUrl(QString("http://" + QtNetworkSettings::serverName() + "/qtest/qxmlquery/wellFormed.xml"))
<< QByteArray("<!-- a comment --><e from=\"http\">Some Text</e>");
QTest::newRow("ftp scheme")
<< QUrl(QString("ftp://" + QtNetworkSettings::serverName() + "/pub/qxmlquery/wellFormed.xml"))
<< QByteArray("<!-- a comment --><e from=\"ftp\">Some Text</e>");
}
void tst_QXmlQuery::fnDocNetworkAccessFailure() const
{
QFETCH(QUrl, uriToOpen);
QVERIFY(uriToOpen.isValid());
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.bindVariable(QLatin1String("uri"), QVariant(uriToOpen));
query.setQuery(QLatin1String("declare variable $uri external;\ndoc($uri)"));
QVERIFY(query.isValid());
QXmlResultItems result;
query.evaluateTo(&result);
while(!result.next().isNull())
{
/* Just loop until the end. */
}
// TODO do something that triggers a /timeout/.
QVERIFY(result.hasError());
}
void tst_QXmlQuery::fnDocNetworkAccessFailure_data() const
{
QTest::addColumn<QUrl>("uriToOpen");
QTest::newRow("data scheme, not-well-formed")
<< QUrl(QLatin1String("data:application/xml;base64,PGUvg==="));
QTest::newRow("file scheme, non-existant file")
<< QUrl(QLatin1String("file:///example.com/does/notExist.xml"));
QTest::newRow("http scheme, file not found")
<< QUrl(QLatin1String("http://www.example.com/does/not/exist.xml"));
QTest::newRow("http scheme, nonexistent host")
<< QUrl(QLatin1String("http://this.host.does.not.exist.I.SWear"));
QTest::newRow("qrc scheme, not well-formed")
<< QUrl(QLatin1String("qrc:/QXmlQueryTestData/notWellformed.xml"));
QTest::newRow("'qrc:/', non-existing file")
<< QUrl(QLatin1String("qrc:/QXmlQueryTestData/data/thisFileDoesNotExist.xml"));
if(!m_testNetwork)
return;
QTest::newRow("http scheme, not well-formed")
<< QUrl(QString("http://" + QtNetworkSettings::serverName() + "/qtest/qxmlquery/notWellformed.xml"));
QTest::newRow("https scheme, not well-formed")
<< QUrl(QString("https://" + QtNetworkSettings::serverName() + "/qtest/qxmlquery/notWellformedViaHttps.xml"));
QTest::newRow("https scheme, nonexistent host")
<< QUrl(QLatin1String("https://this.host.does.not.exist.I.SWear"));
QTest::newRow("ftp scheme, nonexistent host")
<< QUrl(QLatin1String("ftp://this.host.does.not.exist.I.SWear"));
QTest::newRow("ftp scheme, not well-formed")
<< QUrl(QString("ftp://" + QtNetworkSettings::serverName() + "/pub/qxmlquery/notWellFormed.xml"));
}
/*!
Create a network timeout from a QIODevice binding such
that we ensure we don't hang infinitely.
*/
void tst_QXmlQuery::fnDocOnQIODeviceTimeout() const
{
if(!m_testNetwork)
return;
QTcpServer server;
server.listen(QHostAddress::LocalHost, 1088);
QTcpSocket client;
client.connectToHost("localhost", 1088);
QVERIFY(client.isReadable());
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.bindVariable(QLatin1String("inDevice"), &client);
query.setQuery(QLatin1String("declare variable $inDevice external;\ndoc($inDevice)"));
QVERIFY(query.isValid());
QXmlResultItems result;
query.evaluateTo(&result);
QXmlItem next(result.next());
while(!next.isNull())
{
next = result.next();
}
QVERIFY(result.hasError());
}
/*!
When changing query, the static context must change too, such that
the source locations are updated.
*/
void tst_QXmlQuery::recompilationWithEvaluateToResultFailing() const
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("1 + 1")); /* An arbitrary valid query. */
QVERIFY(query.isValid()); /* Trigger query compilation. */
query.setQuery(QLatin1String("fn:doc('doesNotExist.example.com.xml')")); /* An arbitrary invalid query that make use of a source location. */
QVERIFY(query.isValid()); /* Trigger second compilation. */
QXmlResultItems items;
query.evaluateTo(&items);
items.next();
QVERIFY(items.hasError());
}
void tst_QXmlQuery::secondEvaluationWithEvaluateToResultFailing() const
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("1 + 1")); /* An arbitrary valid query. */
QVERIFY(query.isValid()); /* Trigger query compilation. */
query.setQuery(QLatin1String("fn:doc('doesNotExist.example.com.xml')")); /* An arbitrary invalid query that make use of a source location. */
/* We don't call isValid(). */
QXmlResultItems items;
query.evaluateTo(&items);
items.next();
QVERIFY(items.hasError());
}
/*!
Compilation is triggered in the evaluation function due to no call to QXmlQuery::isValid().
*/
void tst_QXmlQuery::recompilationWithEvaluateToReceiver() const
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("1 + 1")); /* An arbitrary valid query. */
QVERIFY(query.isValid()); /* Trigger query compilation. */
query.setQuery(QLatin1String("fn:doc('doesNotExist.example.com.xml')")); /* An arbitrary invalid query that make use of a source location. */
/* We don't call isValid(). */
QByteArray dummy;
QBuffer buffer(&dummy);
buffer.open(QIODevice::WriteOnly);
QXmlSerializer serializer(query, &buffer);
QVERIFY(!query.evaluateTo(&serializer));
}
void tst_QXmlQuery::evaluateToQStringListOnInvalidQuery() const
{
MessageSilencer silencer;
/* Invoke on a default constructed object. */
{
QXmlQuery query;
QStringList out;
QVERIFY(!query.evaluateTo(&out));
}
/* Invoke on a syntactically invalid query. */
{
QXmlQuery query;
QStringList out;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("1 + "));
QVERIFY(!query.evaluateTo(&out));
}
/* Invoke on a query with the wrong type, one atomic. */
{
QXmlQuery query;
QStringList out;
query.setQuery(QLatin1String("1"));
query.setMessageHandler(&silencer);
QVERIFY(!query.evaluateTo(&out));
}
/* Invoke on a query with the wrong type, one element. */
{
QXmlQuery query;
QStringList out;
query.setQuery(QLatin1String("<e/>"));
QVERIFY(!query.evaluateTo(&out));
}
/* Invoke on a query with the wrong type, mixed nodes & atomics. */
{
QXmlQuery query;
QStringList out;
query.setQuery(QLatin1String("<e/>, 1, 'a string'"));
query.setMessageHandler(&silencer);
QVERIFY(!query.evaluateTo(&out));
}
/* Evaluate the empty sequence. */
{
QXmlQuery query;
QStringList out;
query.setQuery(QLatin1String("()"));
QVERIFY(!query.evaluateTo(&out));
QVERIFY(out.isEmpty());
}
}
void tst_QXmlQuery::evaluateToQStringList() const
{
QFETCH(QString, queryString);
QFETCH(QStringList, expectedOutput);
QXmlQuery query;
query.setQuery(queryString);
QStringList out;
QVERIFY(query.isValid());
QVERIFY(query.evaluateTo(&out));
QCOMPARE(out, expectedOutput);
}
void tst_QXmlQuery::evaluateToQStringListTriggerWarnings() const
{
QXmlQuery query;
QTest::ignoreMessage(QtWarningMsg, "A non-null callback must be passed.");
QCOMPARE(query.evaluateTo(static_cast<QStringList *>(0)),
false);
}
void tst_QXmlQuery::evaluateToQStringList_data() const
{
QTest::addColumn<QString>("queryString");
QTest::addColumn<QStringList>("expectedOutput");
QTest::newRow("One atomic")
<< QString::fromLatin1("(1 + 1) cast as xs:string")
<< QStringList(QString::fromLatin1("2"));
{
QStringList expected;
expected << QLatin1String("2");
expected << QLatin1String("a string");
QTest::newRow("Two atomics")
<< QString::fromLatin1("(1 + 1) cast as xs:string, 'a string'")
<< expected;
}
QTest::newRow("A query which evaluates to sub-types of xs:string.")
<< QString::fromLatin1("xs:NCName('NCName'), xs:normalizedString(' a b c ')")
<< (QStringList() << QString::fromLatin1("NCName")
<< QString::fromLatin1(" a b c "));
QTest::newRow("A query which evaluates to two elements.")
<< QString::fromLatin1("string(<e>theString1</e>), string(<e>theString2</e>)")
<< (QStringList() << QString::fromLatin1("theString1")
<< QString::fromLatin1("theString2"));
}
/*!
Ensure that we don't automatically convert non-xs:string values.
*/
void tst_QXmlQuery::evaluateToQStringListNoConversion() const
{
QXmlQuery query;
query.setQuery(QString::fromLatin1("<e/>"));
QVERIFY(query.isValid());
QStringList result;
QVERIFY(!query.evaluateTo(&result));
}
void tst_QXmlQuery::evaluateToQIODevice() const
{
/* an XQuery, check that no indentation is performed. */
{
QBuffer out;
QVERIFY(out.open(QIODevice::ReadWrite));
QXmlQuery query;
query.setQuery(QLatin1String("<a><b/></a>"));
QVERIFY(query.isValid());
QVERIFY(query.evaluateTo(&out));
QCOMPARE(out.data(), QByteArray("<a><b/></a>"));
}
}
void tst_QXmlQuery::evaluateToQIODeviceTriggerWarnings() const
{
QXmlQuery query;
QTest::ignoreMessage(QtWarningMsg, "The pointer to the device cannot be null.");
QCOMPARE(query.evaluateTo(static_cast<QIODevice *>(0)),
false);
QBuffer buffer;
QTest::ignoreMessage(QtWarningMsg, "The device must be writable.");
QCOMPARE(query.evaluateTo(&buffer),
false);
}
void tst_QXmlQuery::evaluateToQIODeviceSignature() const
{
/* The function should be const. */
{
QBuffer out;
QVERIFY(out.open(QIODevice::ReadWrite));
const QXmlQuery query;
query.evaluateTo(&out);
}
}
void tst_QXmlQuery::evaluateToQIODeviceOnInvalidQuery() const
{
QBuffer out;
QVERIFY(out.open(QIODevice::WriteOnly));
/* On syntactically invalid query. */
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("1 +"));
QVERIFY(!query.isValid());
QVERIFY(!query.evaluateTo(&out));
}
/* On null QXmlQuery instance. */
{
QXmlQuery query;
QVERIFY(!query.evaluateTo(&out));
}
}
void tst_QXmlQuery::setQueryQIODeviceQUrl() const
{
/* Basic test. */
{
QBuffer buffer;
buffer.setData("1, 2, 2 + 1");
QVERIFY(buffer.open(QIODevice::ReadOnly));
QXmlQuery query;
query.setQuery(&buffer);
QVERIFY(query.isValid());
QXmlResultItems result;
query.evaluateTo(&result);
QCOMPARE(result.next().toAtomicValue(), QVariant(1));
QCOMPARE(result.next().toAtomicValue(), QVariant(2));
QCOMPARE(result.next().toAtomicValue(), QVariant(3));
QVERIFY(result.next().isNull());
QVERIFY(!result.hasError());
}
/* Set query that is invalid. */
{
QBuffer buffer;
buffer.setData("1, ");
QVERIFY(buffer.open(QIODevice::ReadOnly));
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(&buffer);
QVERIFY(!query.isValid());
}
/* Check that the base URI passes through. */
{
QBuffer buffer;
buffer.setData("string(static-base-uri())");
QVERIFY(buffer.open(QIODevice::ReadOnly));
QXmlQuery query;
query.setQuery(&buffer, QUrl::fromEncoded("http://www.example.com/QIODeviceQUrl"));
QVERIFY(query.isValid());
QStringList result;
query.evaluateTo(&result);
QCOMPARE(result, QStringList(QLatin1String("http://www.example.com/QIODeviceQUrl")));
}
}
void tst_QXmlQuery::setQueryQIODeviceQUrlTriggerWarnings() const
{
QXmlQuery query;
QTest::ignoreMessage(QtWarningMsg, "A null QIODevice pointer cannot be passed.");
query.setQuery(0);
QBuffer buffer;
QTest::ignoreMessage(QtWarningMsg, "The device must be readable.");
query.setQuery(&buffer);
}
void tst_QXmlQuery::setQueryQString() const
{
/* Basic test. */
{
QXmlQuery query;
query.setQuery(QLatin1String("1, 2, 2 + 1"));
QVERIFY(query.isValid());
QXmlResultItems result;
query.evaluateTo(&result);
QCOMPARE(result.next().toAtomicValue(), QVariant(1));
QCOMPARE(result.next().toAtomicValue(), QVariant(2));
QCOMPARE(result.next().toAtomicValue(), QVariant(3));
QVERIFY(result.next().isNull());
QVERIFY(!result.hasError());
}
/* Set query that is invalid. */
{
MessageSilencer silencer;
QXmlQuery query;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("1, "));
QVERIFY(!query.isValid());
}
/* Check that the base URI passes through. */
{
QXmlQuery query;
query.setQuery(QLatin1String("string(static-base-uri())"), QUrl::fromEncoded("http://www.example.com/QIODeviceQUrl"));
QVERIFY(query.isValid());
QStringList result;
query.evaluateTo(&result);
QCOMPARE(result, QStringList(QLatin1String("http://www.example.com/QIODeviceQUrl")));
}
}
void tst_QXmlQuery::setQueryQUrlSuccess() const
{
#if defined(Q_OS_WINCE) && !defined(_X86_)
QStringList testsToSkip;
testsToSkip << "A valid query via the ftp scheme" << "A valid query via the http scheme";
if (testsToSkip.contains(QTest::currentDataTag()))
QSKIP("Network tests are currently unsupported on Windows CE.");
#endif
QFETCH(QUrl, queryURI);
QFETCH(QByteArray, expectedOutput);
QVERIFY(queryURI.isValid());
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(queryURI);
QVERIFY(query.isValid());
QByteArray out;
QBuffer buffer(&out);
QVERIFY(buffer.open(QIODevice::WriteOnly));
QXmlSerializer serializer(query, &buffer);
query.evaluateTo(&serializer);
QCOMPARE(out, expectedOutput);
}
void tst_QXmlQuery::setQueryQUrlSuccess_data() const
{
QTest::addColumn<QUrl>("queryURI");
QTest::addColumn<QByteArray>("expectedOutput");
QTest::newRow("A valid query via the data scheme")
<< QUrl::fromEncoded("data:application/xml,1%20%2B%201") /* "1 + 1" */
<< QByteArray("2");
QTest::newRow("A valid query via the file scheme")
<< QUrl::fromLocalFile(inputFile(m_xmlPatternsDir + QLatin1String("/queries/") + QLatin1String("onePlusOne.xq")))
<< QByteArray("2");
if(!m_testNetwork)
return;
QTest::newRow("A valid query via the ftp scheme")
<< QUrl::fromEncoded(QString("ftp://" + QtNetworkSettings::serverName() + "/pub/qxmlquery/viaFtp.xq").toLatin1())
<< QByteArray("This was received via FTP");
QTest::newRow("A valid query via the http scheme")
<< QUrl::fromEncoded(QString("http://" + QtNetworkSettings::serverName() + "/qtest/qxmlquery/viaHttp.xq").toLatin1())
<< QByteArray("This was received via HTTP.");
}
void tst_QXmlQuery::setQueryQUrlFailSucceed() const
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("1 + 1"));
QVERIFY(query.isValid());
query.setQuery(QUrl::fromEncoded("file://example.com/does/not/exist"));
QVERIFY(!query.isValid());
}
void tst_QXmlQuery::setQueryQUrlFailure() const
{
QFETCH(QUrl, queryURI);
MessageSilencer silencer;
QXmlQuery query;
query.setMessageHandler(&silencer);
query.setQuery(queryURI);
QVERIFY(!query.isValid());
}
void tst_QXmlQuery::setQueryQUrlFailure_data() const
{
QTest::addColumn<QUrl>("queryURI");
QTest::newRow("Query via file:// that does not exist.")
<< QUrl::fromEncoded("file://example.com/does/not/exist");
QTest::newRow("A query via file:// that is completely empty, but readable.")
<< QUrl::fromLocalFile(QCoreApplication::applicationFilePath()).resolved(QUrl("../xmlpatterns/queries/completelyEmptyQuery.xq"));
{
const QString name(QLatin1String("nonReadableFile.xq"));
QFile outFile(name);
QVERIFY(outFile.open(QIODevice::WriteOnly));
outFile.write(QByteArray("1"));
outFile.close();
/* On some windows versions, this fails, so we don't check that this works with QVERIFY. */
outFile.setPermissions(QFile::Permissions(QFile::Permissions()));
QTest::newRow("Query via file:/ that does not have read permissions.")
<< QUrl::fromLocalFile(QCoreApplication::applicationFilePath()).resolved(QUrl("nonReadableFile.xq"));
}
if(!m_testNetwork)
return;
QTest::newRow("Query via HTTP that does not exist.")
<< QUrl::fromEncoded("http://example.com/NoQuery/ISWear");
/*
QTest::newRow("Query via FTP that does not exist.")
<< QUrl::fromEncoded("ftp://example.com/NoQuery/ISWear");
*/
QTest::newRow("A query via http:// that is completely empty, but readable.")
<< QUrl::fromEncoded(QString(
"http://" + QtNetworkSettings::serverName() + "/qtest/qxmlquery/completelyEmptyQuery.xq").toLatin1());
QTest::newRow("A query via ftp:// that is completely empty, but readable.")
<< QUrl::fromEncoded(QString(
"ftp://" + QtNetworkSettings::serverName() + "/pub/qxmlquery/completelyEmptyQuery.xq").toLatin1());
}
void tst_QXmlQuery::setQueryQUrlBaseURI() const
{
QFETCH(QUrl, inputBaseURI);
QFETCH(QUrl, expectedBaseURI);
QXmlQuery query;
query.setQuery(QUrl(QLatin1String("qrc:/QXmlQueryTestData/queries/staticBaseURI.xq")), inputBaseURI);
QVERIFY(query.isValid());
QStringList result;
QVERIFY(query.evaluateTo(&result));
QCOMPARE(result.count(), 1);
if(qstrcmp(QTest::currentDataTag(), "Relative base URI") == 0)
checkBaseURI(QUrl(result.first()), QCoreApplication::applicationFilePath());
else
QCOMPARE(result.first(), expectedBaseURI.toString());
}
void tst_QXmlQuery::setQueryQUrlBaseURI_data() const
{
QTest::addColumn<QUrl>("inputBaseURI");
QTest::addColumn<QUrl>("expectedBaseURI");
QTest::newRow("absolute HTTP")
<< QUrl(QLatin1String("http://www.example.com/"))
<< QUrl(QLatin1String("http://www.example.com/"));
QTest::newRow("None, so the query URI is used")
<< QUrl()
<< QUrl(QLatin1String("qrc:/QXmlQueryTestData/queries/staticBaseURI.xq"));
QTest::newRow("Relative base URI")
<< QUrl(QLatin1String("../data/relative.uri"))
<< QUrl();
}
/*!
1. Create a valid query.
2. Call setQuery(QUrl), with a query file that doesn't exist.
3. Verify that the query has changed state into invalid.
*/
void tst_QXmlQuery::setQueryWithNonExistentQUrlOnValidQuery() const
{
QXmlQuery query;
MessageSilencer messageSilencer;
query.setMessageHandler(&messageSilencer);
query.setQuery(QLatin1String("1 + 1"));
QVERIFY(query.isValid());
query.setQuery(QUrl::fromEncoded("qrc:/QXmlQueryTestData/DOESNOTEXIST.xq"));
QVERIFY(!query.isValid());
}
/*!
1. Create a valid query.
2. Call setQuery(QUrl), with a query file that is invalid.
3. Verify that the query has changed state into invalid.
*/
void tst_QXmlQuery::setQueryWithInvalidQueryFromQUrlOnValidQuery() const
{
QXmlQuery query;
MessageSilencer messageSilencer;
query.setMessageHandler(&messageSilencer);
query.setQuery(QLatin1String("1 + 1"));
QVERIFY(query.isValid());
query.setQuery(QUrl::fromEncoded("qrc:/QXmlQueryTestData/queries/syntaxError.xq"));
QVERIFY(!query.isValid());
}
/*!
This triggered two bugs:
- First, the DynamicContext wasn't assigned to QXmlResultItems, meaning it went out of
scope and therefore deallocated the document pool, and calls
to QXmlResultItems::next() would use dangling pointers.
- Conversion between QPatternist::Item and QXmlItem was incorrectly done, leading to nodes
being treated as atomic values, and subsequent crashes.
*/
void tst_QXmlQuery::retrieveNameFromQuery() const
{
QFETCH(QString, queryString);
QFETCH(QString, expectedName);
QXmlQuery query;
query.setQuery(queryString);
QVERIFY(query.isValid());
QXmlResultItems result;
query.evaluateTo(&result);
QVERIFY(!result.hasError());
const QXmlItem item(result.next());
QVERIFY(!result.hasError());
QVERIFY(!item.isNull());
QVERIFY(item.isNode());
const QXmlNodeModelIndex node(item.toNodeModelIndex());
QVERIFY(!node.isNull());
QCOMPARE(node.model()->name(node).localName(query.namePool()), expectedName);
}
void tst_QXmlQuery::retrieveNameFromQuery_data() const
{
QTest::addColumn<QString>("queryString");
QTest::addColumn<QString>("expectedName");
QTest::newRow("Document-node")
<< QString::fromLatin1("document{<elementName/>}")
<< QString();
QTest::newRow("Element")
<< QString::fromLatin1("document{<elementName/>}/*")
<< QString::fromLatin1("elementName");
}
/*!
Binding a null QString leads to no variable binding, but an
empty non-null QString is possible.
*/
void tst_QXmlQuery::bindEmptyNullString() const
{
MessageSilencer messageHandler;
QXmlQuery query;
query.setMessageHandler(&messageHandler);
query.setQuery(QLatin1String("declare variable $v external; $v"));
/* Here, we effectively pass an invalid QVariant. */
query.bindVariable(QLatin1String("v"), QVariant(QString()));
QVERIFY(!query.isValid());
QStringList result;
QVERIFY(!query.evaluateTo(&result));
}
void tst_QXmlQuery::bindEmptyString() const
{
QXmlQuery query;
query.bindVariable(QLatin1String("v"), QVariant(QString(QLatin1String(""))));
query.setQuery(QLatin1String("declare variable $v external; $v"));
QVERIFY(query.isValid());
QStringList result;
QVERIFY(query.evaluateTo(&result));
QStringList expected((QString()));
QCOMPARE(result, expected);
}
void tst_QXmlQuery::cleanupTestCase() const
{
/* Remove a weird file we created. */
const QString name(QLatin1String("nonReadableFile.xq"));
if(QFile::exists(name))
{
QFile file(name);
QVERIFY(file.setPermissions(QFile::WriteOwner));
QVERIFY(file.remove());
}
}
void tst_QXmlQuery::declareUnavailableExternal() const
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("declare variable $var external;"
"1 + 1"));
/* We do not bind $var with QXmlQuery::bindVariable(). */
QVERIFY(!query.isValid());
}
/*!
This test triggers an assert in one of the cache iterator
with MSVC 2005 when compiled in debug mode.
*/
void tst_QXmlQuery::msvcCacheIssue() const
{
QXmlQuery query;
query.bindVariable(QLatin1String("externalVariable"), QXmlItem("Variable Value"));
query.setQuery(QUrl::fromLocalFile(m_xmlPatternsDir + QLatin1String("/queries/") + QString::fromLatin1("externalVariableUsedTwice.xq")));
QStringList result;
QVERIFY(query.evaluateTo(&result));
QCOMPARE(result,
QStringList() << QString::fromLatin1("Variable Value") << QString::fromLatin1("Variable Value"));
}
void tst_QXmlQuery::unavailableExternalVariable() const
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("declare variable $foo external; 1"));
QVERIFY(!query.isValid());
}
/*!
Ensure that setUriResolver() affects \c fn:doc() and \c fn:doc-available().
*/
void tst_QXmlQuery::useUriResolver() const
{
class TestUriResolver : public QAbstractUriResolver
, private TestFundament
{
public:
TestUriResolver() {}
virtual QUrl resolve(const QUrl &relative,
const QUrl &baseURI) const
{
Q_UNUSED(relative);
QString fixedInputFile = inputFile(m_xmlPatternsDir + QLatin1String("/queries/") + QLatin1String("simpleDocument.xml"));
#ifdef Q_OS_WIN
// A file path with drive letter is not a valid relative URI, so remove the drive letter.
// Note that can't just use inputFileAsURI() instead of inputFile() as that doesn't
// produce a relative URI either.
if (fixedInputFile.size() > 1 && fixedInputFile.at(1) == QLatin1Char(':'))
fixedInputFile.remove(0, 2);
#endif
return baseURI.resolved(fixedInputFile);
}
};
const TestUriResolver uriResolver;
QXmlQuery query;
query.setUriResolver(&uriResolver);
query.setQuery(QLatin1String("let $i := 'http://www.example.com/DoesNotExist'"
"return (string(doc($i)), doc-available($i))"));
QXmlResultItems result;
query.evaluateTo(&result);
QVERIFY(!result.hasError());
QCOMPARE(result.next().toAtomicValue().toString(), QString::fromLatin1("text text node"));
QCOMPARE(result.next().toAtomicValue().toBool(), true);
QVERIFY(result.next().isNull());
QVERIFY(!result.hasError());
}
void tst_QXmlQuery::queryWithFocusAndVariable() const
{
QXmlQuery query;
query.setFocus(QXmlItem(5));
query.bindVariable(QLatin1String("var"), QXmlItem(2));
query.setQuery(QLatin1String("string(. * $var)"));
QStringList result;
QVERIFY(query.evaluateTo(&result));
QCOMPARE(result, QStringList(QLatin1String("10")));
}
void tst_QXmlQuery::undefinedFocus() const
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("."));
QVERIFY(!query.isValid());
}
void tst_QXmlQuery::basicFocusUsage() const
{
QXmlQuery query;
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setFocus(QXmlItem(5));
query.setQuery(QLatin1String("string(. * .)"));
QVERIFY(query.isValid());
QStringList result;
QVERIFY(query.evaluateTo(&result));
QCOMPARE(result, QStringList(QLatin1String("25")));
}
/*!
Triggers an ownership related crash.
*/
void tst_QXmlQuery::copyCheckMessageHandler() const
{
QXmlQuery query;
QCOMPARE(query.messageHandler(), static_cast<QAbstractMessageHandler *>(0));
query.setQuery(QLatin1String("doc('qrc:/QXmlQueryTestData/data/oneElement.xml')"));
/* By now, we should have set the builtin message handler. */
const QAbstractMessageHandler *const messageHandler = query.messageHandler();
QVERIFY(messageHandler);
{
/* This copies QXmlQueryPrivate::m_ownerObject, and its destructor
* will delete it, and hence the builtin message handler attached to it. */
QXmlQuery copy(query);
}
QXmlResultItems result;
query.evaluateTo(&result);
while(!result.next().isNull())
{
}
QVERIFY(!result.hasError());
}
void tst_QXmlQuery::queryLanguage() const
{
/* Check default value. */
{
const QXmlQuery query;
QCOMPARE(query.queryLanguage(), QXmlQuery::XQuery10);
}
/* Check default value of copies default instance. */
{
const QXmlQuery query1;
const QXmlQuery query2(query1);
QCOMPARE(query1.queryLanguage(), QXmlQuery::XQuery10);
QCOMPARE(query2.queryLanguage(), QXmlQuery::XQuery10);
}
}
void tst_QXmlQuery::queryLanguageSignature() const
{
/* This getter should be const. */
QXmlQuery query;
query.queryLanguage();
}
void tst_QXmlQuery::enumQueryLanguage() const
{
/* These enum values should be possible to OR for future plans. */
QCOMPARE(int(QXmlQuery::XQuery10), 1);
QCOMPARE(int(QXmlQuery::XSLT20), 2);
QCOMPARE(int(QXmlQuery::XmlSchema11IdentityConstraintSelector), 1024);
QCOMPARE(int(QXmlQuery::XmlSchema11IdentityConstraintField), 2048);
QCOMPARE(int(QXmlQuery::XPath20), 4096);
}
void tst_QXmlQuery::setInitialTemplateNameQXmlName() const
{
QXmlQuery query(QXmlQuery::XSLT20);
QXmlNamePool np(query.namePool());
const QXmlName name(np, QLatin1String("main"));
query.setInitialTemplateName(name);
QCOMPARE(query.initialTemplateName(), name);
query.setQuery(QUrl(inputFileAsURI(m_xmlPatternsDir + QLatin1String("/stylesheets/namedTemplate.xsl"))));
QVERIFY(query.isValid());
QBuffer result;
QVERIFY(result.open(QIODevice::ReadWrite));
QXmlSerializer serializer(query, &result);
query.evaluateTo(&serializer);
QCOMPARE(result.data(), QByteArray("1 2 3 4 5"));
// TODO invoke a template which has required params.
}
void tst_QXmlQuery::setInitialTemplateNameQXmlNameSignature() const
{
QXmlQuery query;
QXmlNamePool np(query.namePool());
const QXmlName name(np, QLatin1String("foo"));
/* The signature should take a const reference. */
query.setInitialTemplateName(name);
}
void tst_QXmlQuery::setInitialTemplateNameQString() const
{
QXmlQuery query;
QXmlNamePool np(query.namePool());
query.setInitialTemplateName(QLatin1String("foo"));
QCOMPARE(query.initialTemplateName(), QXmlName(np, QLatin1String("foo")));
}
void tst_QXmlQuery::setInitialTemplateNameQStringSignature() const
{
const QString name(QLatin1String("name"));
QXmlQuery query;
/* We should take a const reference. */
query.setInitialTemplateName(name);
}
void tst_QXmlQuery::initialTemplateName() const
{
/* Check our default value. */
QXmlQuery query;
QCOMPARE(query.initialTemplateName(), QXmlName());
QVERIFY(query.initialTemplateName().isNull());
}
void tst_QXmlQuery::initialTemplateNameSignature() const
{
const QXmlQuery query;
/* This should be a const member. */
query.initialTemplateName();
}
void tst_QXmlQuery::setNetworkAccessManager() const
{
/* Ensure fn:doc() picks up the right QNetworkAccessManager. */
{
NetworkOverrider networkOverrider(QUrl(QLatin1String("tag:example.com:DOESNOTEXIST")),
QUrl(inputFileAsURI(m_xmlPatternsDir + QLatin1String("/queries/simpleDocument.xml"))));
QVERIFY(networkOverrider.isValid());
QXmlQuery query;
query.setNetworkAccessManager(&networkOverrider);
query.setQuery(QLatin1String("string(doc('tag:example.com:DOESNOTEXIST'))"));
QVERIFY(query.isValid());
QStringList result;
QVERIFY(query.evaluateTo(&result));
QCOMPARE(result, QStringList(QLatin1String("text text node")));
}
/* Ensure setQuery() is using the right network manager. */
{
NetworkOverrider networkOverrider(QUrl(QLatin1String("tag:example.com:DOESNOTEXIST")),
QUrl(inputFileAsURI(m_xmlPatternsDir + QLatin1String("/queries/concat.xq"))));
QVERIFY(networkOverrider.isValid());
QXmlQuery query;
query.setNetworkAccessManager(&networkOverrider);
query.setQuery(QUrl("tag:example.com:DOESNOTEXIST"));
QVERIFY(query.isValid());
QStringList result;
QVERIFY(query.evaluateTo(&result));
QCOMPARE(result, QStringList(QLatin1String("abcdef")));
}
}
void tst_QXmlQuery::networkAccessManagerSignature() const
{
/* Const object. */
const QXmlQuery query;
/* The function should be const. */
query.networkAccessManager();
}
void tst_QXmlQuery::networkAccessManagerDefaultValue() const
{
const QXmlQuery query;
QCOMPARE(query.networkAccessManager(), static_cast<QNetworkAccessManager *>(0));
}
void tst_QXmlQuery::networkAccessManager() const
{
/* Test that we return the network manager that was set. */
{
QNetworkAccessManager manager;
QXmlQuery query;
query.setNetworkAccessManager(&manager);
QCOMPARE(query.networkAccessManager(), &manager);
}
}
/*!
\internal
\since 4.5
1. Load a document into QXmlQuery's document cache, by executing a query which does it.
2. Set a focus
3. Change query, to one which uses the focus
4. Evaluate
Used to crash.
*/
void tst_QXmlQuery::multipleDocsAndFocus() const
{
QXmlQuery query;
/* We use string concatenation, since variable bindings might disturb what
* we're testing. */
query.setQuery(QLatin1String("string(doc('") +
inputFile(m_xmlPatternsDir + QLatin1String("/queries/simpleDocument.xml")) +
QLatin1String("'))"));
query.setFocus(QUrl(inputFileAsURI(m_xmlPatternsDir + QLatin1String("/stylesheets/documentElement.xml"))));
query.setQuery(QLatin1String("string(.)"));
QStringList result;
QVERIFY(query.evaluateTo(&result));
}
/*!
\internal
\since 4.5
1. Set a focus
2. Set a query
3. Evaluate
4. Change focus
5. Evaluate
Used to crash.
*/
void tst_QXmlQuery::multipleEvaluationsWithDifferentFocus() const
{
QXmlQuery query;
QStringList result;
query.setFocus(QUrl(inputFileAsURI(m_xmlPatternsDir + QLatin1String("/stylesheets/documentElement.xml"))));
query.setQuery(QLatin1String("string(.)"));
QVERIFY(query.evaluateTo(&result));
query.setFocus(QUrl(inputFileAsURI(m_xmlPatternsDir + QLatin1String("/stylesheets/documentElement.xml"))));
QVERIFY(query.evaluateTo(&result));
}
void tst_QXmlQuery::bindVariableQXmlQuery() const
{
QFETCH(QString, query1);
QFETCH(QString, query2);
QFETCH(QString, expectedOutput);
QFETCH(bool, expectedSuccess);
MessageSilencer silencer;
QXmlQuery xmlQuery1;
xmlQuery1.setMessageHandler(&silencer);
xmlQuery1.setQuery(query1);
QXmlQuery xmlQuery2(xmlQuery1);
xmlQuery2.bindVariable("query1", xmlQuery1);
xmlQuery2.setQuery(query2);
QString output;
const bool querySuccess = xmlQuery2.evaluateTo(&output);
QCOMPARE(querySuccess, expectedSuccess);
if(querySuccess)
QCOMPARE(output, expectedOutput);
}
void tst_QXmlQuery::bindVariableQXmlQuery_data() const
{
QTest::addColumn<QString>("query1");
QTest::addColumn<QString>("query2");
QTest::addColumn<QString>("expectedOutput");
QTest::addColumn<bool>("expectedSuccess");
QTest::newRow("First query has one atomic value.")
<< "2"
<< "1, $query1, 3"
<< "1 2 3\n"
<< true;
QTest::newRow("First query has two atomic values.")
<< "2, 3"
<< "1, $query1, 4"
<< "1 2 3 4\n"
<< true;
QTest::newRow("First query is a node.")
<< "<e/>"
<< "1, $query1, 3"
<< "1<e/>3\n"
<< true;
/* This is a good test, because it triggers the exception in the
* bindVariable() call, as supposed to when the actual evaluation is done.
*/
QTest::newRow("First query has a dynamic error.")
<< "error()"
<< "1, $query1"
<< QString() /* We don't care. */
<< false;
}
void tst_QXmlQuery::bindVariableQStringQXmlQuerySignature() const
{
QXmlQuery query1;
query1.setQuery("'dummy'");
QXmlQuery query2;
const QString name(QLatin1String("name"));
/* We should be able to take a const QXmlQuery reference. Evaluation never mutate
* QXmlQuery, and evaluation is what we do here. */
query2.bindVariable(name, const_cast<const QXmlQuery &>(query1));
}
void tst_QXmlQuery::bindVariableQXmlNameQXmlQuerySignature() const
{
QXmlNamePool np;
QXmlQuery query1(np);
query1.setQuery("'dummy'");
QXmlQuery query2;
const QXmlName name(np, QLatin1String("name"));
/* We should be able to take a const QXmlQuery reference. Evaluation never mutate
* QXmlQuery, and evaluation is what we do here. */
query2.bindVariable(name, const_cast<const QXmlQuery &>(query1));
}
/*!
Check that the QXmlName is handled correctly.
*/
void tst_QXmlQuery::bindVariableQXmlNameQXmlQuery() const
{
QXmlNamePool np;
QXmlQuery query1;
query1.setQuery(QLatin1String("1"));
QXmlQuery query2(np);
query2.bindVariable(QXmlName(np, QLatin1String("theName")), query1);
query2.setQuery("$theName");
QString result;
query2.evaluateTo(&result);
QCOMPARE(result, QString::fromLatin1("1\n"));
}
void tst_QXmlQuery::bindVariableQXmlQueryInvalidate() const
{
QXmlQuery query;
query.bindVariable(QLatin1String("name"), QVariant(1));
query.setQuery("$name");
QVERIFY(query.isValid());
QXmlQuery query2;
query2.setQuery("'query2'");
query.bindVariable(QLatin1String("name"), query2);
QVERIFY(!query.isValid());
}
void tst_QXmlQuery::unknownSourceLocation() const
{
QBuffer b;
b.setData("<a><b/><b/></a>");
b.open(QIODevice::ReadOnly);
MessageSilencer silencer;
QXmlQuery query;
query.bindVariable(QLatin1String("inputDocument"), &b);
query.setMessageHandler(&silencer);
query.setQuery(QLatin1String("doc($inputDocument)/a/(let $v := b/string() return if ($v) then $v else ())"));
QString output;
query.evaluateTo(&output);
}
void tst_QXmlQuery::identityConstraintSuccess() const
{
QXmlQuery::QueryLanguage queryLanguage = QXmlQuery::XmlSchema11IdentityConstraintSelector;
/* We run this code for Selector and Field. */
for(int i = 0; i < 3; ++i)
{
QXmlNamePool namePool;
QXmlResultItems result;
QXmlItem node;
{
QXmlQuery nodeSource(namePool);
nodeSource.setQuery(QLatin1String("<e/>"));
nodeSource.evaluateTo(&result);
node = result.next();
}
/* Basic use:
* 1. The focus is undefined, but it's still valid.
* 2. We never evaluate. */
{
QXmlQuery query(queryLanguage);
query.setQuery(QLatin1String("a"));
QVERIFY(query.isValid());
}
/* Basic use:
* 1. The focus is undefined, but it's still valid.
* 2. We afterwards set the focus. */
{
QXmlQuery query(queryLanguage, namePool);
query.setQuery(QLatin1String("a"));
query.setFocus(node);
QVERIFY(query.isValid());
}
/* Basic use:
* 1. The focus is undefined, but it's still valid.
* 2. We afterwards set the focus.
* 3. We evaluate. */
{
QXmlQuery query(queryLanguage, namePool);
query.setQuery(QString(QLatin1Char('.')));
query.setFocus(node);
QVERIFY(query.isValid());
QString result;
QVERIFY(query.evaluateTo(&result));
QCOMPARE(result, QString::fromLatin1("<e/>\n"));
}
/* A slightly more complex Field. */
{
QXmlQuery query(queryLanguage);
query.setQuery(QLatin1String("* | .//xml:*/."));
QVERIFY(query.isValid());
}
/* @ is only allowed in Field. */
if(queryLanguage == QXmlQuery::XmlSchema11IdentityConstraintField)
{
QXmlQuery query(QXmlQuery::XmlSchema11IdentityConstraintField);
query.setQuery(QLatin1String("@abc"));
QVERIFY(query.isValid());
}
/* Field allows attribute:: and child:: .*/
if(queryLanguage == QXmlQuery::XmlSchema11IdentityConstraintField)
{
QXmlQuery query(QXmlQuery::XmlSchema11IdentityConstraintField);
query.setQuery(QLatin1String("attribute::name | child::name"));
QVERIFY(query.isValid());
}
/* Selector allows only child:: .*/
{
QXmlQuery query(QXmlQuery::XmlSchema11IdentityConstraintSelector);
query.setQuery(QLatin1String("child::name"));
QVERIFY(query.isValid());
}
if(i == 0)
queryLanguage = QXmlQuery::XmlSchema11IdentityConstraintField;
else if(i == 1)
queryLanguage = QXmlQuery::XPath20;
}
}
Q_DECLARE_METATYPE(QXmlQuery::QueryLanguage);
/*!
We just do some basic tests for boot strapping and sanity checking. The actual regression
testing is in the Schema suite.
*/
void tst_QXmlQuery::identityConstraintFailure() const
{
QFETCH(QXmlQuery::QueryLanguage, queryLanguage);
QFETCH(QString, inputQuery);
QXmlQuery query(queryLanguage);
MessageSilencer silencer;
query.setMessageHandler(&silencer);
query.setQuery(inputQuery);
QVERIFY(!query.isValid());
}
void tst_QXmlQuery::identityConstraintFailure_data() const
{
QTest::addColumn<QXmlQuery::QueryLanguage>("queryLanguage");
QTest::addColumn<QString>("inputQuery");
QTest::newRow("We don't have element constructors in identity constraint pattern, "
"it's an XQuery feature(Selector).")
<< QXmlQuery::XmlSchema11IdentityConstraintSelector
<< QString::fromLatin1("<e/>");
QTest::newRow("We don't have functions in identity constraint pattern, "
"it's an XPath feature(Selector).")
<< QXmlQuery::XmlSchema11IdentityConstraintSelector
<< QString::fromLatin1("current-time()");
QTest::newRow("We don't have element constructors in identity constraint pattern, "
"it's an XQuery feature(Field).")
<< QXmlQuery::XmlSchema11IdentityConstraintSelector
<< QString::fromLatin1("<e/>");
QTest::newRow("We don't have functions in identity constraint pattern, "
"it's an XPath feature(Field).")
<< QXmlQuery::XmlSchema11IdentityConstraintSelector
<< QString::fromLatin1("current-time()");
QTest::newRow("@attributeName is disallowed for the selector.")
<< QXmlQuery::XmlSchema11IdentityConstraintSelector
<< QString::fromLatin1("@abc");
QTest::newRow("attribute:: is disallowed for the selector.")
<< QXmlQuery::XmlSchema11IdentityConstraintSelector
<< QString::fromLatin1("attribute::name");
QTest::newRow("ancestor::name is disallowed for the selector.")
<< QXmlQuery::XmlSchema11IdentityConstraintSelector
<< QString::fromLatin1("ancestor::name");
QTest::newRow("ancestor::name is disallowed for the field.")
<< QXmlQuery::XmlSchema11IdentityConstraintField
<< QString::fromLatin1("ancestor::name");
}
QTEST_MAIN(tst_QXmlQuery)
#include "tst_qxmlquery.moc"
|