Arquillian 测试带有 Seam 2.2.1 和 JSF 1.2 以及 Richfaces 3.3.3 和托管 JBoss 7.2 的应用程序

Arquillian tests for an application with Seam 2.2.1 with JSF 1.2 and Richfaces 3.3.3 with managed JBoss 7.2

我正在使用遗留代码基于:

该应用程序本身 运行 在 Wildfly 9.0.2 上运行良好,使用了一些依赖项,例如 JSF 1.2,而不是 Wildfly 9.0.2 默认值。 Arquillian 配置是 运行 在托管 JBoss 7.2

在将此应用移至较新的技术堆栈之前,我需要编写一个广泛的测试套件。为此,我想使用 Arquillian。

我已经设法让一些东西开始工作了。然而,对于某些测试,我需要创建一个身份并让 Seam 将我(测试)识别为登录用户。这就是我卡住的地方。

无论我尝试什么,我都会得到这个异常:

ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/ISMS_Tool]] (http-/0:0:0:0:0:0:0:0:8180-2) 
JBWEB000211: Session event listener threw exception: 
org.jboss.seam.InstantiationException: Could not instantiate Seam component: 
org.jboss.seam.security.ruleBasedPermissionResolver
at org.jboss.seam.Component.newInstance(Component.java:2170) 
[jboss-seam-2.2.1.Final.jar:2.2.1.Final]
at org.jboss.seam.contexts.Contexts.startup(Contexts.java:304) [jboss-seam-2.2.1.Final.jar:2.2.1.Final]

Caused by: org.jboss.seam.InstantiationException: Could not instantiate Seam component: securityRules
Caused by: java.lang.NullPointerException
at org.drools.base.evaluators.EvaluatorRegistry.addEvaluatorDefinition(EvaluatorRegistry.java:155) [drools-core-5.6.0.Final.jar:5.6.0.Final]

[我主要使用此存储库中的代码]https://github.com/omidp/seam/tree/master/seam-integration-tests/src/test/java/org/jboss/seam/test/integration/security

SecurityTest.java 是我在这里工作的目标。

下面的代码将解决上面的问题。我希望它对其他使用遗留应用程序的开发人员有用。

@RunWith(Arquillian.class)
@ArquillianSuiteDeployment
public class seamIntegrationTest  {

private AssetAction assetAction;
private EntityManager entityManager;
private User user;

@Deployment
public static Archive<?> createTestArchive() {
    MavenResolverSystem resolver = Maven.resolver();
    resolver.loadPomFromFile("pom.xml");
    return ShrinkWrap.create(WebArchive.class, "my.war")
            .addPackages(true, "de.abc.ui")
            .addPackages(true, "de.abc")
            .addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml")
            .addAsResource("META-INF/jboss-deployment-structure.xml", "META-INF/jboss-deployment-structure.xml")
            .addAsResource("META-INF/jaxws-endpoint-config.xml", "META-INF/jaxws-endpoint-config.xml")
            .addAsResource("META-INF/jboss-webservices.xml", "META-INF/jboss-webservices.xml")
            .addAsResource("de/abc/tool/webservice/handler/ws_security_handler.xml")
            .addAsResource("components.properties")
            .addAsResource("security.drl")
            .addAsResource(EmptyAsset.INSTANCE, "seam.properties")
            .addAsWebInfResource("components.xml")
            .setWebXML("web.xml")
            .addAsLibraries(
                    resolver.loadPomFromFile("pom.xml").importRuntimeAndTestDependencies().resolve().withTransitivity().asFile());
}



@Before
public void setUp() throws Exception {
    MockFacesContext mfc = new MockFacesContext(new MockExternalContext(), new MockApplication());
    UIViewRoot viewRoot = mock(UIViewRoot.class);
    when(viewRoot.findComponent(anyString())).thenReturn(mock(UIForm.class));
    mfc.setViewRoot(viewRoot);

    Method m = ReflectionUtils.findMethod(FacesContext.class, "setCurrentInstance", FacesContext.class);
    m.setAccessible(true);
    m.invoke(null, mfc);

    Credentials credentials = new Credentials();
    credentials.setUsername("user");
    credentials.setPassword("password");// these should be an existing user in the database
    credentials.setInitialized(true);

    Principal principal = new Principal() {
        @Override
        public String getName() {
            return "principalname";
        }
    };

    ExtendedIdentity identity = new ExtendedIdentity();
    identity.create();

    Field c = ReflectionUtils.findField(ExtendedIdentity.class, "credentials");
    c.setAccessible(true);
    ReflectionUtils.setField(c, identity, credentials);

    c = ReflectionUtils.findField(ExtendedIdentity.class, "principal");
    c.setAccessible(true);
    ReflectionUtils.setField(c, identity, principal);

    identity.login();
    Contexts.getSessionContext().set(Component.getComponentName(Identity.class), identity);

    List<User> users = entityManager.createNamedQuery(User.Q_getUserByUserName)
            .setParameter("username", "password")
            .getResultList();
    if (!users.isEmpty()) {
        user = users.get(0);
        Contexts.getSessionContext().set("loginUser", user);
        for (Role role : user.getRoles()) {
            identity.addRole(role.getRolename());
        }
    }

}

// This is the dialog that can only be accessed when a user is successfully logged in:
@Test
public void openCreateNewDialog() throws Exception {

    assetAction.setAssetId(126);
    assetAction.setAsset(entityManager.find(Asset.class, 126));

    assetAction.openNewChildAssetDialog();
    String result = assetAction.saveNewChildAsset();
}

}