import org.sonar.api.i18n.I18n;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Scopes;
+import org.sonar.api.utils.internal.Uuids;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.component.ComponentKeys;
import org.sonar.core.resource.ResourceDao;
}
checkKeyFormat(qualifier, kee);
- String uuid = UUID.randomUUID().toString();
+ String uuid = Uuids.create();
resourceDao.insertOrUpdate(
new ResourceDto()
.setUuid(uuid)
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.AbstractListHandler;
import org.sonar.api.rule.Severity;
+import org.sonar.api.utils.internal.Uuids;
import org.sonar.core.persistence.Database;
import org.sonar.server.db.migrations.SqlUtil;
if (componentId == null) {
continue;
}
- String issueKey = UUID.randomUUID().toString();
+ String issueKey = Uuids.create();
String status, severity, reporter = null;
boolean manualSeverity;
Object createdAt = Objects.firstNonNull(row.get(CREATED_AT), ONE_YEAR_AGO);
String login = referentials.userLogin((Long) comment.get(USER_ID));
if (login != null) {
Object[] params = new Object[6];
- params[0] = UUID.randomUUID().toString();
+ params[0] = Uuids.create();
params[1] = comment.get(ISSUE_KEY);
params[2] = login;
params[3] = comment.get(REVIEW_TEXT);
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.sonar.api.resources.Scopes;
+import org.sonar.api.utils.internal.Uuids;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.migration.v50.Component;
import org.sonar.core.persistence.migration.v50.Migration50Mapper;
import java.util.List;
import java.util.Map;
import java.util.Timer;
-import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import static com.google.common.collect.Lists.newArrayList;
private void migrateComponentsWithoutUuid(DbSession readSession, DbSession writeSession) {
for (Component component : readSession.getMapper(Migration50Mapper.class).selectComponentsWithoutUuid()) {
- String uuid = UUID.randomUUID().toString();
+ String uuid = Uuids.create();
component.setUuid(uuid);
component.setProjectUuid(uuid);
String existingUuid = component.getUuid();
String uuid = existingUuid == null ? uuidByComponentId.get(component.getId()) : existingUuid;
if (uuid == null) {
- String newUuid = UUID.randomUUID().toString();
+ String newUuid = Uuids.create();
uuidByComponentId.put(component.getId(), newUuid);
return newUuid;
}
add_column 'action_plans', 'kee', :string, :null => true, :limit => 100
ActionPlan.reset_column_information
ActionPlan.all.each do |a|
- a.update_attributes!(:kee => Java::JavaUtil::UUID.randomUUID().toString())
+ a.update_attributes!(:kee => Java::OrgSonarApiUtilsInternal::Uuids.create())
end
end
import org.sonar.api.resources.Scopes;
import org.sonar.api.security.ResourcePermissions;
import org.sonar.api.utils.SonarException;
+import org.sonar.api.utils.internal.Uuids;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.UUID;
public final class DefaultResourcePersister implements ResourcePersister {
model.setEnabled(Boolean.TRUE);
model.setDescription(resource.getDescription());
model.setKey(resource.getEffectiveKey());
- model.setUuid(UUID.randomUUID().toString());
+ model.setUuid(Uuids.create());
model.setPath(resource.getPath());
Language language = resource.getLanguage();
if (language != null) {
<!-- tests -->
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.mockito</groupId>
- <artifactId>mockito-core</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.hamcrest</groupId>
- <artifactId>hamcrest-all</artifactId>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar-testing-harness</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<artifactId>dbunit</artifactId>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>org.easytesting</groupId>
- <artifactId>fest-assert</artifactId>
- <scope>test</scope>
- </dependency>
<dependency>
<groupId>com.google.code.bean-matchers</groupId>
<artifactId>bean-matchers</artifactId>
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.utils.KeyValueFormat;
+import org.sonar.api.utils.internal.Uuids;
import org.sonar.core.activity.Activity;
import org.sonar.core.activity.ActivityLog;
import org.sonar.core.persistence.Dto;
-import java.util.UUID;
-
/**
* @since 4.4
*/
private String data;
protected ActivityDto() {
- this.key = UUID.randomUUID().toString();
+ this.key = Uuids.create();
}
@Override
package org.sonar.core.issue;
import org.sonar.api.issue.ActionPlan;
+import org.sonar.api.utils.internal.Uuids;
import java.util.Date;
-import java.util.UUID;
public class ActionPlanStats extends DefaultActionPlan {
public static ActionPlanStats create(String name) {
ActionPlanStats actionPlan = new ActionPlanStats();
- actionPlan.setKey(UUID.randomUUID().toString());
+ actionPlan.setKey(Uuids.create());
Date now = new Date();
actionPlan.setName(name);
actionPlan.setStatus(ActionPlan.STATUS_OPEN);
package org.sonar.core.issue;
import org.sonar.api.issue.ActionPlan;
+import org.sonar.api.utils.internal.Uuids;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-
import java.util.Date;
-import java.util.UUID;
public class DefaultActionPlan implements ActionPlan {
public static DefaultActionPlan create(String name) {
DefaultActionPlan actionPlan = new DefaultActionPlan();
- actionPlan.setKey(UUID.randomUUID().toString());
+ actionPlan.setKey(Uuids.create());
Date now = new Date();
actionPlan.setName(name);
actionPlan.setStatus(ActionPlan.STATUS_OPEN);
package org.sonar.core.issue;
import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.sonar.api.issue.Issuable;
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.rule.RuleKey;
+import org.sonar.api.utils.internal.Uuids;
import javax.annotation.Nullable;
-
import java.util.Map;
-import java.util.UUID;
public class DefaultIssueBuilder implements Issuable.IssueBuilder {
Preconditions.checkNotNull(ruleKey, "Rule key must be set");
DefaultIssue issue = new DefaultIssue();
- String key = UUID.randomUUID().toString();
- Preconditions.checkState(!Strings.isNullOrEmpty(key), "Fail to generate issue key");
+ String key = Uuids.create();
issue.setKey(key);
issue.setComponentKey(componentKey);
issue.setProjectKey(projectKey);
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.Duration;
import org.sonar.api.utils.KeyValueFormat;
+import org.sonar.api.utils.internal.Uuids;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.persistence.Dto;
import org.sonar.core.rule.RuleDto;
return new IssueDto()
.setProjectId(Long.valueOf(project.getId()))
.setRuleId(rule.getId())
- .setKee(UUID.randomUUID().toString());
+ .setKee(Uuids.create());
}
}
import org.sonar.api.component.Component;
import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.System2;
+import org.sonar.api.utils.internal.Uuids;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.component.SnapshotDto;
import org.sonar.core.persistence.DaoComponent;
if (resource.getId() == null) {
// Fix for Views
if (resource.getUuid() == null && Scopes.PROJECT.equals(resource.getScope())) {
- String uuid = UUID.randomUUID().toString();
+ String uuid = Uuids.create();
resource.setUuid(uuid);
resource.setProjectUuid(uuid);
}
import org.sonar.api.batch.sensor.internal.DefaultStorable;
import org.sonar.api.batch.sensor.issue.Issue;
import org.sonar.api.rule.RuleKey;
+import org.sonar.api.utils.internal.Uuids;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import java.util.UUID;
-
public class DefaultIssue extends DefaultStorable implements Issue {
private static final String INPUT_DIR_SHOULD_BE_NON_NULL = "InputDir should be non null";
public DefaultIssue() {
super(null);
- this.key = UUID.randomUUID().toString();
+ this.key = Uuids.create();
}
public DefaultIssue(SensorStorage storage) {
super(storage);
- this.key = UUID.randomUUID().toString();
+ this.key = Uuids.create();
}
@Override
package org.sonar.api.issue.internal;
import org.sonar.api.issue.IssueComment;
+import org.sonar.api.utils.internal.Uuids;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-
import java.io.Serializable;
import java.util.Date;
-import java.util.UUID;
/**
* PLUGINS MUST NOT BE USED THIS CLASS, EXCEPT FOR UNIT TESTING.
public static DefaultIssueComment create(String issueKey, @Nullable String login, String markdownText) {
DefaultIssueComment comment = new DefaultIssueComment();
comment.setIssueKey(issueKey);
- comment.setKey(UUID.randomUUID().toString());
+ comment.setKey(Uuids.create());
Date now = new Date();
comment.setUserLogin(login);
comment.setMarkdownText(markdownText);
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.sonar.api.utils.internal.Uuids;
import java.util.HashSet;
import java.util.Set;
-import java.util.UUID;
import java.util.regex.Pattern;
/**
if (!objectsWithoutToString.add(component.getClass())) {
log.warn(String.format("Bad component key: %s. Please implement toString() method on class %s", key, component.getClass().getName()));
}
- key += UUID.randomUUID().toString();
+ key += Uuids.create();
}
return new StringBuilder().append(component.getClass().getCanonicalName()).append("-").append(key).toString();
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.utils.internal;
+
+import java.util.UUID;
+
+/**
+ * @since 5.0
+ */
+public class Uuids {
+
+ private Uuids() {
+ // only static fields
+ }
+
+ /**
+ * Create a universally unique identifier. Underlying algorithm can change over SQ versions.
+ */
+ public static String create() {
+ return UUID.randomUUID().toString();
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.utils.internal;
+
+import com.google.common.collect.Sets;
+import org.junit.Test;
+import org.sonar.test.TestUtils;
+
+import java.util.Set;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class UuidsTest {
+
+ @Test
+ public void create_unique() throws Exception {
+ Set<String> all = Sets.newHashSet();
+ for (int i = 0; i < 50; i++) {
+ String uuid = Uuids.create();
+ assertThat(uuid).isNotEmpty();
+ all.add(uuid);
+ }
+ assertThat(all).hasSize(50);
+ }
+
+ @Test
+ public void constructor_is_private() throws Exception {
+ TestUtils.hasOnlyPrivateConstructors(Uuids.class);
+ }
+}