/*
 * JBoss, Home of Professional Open Source
 * Copyright 2009, Red Hat Middleware LLC, 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.test.osgi.framework.classloader;

import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import org.jboss.osgi.testing.OSGiFrameworkTest;
import org.jboss.test.osgi.framework.classloader.support.a.A;
import org.jboss.test.osgi.framework.classloader.support.b.B;
import org.jboss.test.osgi.framework.classloader.support.c.CA;
import org.junit.Test;
import org.osgi.framework.Bundle;

/**
 * BundleClassPathTest.
 * 
 * @author thomas.diesler@jboss.com
 * @since 07-Oct-2009
 */
public class BundleClassPathTestCase extends OSGiFrameworkTest {

    @Test
    public void testBundleClassPath() throws Exception {
        URL bundleURL = getTestArchiveURL("bundle-classpath.war");
        Bundle bundle = installBundle(bundleURL.toExternalForm());
        try {
            assertLoadClass(bundle, A.class.getName(), bundle);
            assertLoadClass(bundle, B.class.getName(), bundle);
            assertLoadClass(bundle, CA.class.getName(), bundle);
            
            URL url = bundle.getEntry("message.txt");
            assertEquals("/message.txt", url.getPath());

            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            assertEquals("Hello from Resource", br.readLine());
        } finally {
            bundle.uninstall();
        }
    }
}

@@ -1125,38 +1125,175 @@
 ls;%0A
-%0Aimport java.io.BufferedReader
+import static org.junit.Assert.assertTrue;%0A%0Aimport java.io.BufferedReader;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
 ;%0Aim
@@ -1344,16 +1344,41 @@
 net.URL;
+%0Aimport java.util.Arrays;
 %0A%0Aimport
@@ -1695,17 +1695,16 @@
 Test.%0A *
- 
 %0A * @aut
@@ -2232,28 +2232,16 @@
 undle);%0A
-            
 %0A       
@@ -2570,10 +2570,1536 @@
 %7D%0A    %7D%0A
+%0A    @Test%0A    public void testGetJarResource() throws Exception %7B%0A        URL bundleURL = getTestArchiveURL(%22bundle-classpath.war%22);%0A        Bundle bundle = installBundle(bundleURL.toExternalForm());%0A        try %7B%0A            URL compareURL = getTestArchiveURL(%22bundle-classpath-c.jar%22);%0A            byte%5B%5D compareBytes = suck(compareURL.openStream());%0A            assertTrue(%22Precondition%22, compareBytes.length %3E 0);%0A%0A            URL jarURL = bundle.getResource(%22bundle-classpath-c.jar%22);%0A            byte%5B%5D actualBytes = suck(jarURL.openStream());%0A            assertTrue(%22Returned bytes should be the same%22, Arrays.equals(compareBytes, actualBytes));%0A%0A            // TODO also check that the jar: url works on the URL returned%0A        %7D finally %7B%0A            bundle.uninstall();%0A        %7D%0A    %7D%0A%0A    private static byte%5B%5D suck(InputStream is) throws IOException %7B%0A        ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A        try %7B%0A            byte%5B%5D bytes = new byte%5B8192%5D;%0A%0A            int length = 0;%0A            int offset = 0;%0A%0A            while ((length = is.read(bytes, offset, bytes.length - offset)) != -1) %7B%0A                offset += length;%0A%0A                if (offset == bytes.length) %7B%0A                    baos.write(bytes, 0, bytes.length);%0A                    offset = 0;%0A                %7D%0A            %7D%0A            if (offset != 0) %7B%0A                baos.write(bytes, 0, offset);%0A            %7D%0A%0A            return baos.toByteArray();%0A        %7D finally %7B%0A            is.close();%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Test for AS7-937