PortFileManager { public void writeUsedPortFile( String filePath ) throws FileNotFoundException, IOException { StringBuilder sb = new StringBuilder(); for ( Integer port : usedPorts ) { if ( sb.length() > 0 ) { sb.append( "," ); } sb.append( port.toString() ); } File usedPortFile = new File( filePath ); usedPortFile.getParentFile().mkdirs(); RandomAccessFile fileStore = new RandomAccessFile( usedPortFile, "rw" ); try { getLock( fileStore ); fileStore.writeUTF( sb.toString() ); } finally { fileStore.close(); } return; } private  PortFileManager(); static PortFileManager getInstance(); boolean addPort( Integer port ); boolean removePort( Integer port ); void writeUsedPortFile( String filePath ); void clear();  }
@Test public void testWriteUsedPortFile() throws FileNotFoundException, IOException { PortFileManager.instance = null; PortFileManager portFileManager = PortFileManager.getInstance(); Integer[] mockPorts = new Integer[] { 1, 2, 3, 4, 5, 15, 20 }; for ( Integer mockPort : mockPorts ) { portFileManager.addPort( mockPort ); } String testFolder = "./bin/test/cacheTest"; File folder = new File( testFolder ); if ( !folder.exists() ) { folder.mkdirs(); } portFileManager.writeUsedPortFile( testFolder + "/testUsedPorts" ); @SuppressWarnings( "unchecked" ) List<Integer> mockPortsList = new ArrayList<Integer>( Arrays.asList( mockPorts ) ); testPortsWereWrittenToPortFile( testFolder + "/testUsedPorts", mockPortsList ); }