| 1 |
/* |
| 2 |
* Copyright (C) 2008 The Guava 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 |
* http://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 com.google.common.collect.testing; |
| 18 |
|
| 19 |
import com.google.common.annotations.GwtIncompatible; |
| 20 |
import java.io.ByteArrayInputStream; |
| 21 |
import java.io.ByteArrayOutputStream; |
| 22 |
import java.io.IOException; |
| 23 |
import java.io.ObjectInputStream; |
| 24 |
import java.io.ObjectOutputStream; |
| 25 |
import java.util.Collection; |
| 26 |
import java.util.List; |
| 27 |
|
| 28 |
/** |
| 29 |
* Reserializes the sets created by another test set generator. |
| 30 |
* |
| 31 |
* <p>TODO: make CollectionTestSuiteBuilder test reserialized collections |
| 32 |
* |
| 33 |
* @author Jesse Wilson |
| 34 |
*/ |
| 35 |
@GwtIncompatible |
| 36 |
public class ReserializingTestCollectionGenerator<E> implements TestCollectionGenerator<E> { |
| 37 |
private final TestCollectionGenerator<E> delegate; |
| 38 |
|
| 39 |
ReserializingTestCollectionGenerator(TestCollectionGenerator<E> delegate) { |
| 40 |
this.delegate = delegate; |
| 41 |
} |
| 42 |
|
| 43 |
public static <E> ReserializingTestCollectionGenerator<E> newInstance( |
| 44 |
TestCollectionGenerator<E> delegate) { |
| 45 |
return new ReserializingTestCollectionGenerator<>(delegate); |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public Collection<E> create(Object... elements) { |
| 50 |
return reserialize(delegate.create(elements)); |
| 51 |
} |
| 52 |
|
| 53 |
@SuppressWarnings("unchecked") |
| 54 |
static <T> T reserialize(T object) { |
| 55 |
try { |
| 56 |
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 57 |
ObjectOutputStream out = new ObjectOutputStream(bytes); |
| 58 |
out.writeObject(object); |
| 59 |
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())); |
| 60 |
return (T) in.readObject(); |
| 61 |
} catch (IOException | ClassNotFoundException e) { |
| 62 |
Helpers.fail(e, e.getMessage()); |
| 63 |
} |
| 64 |
throw new AssertionError("not reachable"); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public SampleElements<E> samples() { |
| 69 |
return delegate.samples(); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public E[] createArray(int length) { |
| 74 |
return delegate.createArray(length); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public Iterable<E> order(List<E> insertionOrder) { |
| 79 |
return delegate.order(insertionOrder); |
| 80 |
} |
| 81 |
} |