1 (defpackage #:topic-maps.xtm-mergemap.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.variant.tests #:condition-from)
4 (:import-from #:topic-maps.mergemap.tests #:resources #:original-resources #:local #:si #:valid #:observe #:call-with-import-observer)
5 (:import-from #:topic-maps.ctm.tests #:binary-file)
6 (:export #:run-tests #:write-report #:write-reports #:checks))
7 (in-package #:topic-maps.xtm-mergemap.tests)
8 (defparameter *doc* "https://example.invalid/maps/main.ctm")
9 (defparameter *xtm* "http://psi.topicmaps.org/iso13250/xtm")
10 (defparameter *ctm* "http://psi.topicmaps.org/iso13250/ctm")
11 (defparameter *child* "https://example.invalid/maps/child.xtm")
12 (defparameter *xml* "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'><topic id='lars'><instanceOf><topicRef href='#person'/></instanceOf></topic></topicMap>")
14 (defvar *observations* nil)
15 (defvar *rejections* nil)
16 (defvar *example-map* nil)
17 (defvar *example-child* nil)
18 (defvar *original-result* nil)
19 (defun checks () *checks*)
20 (defun check (label truth)
21 (unless truth (error "XTM Mergemap check failed: ~A" label))
22 (push label *checks*) (format t "XTM-MERGEMAP-CHECK-PASS ~A~%" label))
23 (defun fields (&rest args) (apply #'topic-maps.test-foundation::fields args))
24 (defun bytes (text &optional (encoding :utf-8) bom)
25 (concatenate '(vector (unsigned-byte 8))
26 (if bom (ecase encoding (:utf-8 #(239 187 191)) (:utf-16le #(255 254)) (:utf-16be #(254 255))) #())
27 (babel:string-to-octets text :encoding encoding :use-bom nil)))
28 (defun directive (resource &optional (notation *xtm*)) (format nil "%mergemap <~A> <~A>" resource notation))
29 (defun read-map (text &rest keys)
30 (apply #'topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name "memory:parent" keys))
31 (defun current (item) (topic-maps.tmdm::canonical-item item))
32 (defun handles (map) (topic-maps.import.tests::handles map))
33 (defun state (map held) (topic-maps.import.tests::state map held))
34 (defun xml (map &optional (base *doc*)) (topic-maps.cxtm:canonicalize map :base-locator base))
35 (defun core (map name) (si map (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" name)))
36 (defun graph (label map owner player)
37 (let* ((instance (core map "instance")) (type (core map "type"))
38 (a (find-if (lambda (a) (some (lambda (r) (and (eq instance (association-role-type r))
39 (eq owner (association-role-player r)))) (association-roles a))) (topic-map-associations map)))
40 (roles (when a (association-roles a))))
41 (check (concatenate 'string label ":association-type-eq") (and a (eq (association-type a) (core map "type-instance"))))
42 (check (concatenate 'string label ":role-types-and-players-eq")
43 (and (= 2 (length roles)) (every (lambda (pair) (some (lambda (r) (and (eq (first pair) (association-role-type r))
44 (eq (second pair) (association-role-player r)))) roles))
45 (list (list instance owner) (list type player)))))
46 (check (concatenate 'string label ":parents-and-roles-played-eq")
47 (and (eq map (association-parent a)) (eq map (topic-parent owner)) (eq map (topic-parent player))
48 (every (lambda (r) (and (eq a (association-role-parent r)) (member r (topic-roles-played (association-role-player r)) :test #'eq))) roles)))
50 (defun separate-locals (label map parent child)
51 (check (concatenate 'string label ":seven-topics-two-associations-four-roles")
52 (and (= 7 (length (topic-map-topics map))) (= 2 (length (topic-map-associations map)))
53 (= 4 (loop for a in (topic-map-associations map) sum (length (association-roles a))))))
54 (let ((po (local map (concatenate 'string parent "#lars"))) (pt (local map (concatenate 'string parent "#person")))
55 (co (local map (concatenate 'string child "#lars"))) (ct (local map (concatenate 'string child "#person"))))
56 (check (concatenate 'string label ":distinct-local-identities-no-include-aliases")
57 (and po pt co ct (= 4 (length (remove-duplicates (list po pt co ct) :test #'eq)))
58 (every (lambda (topic) (= 1 (length (topic-item-identifiers topic)))) (list po pt co ct))))
59 (graph (concatenate 'string label ":parent") map po pt)
60 (graph (concatenate 'string label ":child") map co ct))
61 (check (concatenate 'string label ":three-shared-core-subjects")
62 (and (every (lambda (name) (let ((topic (core map name))) (and topic (null (topic-item-identifiers topic))
63 (= 1 (length (topic-subject-identifiers topic)))))) '("type-instance" "instance" "type")) (valid map))))
64 (defun pure (label map &optional retained)
65 (let* ((held (append retained (handles map))) (before (state map held)) (output (xml map)))
66 (check (concatenate 'string label ":deterministic") (string= output (xml map)))
67 (check (concatenate 'string label ":raw-slots-and-retained-handles-unchanged") (equal before (state map held)))))
68 (defun call-with-xtm-observer (observer thunk)
69 (let ((original (symbol-function 'topic-maps.xtm:deserialize-octets)))
71 (progn (setf (symbol-function 'topic-maps.xtm:deserialize-octets)
72 (lambda (bytes &rest keys) (funcall observer original bytes keys)))
74 (setf (symbol-function 'topic-maps.xtm:deserialize-octets) original))))
76 (defun original-case ()
77 (unless topic-maps.ctm.tests::*cxtm-corpus*
78 (setf topic-maps.ctm.tests::*cxtm-corpus* (topic-maps.test-foundation:discover-corpus)))
79 (let* ((id "ctm/in/mergemap-xtm.ctm")
80 (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id)))
81 (doc (topic-maps.test-foundation:test-case-document-iri case))
82 (child (concatenate 'string (subseq doc 0 (1+ (position #\/ doc :from-end t))) "mergemap-xtm.sub"))
83 (source-bytes (binary-file (merge-pathnames "mergemap-xtm.sub" (topic-maps.test-foundation:test-case-source-pathname case))))
84 (child-map nil) (imports 0) (reader-calls 0))
85 (multiple-value-bind (resolver calls) (resources (list (list child source-bytes "ctm/in/mergemap-xtm.sub")))
86 (let* ((map (call-with-xtm-observer
87 (lambda (saved bytes keys)
89 (check "original:raw-vector-eq-at-XTM-reader" (eq bytes source-bytes))
90 (check "original:XTM-document-and-source-key" (and (equal child (getf keys :document-iri))
91 (equal "ctm/in/mergemap-xtm.sub" (getf keys :source-key))))
92 (apply saved bytes keys))
93 (lambda () (call-with-import-observer
94 (lambda (saved target source)
95 (incf imports) (setf child-map source)
96 (let* ((held (handles source)) (before (state source held)) (output (xml source child)))
97 (multiple-value-bind (target resolve) (funcall saved target source)
98 (check "original:actual-child-map-and-import-operation" (and (= 5 (length (topic-map-topics source))) (= 1 (length (topic-map-associations source))) (not (eq source target))))
99 (check "original:source-preserved-after-import" (and (equal before (state source held)) (string= output (xml source child))))
100 (check "original:imported-content-is-copied-and-resolves-eq"
101 (every (lambda (item) (and (not (eq item source)) (not (eq item (funcall resolve item)))
102 (member (funcall resolve item) (handles target) :test #'eq))) (remove source held)))
103 (values target resolve))))
104 (lambda () (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
105 :document-iri doc :source-name id :resource-resolver resolver))))))
106 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
107 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case))))
108 (separate-locals "original" map doc child) (pure "original" map)
109 (check "original:one-request-one-reader-one-import" (and (= 1 reader-calls imports) (= 1 (length (funcall calls)))))
110 (check "original:1519-byte-original-baseline-exact" (and (= 1519 (length expected) (length actual)) (equalp actual expected)))
111 (topic-maps.test-foundation:record-result case :cxtm-match)
112 (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.16.3" "XTM 2.0 4.3-4.9,4.20" "TMDM 6.2,6.6-6.7,7.2" "CXTM 3.18"))
113 (setf *original-result* (list :id id :base doc :actual (xml map doc) :expected (babel:octets-to-string expected :encoding :utf-8) :match t :bytes (length actual)))
114 (setf topic-maps.ctm.tests::*cxtm-results* (append (remove id topic-maps.ctm.tests::*cxtm-results* :key (lambda (r) (getf r :id)) :test #'equal) (list *original-result*)))
115 (setf *example-map* map *example-child* child-map)
116 (push (fields "id" id "documentIRI" doc "resourceIRI" child "sourceKey" "ctm/in/mergemap-xtm.sub"
117 "requests" (coerce (funcall calls) 'vector) "model" (observe map) "childModel" (observe child-map)
118 "actualCXTM" (getf *original-result* :actual) "expectedCXTM" (getf *original-result* :expected) "bytes" 1519) *observations*)))))
120 (defun dispatch-tests ()
121 (dolist (spec (list (list "relative-full-IRI" (directive "child.xtm"))
122 (list "absolute-full-IRI" (directive *child*))
123 (list "notation-QName" (format nil "%prefix n <http://psi.topicmaps.org/iso13250/>~%%mergemap <child.xtm> n:xtm"))
124 (list "resource-and-notation-QName" (format nil "%prefix r <./>~%%prefix n <http://psi.topicmaps.org/iso13250/>~%%mergemap r:child.xtm n:xtm"))))
125 (destructuring-bind (label text) spec
126 (multiple-value-bind (resolver calls) (resources (list (list *child* (bytes *xml*) "test:source")))
127 (let ((map (read-map (format nil "~A~%lars isa person." text) :resource-resolver resolver)))
128 (separate-locals label map *doc* *child*)
129 (check (concatenate 'string label ":exact-resolver-contract")
130 (equal (topic-maps.test-foundation::json-object-members (first (funcall calls)))
131 (topic-maps.test-foundation::json-object-members
132 (fields "resourceIRI" *child* "purpose" "MERGEMAP" "fromDocumentIRI" *doc* "notation" *xtm* "sourceKey" "test:source"))))))))
134 (dolist (key '("unrelated:diagnostic-label" "different:diagnostic-label" nil))
135 (multiple-value-bind (resolver calls) (resources (list (list *child* (bytes *xml*) key)))
136 (declare (ignore calls))
137 (let ((map (read-map (format nil "~A~%lars isa person." (directive "child.xtm")) :resource-resolver resolver)))
138 (push (xml map) outputs))))
139 (check "source-key:does-not-change-topic-identifiers-or-model" (every (lambda (x) (equal x (first outputs))) outputs)))
140 (let* ((encoded (bytes (concatenate 'string "<?xml version='1.0' encoding='UTF-16'?>"
141 (substitute #\Space #\Newline *xml*)) :utf-16le t))
142 (before (copy-seq encoded)) (decoder-calls 0) (xml-seen nil)
143 (saved-decoder (symbol-function 'topic-maps.ctm::decode-ctm-resource))
144 (saved-boundary (symbol-function 'topic-maps.xml:parse-octets)))
145 (check "UTF16:negative-control-CTM-UTF8-cannot-decode"
146 (typep (condition-from (lambda () (topic-maps.ctm::decode-utf8 encoded))) 'topic-maps.ctm:ctm-rejection))
147 (multiple-value-bind (resolver calls) (resources (list (list *child* encoded "UTF16:child")))
148 (declare (ignore calls))
151 (setf (symbol-function 'topic-maps.ctm::decode-ctm-resource)
152 (lambda (&rest args) (incf decoder-calls) (apply saved-decoder args)))
153 (setf (symbol-function 'topic-maps.xml:parse-octets)
154 (lambda (bytes handler) (setf xml-seen (eq bytes encoded)) (funcall saved-boundary bytes handler)))
155 (let ((map (read-map (format nil "~A~%lars isa person." (directive "child.xtm")) :resource-resolver resolver)))
156 (separate-locals "UTF16" map *doc* *child*)
157 (check "UTF16:original-bytes-eq-at-qualified-XML-boundary" xml-seen)
158 (check "UTF16:no-CTM-decoding-no-byte-mutation" (and (zerop decoder-calls) (equalp before encoded)))))
159 (setf (symbol-function 'topic-maps.ctm::decode-ctm-resource) saved-decoder
160 (symbol-function 'topic-maps.xml:parse-octets) saved-boundary))))
161 (multiple-value-bind (resolver calls) (resources (list (list *child* (map 'vector #'identity (bytes *xml*)) "generic-vector")))
162 (declare (ignore calls))
163 (let ((map (read-map (directive "child.xtm") :resource-resolver resolver)))
164 (check "resolver:generic-byte-vector-storage-supported-without-decoding" (= 5 (length (topic-map-topics map)))))))
166 (defun repeat-and-mixed-tests ()
167 (let ((old-items nil) (imports 0))
168 (multiple-value-bind (resolver calls) (resources (list (list *child* (bytes *xml*) "repeat:child")))
169 (let ((map (call-with-import-observer
170 (lambda (saved target source)
172 (let* ((held (handles source)) (before (state source held)))
173 (multiple-value-bind (result resolve) (funcall saved target source)
174 (check (format nil "repeat:~D:source-raw-state-preserved" imports) (equal before (state source held)))
176 (setf old-items (mapcar (lambda (item) (funcall resolve item)) (remove source held)))
177 (check "repeat:all-retained-target-handles-canonical" (every (lambda (item) (member (current item) (handles target) :test #'eq)) old-items)))
178 (values result resolve))))
179 (lambda () (read-map (format nil "~A~%~A~%lars isa person." (directive "child.xtm") (directive "child.xtm")) :resource-resolver resolver)))))
180 (check "repeat:two-actual-resolver-and-import-calls" (and (= 2 imports) (= 2 (length (funcall calls)))))
181 (separate-locals "repeat" map *doc* *child*) (pure "repeat" map old-items))))
183 (dolist (reverse '(nil t))
184 (multiple-value-bind (resolver calls)
185 (resources (list (list *child* (bytes *xml*) "mixed:xtm")
186 (list "https://example.invalid/maps/child.ctm" (bytes "lars isa person.") "mixed:ctm")))
187 (let* ((directives (list (directive "child.xtm") (directive "child.ctm" *ctm*)))
188 (text (format nil "~{~A~%~}lars isa person." (if reverse (reverse directives) directives)))
189 (map (read-map text :resource-resolver resolver)))
190 (check (format nil "mixed:~A:actual-model-counts-and-canonical-refs" reverse)
191 (and (= 9 (length (topic-map-topics map))) (= 3 (length (topic-map-associations map))) (valid map)))
192 (check (format nil "mixed:~A:notation-dispatch-order" reverse)
193 (equal (mapcar (lambda (r) (cdr (assoc "notation" (topic-maps.test-foundation::json-object-members r) :test #'equal))) (funcall calls))
194 (if reverse (list *ctm* *xtm*) (list *xtm* *ctm*))))
195 (pure (format nil "mixed:~A" reverse) map) (push (xml map) results))))
196 (check "mixed:construction-order-independent-CXTM" (equal (first results) (second results))))
197 (multiple-value-bind (resolver calls)
198 (resources (list (list "https://example.invalid/maps/sub/child.ctm"
199 (bytes (format nil "~A~%lars isa person." (directive "../child.xtm"))) "nested:ctm")
200 (list *child* (bytes *xml*) "nested:xtm")))
201 (let* ((map (read-map (format nil "~A~%lars isa person." (directive "sub/child.ctm" *ctm*)) :resource-resolver resolver))
202 (second-request (topic-maps.test-foundation::json-object-members (second (funcall calls)))))
203 (check "nested:relative-XTM-resource-and-child-caller-context"
204 (and (= 9 (length (topic-map-topics map))) (= 3 (length (topic-map-associations map)))
205 (equal *child* (cdr (assoc "resourceIRI" second-request :test #'equal)))
206 (equal "https://example.invalid/maps/sub/child.ctm" (cdr (assoc "fromDocumentIRI" second-request :test #'equal))) (valid map)))))
207 (let ((source "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'><topic id='lars'><instanceOf><topicRef href='#person'/></instanceOf></topic><topic id='person'><subjectIdentifier href='urn:person'/></topic></topicMap>"))
208 (multiple-value-bind (resolver calls) (resources (list (list *child* (bytes source) "forward:child")))
209 (declare (ignore calls))
210 (let* ((map (read-map (format nil "person <urn:person>.~%~A~%lars isa person." (directive "child.xtm")) :resource-resolver resolver))
211 (player (local map (concatenate 'string *doc* "#person"))))
212 (check "forward:identity-merge-shared-player-EQ" (and (= 6 (length (topic-map-topics map)))
213 (eq player (local map (concatenate 'string *child* "#person"))) (eq player (si map "urn:person")) (= 2 (length (topic-item-identifiers player)))))
214 (graph "forward:child" map (local map (concatenate 'string *child* "#lars")) player)
215 (graph "forward:parent" map (local map (concatenate 'string *doc* "#lars")) player)))))
217 (defun metadata-tests ()
218 ;; Model API instrumentation is explicitly separate from the XTM reader:
219 ;; XTM Reification is still unsupported. Test the actual import boundary's
220 ;; exclusion of the source Map item, including its existing Reifier edge.
222 (multiple-value-bind (resolver calls)
223 (resources (list (list *child* (bytes "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'><itemIdentity href='urn:child-map'/><topic id='t'/></topicMap>") "metadata:child")))
224 (declare (ignore calls))
225 (let ((map (call-with-import-observer
226 (lambda (saved target source)
227 (add-item-identifier target "urn:target-map")
228 (set-item-reifier target (intern-topic target :subject-identifier "urn:target-reifier"))
229 (set-item-reifier source (first (topic-map-topics source)))
230 (let* ((held (handles source)) (before (state source held))
231 (source-reifier (item-reifier source)) (target-reifier (item-reifier target)))
232 (multiple-value-bind (result resolve) (funcall saved target source)
234 (check "metadata:source-map-state-and-reifier-EQ-preserved" (and (equal before (state source held)) (eq source (topic-reified source-reifier))))
235 (check "metadata:map-identifiers-and-map-reifier-not-copied"
236 (and (equal '("urn:target-map") (item-identifiers target)) (eq target-reifier (item-reifier target))
237 (null (topic-reified (funcall resolve source-reifier)))))
238 (check "metadata:source-map-not-a-content-item" (typep (condition-from (lambda () (funcall resolve source))) 'map-import-error))
239 (values result resolve))))
240 (lambda () (read-map (directive "child.xtm") :resource-resolver resolver)))))
241 (check "metadata:actual-import-was-reached" (and seen (valid map)))))))
243 (define-condition resolver-denied (error) ())
244 (defun failure (label text class code &key resolver expected-cause)
245 (let* ((c (condition-from (lambda () (read-map text :resource-resolver resolver))))
246 (frame (when (typep c 'topic-maps.ctm:ctm-error) (first (topic-maps.ctm:error-context c)))))
247 (check (concatenate 'string label ":condition-and-code") (and (eq (type-of c) class) (eq code (topic-maps.ctm:error-code c))))
248 (check (concatenate 'string label ":parent-directive-location")
249 (and (equal "memory:parent" (topic-maps.ctm:error-source c))
250 (= (search "%mergemap" text) (topic-maps.ctm:error-position c))
251 (plusp (topic-maps.ctm:error-line c)) (plusp (topic-maps.ctm:error-column c))))
252 (check (concatenate 'string label ":resource-and-notation-context")
253 (and (eq :mergemap (getf frame :purpose)) (equal *child* (getf frame :resource-iri)) (equal *doc* (getf frame :document-iri))
254 (equal *xtm* (getf frame :notation))))
255 (when expected-cause (check (concatenate 'string label ":original-condition-retained") (typep (topic-maps.ctm:error-cause c) expected-cause)))
256 (push (fields "label" label "class" (symbol-name (type-of c)) "code" (symbol-name (topic-maps.ctm:error-code c))
257 "source" (topic-maps.ctm:error-source c) "position" (topic-maps.ctm:error-position c)
258 "line" (topic-maps.ctm:error-line c) "column" (topic-maps.ctm:error-column c)
259 "resourceIRI" (getf frame :resource-iri) "sourceKey" (getf frame :resource-source)
260 "causeClass" (when (topic-maps.ctm:error-cause c) (symbol-name (type-of (topic-maps.ctm:error-cause c))))) *rejections*)
262 (defun negative-tests ()
263 (let ((text (format nil "before.~%~A" (directive "child.xtm"))))
264 (failure "missing-resolver" text 'topic-maps.ctm:ctm-unsupported :resource-resolver-required)
265 (failure "unknown-resource" text 'topic-maps.ctm:ctm-resource-error :resource-unavailable :resolver (lambda (&rest args) (declare (ignore args)) nil))
266 (failure "resolver-refusal" text 'topic-maps.ctm:ctm-resource-error :resource-resolver-failure
267 :resolver (lambda (&rest args) (declare (ignore args)) (error 'resolver-denied)) :expected-cause 'resolver-denied)
268 (dolist (spec '(("text" "source") (#(256) "source") (#(60) 42)))
269 (failure (format nil "invalid-result:~S" spec) text 'topic-maps.ctm:ctm-resource-error :invalid-resource-result
270 :resolver (lambda (&rest args) (declare (ignore args)) (values (first spec) (second spec)))))
272 (list "malformed-XML" (bytes "<topicMap><a></topicMap>") 'topic-maps.xtm:xtm-xml-error :xml-not-well-formed)
273 (list "invalid-UTF8" (concatenate '(vector (unsigned-byte 8)) (bytes "<r a='") #(195 40) (bytes "'/>")) 'topic-maps.xtm:xtm-xml-error :xml-not-well-formed)
274 (list "DOCTYPE-file-denied" (bytes (concatenate 'string "<!DOCTYPE topicMap SYSTEM 'file:///tmp/must-not-read'>" *xml*)) 'topic-maps.xtm:xtm-xml-error :doctype-forbidden)
275 (list "bad-version" (bytes "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.1'/>") 'topic-maps.xtm:xtm-rejection :invalid-version)
276 (list "bad-namespace" (bytes "<topicMap version='2.0'/>") 'topic-maps.xtm:xtm-rejection :invalid-namespace)
277 (list "bad-id" (bytes "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'><topic id='2bad'/></topicMap>") 'topic-maps.xtm:xtm-rejection :invalid-id)
278 (list "unsupported-XTM-mergeMap" (bytes "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'><mergeMap href='other.xtm'/></topicMap>") 'topic-maps.xtm:xtm-unsupported :resource-resolver-required)
279 (list "unsupported-occurrence-reification" (bytes "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'><mergeMap href='urn:22s:requires-explicit-resolver'/><topic id='a'><occurrence reifier='#r'><type><topicRef href='#t'/></type><resourceData datatype='http://www.w3.org/2001/XMLSchema#anyType'>O</resourceData></occurrence></topic></topicMap>") 'topic-maps.xtm:xtm-unsupported :resource-resolver-required)
280 (list "unsupported-name-variant-resourceData-datatype" (bytes "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'><mergeMap href='urn:22s:requires-explicit-resolver'/><topic id='t'><name><value>N</value><variant><scope><topicRef href='#s'/></scope><resourceData datatype='http://www.w3.org/2001/XMLSchema#anyType'>42</resourceData></variant></name></topic></topicMap>") 'topic-maps.xtm:xtm-unsupported :resource-resolver-required)))
281 (destructuring-bind (label raw cause code) spec
282 (multiple-value-bind (resolver calls) (resources (list (list *child* raw "bad:child-source")))
283 (let* ((c (failure label text (if (eq cause 'topic-maps.xtm:xtm-unsupported) 'topic-maps.ctm:mergemap-source-unsupported 'topic-maps.ctm:mergemap-source-error)
284 code :resolver resolver :expected-cause cause))
285 (original (topic-maps.ctm:error-cause c)))
286 (check (concatenate 'string label ":child-source-document-and-XML-position")
287 (and (= 1 (length (funcall calls))) (equal "bad:child-source" (topic-maps.xtm:error-source-key original))
288 (equal *child* (topic-maps.xtm:error-document-iri original)) (integerp (topic-maps.xtm:error-line original))
289 (integerp (topic-maps.xtm:error-column original))
290 (not (typep c 'topic-maps.ctm:ctm-rejection))))))))
291 (multiple-value-bind (resolver calls) (resources (list (list *child* (bytes "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'><topic id='a'><subjectIdentifier href='http://psi.topicmaps.org/iso13250/model/type'/><subjectIdentifier href='http://psi.topicmaps.org/iso13250/model/instance'/></topic></topicMap>") "bad:child-model")))
292 (declare (ignore calls))
293 (failure "TMDM-child-core-conflict" text 'topic-maps.ctm:mergemap-source-error :core-subject-conflict :resolver resolver :expected-cause 'core-subject-error)))
295 (let ((c (condition-from (lambda () (read-map (directive "child.xtm" "urn:unknown") :resource-resolver (lambda (&rest args) (declare (ignore args)) (incf requests)))))))
296 (check "unknown-notation:is-CTM-rejection-before-fetch" (and (typep c 'topic-maps.ctm:ctm-rejection) (eq :unknown-mergemap-notation (topic-maps.ctm:error-code c)) (zerop requests))))))
298 (defun conflict-tests ()
299 (dolist (kind '(:item :core))
300 (multiple-value-bind (resolver calls) (resources (list (list *child* (bytes *xml*) "conflict:xtm-child")))
301 (declare (ignore calls))
303 (let* ((text (format nil "before.~%~A" (directive "child.xtm")))
304 (c (condition-from (lambda () (call-with-import-observer
305 (lambda (saved target source)
307 ;; Prepare conflicting target metadata using the public model
308 ;; API; the child is the actual untouched XTM reader output.
310 (:item (add-item-identifier target (concatenate 'string *child* "#lars")))
311 (:core (let ((topic (intern-topic target :subject-identifier "http://psi.topicmaps.org/iso13250/model/instance")))
312 (add-item-identifier topic "http://psi.topicmaps.org/iso13250/model/type"))))
313 (let* ((th (handles target)) (sh (handles source)) (tb (state target th)) (sb (state source sh))
314 (tx (xml target)) (sx (xml source))
315 (cause (condition-from (lambda () (funcall saved target source)))))
316 (check (format nil "conflict:~A:actual-import-precise-cause" kind)
317 (ecase kind (:item (typep cause 'item-identifier-error)) (:core (typep cause 'core-subject-error))))
318 (check (format nil "conflict:~A:both-models-and-raw-handles-unchanged" kind)
319 (and (equal tb (state target th)) (equal sb (state source sh)) (equal tx (xml target)) (equal sx (xml source))))
321 (lambda () (read-map text :resource-resolver resolver)))))))
322 (check (format nil "conflict:~A:import-condition-preserved-at-directive" kind)
323 (and reached (typep c 'topic-maps.ctm:ctm-rejection)
324 (= (search "%mergemap" text) (topic-maps.ctm:error-position c))
325 (ecase kind (:item (typep (topic-maps.ctm:error-cause c) 'item-identifier-error))
326 (:core (typep (topic-maps.ctm:error-cause c) 'core-subject-error))))))))))
328 (defun retained-contract-tests ()
329 ;; Repeat the actual old sources with the new adapter already installed.
330 ;; Do not register duplicate contract results or reset prior observations.
331 (let ((corpus (or topic-maps.ctm.tests::*cxtm-corpus* (topic-maps.test-foundation:discover-corpus))))
332 (dolist (name '("mergemap" "mergemap-syntax-qname" "mergemap-template" "mergemap-tm-reifier"
333 "include" "include-prefix" "include-prefix2" "include-template" "include-template-wildcard"
334 "include-tm-reifier" "include-topicref" "include-wildcard" "include-wildcard2"))
335 (let* ((id (format nil "ctm/in/~A.ctm" name))
336 (case (first (topic-maps.test-foundation:select-cases corpus :id id))))
337 (multiple-value-bind (resolver calls) (resources
338 (loop for auxiliary in (topic-maps.test-foundation:corpus-auxiliaries corpus)
339 collect (list (getf auxiliary :document-iri) (binary-file (getf auxiliary :source-pathname)) (getf auxiliary :id))))
340 (declare (ignore calls))
341 (let* ((map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
342 :document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id :resource-resolver resolver))
343 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator (topic-maps.test-foundation:test-case-document-iri case)))
344 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))
345 (old (find id topic-maps.ctm.tests::*cxtm-results* :key (lambda (r) (getf r :id)) :test #'equal)))
346 (check (concatenate 'string "adapter-loaded:" id ":model-and-original-comparison")
347 (and (valid map) (if (member name '("include-template" "include-wildcard2") :test #'equal)
348 (not (equalp actual expected)) (equalp actual expected))))
350 (check (concatenate 'string "adapter-loaded:" id ":unchanged-executed-output")
351 (equalp actual (bytes (getf old :actual)))))))))
352 (dolist (spec '(("mergemap-missing-syntax-iri" topic-maps.ctm:ctm-rejection :expected-mergemap-notation)
353 ("mergemap-template" topic-maps.ctm:undefined-template :undefined-template)
354 ("mergemap-unknown-syntax" topic-maps.ctm:ctm-rejection :unknown-mergemap-notation)
355 ("include-prefix" topic-maps.ctm:undefined-prefix :undefined-prefix)
356 ("include-prefix2" topic-maps.ctm:undefined-prefix :undefined-prefix)
357 ("include-xtm" topic-maps.ctm:ctm-rejection :invalid-iri)))
358 (destructuring-bind (name class code) spec
359 (let* ((id (format nil "ctm/invalid/~A.ctm" name))
360 (case (first (topic-maps.test-foundation:select-cases corpus :id id))))
361 (multiple-value-bind (resolver calls) (resources
362 (loop for auxiliary in (topic-maps.test-foundation:corpus-auxiliaries corpus)
363 collect (list (getf auxiliary :document-iri) (binary-file (getf auxiliary :source-pathname)) (getf auxiliary :id))))
364 (declare (ignore calls))
365 (let ((condition (condition-from (lambda () (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
366 :document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id :resource-resolver resolver)))))
367 (check (concatenate 'string "adapter-loaded:" id ":same-intended-rejection")
368 (and (eq class (type-of condition)) (eq code (topic-maps.ctm:error-code condition)))))))))))
371 (setf *checks* nil *observations* nil *rejections* nil)
372 (original-case) (dispatch-tests) (repeat-and-mixed-tests) (metadata-tests) (negative-tests) (conflict-tests) (retained-contract-tests)
373 (check "only-one-new-original-contract" (eq :pass (topic-maps.test-foundation:test-case-status
374 (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id "ctm/in/mergemap-xtm.ctm")))))
375 (check "unique-check-names" (= (length *checks*) (length (remove-duplicates *checks* :test #'equal))))
376 (setf *checks* (nreverse *checks*) *observations* (nreverse *observations*) *rejections* (nreverse *rejections*))
377 (format t "XTM-MERGEMAP-PASS: ~D checks; actual 1519-byte original contract PASS~%" (length *checks*)) t)
378 (defun write-report (path)
379 (with-open-file (out path :direction :output :if-exists :supersede)
380 (topic-maps.test-foundation::emit-json-value
381 (fields "kind" "ctm-xtm-mergemap-execution" "passed" (length *checks*) "failed" 0 "checks" (coerce *checks* 'vector)
382 "cases" (coerce *observations* 'vector) "rejections" (coerce *rejections* 'vector)) out) (terpri out)))
384 (defun write-reports (prefix)
385 (write-report (concatenate 'string prefix ".integration.json"))
386 (topic-maps.ctm.tests::write-cxtm-report
387 (concatenate 'string prefix ".validation.json") (concatenate 'string prefix ".cxtm.json")
388 :additional-checks *checks*
389 :scope "Slice 19D2: one actual XTM Mergemap original added; CTM 205 PASS / 4 known FAIL / 64 NOT-RUN after retained regression. Canonicalizer and integration checks here; full retained semantic checks reported separately."))