-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
CheckDependenciesIT.java
173 lines (147 loc) · 5.79 KB
/
CheckDependenciesIT.java
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
/*
* Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.osgi;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import org.apache.felix.utils.manifest.Clause;
import org.apache.felix.utils.manifest.Parser;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.instance.BuildInfoProvider;
import com.hazelcast.nio.IOUtil;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.QuickTest;
import com.hazelcast.util.StringUtil;
@RunWith(HazelcastParallelClassRunner.class)
@Category(QuickTest.class)
public class CheckDependenciesIT extends HazelcastTestSupport {
private static final String MANIFEST_PATH = "META-INF/MANIFEST.MF";
private static final String[] WHITELIST_PREFIXES = new String[]{
// everything from the Java package is OK - it's part of the Java SE platform
"java.",
// with the "javax" package we have to be more specific - do not use just "javax."
// as it contains e.g. javax.servlet which is not part of the SE platform!
"javax.crypto",
"javax.management",
"javax.net.ssl",
"javax.script",
"javax.security.auth",
"javax.transaction.xa",
"javax.xml",
"javax.naming",
// these 2 XML-related packages are part of the platform since Java SE 6
"org.xml.sax",
"org.w3c.dom",
// GSS-API (& Kerberos) related classes - part of JDK since 1.4
"org.ietf.jgss"
};
@Test
public void testNoMandatoryDependencyDeclared() throws IOException, InterruptedException {
Manifest manifest = getHazelcastManifest();
String packages = manifest.getMainAttributes().getValue("Import-Package");
Clause[] clauses = Parser.parseHeader(packages);
for (Clause clause : clauses) {
String name = clause.getName();
String resolution = clause.getDirective("resolution");
checkImport(name, resolution);
}
}
/**
* Verify the {@code HazelcastManifestTransformer} was properly used.
*/
@Test
public void verifyManifestEntries() throws IOException {
Manifest mf = getHazelcastManifest();
Attributes mainAttributes = mf.getMainAttributes();
assertEquals("Unexpected Bundle-Name attribute value", getBundleName(), mainAttributes.getValue("Bundle-Name"));
if (hasMainClass()) {
assertNotNull("The Main-Class attribute is expected", mainAttributes.getValue("Main-Class"));
}
}
protected boolean hasMainClass() {
return true;
}
protected String getBundleName() {
return "Hazelcast(Core)";
}
protected Manifest getHazelcastManifest() throws IOException {
URL hazelcastAllManifestUrl = findHazelcastManifestURL();
InputStream inputStream = null;
try {
inputStream = hazelcastAllManifestUrl.openStream();
return new Manifest(inputStream);
} finally {
IOUtil.closeResource(inputStream);
}
}
private void checkImport(String name, String resolution) {
if ("optional".equals(resolution)) {
return;
}
if (isWhitelisted(name)) {
return;
}
fail("Import " + name + " is not declared as optional");
}
private boolean isWhitelisted(String name) {
String[] whitelistPrefixes = getWhitelistPrefixes();
for (String whitelistPrefix : whitelistPrefixes) {
if (name.startsWith(whitelistPrefix)) {
return true;
}
}
return false;
}
private URL findHazelcastManifestURL() throws IOException {
ClassLoader cl = HazelcastInstance.class.getClassLoader();
Enumeration<URL> resources = cl.getResources(MANIFEST_PATH);
URL matchedUrl = null;
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
String urlString = url.toString();
if (isMatching(urlString)) {
if (matchedUrl == null) {
matchedUrl = url;
} else {
throw new AssertionError("Found multiple matching URLs: " + url + " and " + matchedUrl);
}
}
}
if (matchedUrl == null) {
throw new AssertionError("Cannot find Hazelcast manifest");
}
return matchedUrl;
}
protected String[] getWhitelistPrefixes() {
return WHITELIST_PREFIXES;
}
protected boolean isMatching(String urlString) {
return urlString.contains("hazelcast-" + getMajorVersion() + ".") && urlString.contains("target");
}
protected String getMajorVersion() {
return StringUtil.tokenizeVersionString(BuildInfoProvider.getBuildInfo().getVersion())[0];
}
}