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); } } }
/** * Reads all characters from uri, using the given character set. * It throws an unchecked exception if an error occurs. */ public String readString(URI uri, Charset charset) { return searchForSupportedProcessor(uri).readString(uri, charset); }
@Test public void file_description() { UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]); // the prefix file:/ is removed assertThat(uriReader.description(testFile)).doesNotMatch("file:/.*"); assertThat(uriReader.description(testFile)).matches(".*foo\\.txt"); }
@Test public void file_processor_is_always_available() { UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]); assertThat(uriReader.searchForSupportedProcessor(testFile)).isNotNull(); }
@Test public void file_readString() { UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]); assertThat(uriReader.readString(testFile, StandardCharsets.UTF_8)).isEqualTo("in foo"); }
public UpdateCenterClient(UriReader uriReader, Configuration config) throws URISyntaxException { this.uriReader = uriReader; this.uri = new URI(config.get(URL_PROPERTY).get()); this.isActivated = config.getBoolean(ProcessProperties.Property.SONAR_UPDATECENTER_ACTIVATE.getKey()).get(); Loggers.get(getClass()).info("Update center: " + uriReader.description(uri)); }
@Test public void register_processors() throws Exception { UriReader.SchemeProcessor processor = mock(UriReader.SchemeProcessor.class); when(processor.getSupportedSchemes()).thenReturn(new String[] {"ftp"}); UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[] {processor}); assertThat(uriReader.searchForSupportedProcessor(new URI("ftp://sonarsource.org"))).isNotNull(); } }
@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); }
public UpdateCenterClient(UriReader uriReader, Configuration config) throws URISyntaxException { this.uriReader = uriReader; this.uri = new URI(config.get(URL_PROPERTY).get()); this.isActivated = config.getBoolean(ProcessProperties.Property.SONAR_UPDATECENTER_ACTIVATE.getKey()).get(); Loggers.get(getClass()).info("Update center: " + uriReader.description(uri)); }
/** * Returns a detailed description of the given uri. For example HTTP URIs are completed * with the configured HTTP proxy. */ public String description(URI uri) { SchemeProcessor reader = searchForSupportedProcessor(uri); return reader.description(uri); }
@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); }
/** * Reads all bytes from uri. It throws an unchecked exception if an error occurs. */ public byte[] readBytes(URI uri) { return searchForSupportedProcessor(uri).readBytes(uri); }
@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); }
/** * Reads all bytes from uri. It throws an unchecked exception if an error occurs. */ public byte[] readBytes(URI uri) { return searchForSupportedProcessor(uri).readBytes(uri); }
@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(); }
/** * Reads all characters from uri, using the given character set. * It throws an unchecked exception if an error occurs. */ public String readString(URI uri, Charset charset) { return searchForSupportedProcessor(uri).readString(uri, charset); }
@Test public void ignore_connection_errors() { when(reader.readString(any(URI.class), eq(StandardCharsets.UTF_8))).thenThrow(new SonarException()); assertThat(underTest.getUpdateCenter()).isAbsent(); }