/*
 * JBoss, Home of Professional Open Source.
 * Copyright (c) 2011, 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.testsuite.integration.osgi.as858;

import static org.junit.Assert.assertEquals;

import java.io.InputStream;
import java.util.jar.JarFile;

import javax.inject.Inject;
import javax.naming.NamingException;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.modules.Module;
import org.jboss.modules.ModuleIdentifier;
import org.jboss.modules.ModuleSpec;
import org.jboss.msc.service.ServiceContainer;
import org.jboss.msc.service.ServiceController;
import org.jboss.osgi.framework.ModuleLoaderProvider;
import org.jboss.osgi.framework.Services;
import org.jboss.osgi.testing.ManifestBuilder;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * [AS7-858] Cannot load module when applying resolver results
 *
 * https://issues.jboss.org/browse/AS7-858
 *
 * @author Thomas.Diesler@jboss.com
 */
@RunWith(Arquillian.class)
public class AS858TestCase {

    @Inject
    public ServiceContainer serviceContainer;

    @Deployment
    public static Archive<?> deployment() {
        JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "as858");
        archive.add(new Asset() {
            @Override
            public InputStream openStream() {
                ManifestBuilder builder = ManifestBuilder.newInstance();
                builder.addManifestHeader("Dependencies", "org.jboss.as.osgi,org.jboss.osgi.framework");
                return builder.openStream();
            }}, JarFile.MANIFEST_NAME);
        return archive;
    }

    @Test
    public void testModuleLoader() throws NamingException, Exception {
        ServiceController<?> controller = serviceContainer.getService(Services.MODULE_LOADER_PROVIDER);
        ModuleLoaderProvider loaderProvider = (ModuleLoaderProvider) controller.getValue();

        ModuleIdentifier identifier = ModuleIdentifier.create("deployment.as858.test");
        ModuleSpec moduleSpec = ModuleSpec.build(identifier).create();
        loaderProvider.addModule(moduleSpec);
        Module module = loaderProvider.getModuleLoader().loadModule(identifier);
        assertEquals(identifier, module.getIdentifier());
    }
}

@@ -1352,16 +1352,138 @@
 illian;%0A
+import org.jboss.as.server.moduleservice.ModuleLoadService;%0Aimport org.jboss.as.server.moduleservice.ServiceModuleLoader;%0A
 import o
@@ -1678,24 +1678,66 @@
 Controller;%0A
+import org.jboss.msc.service.ServiceName;%0A
 import org.j
@@ -2173,16 +2173,21 @@
 sults%0A *
+ %3Cp/%3E
 %0A * http
@@ -2564,16 +2564,24 @@
         
+        
 @Overrid
@@ -2590,24 +2590,32 @@
             
+      
+  
 public Input
@@ -2632,24 +2632,32 @@
 nStream() %7B%0A
+        
             
@@ -2733,16 +2733,24 @@
         
+    
+    
 builder.
@@ -2842,24 +2842,32 @@
             
+        
 return build
@@ -2895,17 +2895,42 @@
         
-%7D
+        %7D%0A                
 %7D, JarFi
@@ -3060,16 +3060,122 @@
 ption %7B%0A
+        ModuleIdentifier identifier = ModuleIdentifier.create(%22deployment.as858.test%22);%0A        try %7B%0A    
         
@@ -3274,24 +3274,28 @@
 R);%0A        
+    
 ModuleLoader
@@ -3375,92 +3375,8 @@
     
-    ModuleIdentifier identifier = ModuleIdentifier.create(%22deployment.as858.test%22);%0A
     
@@ -3450,16 +3450,20 @@
         
+  
+  
 loaderPr
@@ -3492,16 +3492,20 @@
 eSpec);%0A
+    
         
@@ -3585,16 +3585,20 @@
         
+ 
+   
 assertEq
@@ -3639,16 +3639,618 @@
 ier());%0A
+        %7D finally %7B%0A            ServiceController%3C?%3E controller = serviceContainer.getService(ServiceName.JBOSS.append(%22module%22, %22spec%22, %22service%22, identifier.getName(), identifier.getSlot()));%0A            if (controller != null) %7B%0A                controller.setMode(ServiceController.Mode.REMOVE);%0A            %7D%0A            controller = serviceContainer.getService(ServiceName.JBOSS.append(%22module%22, %22information%22, %22service%22, identifier.getName(), identifier.getSlot()));%0A            if (controller != null) %7B%0A                controller.setMode(ServiceController.Mode.REMOVE);%0A            %7D%0A        %7D%0A
     %7D%0A%7D%0A

Clean up after test case to multiple test runs do not fail