Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<jenkins.version>${jenkins.baseline}.1</jenkins.version>
<no-test-jar>false</no-test-jar>
<hpi.compatibleSinceVersion>5.2</hpi.compatibleSinceVersion>
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
</properties>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
import java.util.TreeSet;
import edu.umd.cs.findbugs.annotations.NonNull;
import net.sf.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsSessionRule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.junit.jupiter.JenkinsSessionExtension;
import org.kohsuke.stapler.StaplerRequest2;

import static com.cloudbees.hudson.plugins.folder.ChildNameGeneratorTest.asJavaStrings;
Expand All @@ -65,27 +65,27 @@
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;


/**
* Tests {@link ChildNameGenerator} using a generator that leaves the {@link Item#getName()} unmodified but mangles
* the directory.
*/
public class ChildNameGeneratorAltTest {
class ChildNameGeneratorAltTest {

@Rule
public JenkinsSessionRule r = new JenkinsSessionRule();
@RegisterExtension
private final JenkinsSessionExtension extension = new JenkinsSessionExtension();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: extension as a variable name is not so helpful (yes r is not either and that would probably been better as jenkinsSession) but I find extension slightly worse due to the Jenkins Extension infra so it becomes a little unclear when used.


/**
* Given: a computed folder
* When: creating a new instance
* Then: mangling gets applied
*/
@Test
public void createdFromScratch() throws Throwable {
r.then(j -> {
void createdFromScratch() throws Throwable {
extension.then(j -> {
ComputedFolderImpl instance = j.createProject(ComputedFolderImpl.class, "instance");
instance.assertItemNames(0);
instance.recompute(Result.SUCCESS);
Expand All @@ -103,7 +103,7 @@ public void createdFromScratch() throws Throwable {
instance.recompute(Result.SUCCESS);
checkComputedFolder(instance, 2);
});
r.then(j -> {
extension.then(j -> {
TopLevelItem i = j.jenkins.getItem("instance");
assertThat("Item loaded from disk", i, instanceOf(ComputedFolderImpl.class));
ComputedFolderImpl instance = (ComputedFolderImpl) i;
Expand All @@ -120,12 +120,12 @@ public void createdFromScratch() throws Throwable {
// Check child items identity is preserved
assertThat("Items are the same", items, is(newItems));
for (int k = 0; k < items.size(); k++) {
assertSame("Individual items must be the same", items.get(k), newItems.get(k));
assertSame(items.get(k), newItems.get(k), "Individual items must be the same");
}
});
}

private void checkComputedFolder(ComputedFolderImpl instance, int round) throws IOException {
private void checkComputedFolder(ComputedFolderImpl instance, int round) {
boolean windows = false;
for (FreeStyleProject p : instance.getItems()) {
if ("leanbh cu\u0301ig".equals(p.getName())) {
Expand Down Expand Up @@ -230,20 +230,20 @@ private void checkComputedFolder(ComputedFolderImpl instance, int round) throws
}
}

private void checkChild(ComputedFolderImpl instance, String idealName) throws IOException {
private void checkChild(ComputedFolderImpl instance, String idealName) {
String encodedName = encode(idealName);
FreeStyleProject item = instance.getItem(encodedName);
assertThat("We have an item for name " + idealName, item, notNullValue());
assertThat("The root directory if the item for name " + idealName + " is mangled",
item.getRootDir().getName(), is(mangle(idealName)));
}

public static String encode(String s) {
private static String encode(String s) {
// We force every name to NFD to ensure that the test works irrespective of what the filesystem does
return Normalizer.normalize(s, Normalizer.Form.NFD);
}

public static String mangle(String s) {
private static String mangle(String s) {
// We force every name to NFD to ensure that the test works irrespective of what the filesystem does
s = Normalizer.normalize(s, Normalizer.Form.NFD);
String hash = Util.getDigestOf(s);
Expand Down Expand Up @@ -440,7 +440,7 @@ public String recompute(Result result) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
computation.writeWholeLogTo(baos);
String log = baos.toString();
assertEquals(log, result, computation.getResult());
assertEquals(result, computation.getResult(), log);
return log;
}

Expand Down Expand Up @@ -474,6 +474,7 @@ public void assertItemDirs(int round, String... names) {
assertThat(actual, is(new TreeSet<>(Arrays.asList(names))));
}

@SuppressWarnings("unused")
@TestExtension
public static class DescriptorImpl extends AbstractFolderDescriptor {

Expand All @@ -484,6 +485,7 @@ public TopLevelItem newInstance(ItemGroup parent, String name) {
return new ComputedFolderImpl(parent, name);
}

@NonNull
@Override
public <I extends TopLevelItem> ChildNameGenerator<AbstractFolder<I>, I> childNameGenerator() {
return (ChildNameGenerator<AbstractFolder<I>, I>) GENERATOR;
Expand Down Expand Up @@ -533,6 +535,7 @@ public JobProperty<?> reconfigure(StaplerRequest2 req, JSONObject form) {
return this;
}

@SuppressWarnings("unused")
@TestExtension
public static class DescriptorImpl extends JobPropertyDescriptor {
@Override
Expand Down Expand Up @@ -586,6 +589,4 @@ public String dirNameFromLegacy(@NonNull F parent,
return mangle(Normalizer.normalize(legacyDirName, Normalizer.Form.NFD));
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@
import java.util.Set;
import java.util.TreeSet;
import net.sf.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsSessionRule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.junit.jupiter.JenkinsSessionExtension;
import org.kohsuke.stapler.StaplerRequest2;

import static com.cloudbees.hudson.plugins.folder.ChildNameGeneratorAltTest.windowsFFS;
Expand All @@ -63,26 +63,26 @@
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Tests {@link ChildNameGenerator} using a generator where the previous implementation of the computed folder used a
* name encoding algorithm and the new one has switched to {@link ChildNameGenerator}. We want to ensure that the
* name gets decoded from the legacy name and fixed to the correct name while the directory name gets mangled.
*/
public class ChildNameGeneratorRecTest {
class ChildNameGeneratorRecTest {

@Rule
public JenkinsSessionRule r = new JenkinsSessionRule();
@RegisterExtension
private final JenkinsSessionExtension extension = new JenkinsSessionExtension();

/**
* Given: a computed folder
* When: creating a new instance
* Then: mangling gets applied
*/
@Test
public void createdFromScratch() throws Throwable {
r.then(j -> {
void createdFromScratch() throws Throwable {
extension.then(j -> {
ComputedFolderImpl instance = j.createProject(ComputedFolderImpl.class, "instance");
instance.assertItemNames(0);
instance.recompute(Result.SUCCESS);
Expand All @@ -100,7 +100,7 @@ public void createdFromScratch() throws Throwable {
instance.recompute(Result.SUCCESS);
checkComputedFolder(instance, 2);
});
r.then(j -> {
extension.then(j -> {
TopLevelItem i = j.jenkins.getItem("instance");
assertThat("Item loaded from disk", i, instanceOf(ComputedFolderImpl.class));
ComputedFolderImpl instance = (ComputedFolderImpl) i;
Expand All @@ -115,7 +115,7 @@ public void createdFromScratch() throws Throwable {
});
}

private void checkComputedFolder(ComputedFolderImpl instance, int round) throws IOException {
private void checkComputedFolder(ComputedFolderImpl instance, int round) {
// because these were previously encoded with rawEncode we can recover exactly
instance.assertItemNames(round,
"child-one",
Expand Down Expand Up @@ -161,19 +161,19 @@ private void checkComputedFolder(ComputedFolderImpl instance, int round) throws
}
}

private void checkChild(ComputedFolderImpl instance, String idealName) throws IOException {
private void checkChild(ComputedFolderImpl instance, String idealName) {
String encodedName = encode(idealName);
FreeStyleProject item = instance.getItem(encodedName);
assertThat("We have an item for name " + idealName, item, notNullValue());
assertThat("The root directory of the item for name " + idealName + " is mangled",
item.getRootDir().getName(), is(mangle(idealName)));
}

public static String encode(String s) {
private static String encode(String s) {
return s;
}

public static String mangle(String s) {
private static String mangle(String s) {
String hash = Util.getDigestOf(s);
String base = Normalizer.normalize(s, Normalizer.Form.NFD).toLowerCase(Locale.ENGLISH);
StringBuilder buf = new StringBuilder(32);
Expand Down Expand Up @@ -368,7 +368,7 @@ public String recompute(Result result) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
computation.writeWholeLogTo(baos);
String log = baos.toString();
assertEquals(log, result, computation.getResult());
assertEquals(result, computation.getResult(), log);
return log;
}

Expand Down Expand Up @@ -409,6 +409,7 @@ public TopLevelItem newInstance(ItemGroup parent, String name) {
return new ComputedFolderImpl(parent, name);
}

@NonNull
@Override
public <I extends TopLevelItem> ChildNameGenerator<AbstractFolder<I>, I> childNameGenerator() {
return (ChildNameGenerator<AbstractFolder<I>, I>) GENERATOR;
Expand All @@ -434,6 +435,7 @@ public JobProperty<?> reconfigure(StaplerRequest2 req, JSONObject form) {
return this;
}

@SuppressWarnings("unused")
@TestExtension
public static class DescriptorImpl extends JobPropertyDescriptor {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
import java.util.TreeSet;
import edu.umd.cs.findbugs.annotations.NonNull;
import net.sf.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsSessionRule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.junit.jupiter.JenkinsSessionExtension;
import org.kohsuke.stapler.StaplerRequest2;

import static com.cloudbees.hudson.plugins.folder.ChildNameGeneratorAltTest.windowsFFS;
Expand All @@ -65,7 +65,7 @@
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Tests {@link ChildNameGenerator} using a generator that modifies both the {@link Item#getName()} and the directory.
Expand All @@ -89,23 +89,23 @@
* <li><code>niño ocho</code> (Spanish, supposed to be "child eight" round-tripped through Google translate gives
* "eight boy") demonstrates a name that has a NFC chacacter with a different NFD encoding</li>
* </nl>
*
* <p>
* Aside: <a href="https://www.youtube.com/watch?v=LMkJuDVJdTw">Here's what happens when you round-trip through Google
* Translate</a> "Cold, apricot, relaxing satisfaction!"
*/
public class ChildNameGeneratorTest {
class ChildNameGeneratorTest {

@Rule
public JenkinsSessionRule r = new JenkinsSessionRule();
@RegisterExtension
private final JenkinsSessionExtension extension = new JenkinsSessionExtension();

/**
* Given: a computed folder
* When: creating a new instance
* Then: mangling gets applied
*/
@Test
public void createdFromScratch() throws Throwable {
r.then(j -> {
void createdFromScratch() throws Throwable {
extension.then(j -> {
ComputedFolderImpl instance = j.createProject(ComputedFolderImpl.class, "instance");
instance.assertItemNames(0);
instance.recompute(Result.SUCCESS);
Expand All @@ -123,7 +123,7 @@ public void createdFromScratch() throws Throwable {
instance.recompute(Result.SUCCESS);
checkComputedFolder(instance, 2, Normalizer.Form.NFC);
});
r.then(j -> {
extension.then(j -> {
TopLevelItem i = j.jenkins.getItem("instance");
assertThat("Item loaded from disk", i, instanceOf(ComputedFolderImpl.class));
ComputedFolderImpl instance = (ComputedFolderImpl) i;
Expand Down Expand Up @@ -261,7 +261,7 @@ private void checkComputedFolder(ComputedFolderImpl instance, int round, Normali
}
}

private void checkChild(ComputedFolderImpl instance, String idealName) throws IOException {
private void checkChild(ComputedFolderImpl instance, String idealName) {
String encodedName = encode(idealName);
FreeStyleProject item = instance.getItem(encodedName);
assertThat("We have an item for name " + idealName, item, notNullValue());
Expand All @@ -278,12 +278,12 @@ private void checkChild(ComputedFolderImpl instance, String idealName) throws IO
}
}

public static String encode(String s) {
private static String encode(String s) {
// we want to test that the name can be different from the on-disk name
return "$$"+Normalizer.normalize(s, Normalizer.Form.NFD);
}

public static String mangle(String s) {
private static String mangle(String s) {
String hash = Util.getDigestOf(s);
String base = Normalizer.normalize(s, Normalizer.Form.NFD).toLowerCase(Locale.ENGLISH);
StringBuilder buf = new StringBuilder(32);
Expand Down Expand Up @@ -479,7 +479,7 @@ public String recompute(Result result) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
computation.writeWholeLogTo(baos);
String log = baos.toString();
assertEquals(log, result, computation.getResult());
assertEquals(result, computation.getResult(), log);
return log;
}

Expand Down Expand Up @@ -513,6 +513,7 @@ public void assertItemDirs(int round, String... names) {
assertThat(actual, is(new TreeSet<>(Arrays.asList(names))));
}

@SuppressWarnings("unused")
@TestExtension
public static class DescriptorImpl extends AbstractFolderDescriptor {

Expand All @@ -523,13 +524,12 @@ public TopLevelItem newInstance(ItemGroup parent, String name) {
return new ComputedFolderImpl(parent, name);
}

@NonNull
@Override
public <I extends TopLevelItem> ChildNameGenerator<AbstractFolder<I>, I> childNameGenerator() {
return (ChildNameGenerator<AbstractFolder<I>, I>) GENERATOR;
}

}

}

public static class NameProperty extends JobProperty<FreeStyleProject> {
Expand All @@ -548,6 +548,7 @@ public JobProperty<?> reconfigure(StaplerRequest2 req, JSONObject form) {
return this;
}

@SuppressWarnings("unused")
@TestExtension
public static class DescriptorImpl extends JobPropertyDescriptor {
@Override
Expand Down
Loading