/**
 * 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.bugs;

import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.region.policy.FilePendingSubscriberMessageStoragePolicy;
import org.apache.activemq.broker.region.policy.LastImageSubscriptionRecoveryPolicy;
import org.apache.activemq.broker.region.policy.PolicyEntry;
import org.apache.activemq.broker.region.policy.PolicyMap;
import org.apache.activemq.transport.stomp.Stomp;
import org.apache.activemq.transport.stomp.StompConnection;
import org.apache.activemq.util.DefaultTestAppender;
import org.apache.log4j.Appender;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.Assert.fail;

public class AMQ3622Test {
    
    protected BrokerService broker;
    protected AtomicBoolean failed = new AtomicBoolean(false);
    protected String connectionUri;
    protected Appender appender = new DefaultTestAppender() {

        @Override
        public void doAppend(LoggingEvent event) {
            System.err.println(event.getMessage());
            if (event.getThrowableInformation() != null) {
                if (event.getThrowableInformation().getThrowable() instanceof NullPointerException) {
                    failed.set(true);
                }
            }
        }
    };

    @Before
    public void before() throws Exception {
        Logger.getRootLogger().addAppender(appender);

        broker = new BrokerService();
        broker.setDataDirectory("target" + File.separator + "activemq-data");
        broker.setPersistent(true);
        broker.setDeleteAllMessagesOnStartup(true);
        PolicyEntry policy = new PolicyEntry();
        policy.setTopic(">");
        policy.setProducerFlowControl(false);
        policy.setMemoryLimit(1 * 1024 * 1024);
        policy.setPendingSubscriberPolicy(new FilePendingSubscriberMessageStoragePolicy());
        policy.setSubscriptionRecoveryPolicy(new LastImageSubscriptionRecoveryPolicy());
        policy.setExpireMessagesPeriod(500);
        List<PolicyEntry> entries = new ArrayList<PolicyEntry>();
        
        entries.add(policy);
        PolicyMap pMap = new PolicyMap();
        pMap.setPolicyEntries(entries);
        broker.setDestinationPolicy(pMap);

        connectionUri = broker.addConnector("stomp://localhost:0").getPublishableConnectString();

        broker.start();
        broker.waitUntilStarted();
    }

    @After
    public void after() throws Exception {
        broker.stop();
        broker.waitUntilStopped();
        Logger.getRootLogger().removeAppender(appender);
    }
    
    @Test
    public void go() throws Exception {
        StompConnection connection = new StompConnection();
        connection.open("localhost", Integer.parseInt(connectionUri.replace("stomp://localhost:", "")));
        connection.connect("", "");
        connection.subscribe("/topic/foobar", Stomp.Headers.Subscribe.AckModeValues.CLIENT);
        connection.disconnect();
        Thread.sleep(1000);
        
        if (failed.get()) {
            fail("Received NullPointerException");
        }
    }

}

@@ -832,16 +832,177 @@
 .bugs;%0A%0A
+import static org.junit.Assert.fail;%0A%0Aimport java.io.File;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.concurrent.atomic.AtomicBoolean;%0A%0A
 import o
@@ -1686,169 +1686,8 @@
 t;%0A%0A
-import java.io.File;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.concurrent.atomic.AtomicBoolean;%0A%0Aimport static org.junit.Assert.fail;%0A%0A
 publ
@@ -1709,20 +1709,16 @@
 2Test %7B%0A
-    
 %0A    pro
@@ -3057,24 +3057,16 @@
 try%3E();%0A
-        
 %0A       
@@ -3553,20 +3553,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Te
@@ -3676,36 +3676,22 @@
     
-connection.open(%22localhost%22,
+Integer port =
  Int
@@ -3722,25 +3722,49 @@
 Uri.
-replace(%22stomp://
+split(%22:%22)%5B2%5D);%0A        connection.open(%22
 loca
@@ -3772,18 +3772,25 @@
 host
-:
 %22, 
-%22%22)));
+port);        
 %0A   
@@ -3976,24 +3976,16 @@
 (1000);%0A
-        
 %0A       

apply patch for: https://issues.apache.org/jira/browse/AMQ-3896