@Override public Profiler start() { this.startTime = System2.INSTANCE.now(); this.startMessage = null; return this; }
Command(String executable, System2 system) { Preconditions.checkArgument(!StringUtils.isBlank(executable), "Command executable can not be blank"); this.executable = executable; this.env = new HashMap<>(system.envVariables()); this.system = system; }
private Path findSonarHome(ScannerProperties props) { String home = props.property("sonar.userHome"); if (home != null) { return Paths.get(home).toAbsolutePath(); } home = system.envVariable("SONAR_USER_HOME"); if (home != null) { return Paths.get(home).toAbsolutePath(); } home = system.property("user.home"); return Paths.get(home, ".sonar").toAbsolutePath(); }
@Test public void testSetProperty() { String key = "System2Test.testSetProperty"; String value = randomUUID().toString(); System2.INSTANCE.setProperty(key, value); assertThat(System2.INSTANCE.property(key)).isEqualTo(value); }
List<String> toStrings(boolean forLogs) { List<String> command = new ArrayList<>(); if (newShell) { if (system.isOsWindows()) { command.add("cmd"); command.add("/C"); command.add("call"); } else { command.add("sh"); } } command.add(executable); command.addAll(forLogs ? argumentsForLogs : arguments); return unmodifiableList(command); }
@Test public void getDefaultTimezone() { assertThat(System2.INSTANCE.getDefaultTimeZone().getID()).isEqualTo(TimeZone.getDefault().getID()); } }
@Test public void testProperty() { String expected = System.getProperty("java.version"); assertThat(System2.INSTANCE.property("java.version")).isNotNull().isEqualTo(expected); }
@Test public void testEnvVariable() { // assume that there's at least one env variable if (System.getenv().isEmpty()) { fail("Test can't succeed because there are no env variables. How is it possible ?"); } String key = System.getenv().keySet().iterator().next(); String expected = System.getenv(key); assertThat(System2.INSTANCE.envVariable(key)).isNotNull().isEqualTo(expected); assertThat(System2.INSTANCE.envVariable("UNKNOWN_VAR")).isNull(); }
private static String getScript(String name) throws IOException { String filename; if (System2.INSTANCE.isOsWindows()) { filename = name + ".bat"; } else { filename = name + ".sh"; } return new File("src/test/scripts/" + filename).getCanonicalPath(); }
public SearchIdResult<String> search(ProjectMeasuresQuery query, SearchOptions searchOptions) { SearchRequestBuilder requestBuilder = client .prepareSearch(INDEX_TYPE_PROJECT_MEASURES) .setFetchSource(false) .setFrom(searchOptions.getOffset()) .setSize(searchOptions.getLimit()); BoolQueryBuilder esFilter = boolQuery(); Map<String, QueryBuilder> filters = createFilters(query); filters.values().forEach(esFilter::must); requestBuilder.setQuery(esFilter); addFacets(requestBuilder, searchOptions, filters, query); addSort(query, requestBuilder); return new SearchIdResult<>(requestBuilder.get(), id -> id, system2.getDefaultTimeZone()); }
/** * Checks if a proxy was passed with command line parameters or configured in the system. * If only an HTTP proxy was configured then it's properties are copied to the HTTPS proxy (like SonarQube configuration) * @return True iff a proxy was configured to be used in the plugin. */ public boolean isProxyConnectionEnabled() { return system2.property(HTTP_PROXY_HOSTNAME) != null || system2.property(HTTPS_PROXY_HOSTNAME) != null || system2.property(PROXY_SOCKS_HOSTNAME) != null; }
private void validateCreationDateBounds(@Nullable Date createdBefore, @Nullable Date createdAfter) { Preconditions.checkArgument(createdAfter == null || createdAfter.before(new Date(system.now())), "Start bound cannot be in the future"); Preconditions.checkArgument(createdAfter == null || createdBefore == null || createdAfter.before(createdBefore), "Start bound cannot be larger or equal to end bound"); }
private boolean isEnvVariable(String propKey) { return propKey.startsWith(ENV_PROP_PREFIX) && system.envVariables().containsKey(propKey.substring(ENV_PROP_PREFIX.length())); }
@Test public void createTempFolderDefault() throws Exception { System2 system = mock(System2.class); tempFolderProvider = new GlobalTempFolderProvider(system); File userHome = temp.newFolder(); when(system.envVariable("SONAR_USER_HOME")).thenReturn(null); when(system.property("user.home")).thenReturn(userHome.getAbsolutePath().toString()); // if nothing is defined, it will be in {user.home}/.sonar/.sonartmp File defaultSonarHome = new File(userHome.getAbsolutePath(), ".sonar"); File workingDir = new File(defaultSonarHome, CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE).getAbsoluteFile(); try { TempFolder tempFolder = tempFolderProvider.provide(new ScannerProperties(Collections.<String, String>emptyMap())); tempFolder.newDir(); tempFolder.newFile(); assertThat(getCreatedTempDir(workingDir)).exists(); assertThat(getCreatedTempDir(workingDir).list()).hasSize(2); } finally { FileUtils.deleteQuietly(workingDir); } }
@Test public void testIsOsWindows() { assertThat(System2.INSTANCE.isOsWindows()).isEqualTo(SystemUtils.IS_OS_WINDOWS); }
private void assertThatFacetHasSize(IssueQuery issueQuery, String facet, int expectedSize) { SearchResponse result = underTest.search(issueQuery, new SearchOptions().addFacets(singletonList(facet))); Facets facets = new Facets(result, system2.getDefaultTimeZone()); assertThat(facets.get(facet)).hasSize(expectedSize); } }
/** * Checks if a proxy was passed with command line parameters or configured in the system. * If only an HTTP proxy was configured then it's properties are copied to the HTTPS proxy (like SonarQube configuration) * * @return True iff a proxy was configured to be used in the plugin. */ public boolean isProxyConnectionEnabled() { if (configuration.getBoolean(GitLabPlugin.GITLAB_DISABLE_PROXY).orElse(false)) { return false; } return (system2.property(HTTP_PROXY_HOSTNAME) != null || system2.property(HTTPS_PROXY_HOSTNAME) != null || system2.property(PROXY_SOCKS_HOSTNAME) != null); }
/** * now - startedAt */ @CheckForNull private Long computeExecutionTimeMs(CeQueueDto dto) { Long startedAt = dto.getStartedAt(); if (startedAt == null) { return null; } return system2.now() - startedAt; }
private void writeEnvVariables(BufferedWriter fileWriter) throws IOException { fileWriter.append("Environment variables:\n"); Map<String, String> envVariables = system.envVariables(); new TreeSet<>(envVariables.keySet()) .forEach(envKey -> { try { String envValue = isSensitiveEnvVariable(envKey) ? "******" : envVariables.get(envKey); fileWriter.append(String.format(KEY_VALUE_FORMAT, envKey, envValue)).append('\n'); } catch (IOException e) { throw new IllegalStateException(e); } }); }
private Path findSonarHome(ScannerProperties props) { String home = props.property("sonar.userHome"); if (home != null) { return Paths.get(home).toAbsolutePath(); } home = system.envVariable("SONAR_USER_HOME"); if (home != null) { return Paths.get(home).toAbsolutePath(); } home = system.property("user.home"); return Paths.get(home, ".sonar").toAbsolutePath(); }