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