Skip to content

Commit f1b01d5

Browse files
committed
Add test-support to build
Issue gh-610
1 parent 86ffe5c commit f1b01d5

File tree

4 files changed

+189
-5
lines changed

4 files changed

+189
-5
lines changed

dependencies/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
api "commons-pool:commons-pool:1.6"
2828
api "org.apache.commons:commons-pool2:2.4.2"
2929
api "ch.qos.logback:logback-classic:1.2.6"
30+
api "com.google.code.typica:typica:1.3"
3031
api "com.google.inject:guice:3.0"
3132
api "com.nimbusds:nimbus-jose-jwt:9.14"
3233
api "com.nimbusds:oauth2-oidc-sdk:9.18"
@@ -46,6 +47,7 @@ dependencies {
4647
api "jakarta.servlet:jakarta.servlet-api:5.0.0"
4748
api "jakarta.xml.bind:jakarta.xml.bind-api:3.0.1"
4849
api "jakarta.persistence:jakarta.persistence-api:3.0.0"
50+
api "javax.activation:activation:1.1"
4951
api "ldapsdk:ldapsdk:4.1"
5052
api "net.sourceforge.htmlunit:htmlunit:2.54.0"
5153
api "net.sourceforge.nekohtml:nekohtml:1.9.22"

settings.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ rootProject.name = 'spring-ldap'
2424
include 'core'
2525
include 'core-tiger'
2626
include 'dependencies'
27-
//include 'test-support'
27+
include 'test-support'
2828
include 'ldif/ldif-core'
2929
//include 'odm'
3030
////include 'sandbox'
@@ -49,4 +49,4 @@ rootProject.children.each { p->
4949
p.name = "spring-ldap-" + name
5050
}
5151

52-
//findProject(":spring-ldap-test-support").name = "spring-ldap-test"
52+
findProject(":spring-ldap-test-support").name = "spring-ldap-test"

test-support/build.gradle

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,28 @@ plugins {
33
}
44

55
dependencies {
6+
management platform(project(":spring-ldap-dependencies"))
67
implementation project(":spring-ldap-core"),
78
project(":spring-ldap-ldif-core"),
8-
"com.google.code.typica:typica:1.3",
9+
"com.google.code.typica:typica",
910
"commons-io:commons-io",
10-
"javax.activation:activation:1.1",
11+
"javax.activation:activation",
1112
"org.springframework:spring-core",
1213
"org.springframework:spring-beans",
1314
"org.springframework:spring-context",
1415
"org.springframework:spring-test"
1516

1617

17-
optional apachedsDependencies
18+
optional "org.apache.directory.server:apacheds-core-entry"
19+
optional "org.apache.directory.server:apacheds-core"
20+
optional "org.apache.directory.server:apacheds-protocol-ldap"
21+
optional "org.apache.directory.server:apacheds-protocol-shared"
22+
optional "org.apache.directory.server:apacheds-server-jndi"
23+
optional "org.apache.directory.shared:shared-ldap"
1824
optional "com.unboundid:unboundid-ldapsdk"
1925

2026
provided "junit:junit"
27+
testImplementation platform('org.junit:junit-bom')
28+
testImplementation "org.junit.vintage:junit-vintage-engine"
29+
testImplementation "org.assertj:assertj-core"
2130
}
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)