|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2023 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package org.scijava.module.event; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertEquals; |
| 33 | +import static org.junit.Assert.assertThrows; |
| 34 | +import static org.junit.Assert.assertNotNull; |
| 35 | + |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Test; |
| 38 | +import org.scijava.Context; |
| 39 | +import org.scijava.event.EventHandler; |
| 40 | +import org.scijava.event.EventService; |
| 41 | +import org.scijava.module.AbstractModule; |
| 42 | +import org.scijava.module.AbstractModuleInfo; |
| 43 | +import org.scijava.module.Module; |
| 44 | +import org.scijava.module.ModuleException; |
| 45 | +import org.scijava.module.ModuleInfo; |
| 46 | +import org.scijava.module.ModuleService; |
| 47 | + |
| 48 | +/** |
| 49 | + * Tests {@link ModuleErroredEvent} behavior. |
| 50 | + * |
| 51 | + * @author Gabriel Selzer |
| 52 | + * @author Curtis Rueden |
| 53 | + */ |
| 54 | +public class ModuleErroredEventTest { |
| 55 | + |
| 56 | + private EventService es; |
| 57 | + private ModuleService module; |
| 58 | + |
| 59 | + @Before |
| 60 | + public void setUp() { |
| 61 | + Context ctx = new Context(); |
| 62 | + es = ctx.getService(EventService.class); |
| 63 | + module = ctx.getService(ModuleService.class); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testModuleErroredEvent() { |
| 68 | + |
| 69 | + // Must be a final boolean array to be included in the below closure |
| 70 | + final Throwable[] caughtException = { null }; |
| 71 | + |
| 72 | + // Add a new EventHandler to change our state |
| 73 | + final Object interestedParty = new Object() { |
| 74 | + |
| 75 | + @EventHandler |
| 76 | + void onEvent(final ModuleErroredEvent e) { |
| 77 | + caughtException[0] = e.getException(); |
| 78 | + } |
| 79 | + }; |
| 80 | + es.subscribe(interestedParty); |
| 81 | + |
| 82 | + // Run the module, ensure we get the exception |
| 83 | + assertThrows(Exception.class, // |
| 84 | + () -> module.run(new TestModuleInfo(), false).get()); |
| 85 | + assertNotNull(caughtException[0]); |
| 86 | + assertEquals("Yay!", caughtException[0].getMessage()); |
| 87 | + } |
| 88 | + |
| 89 | + static class TestModuleInfo extends AbstractModuleInfo { |
| 90 | + |
| 91 | + @Override |
| 92 | + public String getDelegateClassName() { |
| 93 | + return this.getClass().getName(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public Class<?> loadDelegateClass() throws ClassNotFoundException { |
| 98 | + return this.getClass(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public Module createModule() throws ModuleException { |
| 103 | + ModuleInfo thisInfo = this; |
| 104 | + return new AbstractModule() { |
| 105 | + |
| 106 | + @Override |
| 107 | + public ModuleInfo getInfo() { |
| 108 | + return thisInfo; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void run() { |
| 113 | + throw new RuntimeException("Yay!"); |
| 114 | + } |
| 115 | + }; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments