2011/10/9 Mark Thomas <ma...@apache.org>: > On 09/10/2011 15:08, Olivier Lamy wrote: >> Hello, >> I'd like to be able to use a random port when using embedded Tomcat in >> unit tests to test servlets. >> Currently it's "locked" by a test in Connector#startInternal. >> Is it intentional ? > > svn blame would have answered that for you. > >> I'd like to be able to do simply something like : >> >> Tomcat tomcat = new Tomcat(); >> tomcat.setBaseDir(System.getProperty("java.io.tmpdir")); >> tomcat.getConnector().setPort( 0 ); > > If you want a random port, why not just set a random port at this point? Yup but I'm not sure the generated random number will be a free port ! At least ServerSocket ( 0 ) will ensure to use a free port. My goal is to be able to write unit tests using a non harcoded port which I'm sure is free on machines where the build run.
> >> Makes sense for to add getLocalPort on Connector class and remove the >> test if (getPort() < 1) ? > > No. At least having this validation in a separate method to ease override or having a sysprops which make using port 0 possible. Here I have to override startInternal(). see this unit test here [1] method startServer . I just found that complicated/hackhish just to be able to start an embedded servlet container on any free port to run unit tests ... > >> If yes I can start on providing a patch for review. > > I'd be against adding that patch to the code base. > > Mark > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org > For additional commands, e-mail: dev-h...@tomcat.apache.org > > Olivier [1] http://svn.codehaus.org/redback/redback/trunk/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/codehaus/redback/rest/services/AbstractRestServicesTest.java --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org