/*
 * 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.objectweb.jtests.jms.framework;

import javax.jms.JMSException;
import java.io.IOException;
import java.util.Properties;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.objectweb.jtests.jms.admin.Admin;
import org.objectweb.jtests.jms.admin.AdminFactory;

/**
 * Class extending <code>junit.framework.TestCase</code> to
 * provide a new <code>fail()</code> method with an <code>Exception</code>
 * as parameter.
 * <br />
 * Every Test Case for JMS should extend this class instead of <code>junit.framework.TestCase</code>
 */
public abstract class JMSTestCase extends Assert {

   public static final String PROP_FILE_NAME = "provider.properties";

   public static boolean startServer = true;

   protected Admin admin;

   /**
    * Fails a test with an exception which will be used for a message.
    *
    * If the exception is an instance of <code>javax.jms.JMSException</code>, the
    * message of the failure will contained both the JMSException and its linked exception
    * (provided there's one).
    */
   public void fail(final Exception e) {
      if (e instanceof javax.jms.JMSException) {
         JMSException exception = (JMSException) e;
         String message = e.toString();
         Exception linkedException = exception.getLinkedException();
         if (linkedException != null) {
            message += " [linked exception: " + linkedException + "]";
         }
         Assert.fail(message);
      }
      else {
         Assert.fail(e.getMessage());
      }
   }

   /**
    * Should be overridden
    *
    * @return
    */
   protected Properties getProviderProperties() throws IOException {
      Properties props = new Properties();
      props.load(ClassLoader.getSystemResourceAsStream(JMSTestCase.PROP_FILE_NAME));
      return props;
   }

   @Before
   public void setUp() throws Exception {

      // Admin step
      // gets the provider administration wrapper...
      Properties props = getProviderProperties();
      admin = AdminFactory.getAdmin(props);

      if (startServer) {
         admin.startServer();
      }
      admin.start();
   }

   @After
   public void tearDown() throws Exception {
      try {
         admin.stop();

         if (startServer) {
            admin.stopServer();
         }
      }
      finally {

      }
   }

}
