2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2018-01-04 17:26:29 +00:00
|
|
|
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
|
2013-12-11 14:53:41 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.japi;
|
|
|
|
|
|
|
|
|
|
import akka.japi.pf.FI;
|
|
|
|
|
import akka.japi.pf.Match;
|
|
|
|
|
import org.junit.Rule;
|
|
|
|
|
import org.junit.rules.ExpectedException;
|
|
|
|
|
import org.junit.Test;
|
2016-04-06 01:23:21 +02:00
|
|
|
import org.scalatest.junit.JUnitSuite;
|
2013-12-11 14:53:41 +01:00
|
|
|
import scala.MatchError;
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
2016-04-06 01:23:21 +02:00
|
|
|
public class MatchBuilderTest extends JUnitSuite {
|
2013-12-11 14:53:41 +01:00
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
|
public ExpectedException exception = ExpectedException.none();
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void shouldPassBasicMatchTest() {
|
|
|
|
|
Match<Object, Double> pf = Match.create(Match.match(Integer.class, new FI.Apply<Integer, Double>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Double apply(Integer integer) {
|
|
|
|
|
return integer * 10.0;
|
|
|
|
|
}
|
|
|
|
|
}).match(Number.class, new FI.Apply<Number, Double>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Double apply(Number number) {
|
|
|
|
|
return number.doubleValue() * (-10.0);
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
assertTrue("An integer should be multiplied by 10", new Double(47110).equals(pf.match(new Integer(4711))));
|
|
|
|
|
assertTrue("A double should be multiplied by -10", new Double(-47110).equals(pf.match(new Double(4711))));
|
|
|
|
|
|
|
|
|
|
exception.expect(MatchError.class);
|
|
|
|
|
assertFalse("A string should throw a MatchError", new Double(4711).equals(pf.match("4711")));
|
|
|
|
|
}
|
2014-11-08 13:55:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
static class GenericClass<T> {
|
|
|
|
|
T val;
|
|
|
|
|
|
|
|
|
|
public GenericClass(T val) {
|
|
|
|
|
this.val = val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void shouldHandleMatchOnGenericClass() {
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
Match<Object, String> pf = Match.create(Match.matchUnchecked(GenericClass.class, new FI.Apply<GenericClass<String>, String>() {
|
2014-11-08 13:55:38 +01:00
|
|
|
@Override
|
|
|
|
|
public String apply(GenericClass<String> stringGenericClass) {
|
|
|
|
|
return stringGenericClass.val;
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
assertTrue("String value should be extract from GenericMessage", "A".equals(pf.match(new GenericClass<String>("A"))));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void shouldHandleMatchWithPredicateOnGenericClass() {
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
Match<Object, String> pf = Match.create(Match.matchUnchecked(GenericClass.class, new FI.TypedPredicate<GenericClass<String>>() {
|
2014-11-08 13:55:38 +01:00
|
|
|
@Override
|
|
|
|
|
public boolean defined(GenericClass<String> genericClass) {
|
|
|
|
|
return !genericClass.val.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
}, new FI.Apply<GenericClass<String>, String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public String apply(GenericClass<String> stringGenericClass) {
|
|
|
|
|
return stringGenericClass.val;
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
exception.expect(MatchError.class);
|
|
|
|
|
assertTrue("empty GenericMessage should throw match error", "".equals(pf.match(new GenericClass<String>(""))));
|
|
|
|
|
}
|
2013-12-11 14:53:41 +01:00
|
|
|
}
|