<commit_before>/*-
 * Copyright (c) 2014 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fedoraproject.xmvn.locator;

import java.io.InputStream;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * @author <NAME>
 */
public class IsolatedClassRealmTest
{
    protected final Path resourceDir = Paths.get( "src/test/resources" ).toAbsolutePath();
    protected Path jarPath = resourceDir.resolve( "example.jar" );
    protected Path secondJarPath = resourceDir.resolve( "example2.jar" );

    @Test
    public void testImports()
        throws Exception
    {
        try (IsolatedClassRealm realm = new IsolatedClassRealm( null ))
        {
            realm.importPackage( "java.lang" );
            realm.importPackage( "junit" );
            realm.importPackage( "org.eclipse.sisu.space" );

            assertTrue( realm.isImported( "java.lang.Object" ) );
            assertTrue( realm.isImported( "java/lang/Object" ) );
            assertTrue( realm.isImported( "java/lang/Object.class" ) );

            assertFalse( realm.isImported( "java.math.Random" ) );
            assertFalse( realm.isImported( "java/math/Random" ) );
            assertFalse( realm.isImported( "java/math/Random.class" ) );

            assertTrue( realm.isImported( "junit.Assert" ) );
            assertFalse( realm.isImported( "junit" ) );
            assertFalse( realm.isImported( "org.junit.Assert" ) );

            assertTrue( realm.isImported( "org.eclipse.sisu.space.ClassSpace" ) );
            assertFalse( realm.isImported( "org.eclipse.sisu.space.asm.ClassVisitor" ) );
        }
    }

    @Test
    public void testLoadJar()
            throws Exception
    {
        try ( IsolatedClassRealm realm = new IsolatedClassRealm( IsolatedClassRealmTest.class.getClassLoader() ) )
        {
            realm.addJar( jarPath );
            Class clazz = realm.loadClass( "com.example.Example" );
            String data = (String) clazz.getMethod( "getTestData" ).invoke( null, new Object[0] );
            assertEquals( "test", data );
        }
    }

    @Test
    public void testLoadJarDirectory()
            throws Exception
    {
        try ( IsolatedClassRealm realm = new IsolatedClassRealm( IsolatedClassRealmTest.class.getClassLoader() ) )
        {
            realm.addJarDirectory( resourceDir );
            Class clazz = realm.loadClass( "com.example.Example" );
            String data = (String) clazz.getMethod( "getTestData" ).invoke( null, new Object[0] );
            assertEquals( "test", data );
            Class clazz2 = realm.loadClass( "com.example.SecondExample" );
            String data2 = (String) clazz2.getMethod( "getTestData" ).invoke( null, new Object[0] );
            assertEquals( "test-second", data2 );
        }
    }

    @Test
    public void testGetResource()
            throws Exception
    {
        try ( IsolatedClassRealm realm = new IsolatedClassRealm( IsolatedClassRealmTest.class.getClassLoader() ) )
        {
            realm.addJar( jarPath );
            InputStream resourceStream = realm.getResourceAsStream( "secret-file" );
            assertNotNull( resourceStream );
            int read = resourceStream.read();
            assertEquals( '#', read );
        }
    }

    @Test
    public void testGetResources()
            throws Exception
    {
        try ( IsolatedClassRealm realm = new IsolatedClassRealm( IsolatedClassRealmTest.class.getClassLoader() ) )
        {
            realm.addJarDirectory( resourceDir );
            realm.getResource( "secret-file" );
            Enumeration<URL> resources = realm.getResources( "secret-file" );
            assertTrue( resources.hasMoreElements() );
            resources.nextElement();
            assertTrue( resources.hasMoreElements() );
        }
    }
}
<commit_msg>[test] Tests for parent classloader in IsolatedClassRealm<commit_after>/*-
 * Copyright (c) 2014 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fedoraproject.xmvn.locator;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * @author <NAME>
 */
public class IsolatedClassRealmTest
{
    protected final Path resourceDir = Paths.get( "src/test/resources" ).toAbsolutePath();
    protected Path jarPath = resourceDir.resolve( "example.jar" );
    protected Path secondJarPath = resourceDir.resolve( "example2.jar" );

    @Test
    public void testImports()
        throws Exception
    {
        try (IsolatedClassRealm realm = new IsolatedClassRealm( null ))
        {
            realm.importPackage( "java.lang" );
            realm.importPackage( "junit" );
            realm.importPackage( "org.eclipse.sisu.space" );

            assertTrue( realm.isImported( "java.lang.Object" ) );
            assertTrue( realm.isImported( "java/lang/Object" ) );
            assertTrue( realm.isImported( "java/lang/Object.class" ) );

            assertFalse( realm.isImported( "java.math.Random" ) );
            assertFalse( realm.isImported( "java/math/Random" ) );
            assertFalse( realm.isImported( "java/math/Random.class" ) );

            assertTrue( realm.isImported( "junit.Assert" ) );
            assertFalse( realm.isImported( "junit" ) );
            assertFalse( realm.isImported( "org.junit.Assert" ) );

            assertTrue( realm.isImported( "org.eclipse.sisu.space.ClassSpace" ) );
            assertFalse( realm.isImported( "org.eclipse.sisu.space.asm.ClassVisitor" ) );
        }
    }

    @Test
    public void testLoadJar()
            throws Exception
    {
        try ( IsolatedClassRealm realm = new IsolatedClassRealm( IsolatedClassRealmTest.class.getClassLoader() ) )
        {
            realm.addJar( jarPath );
            Class clazz = realm.loadClass( "com.example.Example" );
            String data = (String) clazz.getMethod( "getTestData" ).invoke( null, new Object[0] );
            assertEquals( "test", data );
        }
    }

    @Test
    public void testLoadJarDirectory()
            throws Exception
    {
        try ( IsolatedClassRealm realm = new IsolatedClassRealm( IsolatedClassRealmTest.class.getClassLoader() ) )
        {
            realm.addJarDirectory( resourceDir );
            Class clazz = realm.loadClass( "com.example.Example" );
            String data = (String) clazz.getMethod( "getTestData" ).invoke( null, new Object[0] );
            assertEquals( "test", data );
            Class clazz2 = realm.loadClass( "com.example.SecondExample" );
            String data2 = (String) clazz2.getMethod( "getTestData" ).invoke( null, new Object[0] );
            assertEquals( "test-second", data2 );
        }
    }

    @Test
    public void testGetResource()
            throws Exception
    {
        try ( IsolatedClassRealm realm = new IsolatedClassRealm( IsolatedClassRealmTest.class.getClassLoader() ) )
        {
            realm.addJar( jarPath );
            InputStream resourceStream = realm.getResourceAsStream( "secret-file" );
            assertNotNull( resourceStream );
            int read = resourceStream.read();
            assertEquals( '#', read );
        }
    }

    @Test
    public void testGetResources()
            throws Exception
    {
        try ( IsolatedClassRealm realm = new IsolatedClassRealm( IsolatedClassRealmTest.class.getClassLoader() ) )
        {
            realm.addJarDirectory( resourceDir );
            realm.getResource( "secret-file" );
            Enumeration<URL> resources = realm.getResources( "secret-file" );
            assertTrue( resources.hasMoreElements() );
            resources.nextElement();
            assertTrue( resources.hasMoreElements() );
        }
    }

    class MockClassLoader
            extends ClassLoader
    {
        @Override
        public Class<?> loadClass( String string )
                throws ClassNotFoundException
        {
            return IsolatedClassRealmTest.class;
        }

        @Override
        public URL getResource( String string )
        {
            try
            {
                return new URL( "http://example.com" );
            }
            catch ( MalformedURLException ex )
            {
                throw new RuntimeException();
            }
        }

    }

    @Test
    public void testParentClassloader()
            throws Exception
    {
        try ( IsolatedClassRealm realm = new IsolatedClassRealm( new MockClassLoader() ) )
        {
            realm.addJar( jarPath );
            realm.importPackage( "com.example" );
            Class clazz = realm.loadClass( "com.example.Example" );
            assertEquals( IsolatedClassRealmTest.class, clazz );
        }
    }

    @Test
    public void testParentClassloaderResource()
            throws Exception
    {
        try ( IsolatedClassRealm realm = new IsolatedClassRealm( new MockClassLoader() ) )
        {
            realm.addJar( jarPath );
            realm.importPackage( "resources" );
            URL url = realm.getResource( "resources/secret-file" );
            assertEquals( "http://example.com", url.toString() );
        }
    }
}

{'fraction_non_alphanumeric': 0.10583941605839416, 'fraction_numerical': 0.0029945723376380313, 'mean_word_length': 2.373421717171717, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6038751', 'n_tokens_mistral': 2962, 'n_tokens_neox': 2929, 'n_words': 707}