EditorConfigService { public EditorFileConfig getEditorConfig(final String path) { final List<OutPair> properties; try { properties = editorConfig.getProperties(path); } catch (final EditorConfigException e) { throw new RuntimeException(e); } final SortedSet<ConfigProperty> configProperties = new TreeSet<ConfigProperty>(CONFIG_PROPERTY_TYPE_COMPARATOR); for (final OutPair outPair : properties) { final ConfigProperty configProperty = createConfigProperty(outPair); if (configProperty != null) { configProperties.add(configProperty); } } return new EditorFileConfig(path, Collections.unmodifiableSet(configProperties)); } EditorConfigService(final org.editorconfig.core.EditorConfig editorConfig); EditorFileConfig getEditorConfig(final String path);  }
@Test public void getFileConfigShouldTreatUnknownInsertFinalNewlineValueAsFalse() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.asList( new OutPair("insert_final_newline", "unknown_val") )); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), contains(configProperty(INSERT_FINAL_NEWLINE, false))); }
@Test public void getFileConfigShouldTreatUnknownTrimTrailingWhitespaceValueAsFalse() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.asList( new OutPair("trim_trailing_whitespace", "unknown_val") )); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), contains(configProperty(TRIM_TRAILING_WHITESPACE, false))); }
@Test public void getFileConfigShouldIgnoreInvalidIndentSizeValue() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.asList( new OutPair("indent_size", "yes"), new OutPair("end_of_line", "crlf") )); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), contains(configProperty(END_OF_LINE, CRLF))); }
@Test public void getFileConfigShouldIgnoreZeroIndentSizeValue() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.asList( new OutPair("indent_size", "0"), new OutPair("end_of_line", "crlf") )); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), contains(configProperty(END_OF_LINE, CRLF))); }
@Test public void getFileConfigShouldIgnoreNegativeIndentSizeValue() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.asList( new OutPair("indent_size", "-1"), new OutPair("end_of_line", "crlf") )); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), contains(configProperty(END_OF_LINE, CRLF))); }
@Test public void getFileConfigShouldIgnoreInvalidTabWidthValue() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.asList( new OutPair("tab_width", "yes"), new OutPair("end_of_line", "crlf") )); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), contains(configProperty(END_OF_LINE, CRLF))); }
@Test public void getFileConfigShouldIgnoreZeroTabWidthValue() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.asList( new OutPair("tab_width", "0"), new OutPair("end_of_line", "crlf") )); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), contains(configProperty(END_OF_LINE, CRLF))); }
@Test public void getFileConfigShouldIgnoreNegativeTabWidthValue() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.asList( new OutPair("tab_width", "-1"), new OutPair("end_of_line", "crlf") )); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), contains(configProperty(END_OF_LINE, CRLF))); }
@Test public void getFileConfigShouldReturnFileConfigWithValuesMatchingEditorConfigProperties() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.asList( new OutPair("tab_width", "8"), new OutPair("charset", "utf8") )); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), contains(configProperty(TAB_WIDTH, 8), configProperty(CHARSET, "utf8"))); }
@Test public void getFileConfigShouldReturnFileConfigWithGivenPath() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.<OutPair> asList()); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getPath(), is("path")); }
@Test public void getfileConfigShouldReturnFileConfigWithValuesSortedByType() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.asList( new OutPair("charset", "utf-8"), new OutPair("end_of_line", "crlf"), new OutPair("indent_size", "4"), new OutPair("indent_style", "tab"), new OutPair("insert_final_newline", "true"), new OutPair("tab_width", "4"), new OutPair("trim_trailing_whitespace", "true") )); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), contains( configPropertyWithType(INDENT_STYLE), configPropertyWithType(INDENT_SIZE), configPropertyWithType(TAB_WIDTH), configPropertyWithType(END_OF_LINE), configPropertyWithType(CHARSET), configPropertyWithType(TRIM_TRAILING_WHITESPACE), configPropertyWithType(INSERT_FINAL_NEWLINE) )); }
@Test public void getFileConfigShouldReturnFileConfigWithEmptyValuesWhenEditorConfigHasNoProperties() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Collections.<OutPair> emptyList()); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), is(empty())); }
@Test(expected = RuntimeException.class) public void getFileConfigShouldThrowRuntimeExceptionWhenEditorConfigThrowsException() throws Exception { when(mockEditorConfig.getProperties("path")).thenThrow(EditorConfigException.class); editorConfigService.getEditorConfig("path"); }
@Test public void getFileConfigShouldIgnoreUnknownEditorConfigPropertyKeys() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.asList( new OutPair("tab_width", "8"), new OutPair("unkown_config", "val") )); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), contains(configProperty(TAB_WIDTH, 8))); }
@Test public void getFileConfigShouldIgnoreUnknownIndentStyleValue() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.asList( new OutPair("tab_width", "8"), new OutPair("indent_style", "unknown_val") )); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), contains(configProperty(TAB_WIDTH, 8))); }
@Test public void getFileConfigShouldIgnoreUnknownEndOfLineValue() throws Exception { when(mockEditorConfig.getProperties("path")).thenReturn(Arrays.asList( new OutPair("tab_width", "8"), new OutPair("end_of_line", "unknown_val") )); final EditorFileConfig fileConfig = editorConfigService.getEditorConfig("path"); assertThat(fileConfig.getConfigProperties(), contains(configProperty(TAB_WIDTH, 8))); }