1 package org.codehaus.gmavenplus.util;
2
3 import org.apache.maven.plugin.logging.Log;
4 import org.junit.Test;
5 import org.mockito.ArgumentCaptor;
6
7 import static java.util.Collections.emptyList;
8 import static org.junit.Assert.*;
9 import static org.mockito.Mockito.*;
10
11
12
13
14
15
16
17 public class ClassWranglerTest {
18
19 @Test
20 public void testGetGroovyJar() throws Exception {
21 ClassWrangler classWrangler = spy(new ClassWrangler(emptyList(), null, mock(Log.class)));
22 doThrow(new ClassNotFoundException("Throwing exception to force GMavenPlus to get version from jar.")).when(classWrangler).getClass(anyString());
23 doReturn("some/path/groovy-all-1.5.0.jar").when(classWrangler).getJarPath();
24 assertEquals("groovy-all-1.5.0.jar", classWrangler.getGroovyJar());
25 }
26
27 @Test
28 public void testGetGroovyVersionStringFromGroovySystemThenFromInvokerHelper() throws Exception {
29 ClassWrangler classWrangler = spy(new ClassWrangler(emptyList(), null, mock(Log.class)));
30 doThrow(new ClassNotFoundException("Throwing exception to force GMavenPlus to try other methods."))
31 .when(classWrangler).getClass(anyString());
32 doReturn("some/path/groovy-all-1.5.0.jar").when(classWrangler).getJarPath();
33 ArgumentCaptor<String> classArg = ArgumentCaptor.forClass(String.class);
34 classWrangler.getGroovyVersionString();
35 verify(classWrangler, times(2)).getClass(classArg.capture());
36 assertEquals("groovy.lang.GroovySystem", classArg.getAllValues().get(0));
37 assertEquals("org.codehaus.groovy.runtime.InvokerHelper", classArg.getAllValues().get(1));
38 }
39
40 @Test
41 public void testGetGroovyVersionStringFromJar() throws Exception {
42 ClassWrangler classWrangler = spy(new ClassWrangler(emptyList(), null, mock(Log.class)));
43 doThrow(new ClassNotFoundException("Throwing exception to force GMavenPlus to get version from jar.")).when(classWrangler).getClass(anyString());
44 doReturn("some/path/groovy-all-1.5.0.jar").when(classWrangler).getJarPath();
45 assertEquals("1.5.0", classWrangler.getGroovyVersionString());
46 }
47
48 @Test
49 public void testGetGroovyVersionWithIndyFromJar() throws Exception {
50 ClassWrangler classWrangler = spy(new ClassWrangler(emptyList(), null, mock(Log.class)));
51 doThrow(new ClassNotFoundException("Throwing exception to force GMavenPlus to get version from jar.")).when(classWrangler).getClass(anyString());
52 doReturn("some/path/groovy-all-2.4.0-indy.jar").when(classWrangler).getJarPath();
53 assertEquals("2.4.0", classWrangler.getGroovyVersion().toString());
54 }
55
56 @Test
57 public void testGetGroovyVersionWithGrooidFromJar() 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-2.4.0-grooid.jar").when(classWrangler).getJarPath();
61 assertEquals("2.4.0", classWrangler.getGroovyVersion().toString());
62 }
63
64 @Test
65 public void testIsGroovyIndyTrue() throws Exception {
66 ClassWrangler classWrangler = spy(new ClassWrangler(emptyList(), null, mock(Log.class)));
67 doReturn(null).when(classWrangler).getClass(anyString());
68 assertTrue(classWrangler.isGroovyIndy());
69 }
70
71 @Test
72 public void testIsGroovyIndyFalse() throws Exception {
73 ClassWrangler classWrangler = spy(new ClassWrangler(emptyList(), null, mock(Log.class)));
74 doThrow(new ClassNotFoundException("Throwing exception to make it appear Groovy is not indy.")).when(classWrangler).getClass(anyString());
75 assertFalse(classWrangler.isGroovyIndy());
76 }
77
78 }