/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.component.file.remote;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;

import org.apache.camel.converter.IOConverter;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.AfterClass;
import org.junit.BeforeClass;

public class BaseServerTestSupport extends CamelTestSupport {
    protected static int port;

    @BeforeClass
    public static void initPort() throws Exception {
        File file = new File("./target/ftpport.txt");
        file = file.getAbsoluteFile();

        if (!file.exists()) {
            // start from somewhere in the 21xxx range
            port = 21000 + new Random().nextInt(900);
        } else {
            // read port number from file
            String s = IOConverter.toString(file, null);
            port = Integer.parseInt(s);
            // use next number
            port++;
        }

        // save to file, do not append
        FileOutputStream fos = new FileOutputStream(file, false);
        try {
            fos.write(String.valueOf(port).getBytes());
        } finally {
            fos.close();
        }
    }

    @AfterClass
    public static void resetPort() throws Exception {
        port = 0;
    }

    protected int getPort() {
        return port;
    }
}
@@ -857,124 +857,47 @@
 ort 
-java.io.File;%0Aimport java.io.FileOutputStream;%0Aimport java.util.Random;%0A%0Aimport org.apache.camel.converter.IOConvert
+org.apache.camel.test.AvailablePortFind
 er;%0A
@@ -954,37 +954,8 @@
 rt;%0A
-import org.junit.AfterClass;%0A
 impo
@@ -1157,774 +1157,58 @@
     
-File file = new File(%22./target/ftpport.txt%22);%0A        file = file.getAbsoluteFile();%0A%0A        if (!file.exists()) %7B%0A            // start from somewhere in the 21xxx range%0A            port = 21000 + new Random().nextInt(900);%0A        %7D else %7B%0A            // read port number from file%0A            String s = IOConverter.toString(file, null);%0A            port = Integer.parseInt(s);%0A            // use next number%0A            port++;%0A        %7D%0A%0A        // save to file, do not append%0A        FileOutputStream fos = new FileOutputStream(file, false);%0A        try %7B%0A            fos.write(String.valueOf(port).getBytes());%0A        %7D finally %7B%0A            fos.close();%0A        %7D%0A    %7D%0A%0A    @AfterClass%0A    public static void resetPort() throws Exception %7B%0A        port = 0
+port = AvailablePortFinder.getNextAvailable(21000)
 ;%0A  

Use port finder from camel-test instead of custom code to find free port, when testing camel-ftp.