Skip to content

Commit

Permalink
Formatting and improved bridge method tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Apr 22, 2020
1 parent d536a9f commit 87f37e9
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 41 deletions.
22 changes: 11 additions & 11 deletions src/main/java/org/kohsuke/github/GHObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ public Date getUpdatedAt() throws IOException {
return GitHubClient.parseDate(updated_at);
}

/**
* Get Global node_id from Github object.
*
* @see <a href="https://developer.github.com/v4/guides/using-global-node-ids/">Using Global Node IDs</a>
*
* @return Global Node ID.
*/
public String getNodeId() {
return node_id;
}

/**
* Gets id.
*
Expand All @@ -124,17 +135,6 @@ public long getId() {
return id;
}

/**
* Get Global node_id from Github object.
*
* @see <a href="https://developer.github.com/v4/guides/using-global-node-ids/">Using Global Node IDs</a>
*
* @return Global Node ID.
*/
public String getNodeId() {
return node_id;
}

@SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", justification = "Bridge method of getId")
private Object longToStringOrInt(long id, Class type) {
if (type == String.class)
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/org/kohsuke/github/GHOrganization.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,11 @@ public PagedIterable<GHTeam> listTeams() throws IOException {
* @throws IOException
* the io exception
*
* @see <a href= "https://developer.github.com/v3/teams/#get-team-by-name">documentation</a>
* @deprecated Use {@link GHOrganization#getTeam(long)}
*/
public GHTeam getTeam(long teamId) throws IOException {
return root.createRequest()
.withUrlPath(String.format("/organizations/%d/team/%d", id, teamId))
.fetch(GHTeam.class)
.wrapUp(this);
@Deprecated
public GHTeam getTeam(int teamId) throws IOException {
return getTeam((long) teamId);
}

/**
Expand All @@ -155,10 +153,13 @@ public GHTeam getTeam(long teamId) throws IOException {
* @throws IOException
* the io exception
*
* @deprecated Use {@link GHOrganization#getTeam(long)}
* @see <a href= "https://developer.github.com/v3/teams/#get-team-by-name">documentation</a>
*/
public GHTeam getTeam(int teamId) throws IOException {
return getTeam((long) teamId);
public GHTeam getTeam(long teamId) throws IOException {
return root.createRequest()
.withUrlPath(String.format("/organizations/%d/team/%d", id, teamId))
.fetch(GHTeam.class)
.wrapUp(this);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public GHRateLimit rateLimit() throws IOException {
* @throws IOException
* the io exception
*/
@WithBridgeMethods(GHUser.class)
@WithBridgeMethods(value = GHUser.class)
public GHMyself getMyself() throws IOException {
client.requireCredential();
synchronized (this) {
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ public void testShouldFetchTeamFromOrganization() throws Exception {

assertEquals(teamByName.getId(), teamById.getId());
assertEquals(teamByName.getDescription(), teamById.getDescription());

GHTeam teamById2 = organization.getTeam((int) teamByName.getId());
assertNotNull(teamById2);

assertEquals(teamByName.getId(), teamById2.getId());
assertEquals(teamByName.getDescription(), teamById2.getDescription());

}

@Ignore("Needs mocking check")
Expand Down
67 changes: 47 additions & 20 deletions src/test/java/org/kohsuke/github/BridgeMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,66 @@

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.equalTo;
import javax.annotation.Nonnull;

import static org.hamcrest.Matchers.*;

/**
* @author Kohsuke Kawaguchi
*/
public class BridgeMethodTest extends Assert {

@Test
public void lastStatus() throws IOException {
GHObject obj = new GHIssue();

List<Method> createdAtMethods = new ArrayList<>();
for (Method method : obj.getClass().getMethods()) {
if (method.getName().equalsIgnoreCase("getCreatedAt")) {
if (method.getReturnType() == Date.class) {
createdAtMethods.add(0, method);
} else {
createdAtMethods.add(method);
}
}
}
public void testBridgeMethods() throws IOException {

// Some would say this is redundant, given that bridge methods are so thin anyway
// In the interest of maintaining binary compatibility, we'll do this anyway for a sampling of methods

// Something odd here
// verifyBridgeMethods(new GHCommit(), "getAuthor", GHCommit.GHAuthor.class, GitUser.class);
// verifyBridgeMethods(new GHCommit(), "getCommitter", GHCommit.GHAuthor.class, GitUser.class);

verifyBridgeMethods(GHIssue.class, "getCreatedAt", Date.class, String.class);
verifyBridgeMethods(GHIssue.class, "getId", int.class, long.class, String.class);
verifyBridgeMethods(GHIssue.class, "getUrl", String.class, URL.class);

verifyBridgeMethods(GHOrganization.class, "getHtmlUrl", String.class, URL.class);
verifyBridgeMethods(GHOrganization.class, "getId", int.class, long.class, String.class);
verifyBridgeMethods(GHOrganization.class, "getUrl", String.class, URL.class);

assertThat(createdAtMethods.size(), equalTo(2));
verifyBridgeMethods(GHRepository.class, "getCollaborators", GHPersonSet.class, Set.class);
verifyBridgeMethods(GHRepository.class, "getHtmlUrl", String.class, URL.class);
verifyBridgeMethods(GHRepository.class, "getId", int.class, long.class, String.class);
verifyBridgeMethods(GHRepository.class, "getUrl", String.class, URL.class);

assertThat(createdAtMethods.get(0).getParameterCount(), equalTo(0));
assertThat(createdAtMethods.get(1).getParameterCount(), equalTo(0));
verifyBridgeMethods(GHUser.class, "getFollows", GHPersonSet.class, Set.class);
verifyBridgeMethods(GHUser.class, "getFollowers", GHPersonSet.class, Set.class);
verifyBridgeMethods(GHUser.class, "getOrganizations", GHPersonSet.class, Set.class);
verifyBridgeMethods(GHUser.class, "getId", int.class, long.class, String.class);

verifyBridgeMethods(GHTeam.class, "getId", int.class, long.class, String.class);

// verifyBridgeMethods(GitHub.class, "getMyself", GHMyself.class, GHUser.class);

}

void verifyBridgeMethods(@Nonnull Class<?> targetClass, @Nonnull String methodName, Class<?>... returnTypes) {
List<Class<?>> foundMethods = new ArrayList<>();
Method[] methods = targetClass.getMethods();
for (Method method : methods) {
if (method.getName().equalsIgnoreCase(methodName)) {
// Bridge methods are only
assertThat(method.getParameterCount(), equalTo(0));
foundMethods.add(method.getReturnType());
}
}

assertThat(createdAtMethods.get(0).getReturnType(), is(Date.class));
assertThat(createdAtMethods.get(1).getReturnType(), is(String.class));
assertThat(foundMethods, containsInAnyOrder(returnTypes));
}
}

0 comments on commit 87f37e9

Please sign in to comment.