/** * Must not set submitOnChange on a WToggleButton. If you need a button which submits the form use * {@link com.github.bordertech.wcomponents.WButton}. * @param submitOnChange not used */ @Override public void setSubmitOnChange(final boolean submitOnChange) { throw new SystemException("Cannot set submitOnChange on a toggle button."); }
/** * Test the void void static method by accidently giving parameters. */ @Test public void testStaticVoidVoidInvalidParams() { try { ReflectionUtil.invokeStaticMethod( DummyObject.class, "staticVoidNoParams", new Object[]{new Object()}, new Class[]{Object.class}); Assert.fail("Should have thrown a SystemException"); } catch (SystemException e) { Assert.assertEquals("Incorrect exception cause", NoSuchMethodException.class, e.getCause().getClass()); } }
@Test public void testExecuteNullCondition() { Rule rule = new Rule(); try { rule.execute(); Assert.fail("Should not be able to execute a rule with no condition set."); } catch (SystemException e) { Assert.assertNotNull("Invalid exception message for executing rule with no condition", e.getMessage()); } }
/** * Test when the static method invoked throws an exception. */ @Test public void testStaticInvokeExceptionThrower() { try { ReflectionUtil.invokeStaticMethod(DummyObject.class, "staticExceptionThrower", null, null); Assert.fail("Should have thrown a SystemException"); } catch (SystemException e) { Assert.assertEquals("Incorrect exception cause", InvocationTargetException.class, e.getCause().getClass()); } }
@Test public void testGetTriggerValueInvalidTrigger() { SubordinateTrigger trigger = new MyInvalidTrigger(); AbstractCompare compare = new MyCompare(trigger, null); try { compare.execute(); Assert.fail("Should have thrown exception for invalid subordinate trigger."); } catch (SystemException e) { Assert.assertNotNull("Exception for invalid subordinate trigger should have a message", e.getMessage()); } }
/** * Do not allow defaultSubmitButton on a WToggleButton. * @param defaultSubmitButton not used */ @Override public void setDefaultSubmitButton(final WButton defaultSubmitButton) { throw new SystemException("Cannot set default submit button on a toggle button."); }
/** * Test when an invalid name is provided. */ @Test public void testStaticInvalidMethodName() { try { ReflectionUtil.invokeStaticMethod(DummyObject.class, "doesntExist", null, null); Assert.fail("Should have thrown a SystemException"); } catch (SystemException e) { Assert.assertEquals("Incorrect exception cause", NoSuchMethodException.class, e.getCause().getClass()); } }
/** * Test register - exception on register with key already in use. */ @Test public void testRegisterFail() { final String key = "test123"; WComponent component = new DefaultWComponent(); UIRegistryClassLoaderImpl reg = new UIRegistryClassLoaderImpl(); reg.register(key, component); try { reg.register(key, component); Assert.fail( "attempted registration with key already used should have thrown an exception"); } catch (SystemException e) { String expectedMessage = "Cannot re-register a component. Key = " + key; Assert.assertEquals("exceptions hould have contained message expected", expectedMessage, e.getMessage()); } }
/** * {@inheritDoc}. */ @Override public DesiredCapabilities getDefaultDriverCapabilities() { throw new SystemException("Capabilities not supported by ParameterizedWebDriverType."); }
/** * Test when the static method invoked is private. */ @Test public void testStaticInvokePrivate() { try { ReflectionUtil.invokeStaticMethod(DummyObject.class, "staticPrivateMethod", null, null); Assert.fail("Should have thrown a SystemException"); } catch (SystemException e) { Assert.assertEquals("Incorrect exception cause", NoSuchMethodException.class, e.getCause().getClass()); } }
@Test public void testGetProjectVersion() { String testVersion = "TEST VERSION"; Config.getInstance().setProperty(ConfigurationProperties.PROJECT_VERSION, testVersion); Assert.assertEquals("Wrong project version returned", testVersion, WebUtilities. getProjectVersion()); try { Config.getInstance().clearProperty(ConfigurationProperties.PROJECT_VERSION); WebUtilities.getProjectVersion(); Assert.fail("An exception should have been thrown for a null project version"); } catch (SystemException e) { Assert.assertNotNull("No error message included for null project version", e. getMessage()); } }
/** * @param severity the diagnostic severity. * @return a string representation of the diagnostic severity level. */ private static String getLevel(final int severity) { switch (severity) { case Diagnostic.ERROR: return "error"; case Diagnostic.WARNING: return "warn"; case Diagnostic.INFO: return "info"; case Diagnostic.SUCCESS: return "success"; default: throw new SystemException("Unexpected diagnostic severity"); } }
/** * Test the void void method by accidently giving parameters. */ @Test public void testVoidVoidInvalidParams() { DummyObject dummyObj = new DummyObject(); try { ReflectionUtil.invokeMethod(dummyObj, "voidNoParams", new Object[]{new Object()}, new Class[]{Object.class}); Assert.fail("Should have thrown a SystemException"); } catch (SystemException e) { Assert.assertEquals("Incorrect exception cause", NoSuchMethodException.class, e.getCause().getClass()); } }
/** * Test register - exception on register with key already in use. */ @Test public void testRegisterFail() { final String key = "test123"; WComponent component = new DefaultWComponent(); UIRegistryAmicableImpl reg = new UIRegistryAmicableImpl(); reg.register(key, component); try { reg.register(key, component); Assert.fail( "attempted registration with key already used should have thrown an exception"); } catch (SystemException e) { String expectedMessage = "Cannot re-register a component. Key = " + key; Assert.assertEquals("exceptions hould have contained message expected", expectedMessage, e.getMessage()); } }
/** * Create a SeleniumWMessagesWebElement from a generic WebElement. * * @param element a generic WebElement * @param driver the current WebDriver instance */ public SeleniumWMessagesWebElement(final WebElement element, final WebDriver driver) { super(element, driver); String className = element.getAttribute("class"); if (!className.contains(CLASS_NAME)) { throw new SystemException("Incorrect element selected for SeleniumWMessagesWebElement. Expected className to include `" + CLASS_NAME + "` found: " + className); } }
/** * Test when the method invoked is private. */ @Test public void testInvokePrivate() { DummyObject dummyObj = new DummyObject(); try { ReflectionUtil.invokeMethod(dummyObj, "privateMethod", null, null); Assert.fail("Should have thrown a SystemException"); } catch (SystemException e) { Assert.assertEquals("Incorrect exception cause", NoSuchMethodException.class, e.getCause().getClass()); } }
@Test public void testXssEscaping() throws IOException, SAXException, XpathException { WPartialDateField dateField = new WPartialDateField(); dateField.setData(getMaliciousContent()); try { assertSafeContent(dateField); Assert.fail("Invalid date should not have been parsed."); } catch (SystemException e) { Assert.assertNotNull("Exception has no message", e.getMessage()); } dateField.setData(null); dateField.setToolTip(getMaliciousAttribute("ui:datefield")); assertSafeContent(dateField); dateField.setAccessibleText(getMaliciousAttribute("ui:datefield")); assertSafeContent(dateField); } }
/** * Attempts to create a Renderer with the given name. * * @param rendererName the name of the Renderer * @return a LayoutManager of the given type, or null if the class was not found. */ private Renderer createRenderer(final String rendererName) { try { Class<?> managerClass = Class.forName(rendererName); Object manager = managerClass.newInstance(); if (!(manager instanceof Renderer)) { throw new SystemException(rendererName + " is not a Renderer"); } return (Renderer) manager; } catch (ClassNotFoundException e) { // Legal - there might not a renderer implementation for the component in this format return null; } catch (InstantiationException e) { throw new SystemException("Failed to instantiate " + rendererName, e); } catch (IllegalAccessException e) { throw new SystemException("Failed to access " + rendererName, e); } }
/** * Test when the method invoked throws an exception. */ @Test public void testInvokeExceptionThrower() { DummyObject dummyObj = new DummyObject(); try { ReflectionUtil.invokeMethod(dummyObj, "exceptionThrower", null, null); Assert.fail("Should have thrown a SystemException"); } catch (SystemException e) { Assert.assertEquals("Incorrect exception cause", InvocationTargetException.class, e.getCause().getClass()); } }
/** * Get the data for a particular row. * * @param row the row index * @return the data for the row */ public ExampleData getExampleData(final int row) { if (data == null) { return null; } if (row < 0 || row >= data.size()) { throw new SystemException("Item out of bounds"); } return data.get(row); }