File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\hudson\FunctionsTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright 2011, OHTAKE Tomohiro.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson;
25
26 import java.util.Locale;
27
28 import hudson.model.Action;
29 import static org.junit.Assert.*;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.jvnet.hudson.test.Bug;
33 import org.kohsuke.stapler.Stapler;
34 import org.kohsuke.stapler.StaplerRequest;
35 import org.powermock.modules.junit4.PowerMockRunner;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import static org.powermock.api.mockito.PowerMockito.*;
38
39 @RunWith(PowerMockRunner.class)
40 public class FunctionsTest {
41 @Test
42 public void testGetActionUrl_absoluteUriWithAuthority(){
43 String[] uris = {
44 "http://example.com/foo/bar",
45 "https://example.com/foo/bar",
46 "ftp://example.com/foo/bar",
47 "svn+ssh://nobody@example.com/foo/bar",
48 };
49 for(String uri : uris) {
50 String result = Functions.getActionUrl(null, createMockAction(uri));
51 assertEquals(uri, result);
52 }
53 }
54
55 @Test
56 @Bug(7725)
57 public void testGetActionUrl_absoluteUriWithoutAuthority(){
58 String[] uris = {
59 "mailto:nobody@example.com",
60 "mailto:nobody@example.com?subject=hello",
61 "javascript:alert('hello')",
62 };
63 for(String uri : uris) {
64 String result = Functions.getActionUrl(null, createMockAction(uri));
65 assertEquals(uri, result);
66 }
67 }
68
69 @Test
70 @PrepareForTest(Stapler.class)
71 public void testGetActionUrl_absolutePath() throws Exception{
72 String contextPath = "/jenkins";
73 StaplerRequest req = createMockRequest(contextPath);
74 String[] paths = {
75 "/",
76 "/foo/bar",
77 };
78 mockStatic(Stapler.class);
79 when(Stapler.getCurrentRequest()).thenReturn(req);
80 for(String path : paths) {
81 String result = Functions.getActionUrl(null, createMockAction(path));
82 assertEquals(contextPath + path, result);
83 }
84 }
85
86 @Test
87 @PrepareForTest(Stapler.class)
88 public void testGetActionUrl_relativePath() throws Exception{
89 String contextPath = "/jenkins";
90 String itUrl = "iturl/";
91 StaplerRequest req = createMockRequest(contextPath);
92 String[] paths = {
93 "foo/bar",
94 "./foo/bar",
95 "../foo/bar",
96 };
97 mockStatic(Stapler.class);
98 when(Stapler.getCurrentRequest()).thenReturn(req);
99 for(String path : paths) {
100 String result = Functions.getActionUrl(itUrl, createMockAction(path));
101 assertEquals(contextPath + "/" + itUrl + path, result);
102 }
103 }
104
105 @Test
106 @PrepareForTest(Stapler.class)
107 public void testGetActionUrl_unparsable() throws Exception{
108 assertEquals(null, Functions.getActionUrl(null, createMockAction("http://nowhere.net/stuff?something=^woohoo")));
109 }
110
111 private static Action createMockAction(String uri) {
112 Action action = mock(Action.class);
113 when(action.getUrlName()).thenReturn(uri);
114 return action;
115 }
116
117 private static StaplerRequest createMockRequest(String contextPath) {
118 StaplerRequest req = mock(StaplerRequest.class);
119 when(req.getContextPath()).thenReturn(contextPath);
120 return req;
121 }
122
123 @Test
124 @Bug(16630)
125 public void testHumanReadableFileSize(){
126 Locale defaultLocale = Locale.getDefault();
127 try{
128 Locale.setDefault(Locale.ENGLISH);
129 assertEquals("0 B", Functions.humanReadableByteSize(0));
130 assertEquals("1023 B", Functions.humanReadableByteSize(1023));
131 assertEquals("1.00 KB", Functions.humanReadableByteSize(1024));
132 assertEquals("1.50 KB", Functions.humanReadableByteSize(1536));
133 assertEquals("20.00 KB", Functions.humanReadableByteSize(20480));
134 assertEquals("1023.00 KB", Functions.humanReadableByteSize(1047552));
135 assertEquals("1.00 MB", Functions.humanReadableByteSize(1048576));
136 assertEquals("1.50 GB", Functions.humanReadableByteSize(1610612700));
137 }finally{
138 Locale.setDefault(defaultLocale);
139 }
140 }
141
142 }
143