Mercurial > hg > release > thermostat-0.5
changeset 624:fca33da6c769
Dynamically assign ports for running webserver in unit test.
Reviewed-by: omajid
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-September/003281.html
author | Roman Kennke <rkennke@redhat.com> |
---|---|
date | Tue, 18 Sep 2012 22:34:55 +0200 |
parents | d771115988ea |
children | bd6dab2e9300 |
files | common/core/src/main/java/com/redhat/thermostat/test/FreePortFinder.java web/client/src/test/java/com/redhat/thermostat/web/client/RESTStorageTest.java web/server/src/test/java/com/redhat/thermostat/web/server/RESTStorageTest.java |
diffstat | 3 files changed, 105 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/core/src/main/java/com/redhat/thermostat/test/FreePortFinder.java Tue Sep 18 22:34:55 2012 +0200 @@ -0,0 +1,75 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + + +package com.redhat.thermostat.test; + +import java.net.BindException; + +public class FreePortFinder { + + + public static interface TryPort { + void tryPort(int port) throws Exception; + } + + private static final int MIN_PORT = 1024; + private static final int MAX_PORT = 49151; + + private static volatile int nextPort = MAX_PORT; + + public static int findFreePort(TryPort tryPort) throws Exception { + int port; + while (true) { + try { + port = nextPort(); + tryPort.tryPort(port); + break; + } catch (BindException ex) { + continue; + } + } + return port; + } + + private static int nextPort() { + nextPort++; + if (nextPort > MAX_PORT) { + nextPort = MIN_PORT; + } + return nextPort; + } +}
--- a/web/client/src/test/java/com/redhat/thermostat/web/client/RESTStorageTest.java Tue Sep 18 21:17:02 2012 +0200 +++ b/web/client/src/test/java/com/redhat/thermostat/web/client/RESTStorageTest.java Tue Sep 18 22:34:55 2012 +0200 @@ -61,6 +61,8 @@ import com.redhat.thermostat.common.storage.Key; import com.redhat.thermostat.common.storage.Query; import com.redhat.thermostat.common.storage.Query.Criteria; +import com.redhat.thermostat.test.FreePortFinder; +import com.redhat.thermostat.test.FreePortFinder.TryPort; import com.redhat.thermostat.web.common.Qualifier; import com.redhat.thermostat.web.common.RESTQuery; @@ -68,13 +70,24 @@ private Server server; + private int port; + private String requestBody; private String responseBody; @Before public void setUp() throws Exception { - server = new Server(8080); + port = FreePortFinder.findFreePort(new TryPort() { + @Override + public void tryPort(int port) throws Exception { + startServer(port); + } + }); + } + + private void startServer(int port) throws Exception { + server = new Server(port); server.setHandler(new AbstractHandler() { @Override @@ -114,7 +127,7 @@ @Test public void testFindPojo() { RESTStorage storage = new RESTStorage(); - storage.setEndpoint("http://localhost:8080/"); + storage.setEndpoint("http://localhost:" + port + "/"); TestObj obj = new TestObj(); obj.setProperty1("fluffor");
--- a/web/server/src/test/java/com/redhat/thermostat/web/server/RESTStorageTest.java Tue Sep 18 21:17:02 2012 +0200 +++ b/web/server/src/test/java/com/redhat/thermostat/web/server/RESTStorageTest.java Tue Sep 18 22:34:55 2012 +0200 @@ -55,6 +55,8 @@ import com.redhat.thermostat.common.storage.Query; import com.redhat.thermostat.common.storage.Query.Criteria; import com.redhat.thermostat.common.storage.Storage; +import com.redhat.thermostat.test.FreePortFinder; +import com.redhat.thermostat.test.FreePortFinder.TryPort; import com.redhat.thermostat.web.client.RESTStorage; import com.redhat.thermostat.web.common.StorageWrapper; @@ -78,7 +80,7 @@ } private Server server; - + private int port; private Storage mockStorage; @Before @@ -86,7 +88,17 @@ mockStorage = mock(Storage.class); StorageWrapper.setStorage(mockStorage); - server = new Server(8080); + port = FreePortFinder.findFreePort(new TryPort() { + + @Override + public void tryPort(int port) throws Exception { + startServer(port); + } + }); + } + + private void startServer(int port) throws Exception { + server = new Server(port); server.setHandler(new WebAppContext("src/main/webapp", "/")); server.start(); } @@ -109,7 +121,7 @@ when(mockStorage.createQuery()).thenReturn(mockQuery); RESTStorage restStorage = new RESTStorage(); - restStorage.setEndpoint("http://localhost:8080/storage"); + restStorage.setEndpoint("http://localhost:" + port + "/storage"); Query query = restStorage.createQuery(); Key<String> key1 = new Key<>("key1", true); Key<Integer> key2 = new Key<>("key2", false);