private UpdateCenter init() { InputStream input = null; try { String content = uriReader.readString(uri, StandardCharsets.UTF_8); java.util.Properties properties = new java.util.Properties(); input = IOUtils.toInputStream(content, StandardCharsets.UTF_8); properties.load(input); return new UpdateCenterDeserializer(Mode.PROD, true).fromProperties(properties); } catch (Exception e) { Loggers.get(getClass()).error("Fail to connect to update center", e); return null; } finally { IOUtils.closeQuietly(input); } } }
@Test public void cache_data() throws Exception { when(reader.readString(new URI(BASE_URL), StandardCharsets.UTF_8)).thenReturn("sonar.versions=2.2,2.3"); underTest.getUpdateCenter(); underTest.getUpdateCenter(); verify(reader, times(1)).readString(new URI(BASE_URL), StandardCharsets.UTF_8); }
@Test public void forceRefresh() throws Exception { when(reader.readString(new URI(BASE_URL), StandardCharsets.UTF_8)).thenReturn("sonar.versions=2.2,2.3"); underTest.getUpdateCenter(); underTest.getUpdateCenter(true); verify(reader, times(2)).readString(new URI(BASE_URL), StandardCharsets.UTF_8); }
@Test public void file_readString() { UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]); assertThat(uriReader.readString(testFile, StandardCharsets.UTF_8)).isEqualTo("in foo"); }
@Test public void downloadUpdateCenter() throws URISyntaxException { when(reader.readString(new URI(BASE_URL), StandardCharsets.UTF_8)).thenReturn("publicVersions=2.2,2.3"); UpdateCenter plugins = underTest.getUpdateCenter().get(); verify(reader, times(1)).readString(new URI(BASE_URL), StandardCharsets.UTF_8); assertThat(plugins.getSonar().getVersions()).containsOnly(Version.create("2.2"), Version.create("2.3")); assertThat(underTest.getLastRefreshDate()).isNotNull(); }
@Test public void ignore_connection_errors() { when(reader.readString(any(URI.class), eq(StandardCharsets.UTF_8))).thenThrow(new SonarException()); assertThat(underTest.getUpdateCenter()).isAbsent(); }
@Test public void file_readString_fails_if_file_not_found() throws Exception { thrown.expect(RuntimeException.class); UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]); uriReader.readString(new URI("file:/notfound"), StandardCharsets.UTF_8); }
private UpdateCenter init() { InputStream input = null; try { String content = uriReader.readString(uri, StandardCharsets.UTF_8); java.util.Properties properties = new java.util.Properties(); input = IOUtils.toInputStream(content, StandardCharsets.UTF_8); properties.load(input); return new UpdateCenterDeserializer(Mode.PROD, true).fromProperties(properties); } catch (Exception e) { Loggers.get(getClass()).error("Fail to connect to update center", e); return null; } finally { IOUtils.closeQuietly(input); } } }