Skip to content

Commit 0eb2271

Browse files
author
vitali.prudnikovich
committed
Fix ContextManager#unregisterContext
DEVSIX-6945
1 parent 53ff5e4 commit 0eb2271

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

commons/src/main/java/com/itextpdf/commons/actions/contexts/ContextManager.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ public IContext getContext(String className) {
114114

115115
String getRecognisedNamespace(String className) {
116116
if (className != null) {
117+
String normalizedClassName = normalize(className);
117118
// If both "a" and "a.b" namespaces are registered,
118119
// iText should consider the context of "a.b" for an "a.b" event,
119120
// that's why the contexts are sorted by the length of the namespace
120121
for (String namespace : contextMappings.keySet()) {
121-
//Conversion to lowercase is done to be compatible with possible changes in case of packages/namespaces
122-
if (className.toLowerCase().startsWith(namespace)) {
122+
if (normalizedClassName.startsWith(namespace)) {
123123
return namespace;
124124
}
125125
}
@@ -129,7 +129,7 @@ String getRecognisedNamespace(String className) {
129129

130130
void unregisterContext(Collection<String> namespaces) {
131131
for (String namespace : namespaces) {
132-
contextMappings.remove(namespace);
132+
contextMappings.remove(normalize(namespace));
133133
}
134134
}
135135

@@ -143,11 +143,15 @@ private IContext getNamespaceMapping(String namespace) {
143143
void registerGenericContext(Collection<String> namespaces, Collection<String> products) {
144144
final GenericContext context = new GenericContext(products);
145145
for (String namespace : namespaces) {
146-
//Conversion to lowercase is done to be compatible with possible changes in case of packages/namespaces
147-
contextMappings.put(namespace.toLowerCase(), context);
146+
contextMappings.put(normalize(namespace), context);
148147
}
149148
}
150149

150+
private static String normalize(String namespace) {
151+
// Conversion to lowercase is done to be compatible with possible changes in case of packages/namespaces
152+
return namespace.toLowerCase();
153+
}
154+
151155
private static class LengthComparator implements Comparator<String> {
152156
@Override
153157
public int compare(String o1, String o2) {

commons/src/test/java/com/itextpdf/commons/actions/contexts/ContextManagerTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This file is part of the iText (R) project.
4848

4949
import java.util.Arrays;
5050
import java.util.Collections;
51+
import java.util.List;
5152
import org.junit.Assert;
5253
import org.junit.Test;
5354
import org.junit.experimental.categories.Category;
@@ -95,13 +96,27 @@ public void notRegisteredNamespaceTest() {
9596
@Test
9697
public void unregisterNamespaceTest() {
9798
String testNamespace = "com.hello.world";
99+
String testNamespaceWithCapitals = "com.Bye.World";
100+
List<String> testNamespaces = Arrays.asList(
101+
testNamespace,
102+
testNamespaceWithCapitals
103+
);
104+
98105
ContextManager manager = new ContextManager();
99106
Assert.assertNull(manager.getRecognisedNamespace(testNamespace));
100-
manager.registerGenericContext(Arrays.asList(testNamespace), Arrays.asList("myProduct"));
107+
Assert.assertNull(manager.getRecognisedNamespace(testNamespaceWithCapitals));
108+
109+
manager.registerGenericContext(testNamespaces, Arrays.asList("myProduct"));
110+
101111
Assert.assertEquals(testNamespace,
102112
manager.getRecognisedNamespace(testNamespace + ".MyClass"));
103-
manager.unregisterContext(Arrays.asList(testNamespace));
113+
Assert.assertEquals(testNamespaceWithCapitals.toLowerCase(),
114+
manager.getRecognisedNamespace(testNamespaceWithCapitals + ".MyClass"));
115+
116+
manager.unregisterContext(testNamespaces);
117+
104118
Assert.assertNull(manager.getRecognisedNamespace(testNamespace));
119+
Assert.assertNull(manager.getRecognisedNamespace(testNamespaceWithCapitals));
105120
}
106121

107122
@Test

0 commit comments

Comments
 (0)