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

[JENKINS-36305] Fix TestObject url in order to fix TestActions links #50

Merged
merged 5 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 1 addition & 4 deletions src/main/java/hudson/tasks/junit/History.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,11 @@ public String getUrl() {
}

private void generateUrl() {
Run<?,?> build = o.getRun();
Vlatombe marked this conversation as resolved.
Show resolved Hide resolved
String buildLink = build.getUrl();
String actionUrl = o.getTestResultAction().getUrlName();
final String rootUrl = Helper.getActiveInstance().getRootUrl();
if (rootUrl == null) {
throw new IllegalStateException("Jenkins root URL not available");
}
this.url = rootUrl + buildLink + actionUrl + o.getUrl();
this.url = rootUrl + o.getUrl();
}

public int compareTo(ChartLabel that) {
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/hudson/tasks/junit/TestObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ public Run<?,?> getRun() {

public abstract TestObject getParent();

public abstract String getId();
/**
* Returns url relative to TestResult.
public abstract String getId();

/**
* Returns the URL of this {@link TestObject}, relative to the context root.
*
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we could deprecate the old method and introduce a replacement intended to have different behavior?

Copy link
Member Author

@Vlatombe Vlatombe Sep 30, 2020

Choose a reason for hiding this comment

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

* @return the url relative to {@link TestResult}.
*/
* @return
* String like "job/foo/32/testReport/junit/com.company/Class" with no trailing or leading slash.
*/
public abstract String getUrl();

public abstract TestResult getTestResult();
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/hudson/tasks/test/TestObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,9 @@ public final String getId() {
return id;
}

/**
* Returns url relative to TestResult
*/
@Override
public String getUrl() {
return '/' + getId();
return getRun().getUrl() + getTestResultAction().getUrlName() + "/" + getId();
Copy link
Member

@jtnord jtnord Sep 30, 2020

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

not really sure how to test this =/, I tried with the tests in https://github.com/jenkinsci/cucumber-testresult-plugin and these: https://github.com/testdouble/java-cucumber-example

}

public String getFullDisplayName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ THE SOFTWARE.
<j:if test="${test != null}">
<tr>
<td class="pane">
<a href="${app.rootUrl}${b.url}testReport${p.url}" class="model-link inside">${b.fullDisplayName}</a>
<a href="${app.rootUrl}${p.url}" class="model-link inside">${b.fullDisplayName}</a>
<j:forEach var="badge" items="${test.testActions}">
<st:include it="${badge}" page="badge.jelly" optional="true"/>
</j:forEach>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ THE SOFTWARE.
<j:if test="${p != null}">
<tr>
<td class="pane">
<a href="${app.rootUrl}${b.url}testReport${p.url}" class="model-link">${b.fullDisplayName}</a>
<a href="${app.rootUrl}${p.url}" class="model-link">${b.fullDisplayName}</a>
<j:forEach var="badge" items="${p.testActions}">
<st:include it="${badge}" page="badge.jelly" optional="true"/>
</j:forEach>
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/hudson/tasks/junit/History/index.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ THE SOFTWARE.
<st:include from="${it.testObject}" it="${it.testObject}" page="list.jelly" optional="true"/>
<div>
<j:if test="${it.testObject.run.parent.builds.size() > end}">
<a href="${app.rootUrl}${it.testObject.run.url}testReport${it.testObject.url}/history?start=${end+1}">${%Older}</a>
<a href="${app.rootUrl}${it.testObject.url}/history?start=${end+1}">${%Older}</a>
</j:if>

<j:if test="${start > 0}">
<a href="${app.rootUrl}${it.testObject.run.url}testReport${it.testObject.url}/history${(start-25)>0?'?start='+(start-25):''}">${%Newer}</a>
<a href="${app.rootUrl}${it.testObject.url}/history${(start-25)>0?'?start='+(start-25):''}">${%Newer}</a>
</j:if>  
</div>
</l:main-panel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ THE SOFTWARE.
<j:if test="${p != null}">
<tr>
<td class="pane">
<a href="${app.rootUrl}${b.url}testReport${p.url}" class="model-link">${b.fullDisplayName}</a>
<a href="${app.rootUrl}${p.url}" class="model-link">${b.fullDisplayName}</a>
<j:forEach var="badge" items="${p.testActions}">
<st:include it="${badge}" page="badge.jelly" optional="true"/>
</j:forEach>
Expand Down
73 changes: 70 additions & 3 deletions src/test/java/hudson/tasks/test/TestObjectTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,69 @@
package hudson.tasks.test;

import hudson.model.Run;
import org.junit.Assert;
import org.junit.Test;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

public class TestObjectTest {

public static class TestObjectImpl extends TestObject {
public TestObjectImpl() {
}

@Override
public TestObject getParent() {
return null;
}

@Override
public TestResult getPreviousResult() {
return null;
}

@Override
public TestResult findCorrespondingResult(String id) {
return null;
}

@Override
public float getDuration() {
return 0;
}

@Override
public String getName() {
return "dummy";
}

@Override
public int getPassCount() {
return 0;
}

@Override
public int getFailCount() {
return 0;
}

@Override
public int getSkipCount() {
return 0;
}

@Override
public String getDisplayName() {
return null;
}
}

@Test
public void testSafe() {
String name = "Foo#approve! is called by approve_on_foo?xyz/\\: 50%";
Expand Down Expand Up @@ -37,12 +92,24 @@ public void testSafe() {
for (TestObject t : ts) {
names.add(t.getSafeName());
}
Assert.assertEquals("[t0, t1, t1_2, t1_3, t2, t2_2, t2_3, t2_4, t2_5, t3]", names.toString());
assertEquals("[t0, t1, t1_2, t1_3, t2, t2_2, t2_3, t2_4, t2_5, t3]", names.toString());
Reference<?> r = new WeakReference<Object>(ts.get(4)); // arbitrarily
ts.clear();
System.gc();
Assert.assertNull(r.get());
}
}

@Test
public void getUrlShouldBeRelativeToContextRoot() {
TestObject testObject = spy(new TestObjectImpl());
Run run = mock(Run.class);
AbstractTestResultAction testResultAction = mock(AbstractTestResultAction.class);
doCallRealMethod().when(testResultAction).getUrlName();
doReturn(testResultAction).when(run).getAction(eq(AbstractTestResultAction.class));
doReturn("job/abc/123/").when(run).getUrl();
doReturn(run).when(testObject).getRun();
assertEquals("job/abc/123/testReport/dummy", testObject.getUrl());
}

}