Skip to content

Commit 1d9b257

Browse files
authored
Merge pull request #137 from cip4/test1
Test1
2 parents 64aedaa + 4d34712 commit 1d9b257

3 files changed

Lines changed: 162 additions & 18 deletions

File tree

src/test/java/org/cip4/jdfutility/server/BambiRequestCustomizerTest.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import static org.junit.jupiter.api.Assertions.assertEquals;
4040
import static org.junit.jupiter.api.Assertions.assertThrows;
4141
import static org.mockito.ArgumentMatchers.any;
42-
import static org.mockito.Mockito.when;
42+
import static org.mockito.Mockito.mockStatic;
4343

4444
import java.net.UnknownHostException;
4545

@@ -51,6 +51,7 @@
5151
import org.eclipse.jetty.http.HttpFields.Mutable;
5252
import org.eclipse.jetty.server.Request;
5353
import org.junit.jupiter.api.Test;
54+
import org.mockito.MockedStatic;
5455
import org.mockito.Mockito;
5556

5657
public class BambiRequestCustomizerTest extends JDFUtilityTestBase
@@ -64,34 +65,67 @@ public void testConstruct()
6465
assertEquals(rq.isSniHostCheck(), rq2.isSniHostCheck());
6566
}
6667

68+
class TestCustomizer extends BambiRequestCustomizer
69+
{
70+
71+
private final boolean boom;
72+
73+
private TestCustomizer(final boolean boom)
74+
{
75+
super();
76+
this.boom = boom;
77+
}
78+
79+
private TestCustomizer(final TestCustomizer rc)
80+
{
81+
super(rc);
82+
boom = rc.boom;
83+
}
84+
85+
@Override
86+
void delegate(final Request request, final Mutable responseHeaders)
87+
{
88+
if (boom)
89+
throw new BadMessageException();
90+
}
91+
92+
}
93+
94+
@Test
6795
public void testCustomize() throws UnknownHostException
6896
{
69-
final BambiRequestCustomizer rq = new BambiRequestCustomizer();
97+
final BambiRequestCustomizer rq = new TestCustomizer(false);
7098

7199
final Request request = Mockito.mock(Request.class);
72100
final Mutable m = Mockito.mock(Mutable.class);
73101
rq.customize(request, m);
74102

75-
when(Request.getServerName(any())).thenReturn("localhost");
103+
final MockedStatic<Request> rs = mockStatic(Request.class);
104+
rs.when(() -> Request.getServerName(any())).thenReturn("localhost");
76105
rq.customize(request, m);
77-
when(Request.getServerName(any())).thenReturn("127.0.0.1");
106+
rs.when(() -> Request.getServerName(any())).thenReturn("127.0.0.1");
78107
rq.customize(request, m);
108+
rs.close();
79109

80110
}
81111

112+
@Test
82113
public void testCustomizeBad()
83114
{
84-
final BambiRequestCustomizer rq = new BambiRequestCustomizer();
115+
final BambiRequestCustomizer rq = new TestCustomizer(true);
85116
final SSLEngine e = Mockito.mock(SSLEngine.class);
86117
final SSLSession s = Mockito.mock(SSLSession.class);
87118
Mockito.when(e.getSession()).thenReturn(s);
88119

89120
final Request request = Mockito.mock(Request.class);
90121

91-
when(Request.getServerName(any())).thenReturn("notMyHost");
122+
final MockedStatic<Request> rs = mockStatic(Request.class);
123+
rs.when(() -> Request.getServerName(any())).thenReturn("badhost");
92124
final Mutable m = Mockito.mock(Mutable.class);
93125

94126
assertThrows(BadMessageException.class, () -> rq.customize(request, m));
127+
rs.close();
128+
95129
}
96130

97131
}

src/test/java/org/cip4/jdfutility/server/JettyServerTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public synchronized void testStartStop()
7373
final HTTPDump ns = new HTTPDump();
7474
ns.setPort(getPort());
7575
final int t = Thread.activeCount();
76-
for (int i = 0; i < 100; i++)
76+
for (int i = 0; i < 10; i++)
7777
{
7878
assertTrue(ns.tryStart());
7979
while (!ns.isStarted())
@@ -147,6 +147,12 @@ public synchronized void testIsStarted() throws InterruptedException
147147
ns.setPort(getPort());
148148
assertFalse(ns.isStarted());
149149
assertTrue(ns.tryStart());
150+
for (int i = 0; i < 42; i++)
151+
{
152+
if (!ns.isStarted())
153+
ThreadUtil.sleep(100);
154+
}
155+
150156
assertTrue(ns.isStarted());
151157
ns.stop();
152158
assertFalse(ns.isStarted());
@@ -159,23 +165,17 @@ public synchronized void testIsRunning() throws InterruptedException
159165
final HTTPDump ns = new HTTPDump();
160166
ns.setPort(getPort());
161167
ns.start();
168+
for (int i = 0; i < 42; i++)
169+
{
170+
if (!ns.isRunning())
171+
ThreadUtil.sleep(100);
172+
}
162173
assertTrue(ns.isRunning());
163174
ns.stop();
164175
assertFalse(ns.isStarted());
165176
ns.join();
166177
}
167178

168-
@Test
169-
public void testResHandler() throws InterruptedException
170-
{
171-
final MyResourceHandler rh = new MyResourceHandler("foo", "dummy");
172-
assertEquals("nix", rh.update("nix"));
173-
assertEquals("nix", rh.update("foo/nix"));
174-
assertEquals("http://localhost/bar/nix", rh.update("http://localhost/bar/foo/nix"));
175-
assertEquals("dummy", rh.update(""));
176-
assertEquals("dummy", rh.update("/"));
177-
}
178-
179179
@Test
180180
public void testResHandlerWL() throws InterruptedException
181181
{
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* The CIP4 Software License, Version 1.0
3+
*
4+
* Copyright (c) 2001-2025 The International Cooperation for the Integration of Processes in Prepress, Press and Postpress (CIP4). All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9+
*
10+
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the
11+
* distribution.
12+
*
13+
* 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the The International Cooperation for
14+
* the Integration of Processes in Prepress, Press and Postpress (www.cip4.org)" Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments
15+
* normally appear.
16+
*
17+
* 4. The names "CIP4" and "The International Cooperation for the Integration of Processes in Prepress, Press and Postpress" must not be used to endorse or promote products derived from this software
18+
* without prior written permission. For written permission, please contact info@cip4.org.
19+
*
20+
* 5. Products derived from this software may not be called "CIP4", nor may "CIP4" appear in their name, without prior written permission of the CIP4 organization
21+
*
22+
* Usage of this software in commercial products is subject to restrictions. For details please consult info@cip4.org.
23+
*
24+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25+
* DISCLAIMED. IN NO EVENT SHALL THE INTERNATIONAL COOPERATION FOR THE INTEGRATION OF PROCESSES IN PREPRESS, PRESS AND POSTPRESS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28+
* OF SUCH DAMAGE. ====================================================================
29+
*
30+
* This software consists of voluntary contributions made by many individuals on behalf of the The International Cooperation for the Integration of Processes in Prepress, Press and Postpress and was
31+
* originally based on software copyright (c) 1999-2001, Heidelberger Druckmaschinen AG copyright (c) 1999-2001, Agfa-Gevaert N.V.
32+
*
33+
* For more information on The International Cooperation for the Integration of Processes in Prepress, Press and Postpress , please see <http://www.cip4.org/>.
34+
*
35+
*
36+
*/
37+
package org.cip4.jdfutility.server;
38+
39+
import static org.junit.jupiter.api.Assertions.assertEquals;
40+
import static org.junit.jupiter.api.Assertions.assertFalse;
41+
import static org.junit.jupiter.api.Assertions.assertNotNull;
42+
import static org.mockito.Mockito.mock;
43+
import static org.mockito.Mockito.when;
44+
45+
import org.cip4.jdfutility.JDFUtilityTestBase;
46+
import org.eclipse.jetty.http.HttpURI;
47+
import org.eclipse.jetty.server.Context;
48+
import org.eclipse.jetty.server.Request;
49+
import org.eclipse.jetty.server.Response;
50+
import org.junit.jupiter.api.Test;
51+
52+
class MyResourceHandlerTest extends JDFUtilityTestBase
53+
{
54+
55+
@Test
56+
void testConstruct()
57+
{
58+
final MyResourceHandler h = new MyResourceHandler("abc", sm_dirTestData);
59+
assertNotNull(h);
60+
}
61+
62+
@Test
63+
public void testResHandler() throws Exception
64+
{
65+
final MyResourceHandler rh = new MyResourceHandler("foo", "dummy");
66+
assertEquals("nix", rh.update("nix"));
67+
assertEquals("nix", rh.update("foo/nix"));
68+
assertEquals("http://localhost/bar/nix", rh.update("http://localhost/bar/foo/nix"));
69+
assertEquals("dummy", rh.update(""));
70+
assertEquals("dummy", rh.update("/"));
71+
}
72+
73+
@Test
74+
public void testHandle() throws Exception
75+
{
76+
final MyResourceHandler rh = new MyResourceHandler("foo", "dummy");
77+
final Request r = mock(Request.class);
78+
final Context c = mock(Context.class);
79+
when(r.getContext()).thenReturn(c);
80+
final Response resp = mock(Response.class);
81+
assertFalse(rh.handle(r, resp, null));
82+
}
83+
84+
@Test
85+
public void testHandleDown() throws Exception
86+
{
87+
final MyResourceHandler rh = new MyResourceHandler("foo", "dummy");
88+
final Request r = mock(Request.class);
89+
final Context c = mock(Context.class);
90+
when(r.getContext()).thenReturn(c);
91+
when(r.getHttpURI()).thenReturn(HttpURI.build("abcd"));
92+
when(c.getContextPath()).thenReturn("abc");
93+
final Response resp = mock(Response.class);
94+
assertFalse(rh.handle(r, resp, null));
95+
}
96+
97+
@Test
98+
public void testHandleDown2() throws Exception
99+
{
100+
final MyResourceHandler rh = new MyResourceHandler("foo", "dummy");
101+
final Request r = mock(Request.class);
102+
final Context c = mock(Context.class);
103+
when(r.getContext()).thenReturn(c);
104+
when(r.getHttpURI()).thenReturn(HttpURI.build("foo/abcd"));
105+
when(c.getContextPath()).thenReturn("abc");
106+
final Response resp = mock(Response.class);
107+
assertFalse(rh.handle(r, resp, null));
108+
}
109+
110+
}

0 commit comments

Comments
 (0)