/**
 * 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.activemq.transport.vm;

import java.net.URI;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.jms.JMSException;

import junit.framework.TestCase;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;

public class VMTransportWaitForTest extends TestCase {

    private static final String VM_BROKER_URI_NO_WAIT = 
        "vm://localhost?broker.persistent=false&create=false";
    
    private static final String VM_BROKER_URI_WAIT_FOR_START = 
        VM_BROKER_URI_NO_WAIT + "&waitForStart=20000";
    
    CountDownLatch started = new CountDownLatch(1);
    CountDownLatch gotConnection = new CountDownLatch(1);

    public void testWaitFor() throws Exception {
        try {
            ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(new URI(VM_BROKER_URI_NO_WAIT));
            cf.createConnection();
            fail("expect broker not exist exception");
        } catch (JMSException expectedOnNoBrokerAndNoCreate) {
        }
        
        // spawn a thread that will wait for an embedded broker to start via vm://..
        Thread t = new Thread() {
            public void run() {
                    try {
                        started.countDown();
                        ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(new URI(VM_BROKER_URI_WAIT_FOR_START));
                        cf.createConnection();
                        gotConnection.countDown();
                   
                    } catch (Exception e) {
                        e.printStackTrace();
                        fail("unexpected exception: " + e);
                    }
            }
        };
        t.start();
        started.await(20, TimeUnit.SECONDS);
        Thread.yield();
        assertFalse("has not got connection", gotConnection.await(2, TimeUnit.SECONDS));
        
        BrokerService broker = new BrokerService();
        broker.setPersistent(false);
        broker.addConnector("tcp://localhost:61616");
        broker.start();
        assertTrue("has got connection", gotConnection.await(400, TimeUnit.MILLISECONDS));
        broker.stop(); 
    }
}

@@ -840,16 +840,141 @@
 rt.vm;%0A%0A
+import static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertTrue;%0Aimport static org.junit.Assert.fail;%0A%0A
 import j
@@ -1105,42 +1105,8 @@
 n;%0A%0A
-import junit.framework.TestCase;%0A%0A
 impo
@@ -1203,16 +1203,39 @@
 Service;
+%0Aimport org.junit.Test;
 %0A%0Apublic
@@ -1268,25 +1268,8 @@
 est 
-extends TestCase 
 %7B%0A%0A 
@@ -1322,17 +1322,16 @@
 O_WAIT =
- 
 %0A       
@@ -1386,20 +1386,16 @@
 false%22;%0A
-    
 %0A    pri
@@ -1449,17 +1449,16 @@
 _START =
- 
 %0A       
@@ -1505,20 +1505,16 @@
 20000%22;%0A
-    
 %0A    Cou
@@ -1617,16 +1617,41 @@
 ch(1);%0A%0A
+    @Test(timeout=90000)%0A
     publ
@@ -1974,24 +1974,16 @@
       %7D%0A
-        
 %0A       
@@ -2051,16 +2051,27 @@
 tart via
+%0A        //
  vm://..
@@ -2101,24 +2101,46 @@
  Thread() %7B%0A
+            @Override%0A
             
@@ -2171,38 +2171,30 @@
             
-    
 try %7B%0A
-    
             
@@ -2214,36 +2214,32 @@
 ed.countDown();%0A
-    
                 
@@ -2355,36 +2355,32 @@
                 
-  
-  
 cf.createConnect
@@ -2378,36 +2378,32 @@
 teConnection();%0A
-    
                 
@@ -2437,32 +2437,8 @@
 ();%0A
-                   %0A    
     
@@ -2493,20 +2493,16 @@
         
-    
 e.printS
@@ -2530,28 +2530,24 @@
             
- 
-   
 fail(%22unexpe
@@ -2566,28 +2566,24 @@
 on: %22 + e);%0A
-    
             
@@ -2790,24 +2790,16 @@
 ONDS));%0A
-        
 %0A       
@@ -2884,62 +2884,8 @@
 e);%0A
-        broker.addConnector(%22tcp://localhost:61616%22);%0A
     
@@ -3017,17 +3017,16 @@
 .stop();
- 
 %0A    %7D%0A%7D

Convert to JUnit 4 test and add a timeout, also remove the tcp transport connector on a fixed port as its not needed.