diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/computed/DefaultOrphanedItemStrategy.java b/src/main/java/com/cloudbees/hudson/plugins/folder/computed/DefaultOrphanedItemStrategy.java index b8601790..efeb204b 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/computed/DefaultOrphanedItemStrategy.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/computed/DefaultOrphanedItemStrategy.java @@ -234,7 +234,7 @@ public Collection orphanedItems(ComputedFolder ow if (pruneDeadBranches) { listener.getLogger().printf("Evaluating orphaned items in %s%n", owner.getFullDisplayName()); List candidates = new ArrayList(orphaned); - Collections.sort(candidates, new Comparator() { + candidates.sort(new Comparator() { @Override public int compare(I i1, I i2) { boolean disabled1 = disabled(i1); diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorAltTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorAltTest.java index f1a553d5..99600dee 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorAltTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorAltTest.java @@ -30,7 +30,6 @@ import hudson.AbortException; import hudson.BulkChange; import hudson.Util; -import hudson.model.Descriptor; import hudson.model.FreeStyleProject; import hudson.model.Item; import hudson.model.ItemGroup; @@ -44,6 +43,7 @@ import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.text.Normalizer; import java.util.ArrayList; import java.util.Arrays; @@ -64,12 +64,13 @@ import org.kohsuke.stapler.StaplerRequest; import static com.cloudbees.hudson.plugins.folder.ChildNameGeneratorTest.asJavaStrings; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.anyOf; 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.assertThat; + /** * Tests {@link ChildNameGenerator} using a generator that leaves the {@link Item#getName()} unmodified but mangles @@ -86,7 +87,7 @@ public class ChildNameGeneratorAltTest { * Then: mangling gets applied */ @Test - public void createdFromScratch() throws Exception { + public void createdFromScratch() { r.addStep(new Statement() { @Override public void evaluate() throws Throwable { @@ -334,11 +335,11 @@ public int getRound() { } public List getCreated() { - return created == null ? new ArrayList() : created; + return created == null ? new ArrayList<>() : created; } public List getDeleted() { - return deleted == null ? new ArrayList() : deleted; + return deleted == null ? new ArrayList<>() : deleted; } public Set getFatalKids() { @@ -347,7 +348,7 @@ public Set getFatalKids() { public void setFatalKids(Set fatalKids) { if (!this.fatalKids.equals(fatalKids)) { - this.fatalKids = new TreeSet(fatalKids); + this.fatalKids = new TreeSet<>(fatalKids); try { save(); } catch (IOException e) { @@ -357,7 +358,7 @@ public void setFatalKids(Set fatalKids) { } public void setFatalKids(String... fatalKids) { - setFatalKids(new TreeSet(Arrays.asList(fatalKids))); + setFatalKids(new TreeSet<>(Arrays.asList(fatalKids))); } public List getKids() { @@ -366,7 +367,7 @@ public List getKids() { public void setKids(List kids) { if (!this.kids.equals(kids)) { - this.kids = new ArrayList(kids); + this.kids = new ArrayList<>(kids); try { save(); } catch (IOException e) { @@ -401,7 +402,7 @@ public void removeKid(String kid) { } public void addKids(String... kids) { - List k = new ArrayList(Arrays.asList(kids)); + List k = new ArrayList<>(Arrays.asList(kids)); k.removeAll(this.kids); if (this.kids.addAll(k)) { try { @@ -426,8 +427,8 @@ public void removeKids(String... kid) { protected void computeChildren(ChildObserver observer, TaskListener listener) throws IOException, InterruptedException { round++; - created = new ArrayList(); - deleted = new ArrayList(); + created = new ArrayList<>(); + deleted = new ArrayList<>(); listener.getLogger().println("=== Round #" + round + " ==="); for (String kid : kids) { if (fatalKids.contains(kid)) { @@ -440,11 +441,8 @@ protected void computeChildren(ChildObserver observer, TaskLis if (p == null) { if (observer.mayCreate(encodedKid)) { listener.getLogger().println("creating a child"); - ChildNameGenerator.Trace trace = ChildNameGenerator.beforeCreateItem(this, encodedKid, kid); - try { + try (ChildNameGenerator.Trace trace = ChildNameGenerator.beforeCreateItem(this, encodedKid, kid)) { p = new FreeStyleProject(this, encodedKid); - } finally { - trace.close(); } BulkChange bc = new BulkChange(p); try { @@ -493,32 +491,32 @@ public String recompute(Result result) throws Exception { public void assertItemNames(int round, String... names) { assertEquals(round, this.round); - TreeSet actual = new TreeSet(); + TreeSet actual = new TreeSet<>(); for (FreeStyleProject p : getItems()) { actual.add(p.getName()); } assertThat(asJavaStrings(actual), anyOf( - is(asJavaStrings(new TreeSet(Arrays.asList(names)))), + is(asJavaStrings(new TreeSet<>(Arrays.asList(names)))), is(asJavaStrings(windowsFFS(Normalizer.Form.NFD, names))) )); } public void assertItemShortUrls(int round, String... names) { assertEquals(round, this.round); - TreeSet actual = new TreeSet(); + TreeSet actual = new TreeSet<>(); for (FreeStyleProject p : getItems()) { actual.add(p.getShortUrl()); } - assertThat(actual, is(new TreeSet(Arrays.asList(names)))); + assertThat(actual, is(new TreeSet<>(Arrays.asList(names)))); } public void assertItemDirs(int round, String... names) { assertEquals(round, this.round); - TreeSet actual = new TreeSet(); + TreeSet actual = new TreeSet<>(); for (FreeStyleProject p : getItems()) { actual.add(p.getRootDir().getName()); } - assertThat(actual, is(new TreeSet(Arrays.asList(names)))); + assertThat(actual, is(new TreeSet<>(Arrays.asList(names)))); } @TestExtension @@ -544,7 +542,7 @@ static TreeSet windowsFFS(String... names) { TreeSet alternative = new TreeSet<>(); for (String name: names) { try { - alternative.add(new String(name.getBytes("UTF-8"), "Windows-1252")); + alternative.add(new String(name.getBytes(StandardCharsets.UTF_8), "Windows-1252")); } catch (UnsupportedEncodingException e) { throw new AssertionError("UTF-8 and Windows-1252 are mandated by the JLS", e); } @@ -556,7 +554,7 @@ static TreeSet windowsFFS(Normalizer.Form form, String... names) { TreeSet alternative = new TreeSet<>(); for (String name: names) { try { - alternative.add(Normalizer.normalize(new String(name.getBytes("UTF-8"), "Windows-1252"), form)); + alternative.add(Normalizer.normalize(new String(name.getBytes(StandardCharsets.UTF_8), "Windows-1252"), form)); } catch (UnsupportedEncodingException e) { throw new AssertionError("UTF-8 and Windows-1252 are mandated by the JLS", e); } @@ -576,7 +574,7 @@ public String getName() { } @Override - public JobProperty reconfigure(StaplerRequest req, JSONObject form) throws Descriptor.FormException { + public JobProperty reconfigure(StaplerRequest req, JSONObject form) { return this; } diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorRecTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorRecTest.java index 28eda9fb..9e16c1bc 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorRecTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorRecTest.java @@ -31,7 +31,6 @@ import hudson.AbortException; import hudson.BulkChange; import hudson.Util; -import hudson.model.Descriptor; import hudson.model.FreeStyleProject; import hudson.model.ItemGroup; import hudson.model.Job; @@ -43,7 +42,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.text.Normalizer; import java.util.ArrayList; import java.util.Arrays; @@ -64,12 +63,12 @@ import org.kohsuke.stapler.StaplerRequest; import static com.cloudbees.hudson.plugins.folder.ChildNameGeneratorAltTest.windowsFFS; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.anyOf; 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.assertThat; /** * Tests {@link ChildNameGenerator} using a generator where the previous implementation of the computed folder used a @@ -87,7 +86,7 @@ public class ChildNameGeneratorRecTest { * Then: mangling gets applied */ @Test - public void createdFromScratch() throws Exception { + public void createdFromScratch() { r.addStep(new Statement() { @Override public void evaluate() throws Throwable { @@ -134,7 +133,7 @@ public void evaluate() throws Throwable { */ @Test @LocalData // to enable running on e.g. windows, keep the resource path short, so the test name must be short too - public void upgrade() throws Exception { + public void upgrade() { r.addStep(new Statement() { @Override public void evaluate() throws Throwable { @@ -248,9 +247,9 @@ public static String mangle(String s) { @SuppressWarnings({"unchecked", "rawtypes"}) public static class ComputedFolderImpl extends ComputedFolder { - private Set fatalKids = new TreeSet(); + private Set fatalKids = new TreeSet<>(); - private List kids = new ArrayList(); + private List kids = new ArrayList<>(); /** * The number of computations since either Jenkins was restarted or the folder was created. */ @@ -273,11 +272,11 @@ public int getRound() { } public List getCreated() { - return created == null ? new ArrayList() : created; + return created == null ? new ArrayList<>() : created; } public List getDeleted() { - return deleted == null ? new ArrayList() : deleted; + return deleted == null ? new ArrayList<>() : deleted; } public Set getFatalKids() { @@ -286,7 +285,7 @@ public Set getFatalKids() { public void setFatalKids(Set fatalKids) { if (!this.fatalKids.equals(fatalKids)) { - this.fatalKids = new TreeSet(fatalKids); + this.fatalKids = new TreeSet<>(fatalKids); try { save(); } catch (IOException e) { @@ -305,7 +304,7 @@ public List getKids() { public void setKids(List kids) { if (!this.kids.equals(kids)) { - this.kids = new ArrayList(kids); + this.kids = new ArrayList<>(kids); try { save(); } catch (IOException e) { @@ -340,7 +339,7 @@ public void removeKid(String kid) { } public void addKids(String... kids) { - List k = new ArrayList(Arrays.asList(kids)); + List k = new ArrayList<>(Arrays.asList(kids)); k.removeAll(this.kids); if (this.kids.addAll(k)) { try { @@ -365,8 +364,8 @@ public void removeKids(String... kid) { protected void computeChildren(ChildObserver observer, TaskListener listener) throws IOException, InterruptedException { round++; - created = new ArrayList(); - deleted = new ArrayList(); + created = new ArrayList<>(); + deleted = new ArrayList<>(); listener.getLogger().println("=== Round #" + round + " ==="); for (String kid : kids) { if (fatalKids.contains(kid)) { @@ -432,29 +431,29 @@ public String recompute(Result result) throws Exception { public void assertItemNames(int round, String... names) { assertEquals(round, this.round); - TreeSet actual = new TreeSet(); + TreeSet actual = new TreeSet<>(); for (FreeStyleProject p : getItems()) { actual.add(p.getName()); } - assertThat(actual, anyOf(is(new TreeSet(Arrays.asList(names))), is(windowsFFS(names)))); + assertThat(actual, anyOf(is(new TreeSet<>(Arrays.asList(names))), is(windowsFFS(names)))); } public void assertItemShortUrls(int round, String... names) { assertEquals(round, this.round); - TreeSet actual = new TreeSet(); + TreeSet actual = new TreeSet<>(); for (FreeStyleProject p : getItems()) { actual.add(p.getShortUrl()); } - assertThat(actual, is(new TreeSet(Arrays.asList(names)))); + assertThat(actual, is(new TreeSet<>(Arrays.asList(names)))); } public void assertItemDirs(int round, String... names) { assertEquals(round, this.round); - TreeSet actual = new TreeSet(); + TreeSet actual = new TreeSet<>(); for (FreeStyleProject p : getItems()) { actual.add(p.getRootDir().getName()); } - assertThat(actual, is(new TreeSet(Arrays.asList(names)))); + assertThat(actual, is(new TreeSet<>(Arrays.asList(names)))); } @TestExtension @@ -488,7 +487,7 @@ public String getName() { } @Override - public JobProperty reconfigure(StaplerRequest req, JSONObject form) throws Descriptor.FormException { + public JobProperty reconfigure(StaplerRequest req, JSONObject form) { return this; } @@ -560,11 +559,7 @@ public void recordLegacyName(F parent, J item, String legacyDirName) throws IOEx @NonNull public static String rawDecode(@NonNull String s) { final byte[] bytes; // should be US-ASCII but we can be tolerant - try { - bytes = s.getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new IllegalStateException("JLS specification mandates UTF-8 as a supported encoding", e); - } + bytes = s.getBytes(StandardCharsets.UTF_8); final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); for (int i = 0; i < bytes.length; i++) { final int b = bytes[i]; @@ -580,11 +575,7 @@ public static String rawDecode(@NonNull String s) { } buffer.write(b); } - try { - return new String(buffer.toByteArray(), "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new IllegalStateException("JLS specification mandates UTF-8 as a supported encoding", e); - } + return new String(buffer.toByteArray(), StandardCharsets.UTF_8); } diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorTest.java index 204c402e..7b200c81 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorTest.java @@ -30,7 +30,6 @@ import hudson.AbortException; import hudson.BulkChange; import hudson.Util; -import hudson.model.Descriptor; import hudson.model.FreeStyleProject; import hudson.model.Item; import hudson.model.ItemGroup; @@ -43,6 +42,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.text.Normalizer; import java.util.ArrayList; import java.util.Arrays; @@ -64,12 +64,12 @@ import org.kohsuke.stapler.StaplerRequest; import static com.cloudbees.hudson.plugins.folder.ChildNameGeneratorAltTest.windowsFFS; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.anyOf; 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.assertThat; /** * Tests {@link ChildNameGenerator} using a generator that modifies both the {@link Item#getName()} and the directory. @@ -108,7 +108,7 @@ public class ChildNameGeneratorTest { * Then: mangling gets applied */ @Test - public void createdFromScratch() throws Exception { + public void createdFromScratch() { r.addStep(new Statement() { @Override public void evaluate() throws Throwable { @@ -155,7 +155,7 @@ public void evaluate() throws Throwable { */ @Test @LocalData // to enable running on e.g. windows, keep the resource path short, so the test name must be short too - public void upgrade() throws Exception { + public void upgrade() { // The test data was generated using NFC filename encodings... but when unzipping the name can be changed // to NFD by the filesystem, so we need to check the expected outcome based on the inferred canonical form // used by the filesystem. @@ -193,7 +193,7 @@ public void evaluate() throws Throwable { */ @Test @LocalData // to enable running on e.g. windows, keep the resource path short, so the test name must be short too - public void upgradeNFD() throws Exception { + public void upgradeNFD() { // The test data was generated using NFD filename encodings... but when unzipping the name can be changed // to NFC by the filesystem, so we need to check the expected outcome based on the inferred canonical form // used by the filesystem. @@ -280,7 +280,7 @@ private void checkComputedFolder(ComputedFolderImpl instance, int round, Normali "\u110b\u1161\u110b\u1175 7", "nin\u0303o ocho" )) { - checkChild(instance, new String(name.getBytes("UTF-8"), "Windows-1252")); + checkChild(instance, new String(name.getBytes(StandardCharsets.UTF_8), "Windows-1252")); } } else { @@ -427,9 +427,9 @@ public static String mangle(String s) { public static class ComputedFolderImpl extends ComputedFolder { // TODO refactor the ChildNameGeneratorTests to remove duplication of most of this class - private Set fatalKids = new TreeSet(); + private Set fatalKids = new TreeSet<>(); - private List kids = new ArrayList(); + private List kids = new ArrayList<>(); /** * The number of computations since either Jenkins was restarted or the folder was created. */ @@ -452,11 +452,11 @@ public int getRound() { } public List getCreated() { - return created == null ? new ArrayList() : created; + return created == null ? new ArrayList<>() : created; } public List getDeleted() { - return deleted == null ? new ArrayList() : deleted; + return deleted == null ? new ArrayList<>() : deleted; } public Set getFatalKids() { @@ -465,7 +465,7 @@ public Set getFatalKids() { public void setFatalKids(Set fatalKids) { if (!this.fatalKids.equals(fatalKids)) { - this.fatalKids = new TreeSet(fatalKids); + this.fatalKids = new TreeSet<>(fatalKids); try { save(); } catch (IOException e) { @@ -475,7 +475,7 @@ public void setFatalKids(Set fatalKids) { } public void setFatalKids(String... fatalKids) { - setFatalKids(new TreeSet(Arrays.asList(fatalKids))); + setFatalKids(new TreeSet<>(Arrays.asList(fatalKids))); } public List getKids() { @@ -484,7 +484,7 @@ public List getKids() { public void setKids(List kids) { if (!this.kids.equals(kids)) { - this.kids = new ArrayList(kids); + this.kids = new ArrayList<>(kids); try { save(); } catch (IOException e) { @@ -519,7 +519,7 @@ public void removeKid(String kid) { } public void addKids(String... kids) { - List k = new ArrayList(Arrays.asList(kids)); + List k = new ArrayList<>(Arrays.asList(kids)); k.removeAll(this.kids); if (this.kids.addAll(k)) { try { @@ -544,8 +544,8 @@ public void removeKids(String... kid) { protected void computeChildren(ChildObserver observer, TaskListener listener) throws IOException, InterruptedException { round++; - created = new ArrayList(); - deleted = new ArrayList(); + created = new ArrayList<>(); + deleted = new ArrayList<>(); listener.getLogger().println("=== Round #" + round + " ==="); for (String kid : kids) { if (fatalKids.contains(kid)) { @@ -558,11 +558,8 @@ protected void computeChildren(ChildObserver observer, TaskLis if (p == null) { if (observer.mayCreate(encodedKid)) { listener.getLogger().println("creating a child"); - ChildNameGenerator.Trace trace = ChildNameGenerator.beforeCreateItem(this, encodedKid, kid); - try { + try (ChildNameGenerator.Trace trace = ChildNameGenerator.beforeCreateItem(this, encodedKid, kid)) { p = new FreeStyleProject(this, encodedKid); - } finally { - trace.close(); } BulkChange bc = new BulkChange(p); try { @@ -611,32 +608,32 @@ public String recompute(Result result) throws Exception { public void assertItemNames(int round, String... names) { assertEquals(round, this.round); - TreeSet actual = new TreeSet(); + TreeSet actual = new TreeSet<>(); for (FreeStyleProject p : getItems()) { actual.add(p.getName()); } assertThat(asJavaStrings(actual), anyOf( - is(asJavaStrings(new TreeSet(Arrays.asList(names)))), + is(asJavaStrings(new TreeSet<>(Arrays.asList(names)))), is(asJavaStrings(windowsFFS(Normalizer.Form.NFD, names))) )); } public void assertItemShortUrls(int round, String... names) { assertEquals(round, this.round); - TreeSet actual = new TreeSet(); + TreeSet actual = new TreeSet<>(); for (FreeStyleProject p : getItems()) { actual.add(p.getShortUrl()); } - assertThat(actual, is(new TreeSet(Arrays.asList(names)))); + assertThat(actual, is(new TreeSet<>(Arrays.asList(names)))); } public void assertItemDirs(int round, String... names) { assertEquals(round, this.round); - TreeSet actual = new TreeSet(); + TreeSet actual = new TreeSet<>(); for (FreeStyleProject p : getItems()) { actual.add(p.getRootDir().getName()); } - assertThat(actual, is(new TreeSet(Arrays.asList(names)))); + assertThat(actual, is(new TreeSet<>(Arrays.asList(names)))); } @TestExtension @@ -670,7 +667,7 @@ public String getName() { } @Override - public JobProperty reconfigure(StaplerRequest req, JSONObject form) throws Descriptor.FormException { + public JobProperty reconfigure(StaplerRequest req, JSONObject form) { return this; } diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/ConfigurationAsCodeTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/ConfigurationAsCodeTest.java index 06654e17..497a1b73 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/ConfigurationAsCodeTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/ConfigurationAsCodeTest.java @@ -8,10 +8,10 @@ import java.util.List; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.instanceOf; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; public class ConfigurationAsCodeTest extends RoundTripAbstractTest { diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/FolderTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/FolderTest.java index 263af321..3c90b2d7 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/FolderTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/FolderTest.java @@ -69,8 +69,17 @@ import jenkins.model.RenameAction; import jenkins.util.Timer; import org.acegisecurity.AccessDeniedException; + +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import org.junit.Rule; import org.junit.Test; @@ -199,8 +208,8 @@ private void copyViaHttp(Folder f, JenkinsRule.WebClient wc, String fromName, St try { // Access metadata from another thread. whatRemainedWhenDeleted.put(item.getFullName(), Timer.get().submit(new Callable>() { - @Override public Set call() throws Exception { - Set remaining = new TreeSet(); + @Override public Set call() { + Set remaining = new TreeSet<>(); for (Item i : Jenkins.get().getAllItems()) { remaining.add(i.getFullName()); if (i instanceof Actionable) { @@ -374,8 +383,8 @@ private void copyViaHttp(Folder f, JenkinsRule.WebClient wc, String fromName, St r.jenkins.reload(); // Add another property - Map> grantedPermissions = new HashMap>(); - Set sids = new HashSet(); + Map> grantedPermissions = new HashMap<>(); + Set sids = new HashSet<>(); sids.add("admin"); grantedPermissions.put(Jenkins.ADMINISTER, sids); folder = r.jenkins.getItemByFullName("myFolder", Folder.class); diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder2Test.java b/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder2Test.java index 3bc3d17b..eea7eb78 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder2Test.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder2Test.java @@ -34,8 +34,11 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; + import org.junit.Rule; import org.junit.Test; import org.junit.runners.model.Statement; @@ -83,7 +86,7 @@ public EventableFolder(ItemGroup parent, String name) { } @Override - protected void computeChildren(ChildObserver observer, TaskListener listener) throws IOException, InterruptedException { + protected void computeChildren(ChildObserver observer, TaskListener listener) throws InterruptedException { for (String kid : kids) { FreeStyleProject p = observer.shouldUpdate(kid); try { diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolderTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolderTest.java index 5c9d2643..04b85eba 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolderTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolderTest.java @@ -75,13 +75,19 @@ import org.junit.Assert; import org.junit.Test; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import org.junit.Rule; import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.JenkinsRule; @@ -126,7 +132,7 @@ public void duplicateEntries() throws Exception { d.assertItemNames(5, "A", "B", "C", "D"); assertEquals("[A, B, C, D, B]", d.created.toString()); assertEquals("[B]", d.deleted.toString()); - Map descriptions = new TreeMap(); + Map descriptions = new TreeMap<>(); for (FreeStyleProject p : d.getItems()) { descriptions.put(p.getName(), p.getDescription()); } @@ -468,7 +474,7 @@ public void concurrentEvents() throws Exception { d.onKid("B"); future.get(); waitUntilNoActivityIgnoringThreadDeathUpTo(10000); - List deaths = new ArrayList(); + List deaths = new ArrayList<>(); for (Computer comp : r.jenkins.getComputers()) { for (Executor e : comp.getExecutors()) { if (e.getCauseOfDeath() != null) { @@ -567,8 +573,8 @@ public void waitUntilNoActivityIgnoringThreadDeathUpTo(int timeout) throws Excep return; if (System.currentTimeMillis() - startTime > timeout) { - List building = new ArrayList(); - List deaths = new ArrayList(); + List building = new ArrayList<>(); + List deaths = new ArrayList<>(); for (Computer c : r.jenkins.getComputers()) { for (Executor e : c.getExecutors()) { if (e.isBusy()) { @@ -626,10 +632,10 @@ public boolean isSomethingHappeningIgnoringThreadDeath() { @SuppressWarnings({"unchecked", "rawtypes"}) public static class SampleComputedFolder extends ComputedFolder { - List kids = new ArrayList(); + List kids = new ArrayList<>(); int round; - List created = new ArrayList(); - List deleted = new ArrayList(); + List created = new ArrayList<>(); + List deleted = new ArrayList<>(); private SampleComputedFolder(ItemGroup parent, String name) { super(parent, name); @@ -684,11 +690,11 @@ String recompute(Result result) throws Exception { void assertItemNames(int round, String... names) { assertEquals(round, this.round); - Set actual = new TreeSet(); + Set actual = new TreeSet<>(); for (FreeStyleProject p : getItems()) { actual.add(p.getName()); } - assertEquals(new TreeSet(Arrays.asList(names)).toString(), actual.toString()); + assertEquals(new TreeSet<>(Arrays.asList(names)).toString(), actual.toString()); } @TestExtension @@ -938,10 +944,10 @@ public TopLevelItem newInstance(ItemGroup parent, String name) { public static class CoordinatedComputedFolder extends ComputedFolder { - List kids = new ArrayList(); + List kids = new ArrayList<>(); int round; - List created = new ArrayList(); - List deleted = new ArrayList(); + List created = new ArrayList<>(); + List deleted = new ArrayList<>(); CountDownLatch compute = new CountDownLatch(2); private CoordinatedComputedFolder(ItemGroup parent, String name) { @@ -1038,11 +1044,11 @@ String recompute(Result result) throws Exception { void assertItemNames(int round, String... names) { assertEquals(round, this.round); - Set actual = new TreeSet(); + Set actual = new TreeSet<>(); for (FreeStyleProject p : getItems()) { actual.add(p.getName()); } - assertEquals(new TreeSet(Arrays.asList(names)).toString(), actual.toString()); + assertEquals(new TreeSet<>(Arrays.asList(names)).toString(), actual.toString()); } @TestExtension diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/computed/EventOutputStreamsTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/computed/EventOutputStreamsTest.java index 07a2a5b9..8a2f8044 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/computed/EventOutputStreamsTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/computed/EventOutputStreamsTest.java @@ -27,6 +27,7 @@ import java.io.File; import java.io.OutputStream; import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -38,8 +39,8 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; public class EventOutputStreamsTest { @@ -107,9 +108,9 @@ public void run() { t2.start(); t1.join(); t2.join(); - List as = new ArrayList(); - List bs = new ArrayList(); - for (String line : FileUtils.readLines(file)) { + List as = new ArrayList<>(); + List bs = new ArrayList<>(); + for (String line : FileUtils.readLines(file, StandardCharsets.UTF_8)) { assertThat("Line does not have both thread output: '" + StringEscapeUtils.escapeJava(line)+"'", line.matches("^\\d+[AB](\\d+[AB])+$"), is(false)); assertThat("Line does not contain a null character: '" + StringEscapeUtils.escapeJava(line) + "'", @@ -122,10 +123,10 @@ public void run() { fail("unexpected line: '" + StringEscapeUtils.escapeJava(line) +"'"); } } - List sorted = new ArrayList(as); + List sorted = new ArrayList<>(as); Collections.sort(sorted); assertThat(as, is(sorted)); - sorted = new ArrayList(bs); + sorted = new ArrayList<>(bs); Collections.sort(sorted); assertThat(bs, is(sorted)); } diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ThrottleComputationQueueTaskDispatcherTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ThrottleComputationQueueTaskDispatcherTest.java index 74ebab1d..0289875b 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ThrottleComputationQueueTaskDispatcherTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ThrottleComputationQueueTaskDispatcherTest.java @@ -22,11 +22,11 @@ import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.TestExtension; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; public class ThrottleComputationQueueTaskDispatcherTest { private static final Logger LOGGER = Logger.getLogger(ThrottleComputationQueueTaskDispatcherTest.class.getName()); diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/config/AbstractFolderConfigurationTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/config/AbstractFolderConfigurationTest.java index 81f673e5..df61639c 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/config/AbstractFolderConfigurationTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/config/AbstractFolderConfigurationTest.java @@ -16,9 +16,9 @@ import java.util.logging.Level; import java.util.stream.Collectors; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.emptyIterable; import static org.hamcrest.Matchers.hasSize; -import static org.junit.Assert.assertThat; public class AbstractFolderConfigurationTest { diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/properties/FolderCredentialsProviderTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/properties/FolderCredentialsProviderTest.java index b3a1b35d..acd7bed7 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/properties/FolderCredentialsProviderTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/properties/FolderCredentialsProviderTest.java @@ -62,10 +62,10 @@ import org.jvnet.hudson.test.MockQueueItemAuthenticator; import org.jvnet.hudson.test.TestBuilder; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertThat; public class FolderCredentialsProviderTest { @@ -161,7 +161,7 @@ public void given_folderCredential_when_builtAsUserWithUseItem_then_credentialFo strategy.grant(Computer.BUILD).everywhere().to("bob"); r.jenkins.setAuthorizationStrategy(strategy); - HashMap jobsToUsers = new HashMap(); + HashMap jobsToUsers = new HashMap<>(); jobsToUsers.put(prj.getFullName(), User.getOrCreateByIdOrFullName("bob").impersonate()); MockQueueItemAuthenticator authenticator = new MockQueueItemAuthenticator(jobsToUsers); @@ -188,7 +188,7 @@ public void given_folderCredential_when_builtAsUserWithoutUseItem_then_credentia strategy.grant(Computer.BUILD).everywhere().to("bob"); r.jenkins.setAuthorizationStrategy(strategy); - HashMap jobsToUsers = new HashMap(); + HashMap jobsToUsers = new HashMap<>(); jobsToUsers.put(prj.getFullName(), User.getOrCreateByIdOrFullName("bob").impersonate()); MockQueueItemAuthenticator authenticator = new MockQueueItemAuthenticator(jobsToUsers); @@ -219,7 +219,7 @@ public void given_folderAndSystemCredentials_when_builtAsUserWithUseItem_then_fo strategy.grant(Computer.BUILD).everywhere().to("bob"); r.jenkins.setAuthorizationStrategy(strategy); - HashMap jobsToUsers = new HashMap(); + HashMap jobsToUsers = new HashMap<>(); jobsToUsers.put(prj.getFullName(), User.getOrCreateByIdOrFullName("bob").impersonate()); MockQueueItemAuthenticator authenticator = new MockQueueItemAuthenticator(jobsToUsers); @@ -262,7 +262,7 @@ public void given_nestedFolderAndSystemCredentials_when_builtAsUserWithUseItem_t strategy.grant(Computer.BUILD).everywhere().to("bob"); r.jenkins.setAuthorizationStrategy(strategy); - HashMap jobsToUsers = new HashMap(); + HashMap jobsToUsers = new HashMap<>(); jobsToUsers.put(prj.getFullName(), User.getOrCreateByIdOrFullName("bob").impersonate()); MockQueueItemAuthenticator authenticator = new MockQueueItemAuthenticator(jobsToUsers); @@ -294,8 +294,7 @@ private static class HasCredentialBuilder extends TestBuilder { } @Override - public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) - throws InterruptedException, IOException { + public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) { IdCredentials credentials = CredentialsProvider.findCredentialById(id, IdCredentials.class, build); if (credentials == null) { listener.getLogger().printf("Could not find any credentials with id %s%n", id);