Skip to content

Commit

Permalink
Make collections not-serializable. No unit tests are skipped.
Browse files Browse the repository at this point in the history
  • Loading branch information
jankotek committed Nov 29, 2017
1 parent 08e14f2 commit be099e3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 23 deletions.
21 changes: 19 additions & 2 deletions pom.xml
Expand Up @@ -34,7 +34,7 @@
</scm>

<properties>
<kotlin.version>1.1.4-3</kotlin.version>
<kotlin.version>1.1.60</kotlin.version>
<kotlin.plugin.version>1.1.4-3</kotlin.plugin.version>

<java.target.version>1.8</java.target.version>
Expand Down Expand Up @@ -123,6 +123,23 @@
<version>1.4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down Expand Up @@ -150,7 +167,7 @@
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.plugin.version}</version>
<version>${kotlin.version}</version>

<executions>
<execution>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mapdb/flat/SortedTableMap.kt
Expand Up @@ -24,7 +24,7 @@ class SortedTableMap<K,V>(
protected val volume: Volume,
override val hasValues: Boolean = false
): ConcurrentMap<K, V>, ConcurrentNavigableMap<K, V>, DBConcurrentNavigableMap<K,V>, BTreeMapJava.ConcurrentNavigableMap2<K,V>,
MutableMap<K,V>, Serializable{
MutableMap<K,V>{

abstract class Sink<K, V> : Pump.Sink<Pair<K, V>, SortedTableMap<K, V>>() {
fun put(key: K, value: V) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mapdb/tree/BTreeMapJava.java
Expand Up @@ -396,7 +396,7 @@ static <E> List<E> toList(Collection<E> c) {

public static final class KeySet<E>
extends AbstractSet<E>
implements DBNavigableSet<E>,Serializable {
implements DBNavigableSet<E>{

protected final ConcurrentNavigableMap2<E,Object> m;
private final boolean hasValues;
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/mapdb/DBTest.kt
Expand Up @@ -1336,7 +1336,6 @@ class DBTest{
}

@Test
@Ignore //TODO there is a bug, needs fixing
fun reversed_comparator_restored(){
if(TT.shortTest())
return
Expand Down
25 changes: 14 additions & 11 deletions src/test/java/org/mapdb/tree/BTreeMapTest.kt
Expand Up @@ -10,6 +10,7 @@ import org.junit.Assert.*
import org.mapdb.*
import org.mapdb.store.*
import org.mapdb.tree.BTreeMapJava.*
import java.io.NotSerializableException
import java.math.BigInteger
import java.util.*
import java.util.concurrent.*
Expand Down Expand Up @@ -1370,7 +1371,7 @@ class BTreeMapTest {
}


@Test @Ignore
@Test
fun serialize_clone() {

val m:MutableMap<Int,Int> = DBMaker
Expand All @@ -1383,24 +1384,26 @@ class BTreeMapTest {
m.put(i, i * 10)
}

val m2 = TT.cloneJavaSerialization(m)
assertEquals(ConcurrentSkipListMap::class.java, m2.javaClass)
assertTrue(m2.entries.containsAll(m.entries))
assertTrue(m.entries.containsAll(m2.entries))
shouldThrow<NotSerializableException> {
val m2 = TT.cloneJavaSerialization(m)
assertEquals(ConcurrentSkipListMap::class.java, m2.javaClass)
assertTrue(m2.entries.containsAll(m.entries))
assertTrue(m.entries.containsAll(m2.entries))
}
}

@Test
@Ignore //TODO this fails because class after deserialization implements different interface
fun serialize_set_clone() {
val m = DBMaker.memoryDB().make().treeSet("map", Serializer.INTEGER).createOrOpen()
for (i in 0..999) {
m.add(i)
}

val m2 = TT.cloneJavaSerialization(m)
assertEquals(ConcurrentSkipListSet::class.java, m2.javaClass)
assertTrue(m2.containsAll(m))
assertTrue(m.containsAll(m2))
shouldThrow<NotSerializableException> {
val m2 = TT.cloneJavaSerialization(m)
assertEquals(ConcurrentSkipListSet::class.java, m2.javaClass)
assertTrue(m2.containsAll(m))
assertTrue(m.containsAll(m2))
}
}

@Test fun external_value_null_after_delete(){
Expand Down
Expand Up @@ -6,6 +6,7 @@

import org.junit.*;

import java.io.NotSerializableException;
import java.util.*;
import java.util.concurrent.ConcurrentNavigableMap;

Expand Down Expand Up @@ -793,10 +794,10 @@ public void testRemove3() {
/**
* A deserialized map equals original
*/
@Test @Ignore
@Test(expected = NotSerializableException.class)
public void testSerialization() throws Exception {
NavigableMap x = map5();
NavigableMap y = serialClone(x);
NavigableMap y = serialCloneNoCatch(x);

assertNotSame(x, y);
assertEquals(x.size(), y.size());
Expand Down
23 changes: 18 additions & 5 deletions src/test/java/org/mapdb/tree/jsr166Tests/JSR166Test.java
Expand Up @@ -9,10 +9,7 @@
import junit.framework.*;
import org.mapdb.TT;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -1664,18 +1661,34 @@ byte[] serialBytes(Object o) {
}
}

byte[] serialBytes2(Object o) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(o);
oos.flush();
oos.close();
return bos.toByteArray();
}

@SuppressWarnings("unchecked")
<T> T serialClone(T o) {
try {
ObjectInputStream ois = new ObjectInputStream
(new ByteArrayInputStream(serialBytes(o)));
(new ByteArrayInputStream(serialBytes(o)));
T clone = (T) ois.readObject();
return clone;
} catch (Throwable fail) {
threadUnexpectedException(fail);
return null;
}
}
@SuppressWarnings("unchecked")
<T> T serialCloneNoCatch(T o) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream
(new ByteArrayInputStream(serialBytes2(o)));
T clone = (T) ois.readObject();
return clone;
}

public void assertThrows(Class<? extends Throwable> expectedExceptionClass,
Runnable... throwingActions) {
Expand Down

0 comments on commit be099e3

Please sign in to comment.