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

openmrs / openmrs-core / 43293528472

12 Nov 2026 05:19PM UTC coverage: 66.969% (+0.2%) from 66.721%
43293528472

push

github

ibacher
Validate module ids and contain module paths (#6813)

38 of 42 new or added lines in 4 files covered. (90.48%)

391 existing lines in 8 files now uncovered.

26387 of 39402 relevant lines covered (66.97%)

0.67 hits per line

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

45.9
/api/src/main/java/org/openmrs/module/SqlDiffFileParser.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs.module;
11

12
import java.io.IOException;
13
import java.io.InputStream;
14
import java.io.StringReader;
15
import java.util.List;
16
import java.util.ArrayList;
17
import java.util.SortedMap;
18
import java.util.TreeMap;
19
import java.util.jar.JarFile;
20
import java.util.zip.ZipEntry;
21

22
import javax.xml.parsers.DocumentBuilder;
23
import javax.xml.parsers.DocumentBuilderFactory;
24

25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27
import org.w3c.dom.Document;
28
import org.w3c.dom.Element;
29
import org.w3c.dom.NodeList;
30
import org.xml.sax.InputSource;
31

32
import static org.openmrs.util.XmlUtils.createDocumentBuilder;
33

34
/**
35
 * This class will parse an xml sql diff file
36
 *
37
 * @version 1.0
38
 */
39
public class SqlDiffFileParser {
40

41
        private SqlDiffFileParser() {
42
        }
43
        
44
        private static final Logger log = LoggerFactory.getLogger(SqlDiffFileParser.class);
1 ✔
45
        
46
        private static final String SQLDIFF_CHANGELOG_FILENAME = "sqldiff.xml";
47
        
48
        /**
49
         * Get the diff map. Return a sorted map<version, sql statements>
50
         *
51
         * @return SortedMap<String, String>
52
         * @throws ModuleException
53
         */
54
        public static SortedMap<String, String> getSqlDiffs(Module module) throws ModuleException {
55
                if (module == null) {
1 ✔
UNCOV
56
                        throw new ModuleException("Module cannot be null");
×
57
                }
58
                
59
                SortedMap<String, String> map = new TreeMap<>(new VersionComparator());
1 ✔
60
                
61
                InputStream diffStream;
62
                
63
                // get the diff stream
64
                JarFile jarfile = null;
1 ✔
65
                try {
66
                        try {
67
                                jarfile = new JarFile(module.getFile());
1 ✔
68
                        }
UNCOV
69
                        catch (IOException e) {
×
UNCOV
70
                                throw new ModuleException("Unable to get jar file", module.getName(), e);
×
71
                        }
1 ✔
72
                        
73
                        diffStream = ModuleUtil.getResourceFromApi(jarfile, module.getModuleId(), module.getVersion(),
1 ✔
74
                            SQLDIFF_CHANGELOG_FILENAME);
75
                        if (diffStream == null) {
1 ✔
76
                                // Try the old way. Loading from the root of the omod
77
                                ZipEntry diffEntry = jarfile.getEntry(SQLDIFF_CHANGELOG_FILENAME);
×
78
                                if (diffEntry == null) {
×
UNCOV
79
                                        log.debug("No sqldiff.xml found for module: " + module.getName());
×
UNCOV
80
                                        return map;
×
81
                                } else {
82
                                        try {
83
                                                diffStream = jarfile.getInputStream(diffEntry);
×
84
                                        }
85
                                        catch (IOException e) {
×
UNCOV
86
                                                throw new ModuleException("Unable to get sql diff file stream", module.getName(), e);
×
UNCOV
87
                                        }
×
88
                                }
89
                        }
90
                        
91
                        try {
92
                                // turn the diff stream into an xml document
93
                                Document diffDoc;
94
                                try {
95
                                        DocumentBuilder db = createDocumentBuilder();
1 ✔
96
                                        diffDoc = db.parse(diffStream);
1 ✔
97
                                }
UNCOV
98
                                catch (Exception e) {
×
UNCOV
99
                                        throw new ModuleException("Error parsing diff sqldiff.xml file", module.getName(), e);
×
100
                                }
1 ✔
101
                                
102
                                Element rootNode = diffDoc.getDocumentElement();
1 ✔
103
                                
104
                                String diffVersion = rootNode.getAttribute("version");
1 ✔
105
                                
106
                                if (!validConfigVersions().contains(diffVersion)) {
1 ✔
UNCOV
107
                                        throw new ModuleException("Invalid config version: " + diffVersion, module.getModuleId());
×
108
                                }
109
                                
110
                                NodeList diffNodes = getDiffNodes(rootNode, diffVersion);
1 ✔
111
                                
112
                                if (diffNodes != null && diffNodes.getLength() > 0) {
1 ✔
UNCOV
113
                                        int i = 0;
×
UNCOV
114
                                        while (i < diffNodes.getLength()) {
×
UNCOV
115
                                                Element el = (Element) diffNodes.item(i++);
×
116
                                                String version = getElement(el, diffVersion, "version");
×
117
                                                String sql = getElement(el, diffVersion, "sql");
×
118
                                                map.put(version, sql);
×
119
                                        }
×
120
                                }
121
                        }
122
                        catch (ModuleException e) {
×
UNCOV
123
                                if (diffStream != null) {
×
124
                                        try {
125
                                                diffStream.close();
×
126
                                        }
UNCOV
127
                                        catch (IOException io) {
×
128
                                                log.error("Error while closing config stream for module: " + module.getModuleId(), io);
×
UNCOV
129
                                        }
×
130
                                }
131
                                
132
                                // rethrow the moduleException
UNCOV
133
                                throw e;
×
134
                        }
1 ✔
135
                        
136
                }
137
                finally {
138
                        try {
139
                                if (jarfile != null) {
1 ✔
140
                                        jarfile.close();
1 ✔
141
                                }
142
                        }
UNCOV
143
                        catch (IOException e) {
×
UNCOV
144
                                log.warn("Unable to close jarfile: " + jarfile.getName());
×
145
                        }
1 ✔
146
                }
147
                return map;
1 ✔
148
        }
149
        
150
        /**
151
         * Generic method to get a module tag
152
         *
153
         * @param element
154
         * @param version
155
         * @param tag
156
         * @return
157
         */
158
        private static String getElement(Element element, String version, String tag) {
UNCOV
159
                if (element.getElementsByTagName(tag).getLength() > 0) {
×
UNCOV
160
                        return element.getElementsByTagName(tag).item(0).getTextContent();
×
161
                }
162
                return "";
×
163
        }
164
        
165
        /**
166
         * List of the valid sqldiff versions
167
         *
168
         * @return
169
         */
170
        private static List<String> validConfigVersions() {
171
                List<String> versions = new ArrayList<>();
1 ✔
172
                versions.add("1.0");
1 ✔
173
                return versions;
1 ✔
174
        }
175
        
176
        /**
177
         * Finds the nodes that contain diff information
178
         *
179
         * @param element
180
         * @param version
181
         * @return
182
         */
183
        private static NodeList getDiffNodes(Element element, String version) {
184
                NodeList diffNodes = null;
1 ✔
185
                
186
                if ("1.0".equals(version)) {
1 ✔
187
                        diffNodes = element.getElementsByTagName("diff");
1 ✔
188
                }
189
                
190
                return diffNodes;
1 ✔
191
        }
192
        
193
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc