pekko/akka-actor-tests/src/test/java/akka/actor/JavaExtension.java

78 lines
2 KiB
Java
Raw Normal View History

/**
2013-01-09 01:47:48 +01:00
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.actor;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
2011-11-21 15:58:01 +01:00
import akka.testkit.AkkaSpec;
2011-11-21 15:58:01 +01:00
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.Config;
import static org.junit.Assert.*;
public class JavaExtension {
static class TestExtensionId extends AbstractExtensionId<TestExtension> implements ExtensionIdProvider {
public final static TestExtensionId TestExtensionProvider = new TestExtensionId();
public ExtensionId<TestExtension> lookup() {
return TestExtensionId.TestExtensionProvider;
}
public TestExtension createExtension(ExtendedActorSystem i) {
return new TestExtension(i);
}
}
static class TestExtension implements Extension {
public final ExtendedActorSystem system;
public TestExtension(ExtendedActorSystem i) {
system = i;
}
}
static class OtherExtension implements Extension {
static final ExtensionKey<OtherExtension> key = new ExtensionKey<OtherExtension>(OtherExtension.class) {
};
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;
public OtherExtension(ExtendedActorSystem system) {
this.system = system;
}
}
private static ActorSystem system;
@BeforeClass
public static void beforeAll() {
Config c = ConfigFactory.parseString("akka.extensions = [ \"akka.actor.JavaExtension$TestExtensionId\" ]")
.withFallback(AkkaSpec.testConf());
system = ActorSystem.create("JavaExtension", c);
}
@AfterClass
public static void afterAll() {
system.shutdown();
system = null;
}
@Test
public void mustBeAccessible() {
assertTrue(system.hasExtension((TestExtensionId.TestExtensionProvider)));
assertSame(system.extension(TestExtensionId.TestExtensionProvider).system, system);
assertSame(TestExtensionId.TestExtensionProvider.apply(system).system, system);
}
@Test
public void mustBeAdHoc() {
assertSame(OtherExtension.key.apply(system).system, system);
}
}