gt:         mock.checking(new Expectations() {
context: /*******************************************************************************
   * Copyright (c) 2012 IBM Corporation and others.
   * All rights reserved. This program and the accompanying materials
   * are made available under the terms of the Eclipse Public License v1.0
   * which accompanies this distribution, and is available at
   * http://www.eclipse.org/legal/epl-v10.html
   *
   * Contributors:
   *     IBM Corporation - initial API and implementation
   *******************************************************************************/
  package com.ibm.ws.security.quickstart.internal;
  
  import static org.junit.Assert.assertTrue;
  
  import java.util.Dictionary;
  import java.util.HashMap;
  import java.util.Hashtable;
  import java.util.Map;
  
  import org.jmock.Expectations;
  import org.jmock.Mockery;
  import org.jmock.integration.junit4.JUnit4Mockery;
  import org.junit.After;
  import org.junit.Before;
  import org.junit.Rule;
  import org.junit.Test;
  import org.junit.rules.TestName;
  import org.junit.rules.TestRule;
  import org.osgi.framework.BundleContext;
  import org.osgi.framework.Constants;
  import org.osgi.framework.ServiceReference;
  import org.osgi.framework.ServiceRegistration;
  
  import com.ibm.websphere.ras.ProtectedString;
  import com.ibm.ws.management.security.ManagementRole;
  import com.ibm.ws.security.registry.UserRegistry;
  import com.ibm.ws.security.registry.UserRegistryService;
  import com.ibm.wsspi.kernel.service.utils.SerializableProtectedString;
  
  import test.common.SharedOutputManager;
  
  /**
   *
   */
  @SuppressWarnings("unchecked")
  public class QuickStartSecurityTest {
      private final Mockery mock = new JUnit4Mockery();
      private final BundleContext bc = mock.mock(BundleContext.class);
  
      private final ServiceReference<UserRegistry> otherURConfigRef = mock.mock(ServiceReference.class, "otherConfigRef");
      private final ServiceReference<UserRegistry> yetAnotherURConfigRef = mock.mock(ServiceReference.class, "yetAnotherURConfigRef");
      private final ServiceReference<ManagementRole> otherRoleRef = mock.mock(ServiceReference.class, "otherRoleRef");
      private final ServiceReference<ManagementRole> yetAnotherRoleRef = mock.mock(ServiceReference.class, "yetAnotherRoleRef");
  
      private final ServiceRegistration<UserRegistry> mgmtURConfigReg = mock.mock(ServiceRegistration.class, "mgmtConfigReg");
      private final ServiceRegistration<ManagementRole> mgmtRoleReg = mock.mock(ServiceRegistration.class, "mgmtRoleReg");
  
      private QuickStartSecurity quickStartSecurity;
  
      @Rule
      public TestName name = new TestName();
  
      private static SharedOutputManager outputMgr = SharedOutputManager.getInstance();
      /**
       * Using the test rule will drive capture/restore and will dump on error..
       * Notice this is not a static variable, though it is being assigned a value we
       * allocated statically. -- the normal-variable-ness is for before/after processing
       */
      @Rule
      public TestRule managerRule = outputMgr;
  
      @Before
      public void setUp() {
          mock.checking(new Expectations() {
              {
  
                  allowing(otherURConfigRef).compareTo(otherURConfigRef);
                  will(returnValue(0));
                  allowing(otherURConfigRef).getProperty(Constants.SERVICE_ID);
                  will(returnValue(67890L));
                  allowing(otherURConfigRef).getProperty(Constants.SERVICE_RANKING);
                  will(returnValue(0));
  
                  allowing(yetAnotherURConfigRef).compareTo(otherURConfigRef);
                  will(returnValue(1));
                  allowing(otherURConfigRef).compareTo(yetAnotherURConfigRef);
                  will(returnValue(1));
                  allowing(yetAnotherURConfigRef).compareTo(yetAnotherURConfigRef);
                  will(returnValue(0));
                  allowing(yetAnotherURConfigRef).getProperty(Constants.SERVICE_ID);
                  will(returnValue(54321L));
                  allowing(yetAnotherURConfigRef).getProperty(Constants.SERVICE_RANKING);
                  will(returnValue(0));
  
                  allowing(otherRoleRef).compareTo(otherRoleRef);
                  will(returnValue(0));
                  allowing(otherRoleRef).getProperty(Constants.SERVICE_ID);
                  will(returnValue(67890L));
                  allowing(otherRoleRef).getProperty(Constants.SERVICE_RANKING);
                  will(returnValue(0));
  
                  allowing(yetAnotherRoleRef).compareTo(yetAnotherRoleRef);
                  will(returnValue(0));
                  allowing(yetAnotherRoleRef).getProperty(Constants.SERVICE_ID);
                  will(returnValue(54321L));
                  allowing(yetAnotherRoleRef).getProperty(Constants.SERVICE_RANKING);
                  will(returnValue(0));
              }
          });
          quickStartSecurity = new QuickStartSecurity();
      }
  
      @After
      public void tearDown() {
          mock.assertIsSatisfied();
      }
  
      private QuickStartSecurityConfig config(final String user, final String pw, final String[] urs, final String[] ars) {
          return new QuickStartSecurityConfig() {
  
              //osgi object converter converts null to "" for known types.
              @Override
              public String userName() {
                  return user == null ? "" : user;
              }
  
              @Override
              public SerializableProtectedString userPassword() {
                  return pw == null ? null : new SerializableProtectedString(pw.toCharArray());
              }
  
              @Override
              public String[] UserRegistry() {
                  return urs == null ? new String[] {} : urs;
              }
  
              @Override
              public String[] ManagementRole() {
                  return ars == null ? new String[] {} : ars;
              }
          };
      }
  
      /**
       * Set up the mock expectations to handle a registration of the
       * quick start security UserRegistry.
       *
       * @param user
       * @param password
       */
      private void registerConfigurationExpectations(final String user, final String password) {
          final Dictionary<String, Object> configProps = new Hashtable<String, Object>();
          configProps.put("id", QuickStartSecurity.QUICK_START_SECURITY_REGISTRY_ID);
          configProps.put("config.id", QuickStartSecurity.QUICK_START_SECURITY_REGISTRY_ID);
          configProps.put(UserRegistryService.REGISTRY_TYPE, QuickStartSecurity.QUICK_START_SECURITY_REGISTRY_TYPE);
          configProps.put("service.vendor", "IBM");
          configProps.put(QuickStartSecurity.CFG_KEY_USER, user);
  
          mock.checking(new Expectations() {
              {
                  one(bc).registerService(with(UserRegistry.class),
                                          with(any(UserRegistry.class)),
                                          with(equal(configProps)));
                  will(returnValue(mgmtURConfigReg));
              }
          });
      }
  
      /**
       * Set up the mock expectations to prevent a registration of the
       * quick start security UserRegistry.
       *
       * @param user defaultAdminUser. If null, the property won't be set for the expectations.
       * @param passwordd efaultAdminPassword. If null, the property won't be set for the expectations.
       */
      private void doesNotRegisterConfigurationExpectations() {
          mock.checking(new Expectations() {
              {
                  never(bc).registerService(with(UserRegistry.class),
                                            with(any(UserRegistry.class)),
                                            with(any(Dictionary.class)));
              }
          });
      }
  
      /**
       * Set up the mock expectations to handle a de-registration of the
       * quick start security UserRegistry.
       */
      private void unregisterConfigurationExpectations() {
          mock.checking(new Expectations() {
              {
                  one(mgmtURConfigReg).unregister();
              }
          });
      }
  
      /**
       * Set up the mock expectations to handle a registration of the
       * QuickStartSecurityAdministratorRole.
       */
      private void registerRoleExpectations() {
          final Dictionary<String, Object> configProps = new Hashtable<String, Object>();
          configProps.put(ManagementRole.MANAGEMENT_ROLE_NAME, QuickStartSecurity.QUICK_START_ADMINISTRATOR_ROLE_NAME);
          configProps.put("service.vendor", "IBM");
  
          mock.checking(new Expectations() {
              {
                  one(bc).registerService(with(ManagementRole.class),
                                          with(any(QuickStartSecurityAdministratorRole.class)),
                                          with(equal(configProps)));
                  will(returnValue(mgmtRoleReg));
              }
          });
      }
  
      /**
       * Set up the mock expectations to prevent a registration of the
       * quick start security UserRegistry.
       *
       * @param user defaultAdminUser. If null, the property won't be set for the expectations.
       * @param passwordd efaultAdminPassword. If null, the property won't be set for the expectations.
       */
      private void doesNotRegisterRoleExpectations() {
          mock.checking(new Expectations() {
              {
                  never(bc).registerService(with(ManagementRole.class),
                                            with(any(QuickStartSecurityAdministratorRole.class)),
                                            with(any(Dictionary.class)));
              }
          });
      }
  
      /**
       * Set up the mock expectations to handle a de-registration of the
       * QuickStartSecurityAdministratorRole.
       */
      private void unregisterRoleExpectations() {
  