1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.metapath;
7   
8   import org.jmock.Expectations;
9   import org.jmock.Mockery;
10  import org.jmock.junit5.JUnit5Mockery;
11  import org.jmock.lib.concurrent.Synchroniser;
12  import org.junit.jupiter.api.extension.RegisterExtension;
13  
14  import java.io.File;
15  import java.net.URI;
16  import java.util.stream.Stream;
17  
18  import dev.metaschema.core.metapath.item.node.IDocumentNodeItem;
19  import dev.metaschema.core.metapath.item.node.INodeItem;
20  import dev.metaschema.core.util.ObjectUtils;
21  import edu.umd.cs.findbugs.annotations.NonNull;
22  
23  public class ExpressionTestBase {
24    /**
25     * A Metaschema model namespace for use in unit tests.
26     */
27    @NonNull
28    protected static final String NS = "http://example.com/ns";
29  
30    @SuppressWarnings("exports")
31    @NonNull
32    @RegisterExtension
33    public final Mockery context = new JUnit5Mockery() {
34      {
35        setThreadingPolicy(new Synchroniser());
36      }
37    };
38  
39    /**
40     * Get the mocking context.
41     *
42     * @return the mocking context
43     */
44    @NonNull
45    protected Mockery getContext() {
46      return context;
47    }
48  
49    /**
50     * Construct a new dynamic context for testing.
51     *
52     * @return the dynamic context
53     */
54    @NonNull
55    protected static DynamicContext newDynamicContext() {
56      URI baseUri = ObjectUtils.notNull(new File("").getAbsoluteFile().toURI());
57  
58      return new DynamicContext(StaticContext.builder()
59          .baseUri(baseUri)
60          .defaultModelNamespace(NS)
61          .build());
62    }
63  
64    /**
65     * Get a mocked document node item.
66     *
67     * @return the mocked node item
68     */
69    @NonNull
70    protected IDocumentNodeItem newDocumentNodeMock() {
71      IDocumentNodeItem retval = getContext().mock(IDocumentNodeItem.class);
72      assert retval != null;
73  
74      getContext().checking(new Expectations() {
75        { // NOPMD - intentional
76          allowing(retval).getNodeItem();
77          will(returnValue(retval));
78          allowing(retval).ancestorOrSelf();
79          will(returnValue(Stream.of(retval)));
80        }
81      });
82  
83      return retval;
84    }
85  
86    /**
87     * Get a mocked node item.
88     *
89     * @param mockName
90     *          the name of the mocked object
91     *
92     * @return the mocked node item
93     */
94    @NonNull
95    protected INodeItem newNonDocumentNodeMock(@NonNull String mockName) {
96      INodeItem retval = getContext().mock(INodeItem.class, mockName);
97      assert retval != null;
98  
99      getContext().checking(new Expectations() {
100       { // NOPMD - intentional
101         allowing(retval).getNodeItem();
102         will(returnValue(retval));
103         allowing(retval).ancestorOrSelf();
104         will(returnValue(Stream.of(retval)));
105       }
106     });
107 
108     return retval;
109   }
110 }