/** Create a fresh instance of DurianPlugins for each test, and save the system properties that we're gonna futz with. */ @Before public void before() { DurianPlugins.resetForTesting(); for (Class<?> plugin : pluginsToStore) { String key = DurianPlugins.PROPERTY_PREFIX + plugin.getCanonicalName(); // store the current value Optional<String> value = Optional.ofNullable(System.getProperty(key)); state.put(plugin, value); // then clear the property System.clearProperty(key); } }
/** Restore the system properties. */ @After public void after() { for (Class<?> plugin : pluginsToStore) { String key = DurianPlugins.PROPERTY_PREFIX + plugin.getCanonicalName(); // restore the stored value Optional<String> value = state.get(plugin); if (value.isPresent()) { System.setProperty(key, value.get()); } else { System.clearProperty(key); } } DurianPlugins.resetForTesting(); }
@Test(expected = AssertionError.class) public void testAssertionPlugin() { DurianPlugins.resetForTesting(); Errors.resetForTesting(); try { // set the Log handler to the be the OnErrorThrowAssertion System.setProperty("durian.plugins.com.diffplug.common.base.Errors.Plugins.Log", "com.diffplug.common.base.Errors$Plugins$OnErrorThrowAssertion"); // send something to the suppress handler, but we should get an AssertionError Errors.log().run(() -> { throw new RuntimeException("Didn't see this coming."); }); } finally { System.clearProperty("durian.plugins.com.diffplug.common.base.Errors.Plugins.Suppress"); DurianPlugins.resetForTesting(); Errors.resetForTesting(); } }
@Test public void testWiresCrossed() { DurianPlugins.resetForTesting(); Errors.resetForTesting(); DurianPlugins.register(Errors.Plugins.Log.class, new TestHandler("Log")); DurianPlugins.register(Errors.Plugins.Dialog.class, new TestHandler("Dialog")); try { Errors.suppress().run(ErrorsTest::throwException); try { Errors.rethrow().run(ErrorsTest::throwException); } catch (RuntimeException e) { // it should pass the RuntimeException unphased Assert.assertNull(e.getCause()); } try { Errors.log().run(ErrorsTest::throwException); } catch (RuntimeException e) { Assert.assertEquals("Log", e.getMessage()); } try { Errors.dialog().run(ErrorsTest::throwException); } catch (RuntimeException e) { Assert.assertEquals("Dialog", e.getMessage()); } } finally { DurianPlugins.resetForTesting(); Errors.resetForTesting(); } }