• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.73
/exist-core/src/main/java/org/exist/test/ExistXmldbEmbeddedServer.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 *
24
 * NOTE: Parts of this file contain code from 'The eXist-db Authors'.
25
 *       The original license header is included below.
26
 *
27
 * =====================================================================
28
 *
29
 * eXist-db Open Source Native XML Database
30
 * Copyright (C) 2001 The eXist-db Authors
31
 *
32
 * info@exist-db.org
33
 * http://www.exist-db.org
34
 *
35
 * This library is free software; you can redistribute it and/or
36
 * modify it under the terms of the GNU Lesser General Public
37
 * License as published by the Free Software Foundation; either
38
 * version 2.1 of the License, or (at your option) any later version.
39
 *
40
 * This library is distributed in the hope that it will be useful,
41
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43
 * Lesser General Public License for more details.
44
 *
45
 * You should have received a copy of the GNU Lesser General Public
46
 * License along with this library; if not, write to the Free Software
47
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
48
 */
49
package org.exist.test;
50

51
import org.exist.EXistException;
52
import org.exist.TestUtils;
53
import org.exist.storage.serializers.EXistOutputKeys;
54
import org.exist.util.DatabaseConfigurationException;
55
import org.exist.util.MimeTable;
56
import org.exist.util.MimeType;
57
import org.exist.xmldb.EXistCollection;
58
import org.exist.xmldb.EXistXQueryService;
59
import org.exist.xmldb.XmldbURI;
60
import org.junit.rules.ExternalResource;
61
import org.xmldb.api.DatabaseManager;
62
import org.xmldb.api.base.*;
63
import org.xmldb.api.modules.BinaryResource;
64
import org.xmldb.api.modules.CollectionManagementService;
65
import org.xmldb.api.modules.XMLResource;
66

67
import javax.annotation.Nullable;
68
import javax.xml.transform.OutputKeys;
69
import java.io.IOException;
70
import java.nio.file.Path;
71
import java.util.Map;
72
import java.util.Properties;
73

74
import static org.junit.Assert.assertEquals;
75
import static org.junit.Assert.fail;
76

77
/**
78
 * Exist embedded XML:DB Server Rule for JUnit.
79
 */
80
public class ExistXmldbEmbeddedServer extends ExternalResource {
81

82
    private final boolean asGuest;
83
    private final ExistEmbeddedServer existEmbeddedServer;
84

85
    private Database database = null;
1✔
86
    private Collection root = null;
1✔
87
    private EXistXQueryService xpathQueryService = null;
1✔
88

89
    public ExistXmldbEmbeddedServer() {
90
        this(false, false);
×
91
    }
×
92

93
    /**
94
     * @param asGuest Use the guest account, default is the admin account
95
     */
96
    public ExistXmldbEmbeddedServer(final boolean asGuest) {
97
        this(asGuest, false);
×
98
    }
×
99

100
    /**
101
     * @param asGuest Use the guest account, default is the admin account
102
     * @param disableAutoDeploy Whether auto-deployment of XARs should be disabled
103
     */
104
    public ExistXmldbEmbeddedServer(final boolean asGuest, final boolean disableAutoDeploy) {
105
        this(asGuest, disableAutoDeploy, false);
×
106
    }
×
107

108
    /**
109
     * @param asGuest Use the guest account, default is the admin account
110
     * @param disableAutoDeploy Whether auto-deployment of XARs should be disabled
111
     * @param useTemporaryStorage Whether the data and journal folder should use temporary storage
112
     */
113
    public ExistXmldbEmbeddedServer(final boolean asGuest, final boolean disableAutoDeploy, final boolean useTemporaryStorage) {
1✔
114
        this.existEmbeddedServer = new ExistEmbeddedServer(disableAutoDeploy, useTemporaryStorage);
1✔
115
        this.asGuest = asGuest;
1✔
116
    }
1✔
117

118
    /**
119
     * @param asGuest Use the guest account, default is the admin account
120
     * @param disableAutoDeploy Whether auto-deployment of XARs should be disabled
121
     * @param useTemporaryStorage Whether the data and journal folder should use temporary storage
122
     * @param configFile path to conf.xml configuration file
123
     */
124
    public ExistXmldbEmbeddedServer(final boolean asGuest, final boolean disableAutoDeploy, final boolean useTemporaryStorage, @Nullable final Path configFile) {
1✔
125
        this.existEmbeddedServer = new ExistEmbeddedServer(null, configFile, null, disableAutoDeploy, useTemporaryStorage);
1✔
126
        this.asGuest = asGuest;
1✔
127
    }
1✔
128

129
    @Override
130
    protected void before() throws Throwable {
131
        startDb();
1✔
132
        super.before();
1✔
133
    }
1✔
134

135
    private void startDb() throws ClassNotFoundException, IllegalAccessException, InstantiationException, XMLDBException {
136
        try {
137
            existEmbeddedServer.startDb();
1✔
138
        } catch (final DatabaseConfigurationException | EXistException | IOException e) {
1✔
139
            throw new XMLDBException(ErrorCodes.INVALID_DATABASE, e);
×
140
        }
141
        startXmldb();
1✔
142
    }
1✔
143

144
    private void startXmldb() throws ClassNotFoundException, IllegalAccessException, InstantiationException, XMLDBException {
145
        if (database == null) {
1!
146
            // initialize driver
147
            final Class<?> cl = Class.forName("org.exist.xmldb.DatabaseImpl");
1✔
148
            database = (Database) cl.newInstance();
1✔
149
            database.setProperty("create-database", "true");
1✔
150
            DatabaseManager.registerDatabase(database);
1✔
151
            if (asGuest) {
1✔
152
                root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, TestUtils.GUEST_DB_USER, TestUtils.GUEST_DB_PWD);
1✔
153
            } else {
1✔
154
                root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
1✔
155
            }
156
            xpathQueryService = root.getService(EXistXQueryService.class);
1✔
157
        } else {
1✔
158
            throw new IllegalStateException("ExistXmldbEmbeddedServer already running");
×
159
        }
160
    }
1✔
161

162
    public void restart() throws ClassNotFoundException, InstantiationException, XMLDBException, IllegalAccessException {
163
        restart(false);
1✔
164
    }
1✔
165

166
    public void restart(final boolean clearTemporaryStorage) throws ClassNotFoundException, InstantiationException, XMLDBException, IllegalAccessException {
167
        stopDb(clearTemporaryStorage);
1✔
168
        startDb();
1✔
169
    }
1✔
170

171
    @Override
172
    protected void after() {
173
        stopDb(true);
1✔
174
        super.after();
1✔
175
    }
1✔
176

177
    private void stopDb(final boolean clearTemporaryStorage) {
178
        try {
179
            stopXmlDb();
1✔
180
        } catch (final XMLDBException e) {
1✔
181
            e.printStackTrace();
×
182
            fail(e.getMessage());
×
183
        }
184
        existEmbeddedServer.stopDb(clearTemporaryStorage);
1✔
185
    }
1✔
186

187
    private void stopXmlDb() throws XMLDBException {
188
        if (database != null) {
1!
189
            root.close();
1✔
190
            DatabaseManager.deregisterDatabase(database);
1✔
191

192
            // clear instance variables
193
            xpathQueryService = null;
1✔
194
            root = null;
1✔
195
            database = null;
1✔
196
        } else {
1✔
197
            throw new IllegalStateException("ExistXmldbEmbeddedServer already stopped");
×
198
        }
199
    }
1✔
200

201

202
    public ResourceSet executeQuery(final String query) throws XMLDBException {
203
        final CompiledExpression compiledQuery = xpathQueryService.compile(query);
1✔
204
        return xpathQueryService.execute(compiledQuery);
1✔
205
    }
206

207
    public ResourceSet executeQuery(final String query, final Map<String, Object> externalVariables)
208
            throws XMLDBException {
209
        for (final Map.Entry<String, Object> externalVariable : externalVariables.entrySet()) {
1✔
210
            xpathQueryService.declareVariable(externalVariable.getKey(), externalVariable.getValue());
1✔
211
        }
212
        final CompiledExpression compiledQuery = xpathQueryService.compile(query);
1✔
213
        final ResourceSet result = xpathQueryService.execute(compiledQuery);
1✔
214
        xpathQueryService.clearVariables();
1✔
215
        return result;
1✔
216
    }
217

218
    public String executeOneValue(final String query) throws XMLDBException {
219
        final ResourceSet results = executeQuery(query);
1✔
220
        assertEquals(1, results.getSize());
1✔
221
        return results.getResource(0).getContent().toString();
1✔
222
    }
223

224
    public Collection createCollection(final Collection collection, final String collectionName) throws XMLDBException {
225
        final CollectionManagementService collectionManagementService =
1✔
226
                collection.getService(CollectionManagementService.class);
1✔
227
        Collection newCollection = collection.getChildCollection(collectionName);
1✔
228
        if (newCollection == null) {
1✔
229
            collectionManagementService.createCollection(collectionName);
1✔
230
        }
231

232
        final XmldbURI uri = XmldbURI.LOCAL_DB_URI.resolveCollectionPath(((EXistCollection) collection).getPathURI().append(collectionName));
1✔
233
        if (asGuest) {
1!
234
            newCollection = DatabaseManager.getCollection(uri.toString(), TestUtils.GUEST_DB_USER, TestUtils.GUEST_DB_PWD);
×
235
        } else {
×
236
            newCollection = DatabaseManager.getCollection(uri.toString(), TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
1✔
237
        }
238

239
        return newCollection;
1✔
240
    }
241

242
    public static void storeResource(final Collection collection, final String documentName, final byte[] content)
243
            throws XMLDBException {
244
        final MimeType mime = MimeTable.getInstance().getContentTypeFor(documentName);
1✔
245
        final Class<? extends Resource> type = mime.isXMLType() ? XMLResource.class : BinaryResource.class;
1✔
246
        try (final Resource resource = collection.createResource(documentName, type)) {
1✔
247
            resource.setContent(content);
1✔
248
            collection.storeResource(resource);
1✔
249
        }
250
    }
1✔
251

252
    public static String getXMLResource(final Collection collection, final String resource) throws XMLDBException {
253
        collection.setProperty(OutputKeys.INDENT, "yes");
×
254
        collection.setProperty(EXistOutputKeys.EXPAND_XINCLUDES, "no");
×
255
        collection.setProperty(EXistOutputKeys.PROCESS_XSL_PI, "yes");
×
256
        final XMLResource res = (XMLResource) collection.getResource(resource);
×
257
        return res.getContent().toString();
×
258
    }
259

260
    public Collection getRoot() {
261
        return root;
1✔
262
    }
263
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc