1 /* 2 * Copyright (c) 2017 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.internal.configuration.plugins; 6 7 import org.junit.Rule; 8 import org.junit.Test; 9 import org.junit.rules.TemporaryFolder; 10 import org.mockito.InjectMocks; 11 import org.mockito.Mock; 12 import org.mockito.internal.util.io.IOUtil; 13 import org.mockito.plugins.PluginSwitch; 14 import org.mockitoutil.TestBase; 15 16 import java.io.File; 17 import java.net.URL; 18 import java.util.Collections; 19 20 import static java.util.Arrays.asList; 21 import static org.junit.Assert.*; 22 import static org.assertj.core.api.Assertions.assertThat; 23 import static org.mockito.Matchers.anyString; 24 import static org.mockito.Mockito.when; 25 26 public class PluginFinderTest extends TestBase { 27 28 @Mock 29 PluginSwitch switcher; 30 @InjectMocks PluginFinder finder; 31 public @Rule TemporaryFolder tmp = new TemporaryFolder(); 32 empty_resources()33 @Test public void empty_resources() { 34 assertNull(finder.findPluginClass(Collections.<URL>emptyList())); 35 } 36 no_valid_impl()37 @Test public void no_valid_impl() throws Exception { 38 File f = tmp.newFile(); 39 40 //when 41 IOUtil.writeText(" \n ", f); 42 43 //then 44 assertNull(finder.findPluginClass(asList(f.toURI().toURL()))); 45 } 46 single_implementation()47 @Test public void single_implementation() throws Exception { 48 File f = tmp.newFile(); 49 when(switcher.isEnabled("foo.Foo")).thenReturn(true); 50 51 //when 52 IOUtil.writeText(" foo.Foo ", f); 53 54 //then 55 assertEquals("foo.Foo", finder.findPluginClass(asList(f.toURI().toURL()))); 56 } 57 single_implementation_disabled()58 @Test public void single_implementation_disabled() throws Exception { 59 File f = tmp.newFile(); 60 when(switcher.isEnabled("foo.Foo")).thenReturn(false); 61 62 //when 63 IOUtil.writeText(" foo.Foo ", f); 64 65 //then 66 assertEquals(null, finder.findPluginClass(asList(f.toURI().toURL()))); 67 } 68 multiple_implementations_only_one_enabled()69 @Test public void multiple_implementations_only_one_enabled() throws Exception { 70 File f1 = tmp.newFile(); File f2 = tmp.newFile(); 71 72 when(switcher.isEnabled("Bar")).thenReturn(true); 73 74 //when 75 IOUtil.writeText("Foo", f1); IOUtil.writeText("Bar", f2); 76 77 //then 78 assertEquals("Bar", finder.findPluginClass(asList(f1.toURI().toURL(), f2.toURI().toURL()))); 79 } 80 multiple_implementations_only_one_useful()81 @Test public void multiple_implementations_only_one_useful() throws Exception { 82 File f1 = tmp.newFile(); File f2 = tmp.newFile(); 83 84 when(switcher.isEnabled(anyString())).thenReturn(true); 85 86 //when 87 IOUtil.writeText(" ", f1); IOUtil.writeText("X", f2); 88 89 //then 90 assertEquals("X", finder.findPluginClass(asList(f1.toURI().toURL(), f2.toURI().toURL()))); 91 } 92 multiple_empty_implementations()93 @Test public void multiple_empty_implementations() throws Exception { 94 File f1 = tmp.newFile(); File f2 = tmp.newFile(); 95 96 when(switcher.isEnabled(anyString())).thenReturn(true); 97 98 //when 99 IOUtil.writeText(" ", f1); IOUtil.writeText("\n", f2); 100 101 //then 102 assertEquals(null, finder.findPluginClass(asList(f1.toURI().toURL(), f2.toURI().toURL()))); 103 } 104 problems_loading_impl()105 @Test public void problems_loading_impl() throws Exception { 106 when(switcher.isEnabled(anyString())).thenThrow(new RuntimeException("Boo!")); 107 108 try { 109 //when 110 finder.findPluginClass(asList(new File("xxx").toURI().toURL())); 111 //then 112 fail(); 113 } catch(Exception e) { 114 assertThat(e).hasMessageContaining("xxx"); 115 e.getCause().getMessage().equals("Boo!"); 116 } 117 } 118 } 119