2012-06-15 13:04:10 +02:00
|
|
|
/**
|
2014-02-02 19:05:45 -06:00
|
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
2012-06-15 13:04:10 +02:00
|
|
|
*/
|
|
|
|
|
|
2011-01-04 13:24:28 +01:00
|
|
|
package akka.actor;
|
|
|
|
|
|
2012-04-10 12:07:14 +02:00
|
|
|
import akka.event.Logging;
|
|
|
|
|
import akka.event.Logging.LoggerInitialized;
|
2011-01-04 13:24:28 +01:00
|
|
|
import akka.japi.Creator;
|
2014-03-12 14:43:18 +01:00
|
|
|
import akka.routing.GetRoutees;
|
2012-04-10 12:07:14 +02:00
|
|
|
import akka.routing.FromConfig;
|
|
|
|
|
import akka.routing.NoRouter;
|
2013-05-02 17:12:36 +02:00
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
2011-11-30 15:16:20 +01:00
|
|
|
import akka.testkit.AkkaSpec;
|
2013-07-03 09:47:22 +02:00
|
|
|
import akka.testkit.TestProbe;
|
2011-11-30 15:16:20 +01:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
import org.junit.ClassRule;
|
2011-01-04 13:24:28 +01:00
|
|
|
import org.junit.Test;
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
|
|
public class JavaAPI {
|
|
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
@ClassRule
|
2013-07-03 09:47:22 +02:00
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource = new AkkaJUnitActorSystemResource("JavaAPI",
|
|
|
|
|
AkkaSpec.testConf());
|
2011-11-30 15:16:20 +01:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
2011-11-30 15:16:20 +01:00
|
|
|
|
2012-04-10 12:07:14 +02:00
|
|
|
// compilation tests
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
public void mustCompile() {
|
2012-04-10 16:49:29 +02:00
|
|
|
final Kill kill = Kill.getInstance();
|
|
|
|
|
final PoisonPill pill = PoisonPill.getInstance();
|
|
|
|
|
final ReceiveTimeout t = ReceiveTimeout.getInstance();
|
2012-04-10 12:07:14 +02:00
|
|
|
|
2012-04-10 16:49:29 +02:00
|
|
|
final LocalScope ls = LocalScope.getInstance();
|
|
|
|
|
final NoScopeGiven noscope = NoScopeGiven.getInstance();
|
2013-07-03 09:47:22 +02:00
|
|
|
|
2012-04-10 12:07:14 +02:00
|
|
|
final LoggerInitialized x = Logging.loggerInitialized();
|
2013-07-03 09:47:22 +02:00
|
|
|
|
2014-03-12 14:43:18 +01:00
|
|
|
final GetRoutees r = GetRoutees.getInstance();
|
2012-04-10 16:49:29 +02:00
|
|
|
final NoRouter nr = NoRouter.getInstance();
|
|
|
|
|
final FromConfig fc = FromConfig.getInstance();
|
2012-04-10 12:07:14 +02:00
|
|
|
}
|
2011-10-13 13:41:44 +02:00
|
|
|
|
2011-11-15 11:34:39 +01:00
|
|
|
@Test
|
2011-11-30 15:16:20 +01:00
|
|
|
public void mustBeAbleToCreateActorRefFromClass() {
|
2013-05-02 17:00:44 +02:00
|
|
|
ActorRef ref = system.actorOf(Props.create(JavaAPITestActor.class));
|
2011-11-15 11:34:39 +01:00
|
|
|
assertNotNull(ref);
|
2011-01-04 13:24:28 +01:00
|
|
|
}
|
2014-03-12 14:43:18 +01:00
|
|
|
|
2014-01-17 09:12:44 +01:00
|
|
|
public static Props mkProps() {
|
|
|
|
|
return Props.create(new Creator<Actor>() {
|
2011-11-15 11:34:39 +01:00
|
|
|
public Actor create() {
|
|
|
|
|
return new JavaAPITestActor();
|
|
|
|
|
}
|
2014-01-17 09:12:44 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:16:18 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public static Props mkErasedProps() {
|
|
|
|
|
return Props.create(JavaAPITestActor.class, new Creator() {
|
|
|
|
|
public Object create() {
|
|
|
|
|
return new JavaAPITestActor();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-17 09:12:44 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToCreateActorRefFromFactory() {
|
|
|
|
|
ActorRef ref = system.actorOf(mkProps());
|
2011-11-15 11:34:39 +01:00
|
|
|
assertNotNull(ref);
|
2011-01-04 13:24:28 +01:00
|
|
|
}
|
2013-07-03 09:47:22 +02:00
|
|
|
|
2014-02-13 16:16:18 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToCreateActorRefFromErasedFactory() {
|
|
|
|
|
ActorRef ref = system.actorOf(mkErasedProps());
|
|
|
|
|
assertNotNull(ref);
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-03 09:47:22 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToCreateActorWIthConstructorParams() {
|
|
|
|
|
ActorRef ref = system.actorOf(Props.create(ActorWithConstructorParams.class, "a", "b", new Integer(17), 18));
|
|
|
|
|
final TestProbe probe = new TestProbe(system);
|
|
|
|
|
probe.send(ref, "get");
|
|
|
|
|
probe.expectMsg("a-b-17-18");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToCreateActorWIthBoxedAndUnBoxedConstructorParams() {
|
|
|
|
|
ActorRef ref = system.actorOf(Props.create(ActorWithConstructorParams.class, "a", "b", 17, new Integer(18)));
|
|
|
|
|
final TestProbe probe = new TestProbe(system);
|
|
|
|
|
probe.send(ref, "get");
|
|
|
|
|
probe.expectMsg("a-b-17-18");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToCreateActorWIthNullConstructorParams() {
|
|
|
|
|
ActorRef ref = system.actorOf(Props.create(ActorWithConstructorParams.class, "a", null, null, 18));
|
|
|
|
|
final TestProbe probe = new TestProbe(system);
|
|
|
|
|
probe.send(ref, "get");
|
|
|
|
|
probe.expectMsg("a-null-null-18");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToCreateActorWIthNullConstructorParams2() {
|
|
|
|
|
// without this Object array wrapper it will not compile: "reference to create is ambiguous"
|
|
|
|
|
ActorRef ref = system.actorOf(Props.create(ActorWithConstructorParams.class, new Object[] { null }));
|
|
|
|
|
final TestProbe probe = new TestProbe(system);
|
|
|
|
|
probe.send(ref, "get");
|
|
|
|
|
probe.expectMsg("null-undefined-0-0");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToCreateActorWIthNullConstructorParams3() {
|
|
|
|
|
ActorRef ref = system.actorOf(Props.create(ActorWithConstructorParams.class, "a", null));
|
|
|
|
|
final TestProbe probe = new TestProbe(system);
|
|
|
|
|
probe.send(ref, "get");
|
|
|
|
|
probe.expectMsg("a-null-0-0");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class ActorWithConstructorParams extends UntypedActor {
|
|
|
|
|
|
|
|
|
|
private final String a;
|
|
|
|
|
private final String b;
|
|
|
|
|
private final Integer c;
|
|
|
|
|
private final int d;
|
|
|
|
|
|
|
|
|
|
public ActorWithConstructorParams(String a, String b, Integer c, int d) {
|
|
|
|
|
this.a = a;
|
|
|
|
|
this.b = b;
|
|
|
|
|
this.c = c;
|
|
|
|
|
this.d = d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActorWithConstructorParams(String a, Object b) {
|
|
|
|
|
this.a = a;
|
|
|
|
|
this.b = String.valueOf(b);
|
|
|
|
|
this.c = 0;
|
|
|
|
|
this.d = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActorWithConstructorParams(String a, int d) {
|
|
|
|
|
this.a = a;
|
|
|
|
|
this.b = "undefined";
|
|
|
|
|
this.c = 0;
|
|
|
|
|
this.d = d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActorWithConstructorParams(Object a) {
|
|
|
|
|
this.a = String.valueOf(a);
|
|
|
|
|
this.b = "undefined";
|
|
|
|
|
this.c = 0;
|
|
|
|
|
this.d = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActorWithConstructorParams(int d) {
|
|
|
|
|
this.a = "undefined";
|
|
|
|
|
this.b = "undefined";
|
|
|
|
|
this.c = 0;
|
|
|
|
|
this.d = d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Object msg) {
|
|
|
|
|
String reply = String.valueOf(a) + "-" + String.valueOf(b) + "-" + String.valueOf(c) + "-" + String.valueOf(d);
|
|
|
|
|
getSender().tell(reply, getSelf());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-04 13:24:28 +01:00
|
|
|
}
|