public String getErrorMessage(Throwable e) { String errorMessage = e.getMessage(); if (e instanceof ScannerException && (errorMessage.startsWith(MAPPING_VALUES_NOT_ALLOWED_HERE_ERROR) || errorMessage.startsWith(SCANNING_A_SIMPLE_KEY_ERROR))) { errorMessage += KEY_VALUE_PAIR_MISSING_OR_INDENTATION_PROBLEM_MSG; } else if (e instanceof ConstructorException && errorMessage.startsWith(CANNOT_CREATE_PROPERTY_ERROR)) { if (errorMessage.contains(UNABLE_TO_FIND_PROPERTY_ERROR)) { //parse for undefined property name String truncatedErrorMessage = errorMessage.substring(errorMessage.indexOf(TRUNCATION_BEGINNING), errorMessage.indexOf(TRUNCATION_END)); String undefinedProperty = truncatedErrorMessage.substring(truncatedErrorMessage.indexOf("\'") + 1, truncatedErrorMessage.lastIndexOf("\'")); errorMessage += "Property \'" + undefinedProperty + "\' is not supported by CloudSlang. Check that \'" + undefinedProperty + "\' is indented properly."; } else if (errorMessage.contains(MAP_CONSTRUCTOR_NOT_FOUND_ERROR)) { errorMessage += KEY_VALUE_PAIR_MISSING_OR_INDENTATION_PROBLEM_MSG; } } return errorMessage; }
public void testStringCharArray() { Yaml yaml = new Yaml(); try { yaml.load(pkg + ".CharArr [ [ abcd ] ]"); fail("Expected exception."); } catch (Exception e) { assertEquals(ConstructorException.class, e.getClass()); assertEquals("Invalid node Character: 'abcd'; length: 4", e.getCause().getMessage()); } }
@Test public void testYamlThrowsConstructorExceptionShouldThrowIllegalArgumentException() { Yaml mockYaml = mock(Yaml.class); when(mockYaml.loadAs(anyString(), Matchers.<Class<Object>>anyObject())).thenThrow(ConstructorException.class); reader.setYaml(mockYaml); reader.read(User.class); }
@Test public void shouldThrowExceptionWhenCannotLoadSettings() throws IOException { shouldLoadWithError(FileNotFoundException.class); FileUtils.writeStringToFile(settingsFile, "something weird"); shouldLoadWithError(ConstructorException.class); settings.save(); settingsFile.setReadable(false); shouldLoadWithError(FileNotFoundException.class); }
@Test(expected = ConstructorException.class) public void testReadYamlFromResource_badResource() { YamlUtils.readChatAlyticsConfig("config/bad-config.yaml"); }
@Test(expected = ConstructorException.class) public void readBadVersion() throws ConfigurationException, UnsupportedEncodingException, IOException { try (Reader in = new InputStreamReader(getClass().getResourceAsStream("/srcdeps-bad-version.yaml"), "utf-8")) { new YamlConfigurationIo().read(in); } }