View Javadoc
1   package org.apache.maven.surefire.booter;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.shared.utils.io.FileUtils;
23  import org.apache.maven.surefire.api.provider.AbstractProvider;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import java.io.File;
28  import java.net.URL;
29  
30  import static java.io.File.pathSeparator;
31  import static org.hamcrest.CoreMatchers.is;
32  import static org.hamcrest.CoreMatchers.not;
33  import static org.hamcrest.CoreMatchers.notNullValue;
34  import static org.hamcrest.MatcherAssert.assertThat;
35  
36  /**
37   * Tests isolated CL.
38   */
39  public class IsolatedClassLoaderTest
40  {
41      private IsolatedClassLoader classLoader;
42  
43      @Before
44      public void prepareClassLoader() throws Exception
45      {
46          classLoader = new IsolatedClassLoader( null, false, "role" );
47  
48          String[] files = FileUtils.fileRead( new File( "target/test-classpath/cp.txt" ), "UTF-8" )
49                  .split( pathSeparator );
50  
51          for ( String file : files )
52          {
53              URL fileUrl = new File( file ).toURL();
54              classLoader.addURL( fileUrl );
55          }
56      }
57  
58      @Test
59      public void shouldLoadIsolatedClass() throws Exception
60      {
61          Class<?> isolatedClass = classLoader.loadClass( AbstractProvider.class.getName() );
62          assertThat( isolatedClass, is( notNullValue() ) );
63          assertThat( isolatedClass.getName(), is( AbstractProvider.class.getName() ) );
64          assertThat( isolatedClass, is( not( (Class) AbstractProvider.class ) ) );
65      }
66  }