Skip to content

Commit f129410

Browse files
committed
Add Java 8 Polyfill for Apache DS tests
Closes gh-9416
1 parent ccfbff4 commit f129410

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

config/spring-security-config.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ dependencies {
3939
testCompile project(':spring-security-cas')
4040
testCompile project(':spring-security-test')
4141
testCompile project(path : ':spring-security-core', configuration : 'tests')
42+
testCompile project(path : ':spring-security-ldap', configuration : 'tests')
4243
testCompile project(path : ':spring-security-oauth2-client', configuration : 'tests')
4344
testCompile project(path : ':spring-security-oauth2-resource-server', configuration : 'tests')
4445
testCompile project(path : ':spring-security-saml2-service-provider', configuration : 'tests')

itest/ldap/embedded-ldap-apacheds-default/spring-security-itest-ldap-embedded-apacheds-default.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ dependencies {
1010
compile project(':spring-security-ldap')
1111

1212
runtime apachedsDependencies
13+
14+
testCompile project(path : ':spring-security-ldap', configuration : 'tests')
1315
}

itest/ldap/embedded-ldap-mode-apacheds/spring-security-itest-ldap-embedded-mode-apacheds.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ dependencies {
1010
compile project(':spring-security-ldap')
1111

1212
runtime apachedsDependencies
13+
14+
testCompile project(path : ':spring-security-ldap', configuration : 'tests')
1315
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.directory.server.core.avltree;
18+
19+
import java.io.ByteArrayInputStream;
20+
import java.io.ByteArrayOutputStream;
21+
import java.io.DataInputStream;
22+
import java.io.DataOutputStream;
23+
import java.io.IOException;
24+
import java.util.Comparator;
25+
26+
import org.apache.directory.shared.ldap.util.StringTools;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
30+
/**
31+
* Class to serialize the Array data.
32+
*
33+
* @author <a href="mailto:[email protected]">Apache Directory Project</a>
34+
* @version $Rev$, $Date$
35+
*/
36+
@SuppressWarnings("unchecked")
37+
public class ArrayMarshaller<E> implements Marshaller<ArrayTree<E>> {
38+
39+
/** static logger */
40+
private static final Logger LOG = LoggerFactory.getLogger(ArrayMarshaller.class);
41+
42+
/** used for serialized form of an empty AvlTree */
43+
private static final byte[] EMPTY_TREE = new byte[1];
44+
45+
/** marshaller to be used for marshalling the keys */
46+
private Marshaller<E> keyMarshaller;
47+
48+
/** key Comparator for the AvlTree */
49+
private Comparator<E> comparator;
50+
51+
/**
52+
* Creates a new instance of AvlTreeMarshaller with a custom key Marshaller.
53+
* @param comparator Comparator to be used for key comparision
54+
* @param keyMarshaller marshaller for keys
55+
*/
56+
public ArrayMarshaller(Comparator<E> comparator, Marshaller<E> keyMarshaller) {
57+
this.comparator = comparator;
58+
this.keyMarshaller = keyMarshaller;
59+
}
60+
61+
/**
62+
* Creates a new instance of AvlTreeMarshaller with the default key Marshaller which
63+
* uses Java Serialization.
64+
* @param comparator Comparator to be used for key comparision
65+
*/
66+
public ArrayMarshaller(Comparator<E> comparator) {
67+
this.comparator = comparator;
68+
this.keyMarshaller = DefaultMarshaller.INSTANCE;
69+
}
70+
71+
/**
72+
* Marshals the given tree to bytes
73+
* @param tree the tree to be marshalled
74+
*/
75+
public byte[] serialize(ArrayTree<E> tree) {
76+
if ((tree == null) || (tree.size() == 0)) {
77+
return EMPTY_TREE;
78+
}
79+
80+
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
81+
DataOutputStream out = new DataOutputStream(byteStream);
82+
byte[] data = null;
83+
84+
try {
85+
out.writeByte(0); // represents the start of an Array byte stream
86+
out.writeInt(tree.size());
87+
88+
for (int position = 0; position < tree.size(); position++) {
89+
E value = tree.get(position);
90+
byte[] bytes = this.keyMarshaller.serialize(value);
91+
92+
// Write the key length
93+
out.writeInt(bytes.length);
94+
95+
// Write the key if its length is not null
96+
if (bytes.length != 0) {
97+
out.write(bytes);
98+
}
99+
}
100+
101+
out.flush();
102+
data = byteStream.toByteArray();
103+
104+
// Try to deserialize, just to see
105+
try {
106+
deserialize(data);
107+
}
108+
catch (NullPointerException npe) {
109+
System.out.println("Bad serialization, tree : [" + StringTools.dumpBytes(data) + "]");
110+
throw npe;
111+
}
112+
113+
out.close();
114+
}
115+
catch (IOException ex) {
116+
ex.printStackTrace();
117+
}
118+
119+
return data;
120+
}
121+
122+
/**
123+
* Creates an Array from given bytes of data.
124+
* @param data byte array to be converted into an array
125+
*/
126+
public ArrayTree<E> deserialize(byte[] data) throws IOException {
127+
try {
128+
if ((data == null) || (data.length == 0)) {
129+
throw new IOException("Null or empty data array is invalid.");
130+
}
131+
132+
if ((data.length == 1) && (data[0] == 0)) {
133+
E[] array = (E[]) new Object[] {};
134+
ArrayTree<E> tree = new ArrayTree<E>(this.comparator, array);
135+
return tree;
136+
}
137+
138+
ByteArrayInputStream bin = new ByteArrayInputStream(data);
139+
DataInputStream din = new DataInputStream(bin);
140+
141+
byte startByte = din.readByte();
142+
143+
if (startByte != 0) {
144+
throw new IOException("wrong array serialized data format");
145+
}
146+
147+
int size = din.readInt();
148+
E[] nodes = (E[]) new Object[size];
149+
150+
for (int i = 0; i < size; i++) {
151+
// Read the object's size
152+
int dataSize = din.readInt();
153+
154+
if (dataSize != 0) {
155+
byte[] bytes = new byte[dataSize];
156+
157+
din.read(bytes);
158+
E key = this.keyMarshaller.deserialize(bytes);
159+
nodes[i] = key;
160+
}
161+
}
162+
163+
ArrayTree<E> arrayTree = new ArrayTree<E>(this.comparator, nodes);
164+
165+
return arrayTree;
166+
}
167+
catch (NullPointerException npe) {
168+
System.out.println("Bad tree : [" + StringTools.dumpBytes(data) + "]");
169+
throw npe;
170+
}
171+
}
172+
173+
}

0 commit comments

Comments
 (0)