View Javadoc
1   /*
2    * Copyright (C) 2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.gmavenplus.util;
18  
19  import org.apache.maven.plugin.logging.Log;
20  import org.junit.Test;
21  import org.mockito.ArgumentCaptor;
22  
23  import static java.util.Collections.emptyList;
24  import static org.junit.Assert.*;
25  import static org.mockito.Mockito.*;
26  
27  
28  /**
29   * Unit tests for the ClassWrangler class.
30   *
31   * @author Keegan Witt
32   */
33  public class ClassWranglerTest {
34  
35      @Test
36      public void testGetGroovyJar() throws Exception {
37          ClassWrangler classWrangler = spy(new ClassWrangler(emptyList(), null, mock(Log.class)));
38          doThrow(new ClassNotFoundException("Throwing exception to force GMavenPlus to get version from jar.")).when(classWrangler).getClass(anyString());
39          doReturn("some/path/groovy-all-1.5.0.jar").when(classWrangler).getJarPath();
40          assertEquals("groovy-all-1.5.0.jar", classWrangler.getGroovyJar());
41      }
42  
43      @Test
44      public void testGetGroovyVersionStringFromGroovySystemThenFromInvokerHelper() throws Exception {
45          ClassWrangler classWrangler = spy(new ClassWrangler(emptyList(), null, mock(Log.class)));
46          doThrow(new ClassNotFoundException("Throwing exception to force GMavenPlus to try other methods."))
47                  .when(classWrangler).getClass(anyString());
48          doReturn("some/path/groovy-all-1.5.0.jar").when(classWrangler).getJarPath();
49          ArgumentCaptor<String> classArg = ArgumentCaptor.forClass(String.class);
50          classWrangler.getGroovyVersionString();
51          verify(classWrangler, times(2)).getClass(classArg.capture());
52          assertEquals("groovy.lang.GroovySystem", classArg.getAllValues().get(0));
53          assertEquals("org.codehaus.groovy.runtime.InvokerHelper", classArg.getAllValues().get(1));
54      }
55  
56      @Test
57      public void testGetGroovyVersionStringFromJar() throws Exception {
58          ClassWrangler classWrangler = spy(new ClassWrangler(emptyList(), null, mock(Log.class)));
59          doThrow(new ClassNotFoundException("Throwing exception to force GMavenPlus to get version from jar.")).when(classWrangler).getClass(anyString());
60          doReturn("some/path/groovy-all-1.5.0.jar").when(classWrangler).getJarPath();
61          assertEquals("1.5.0", classWrangler.getGroovyVersionString());
62      }
63  
64      @Test
65      public void testGetGroovyVersionWithIndyFromJar() throws Exception {
66          ClassWrangler classWrangler = spy(new ClassWrangler(emptyList(), null, mock(Log.class)));
67          doThrow(new ClassNotFoundException("Throwing exception to force GMavenPlus to get version from jar.")).when(classWrangler).getClass(anyString());
68          doReturn("some/path/groovy-all-2.4.0-indy.jar").when(classWrangler).getJarPath();
69          assertEquals("2.4.0", classWrangler.getGroovyVersion().toString());
70      }
71  
72      @Test
73      public void testGetGroovyVersionWithGrooidFromJar() throws Exception {
74          ClassWrangler classWrangler = spy(new ClassWrangler(emptyList(), null, mock(Log.class)));
75          doThrow(new ClassNotFoundException("Throwing exception to force GMavenPlus to get version from jar.")).when(classWrangler).getClass(anyString());
76          doReturn("some/path/groovy-all-2.4.0-grooid.jar").when(classWrangler).getJarPath();
77          assertEquals("2.4.0", classWrangler.getGroovyVersion().toString());
78      }
79  
80      @Test
81      public void testIsGroovyIndyTrue() throws Exception {
82          ClassWrangler classWrangler = spy(new ClassWrangler(emptyList(), null, mock(Log.class)));
83          doReturn(null).when(classWrangler).getClass(anyString());  // make it appear Groovy is indy
84          assertTrue(classWrangler.isGroovyIndy());
85      }
86  
87      @Test
88      public void testIsGroovyIndyFalse() throws Exception {
89          ClassWrangler classWrangler = spy(new ClassWrangler(emptyList(), null, mock(Log.class)));
90          doThrow(new ClassNotFoundException("Throwing exception to make it appear Groovy is not indy.")).when(classWrangler).getClass(anyString());
91          assertFalse(classWrangler.isGroovyIndy());
92      }
93  
94  }