2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
2011-11-17 23:15:19 +01:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2011-11-17 23:15:19 +01:00
|
|
|
package akka.actor;
|
|
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
|
|
|
|
import org.junit.*;
|
2011-11-30 15:16:20 +01:00
|
|
|
import akka.testkit.AkkaSpec;
|
2011-11-21 15:58:01 +01:00
|
|
|
import com.typesafe.config.ConfigFactory;
|
2019-02-17 11:45:39 -08:00
|
|
|
import org.scalatestplus.junit.JUnitSuite;
|
2011-11-21 15:58:01 +01:00
|
|
|
|
2011-11-17 23:15:19 +01:00
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
2016-04-06 01:23:21 +02:00
|
|
|
public class JavaExtension extends JUnitSuite {
|
2011-11-22 13:04:10 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
static class TestExtensionId extends AbstractExtensionId<TestExtension>
|
|
|
|
|
implements ExtensionIdProvider {
|
|
|
|
|
public static final TestExtensionId TestExtensionProvider = new TestExtensionId();
|
2012-01-20 11:30:33 +01:00
|
|
|
|
2011-11-30 15:16:20 +01:00
|
|
|
public ExtensionId<TestExtension> lookup() {
|
2012-01-23 16:11:58 +01:00
|
|
|
return TestExtensionId.TestExtensionProvider;
|
2011-11-30 15:16:20 +01:00
|
|
|
}
|
2011-11-22 13:04:10 +01:00
|
|
|
|
2012-01-24 11:33:40 +01:00
|
|
|
public TestExtension createExtension(ExtendedActorSystem i) {
|
2011-11-25 10:45:22 +01:00
|
|
|
return new TestExtension(i);
|
2011-11-17 23:15:19 +01:00
|
|
|
}
|
|
|
|
|
}
|
2011-11-22 13:04:10 +01:00
|
|
|
|
2011-11-25 10:45:22 +01:00
|
|
|
static class TestExtension implements Extension {
|
2012-01-24 11:33:40 +01:00
|
|
|
public final ExtendedActorSystem system;
|
2011-11-29 11:50:22 +01:00
|
|
|
|
2012-01-24 11:33:40 +01:00
|
|
|
public TestExtension(ExtendedActorSystem i) {
|
2011-11-29 11:50:22 +01:00
|
|
|
system = i;
|
|
|
|
|
}
|
2011-11-25 10:45:22 +01:00
|
|
|
}
|
2012-01-20 11:30:33 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
static class OtherExtensionId extends AbstractExtensionId<OtherExtension>
|
|
|
|
|
implements ExtensionIdProvider {
|
2017-02-17 17:07:15 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
public static final OtherExtensionId OtherExtensionProvider = new OtherExtensionId();
|
2017-02-17 17:07:15 +01:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ExtensionId<OtherExtension> lookup() {
|
|
|
|
|
return OtherExtensionId.OtherExtensionProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public OtherExtension createExtension(ExtendedActorSystem system) {
|
|
|
|
|
return new OtherExtension(system);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-29 23:00:57 +01:00
|
|
|
static class OtherExtension implements Extension {
|
2017-02-17 17:07:15 +01:00
|
|
|
static final ExtensionId<OtherExtension> key = OtherExtensionId.OtherExtensionProvider;
|
2011-11-29 23:00:57 +01:00
|
|
|
|
Bye-bye ReflectiveAccess, introducing PropertyMaster, see #1750
- PropertyMaster is the only place in Akka which calls
ClassLoader.getClass (apart from kernel, which might be special)
- all PropertyMaster methods (there are only three) take a ClassManifest
of what is to be constructed, and they verify that the obtained object
is actually compatible with the required type
Other stuff:
- noticed that I had forgotten to change to ExtendedActorSystem when
constructing Extensions by ExtensionKey (damn you, reflection!)
- moved Serializer.currentSystem into JavaSerializer, because that’s the
only one needing it (it’s only used in readResolve() methods)
- Serializers are constructed now with one-arg constructor taking
ExtendedActorSystem (if that exists, otherwise no-arg as before), to
allow JavaSerializer to do its magic; possibly necessary for others as
well
- Removed all Option[ClassLoader] signatures
- made it so that the ActorSystem will try context class loader, then
the class loader which loaded the class actually calling into
ActorSystem.apply, then the loader which loaded ActorSystemImpl
- for the second of the above I added a (reflectively accessed hopefully
safe) facility for getting caller Class[_] objects by using
sun.reflect.Reflection; this is optional an defaults to None, e.g. on
Android, which means that getting the caller’s classloader is done on
a best effort basis (there’s nothing we can do because a StackTrace
does not contain actual Class[_] objects).
- refactored DurableMailbox to contain the owner val and use that
instead of declaring that in all subclasses
2012-02-09 11:56:43 +01:00
|
|
|
public final ExtendedActorSystem system;
|
2012-01-20 11:30:33 +01:00
|
|
|
|
2012-02-10 11:36:23 +01:00
|
|
|
public OtherExtension(ExtendedActorSystem system) {
|
|
|
|
|
this.system = system;
|
2011-11-29 23:00:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
2011-11-25 10:45:22 +01:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
@ClassRule
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
2019-01-12 04:00:53 +08:00
|
|
|
new AkkaJUnitActorSystemResource(
|
|
|
|
|
"JavaExtension",
|
|
|
|
|
ConfigFactory.parseString(
|
|
|
|
|
"akka.extensions = [ \"akka.actor.JavaExtension$TestExtensionId\" ]")
|
|
|
|
|
.withFallback(AkkaSpec.testConf()));
|
2011-11-17 23:15:19 +01:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
2011-11-22 13:04:10 +01:00
|
|
|
|
2011-11-17 23:15:19 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAccessible() {
|
2012-09-18 15:48:25 +02:00
|
|
|
assertTrue(system.hasExtension((TestExtensionId.TestExtensionProvider)));
|
2012-01-23 16:11:58 +01:00
|
|
|
assertSame(system.extension(TestExtensionId.TestExtensionProvider).system, system);
|
|
|
|
|
assertSame(TestExtensionId.TestExtensionProvider.apply(system).system, system);
|
2011-11-17 23:15:19 +01:00
|
|
|
}
|
2012-01-20 11:30:33 +01:00
|
|
|
|
2011-11-29 23:00:57 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAdHoc() {
|
|
|
|
|
assertSame(OtherExtension.key.apply(system).system, system);
|
|
|
|
|
}
|
2011-11-17 23:15:19 +01:00
|
|
|
}
|