/*
Copyright 2016 McDowell
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package uk.kludje.experimental.test.infer.io;
import org.junit.Assert;
import org.junit.Test;
import uk.kludje.experimental.infer.io.SerializedLambdas;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
// see the caveats in the tested type to see if they apply to unit test failures
public class SerializedLambdasTest {
@Test
public void testToSerializedLambda() {
// init
Foo foo = this::testToSerializedLambda;
// invoke
SerializedLambda serializedLambda = SerializedLambdas.toSerializedLambda(foo);
// verify
Assert.assertNotNull(serializedLambda);
}
@Test(expected = SerializedLambdas.NotASerializedLambdaException.class)
public void testDoesNotImplementWriteReplace() {
// init
Foo foo = new Foo() {
@Override
public void foo() {}
};
// invoke
SerializedLambdas.toSerializedLambda(foo);
}
@Test(expected = SerializedLambdas.NotASerializedLambdaException.class)
public void testNotASerializedLambda() {
class NotALambda implements Foo {
@Override
public void foo() {}
Object writeReplace() {
return "I'm a string, not a SerializedLambda";
}
}
// invoke
SerializedLambdas.toSerializedLambda(new NotALambda());
}
@Test(expected = SerializedLambdas.InspectionException.class)
public void testBadBehaviour() {
class NotALambda implements Foo {
@Override
public void foo() {}
Object writeReplace() {
throw new RuntimeException("Misbehaving type");
}
}
// invoke
SerializedLambdas.toSerializedLambda(new NotALambda());
}
@Test(expected = Throwable.class)
public void testOnNull() {
SerializedLambdas.toSerializedLambda(null);
}
@FunctionalInterface
private interface Foo extends Serializable {
void foo();
}
}