Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #5 from jenkinsci/JENKINS-28913
[FIXED JENKINS-28913] write the id to the correct file.
  • Loading branch information
jtnord committed Jun 15, 2015
2 parents 1a7b37a + 925a30c commit 98adff6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
Expand Up @@ -42,7 +42,7 @@ public void make(PersistenceRoot object) {
File tmp = null;
try {
tmp = File.createTempFile(".unique-id_", ".tmp", object.getRootDir());
FileUtils.writeStringToFile(f, IdStore.generateUniqueID(), StandardCharsets.UTF_8);
FileUtils.writeStringToFile(tmp, IdStore.generateUniqueID(), StandardCharsets.UTF_8);
try {
Files.move(tmp.toPath(), f.toPath(), StandardCopyOption.ATOMIC_MOVE);
}
Expand All @@ -62,7 +62,14 @@ public String get(PersistenceRoot object) {
File f = new File(object.getRootDir(), ID_FILE);
if (f.exists() && f.canRead()) {
try {
return FileUtils.readFileToString(f, StandardCharsets.UTF_8);
String str = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
if (str.length() == 0) {
// regenerate it JENKINS-28913
Files.deleteIfExists(f.toPath());
make(object);
str = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
}
return str;
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Failed to retrieve unique ID for " + object.toString(), ex);
}
Expand Down
57 changes: 54 additions & 3 deletions src/test/java/org/jenkinsci/plugins/uniqueid/IdTest.java
@@ -1,14 +1,27 @@
package org.jenkinsci.plugins.uniqueid;

import com.cloudbees.hudson.plugins.folder.Folder;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Project;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import org.hamcrest.core.Is;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.*;
import com.cloudbees.hudson.plugins.folder.Folder;

import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

public class IdTest {

Expand All @@ -22,6 +35,10 @@ public void project() throws Exception {
IdStore.makeId(p);
String id = IdStore.getId(p);
AbstractBuild build = jenkinsRule.buildAndAssertSuccess(p);

// to be unique we need not null and at least a minimum size.
assertThat("project id", id, notNullValue());
assertThat("project id", id.length(), greaterThan(20));

// a build will get an id computed from its parent.
String buildId = IdStore.getId(build);
Expand All @@ -44,8 +61,42 @@ public void folder() throws Exception {
assertNull(IdStore.getId(f));
IdStore.makeId(f);
String id = IdStore.getId(f);

// to be unique we need not null and at least a minimum size.
assertThat("folder id", id, notNullValue());
assertThat("folder id", id.length(), greaterThan(20));

jenkinsRule.jenkins.reload();
assertEquals(id, IdStore.getId(jenkinsRule.jenkins.getItemByFullName("folder", Folder.class)));

}

@Test
public void uniqueness() throws Exception {
Set<String> ids = new HashSet<String>();
for (int i=0;i<100;i++) {
Project p = jenkinsRule.createFreeStyleProject();
IdStore.makeId(p);
ids.add(IdStore.getId(p));

Folder f = jenkinsRule.jenkins.createProject(Folder.class, "Folder"+i);
IdStore.makeId(f);
ids.add(IdStore.getId(f));
}
assertThat(ids, hasSize(200));
}

@Test
@Issue("JENKINS-28913")
public void correctJenkins28913() throws Exception {
Project<?, ?> p = jenkinsRule.createFreeStyleProject();
File f = new File(p.getRootDir(), "unique-id.txt");
assertThat(f.exists(), is(false));

assertThat("no Id yet made", IdStore.getId(p), nullValue());

assertThat(f.createNewFile(), is(true));

String id = IdStore.getId(p);
assertThat("id file was empty should have been re-gernerated", id.length(), greaterThan(20));
}
}

0 comments on commit 98adff6

Please sign in to comment.