/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.test.integration.ejb.threadpool;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.as.arquillian.container.ManagementClient;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.operations.common.Util;
import org.jboss.as.test.integration.management.ManagementOperations;
import org.jboss.as.test.integration.management.util.MgmtOperationException;
import org.jboss.dmr.ModelNode;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;

import javax.naming.InitialContext;

public class Ejb3ThreadPoolBase {

    static PathAddress DEFAULT_THREAD_POOL_ADDRESS = PathAddress.pathAddress("subsystem", "ejb3").append("thread-pool", "default");

    @ArquillianResource
    static ManagementClient managementClient;

    @ArquillianResource
    InitialContext iniCtx;

    @Deployment
    public static Archive<?> deploy() {
        JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "ejb3singleton.jar");
        jar.addClass(ScheduleSingletonOneTimer.class);
        jar.addClasses(Ejb3ThreadPoolBase.class, Ejb3NonCoreThreadTimeoutTestCase.class, ModelNode.class, PathAddress.class,
                ManagementOperations.class, MgmtOperationException.class);
        jar.addAsManifestResource(new StringAsset("Dependencies: org.jboss.as.controller\n"), "MANIFEST.MF");
        return jar;
    }

    void waitUntilThreadPoolProcessedAtLeast(int tasks, long timeoutInMillis) throws Exception {
        long startTime = System.currentTimeMillis();
        ModelNode readTasks = Util.getReadAttributeOperation(DEFAULT_THREAD_POOL_ADDRESS, "completed-task-count");
        while (executeOperation(readTasks).asInt() < tasks) {
            if (System.currentTimeMillis() - startTime > timeoutInMillis) {
                Assert.fail("There are not enough tasks (expected: " + tasks + ") processed by thread pool in timeout " + timeoutInMillis);
            }
            Thread.sleep(500);
        }
    }

    ModelNode readAttribute(String attribute) throws Exception {
        return executeOperation(Util.getReadAttributeOperation(DEFAULT_THREAD_POOL_ADDRESS, attribute));
    }

    static ModelNode executeOperation(ModelNode op) throws Exception {
        ModelNode result = ManagementOperations.executeOperation(managementClient.getControllerClient(), op);
        return result;
    }
}

@@ -1525,16 +1525,72 @@
 elNode;%0A
+import org.jboss.remoting3.security.RemotingPermission;%0A
 import o
@@ -1614,32 +1614,32 @@
 ap.api.Archive;%0A
-
 import org.jboss
@@ -1828,16 +1828,155 @@
 Context;
+%0Aimport java.io.FilePermission;%0A%0Aimport static org.jboss.as.test.shared.integration.ejb.security.PermissionUtils.createPermissionsXmlAsset;
 %0A%0Apublic
@@ -2720,16 +2720,37 @@
 dencies:
+ org.jboss.remoting3,
  org.jbo
@@ -2783,24 +2783,329 @@
 IFEST.MF%22);%0A
+        jar.addAsManifestResource(createPermissionsXmlAsset(%0A                new RemotingPermission(%22createEndpoint%22),%0A                new RemotingPermission(%22connect%22),%0A                new FilePermission(System.getProperty(%22jboss.inst%22) + %22/standalone/tmp/auth/*%22, %22read%22)%0A        ), %22permissions.xml%22);%0A
         retu

Fix Ejb3NonCoreThreadTimeoutTestCase and Ejb3ThreadReuseTestCase test with security manager