Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use diamond operator, remove superflous throws and fixed some deprecation warnings #151

Merged
merged 1 commit into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public <I extends TopLevelItem> Collection<I> orphanedItems(ComputedFolder<I> ow
if (pruneDeadBranches) {
listener.getLogger().printf("Evaluating orphaned items in %s%n", owner.getFullDisplayName());
List<I> candidates = new ArrayList<I>(orphaned);
Collections.sort(candidates, new Comparator<I>() {
candidates.sort(new Comparator<I>() {
@Override
public int compare(I i1, I i2) {
boolean disabled1 = disabled(i1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -334,11 +335,11 @@ public int getRound() {
}

public List<String> getCreated() {
return created == null ? new ArrayList<String>() : created;
return created == null ? new ArrayList<>() : created;
}

public List<String> getDeleted() {
return deleted == null ? new ArrayList<String>() : deleted;
return deleted == null ? new ArrayList<>() : deleted;
}

public Set<String> getFatalKids() {
Expand All @@ -347,7 +348,7 @@ public Set<String> getFatalKids() {

public void setFatalKids(Set<String> fatalKids) {
if (!this.fatalKids.equals(fatalKids)) {
this.fatalKids = new TreeSet<String>(fatalKids);
this.fatalKids = new TreeSet<>(fatalKids);
try {
save();
} catch (IOException e) {
Expand All @@ -357,7 +358,7 @@ public void setFatalKids(Set<String> fatalKids) {
}

public void setFatalKids(String... fatalKids) {
setFatalKids(new TreeSet<String>(Arrays.asList(fatalKids)));
setFatalKids(new TreeSet<>(Arrays.asList(fatalKids)));
}

public List<String> getKids() {
Expand All @@ -366,7 +367,7 @@ public List<String> getKids() {

public void setKids(List<String> kids) {
if (!this.kids.equals(kids)) {
this.kids = new ArrayList<String>(kids);
this.kids = new ArrayList<>(kids);
try {
save();
} catch (IOException e) {
Expand Down Expand Up @@ -401,7 +402,7 @@ public void removeKid(String kid) {
}

public void addKids(String... kids) {
List<String> k = new ArrayList<String>(Arrays.asList(kids));
List<String> k = new ArrayList<>(Arrays.asList(kids));
k.removeAll(this.kids);
if (this.kids.addAll(k)) {
try {
Expand All @@ -426,8 +427,8 @@ public void removeKids(String... kid) {
protected void computeChildren(ChildObserver<FreeStyleProject> observer, TaskListener listener) throws
IOException, InterruptedException {
round++;
created = new ArrayList<String>();
deleted = new ArrayList<String>();
created = new ArrayList<>();
deleted = new ArrayList<>();
listener.getLogger().println("=== Round #" + round + " ===");
for (String kid : kids) {
if (fatalKids.contains(kid)) {
Expand All @@ -440,11 +441,8 @@ protected void computeChildren(ChildObserver<FreeStyleProject> 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 {
Expand Down Expand Up @@ -493,32 +491,32 @@ public String recompute(Result result) throws Exception {

public void assertItemNames(int round, String... names) {
assertEquals(round, this.round);
TreeSet<String> actual = new TreeSet<String>();
TreeSet<String> actual = new TreeSet<>();
for (FreeStyleProject p : getItems()) {
actual.add(p.getName());
}
assertThat(asJavaStrings(actual), anyOf(
is(asJavaStrings(new TreeSet<String>(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<String> actual = new TreeSet<String>();
TreeSet<String> actual = new TreeSet<>();
for (FreeStyleProject p : getItems()) {
actual.add(p.getShortUrl());
}
assertThat(actual, is(new TreeSet<String>(Arrays.asList(names))));
assertThat(actual, is(new TreeSet<>(Arrays.asList(names))));
}

public void assertItemDirs(int round, String... names) {
assertEquals(round, this.round);
TreeSet<String> actual = new TreeSet<String>();
TreeSet<String> actual = new TreeSet<>();
for (FreeStyleProject p : getItems()) {
actual.add(p.getRootDir().getName());
}
assertThat(actual, is(new TreeSet<String>(Arrays.asList(names))));
assertThat(actual, is(new TreeSet<>(Arrays.asList(names))));
}

@TestExtension
Expand All @@ -544,7 +542,7 @@ static TreeSet<String> windowsFFS(String... names) {
TreeSet<String> 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);
}
Expand All @@ -556,7 +554,7 @@ static TreeSet<String> windowsFFS(Normalizer.Form form, String... names) {
TreeSet<String> 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);
}
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -248,9 +247,9 @@ public static String mangle(String s) {
@SuppressWarnings({"unchecked", "rawtypes"})
public static class ComputedFolderImpl extends ComputedFolder<FreeStyleProject> {

private Set<String> fatalKids = new TreeSet<String>();
private Set<String> fatalKids = new TreeSet<>();

private List<String> kids = new ArrayList<String>();
private List<String> kids = new ArrayList<>();
/**
* The number of computations since either Jenkins was restarted or the folder was created.
*/
Expand All @@ -273,11 +272,11 @@ public int getRound() {
}

public List<String> getCreated() {
return created == null ? new ArrayList<String>() : created;
return created == null ? new ArrayList<>() : created;
}

public List<String> getDeleted() {
return deleted == null ? new ArrayList<String>() : deleted;
return deleted == null ? new ArrayList<>() : deleted;
}

public Set<String> getFatalKids() {
Expand All @@ -286,7 +285,7 @@ public Set<String> getFatalKids() {

public void setFatalKids(Set<String> fatalKids) {
if (!this.fatalKids.equals(fatalKids)) {
this.fatalKids = new TreeSet<String>(fatalKids);
this.fatalKids = new TreeSet<>(fatalKids);
try {
save();
} catch (IOException e) {
Expand All @@ -305,7 +304,7 @@ public List<String> getKids() {

public void setKids(List<String> kids) {
if (!this.kids.equals(kids)) {
this.kids = new ArrayList<String>(kids);
this.kids = new ArrayList<>(kids);
try {
save();
} catch (IOException e) {
Expand Down Expand Up @@ -340,7 +339,7 @@ public void removeKid(String kid) {
}

public void addKids(String... kids) {
List<String> k = new ArrayList<String>(Arrays.asList(kids));
List<String> k = new ArrayList<>(Arrays.asList(kids));
k.removeAll(this.kids);
if (this.kids.addAll(k)) {
try {
Expand All @@ -365,8 +364,8 @@ public void removeKids(String... kid) {
protected void computeChildren(ChildObserver<FreeStyleProject> observer, TaskListener listener) throws
IOException, InterruptedException {
round++;
created = new ArrayList<String>();
deleted = new ArrayList<String>();
created = new ArrayList<>();
deleted = new ArrayList<>();
listener.getLogger().println("=== Round #" + round + " ===");
for (String kid : kids) {
if (fatalKids.contains(kid)) {
Expand Down Expand Up @@ -432,29 +431,29 @@ public String recompute(Result result) throws Exception {

public void assertItemNames(int round, String... names) {
assertEquals(round, this.round);
TreeSet<String> actual = new TreeSet<String>();
TreeSet<String> actual = new TreeSet<>();
for (FreeStyleProject p : getItems()) {
actual.add(p.getName());
}
assertThat(actual, anyOf(is(new TreeSet<String>(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<String> actual = new TreeSet<String>();
TreeSet<String> actual = new TreeSet<>();
for (FreeStyleProject p : getItems()) {
actual.add(p.getShortUrl());
}
assertThat(actual, is(new TreeSet<String>(Arrays.asList(names))));
assertThat(actual, is(new TreeSet<>(Arrays.asList(names))));
}

public void assertItemDirs(int round, String... names) {
assertEquals(round, this.round);
TreeSet<String> actual = new TreeSet<String>();
TreeSet<String> actual = new TreeSet<>();
for (FreeStyleProject p : getItems()) {
actual.add(p.getRootDir().getName());
}
assertThat(actual, is(new TreeSet<String>(Arrays.asList(names))));
assertThat(actual, is(new TreeSet<>(Arrays.asList(names))));
}

@TestExtension
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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];
Expand All @@ -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);
}


Expand Down
Loading