Package com.google.api.adwords.lib

Source Code of com.google.api.adwords.lib.ServiceAccountantManagerTest

// Copyright 2010 Google Inc. All Rights Reserved.
//
// Licensed 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 com.google.api.adwords.lib;

import com.google.api.adwords.v200909.cm.AdGroupSelector;
import com.google.api.adwords.v200909.cm.AdGroupServiceInterface;
import com.google.api.adwords.v200909.cm.CampaignSelector;
import com.google.api.adwords.v200909.cm.CampaignServiceInterface;

import junit.framework.TestCase;

import org.apache.axis.client.Stub;

import java.rmi.Remote;
import java.util.Map;

import javax.xml.rpc.Service;

/**
* Tests the {@link ServiceAccountantManager} class.
*
* @author api.arogal@gmail.com (Adam Rogal)
*/
public class ServiceAccountantManagerTest extends TestCase {
  private volatile int threadFinishedCount = 0;

  @Override
  public void tearDown() throws Exception {
    super.tearDown();
    // Restore to defaults.
    ServiceAccountantManager.getInstance().setAutoCreateAccountant(true);
    ServiceAccountantManager.getInstance().setRetainServices(false);
    ServiceAccountantManager.getInstance().clear();
  }

  /**
   * Tests that an instance can be retrieved.
   */
  public void testGetInstance() {
    assertNotNull(ServiceAccountantManager.getInstance());
  }

  /**
   * Tests that counters can be cleared.
   */
  public void testClearCounters() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    performOperations();
    assertTrue("Operations were not recorded.", instance.getTotalUnitCount() > 0);
    instance.clearCounters();
    assertTrue("Total units counter was not cleared.",
        instance.getTotalUnitCount() == 0);
    assertTrue("Total response time counter were not cleared.",
        instance.getTotalResponseTime() == 0);
    assertTrue("Total operations counter were not cleared.",
        instance.getTotalOperationCount() == 0);
  }

  /**
   * Tests that a service can be put into the manager for a user association.
   */
  public void testPutService() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(false);

    assertTrue("No services should be registered yet.",
        instance.getNumberRegisteredServices() == 0);

    AdWordsUser testUser1 = new AdWordsUser("test_data/test.properties");
    AdWordsUser testUser2 = new AdWordsUser("test_data/test.properties");
    CampaignServiceInterface campaignService =
        testUser1.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);

    instance.putService(((Stub) campaignService), testUser2);

    assertEquals(1, instance.getNumberRegisteredServices());
  }

  /**
   * Tests that a service accountant can be retrieved using a {@link Remote}
   * object.
   */
  public void testGetServiceAccountantRemote() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(true);
    AdWordsUser testUser = new AdWordsUser("test_data/test.properties");

    CampaignServiceInterface campaignService =
        testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService.get(new CampaignSelector());

    assertNotNull(instance.getServiceAccountant(campaignService));
  }

  /**
   * Tests that a service accountant can be retrieved using a {@link Stub}
   * object.
   */
  public void testGetServiceAccountantStub() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(true);
    AdWordsUser testUser = new AdWordsUser("test_data/test.properties");

    CampaignServiceInterface campaignService =
        testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService.get(new CampaignSelector());

    assertNotNull(instance.getServiceAccountant((Stub) campaignService));
  }

  /**
   * Tests that a service accountant can be retrieved using a {@link Service}
   * object.
   */
  public void testGetServiceAccountantService() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(true);
    AdWordsUser testUser = new AdWordsUser("test_data/test.properties");

    CampaignServiceInterface campaignService =
        testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService.get(new CampaignSelector());

    assertNotNull(instance.getServiceAccountant(((Stub) campaignService)._getService()));
  }

  /**
   * Tests that a service can be removed using a {@link Remote} object.
   */
  public void testRemoveServiceRemote() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(true);
    AdWordsUser testUser = new AdWordsUser("test_data/test.properties");

    CampaignServiceInterface campaignService =
        testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService.get(new CampaignSelector());

    instance.removeService(campaignService);
    assertTrue("Service was not removed.", instance.getNumberRegisteredServices() == 0);
  }

  /**
   * Tests that a service can be removed using a {@link Stub} object.
   */
  public void testRemoveServiceStub() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(true);
    AdWordsUser testUser = new AdWordsUser("test_data/test.properties");

    CampaignServiceInterface campaignService =
        testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService.get(new CampaignSelector());

    instance.removeService((Stub) campaignService);
    assertTrue("Service was not removed.", instance.getNumberRegisteredServices() == 0);
  }

  /**
   * Tests that a service can be removed using a {@link Service} object.
   */
  public void testRemoveServiceService() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(true);
    AdWordsUser testUser = new AdWordsUser("test_data/test.properties");

    CampaignServiceInterface campaignService =
        testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService.get(new CampaignSelector());

    instance.removeService(((Stub) campaignService)._getService());
    assertTrue("Service was not removed.", instance.getNumberRegisteredServices() == 0);
  }

  /**
   * Tests that service accountant manager can be cleared of all services.
   */
  public void testClear() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(true);
    AdWordsUser testUser = new AdWordsUser("test_data/test.properties");

    CampaignServiceInterface campaignService =
        testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService.get(new CampaignSelector());
    campaignService = testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService.get(new CampaignSelector());

    instance.clear();

    assertTrue("Services were not removed.", instance.getNumberRegisteredServices() == 0);
    assertTrue("Services were not removed.", instance.getNumberRegisteredServices(testUser) == 0);
  }

  /**
   * Tests that service accountant manager can remove services per user.
   */
  public void testRemoveAllServicesForUser() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(true);

    assertTrue("No services should be registered yet.",
        instance.getNumberRegisteredServices() == 0);

    AdWordsUser testUser1 = new AdWordsUser("test_data/test.properties");
    AdWordsUser testUser2 = new AdWordsUser("test_data/test.properties");
    AdWordsUser testUser3 = new AdWordsUser("test_data/test.properties");
    CampaignServiceInterface campaignService1 =
        testUser1.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    CampaignServiceInterface campaignService2 =
        testUser2.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService1.get(new CampaignSelector());
    campaignService2.get(new CampaignSelector());

    assertEquals(2, instance.getNumberRegisteredServices());
    instance.removeAllServicesForUser(testUser1);
    assertEquals(1, instance.getNumberRegisteredServices());
    instance.removeAllServicesForUser(testUser2);
    assertEquals(0, instance.getNumberRegisteredServices());
    instance.removeAllServicesForUser(testUser3);
    assertEquals(0, instance.getNumberRegisteredServices());
  }

  /**
   * Tests that service accountant manager can get all registered services.
   */
  public void testGetNumberRegisteredServices() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(true);

    assertTrue("No services should be registered yet.",
        instance.getNumberRegisteredServices() == 0);

    AdWordsUser testUser1 = new AdWordsUser("test_data/test.properties");
    AdWordsUser testUser2 = new AdWordsUser("test_data/test.properties");
    CampaignServiceInterface campaignService1 =
        testUser1.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    CampaignServiceInterface campaignService2 =
        testUser2.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService1.get(new CampaignSelector());
    campaignService2.get(new CampaignSelector());

    assertEquals(2, instance.getNumberRegisteredServices());
  }

  /**
   * Tests that service accountant manager can get all services for a user.
   */
  public void testGetNumberRegisteredServicesForUser() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(true);

    assertTrue("No services should be registered yet.",
        instance.getNumberRegisteredServices() == 0);

    AdWordsUser testUser1 = new AdWordsUser("test_data/test.properties");
    AdWordsUser testUser2 = new AdWordsUser("test_data/test.properties");
    CampaignServiceInterface campaignService1 =
        testUser1.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    CampaignServiceInterface campaignService2 =
        testUser2.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService1.get(new CampaignSelector());
    campaignService2.get(new CampaignSelector());

    assertEquals(2, instance.getNumberRegisteredServices());
    assertEquals(1, instance.getNumberRegisteredServices(testUser1));
    assertEquals(1, instance.getNumberRegisteredServices(testUser2));
  }

  /**
   * Tests that service accountant manager can get all retained services for
   * a user.
   */
  public void testGetRetainedServicesForUser() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(true);

    assertTrue("No services should be registered yet.",
        instance.getNumberRegisteredServices() == 0);

    AdWordsUser testUser1 = new AdWordsUser("test_data/test.properties");
    AdWordsUser testUser2 = new AdWordsUser("test_data/test.properties");
    CampaignServiceInterface campaignService1 =
        testUser1.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    CampaignServiceInterface campaignService2 =
        testUser2.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService1.get(new CampaignSelector());
    campaignService2.get(new CampaignSelector());

    instance.setRetainServices(true);

    CampaignServiceInterface campaignService3 =
        testUser1.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    CampaignServiceInterface campaignService4 =
        testUser2.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService3.get(new CampaignSelector());
    campaignService4.get(new CampaignSelector());

    assertEquals(4, instance.getNumberRegisteredServices());
    assertEquals(2, instance.getNumberRetainedServices());
    assertEquals(campaignService3, instance.getRetainedServicesForUser(testUser1)[0]);
    assertEquals(campaignService4, instance.getRetainedServicesForUser(testUser2)[0]);
  }


  /**
   * Tests that service accountant manager can get all retained services.
   */
  public void testGetAllRetainedServices() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(true);

    assertTrue("No services should be registered yet.",
        instance.getNumberRegisteredServices() == 0);
    assertTrue(instance.getAllRetainedServices().isEmpty());

    AdWordsUser testUser1 = new AdWordsUser("test_data/test.properties");
    AdWordsUser testUser2 = new AdWordsUser("test_data/test.properties");
    CampaignServiceInterface campaignService1 =
        testUser1.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    CampaignServiceInterface campaignService2 =
        testUser2.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService1.get(new CampaignSelector());
    campaignService2.get(new CampaignSelector());

    assertEquals(2, instance.getAllRetainedServices().keySet().size());
    instance.setRetainServices(true);

    CampaignServiceInterface campaignService3 =
        testUser1.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    CampaignServiceInterface campaignService4 =
        testUser2.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService3.get(new CampaignSelector());
    campaignService4.get(new CampaignSelector());

    assertEquals(4, instance.getNumberRegisteredServices());
    assertEquals(2, instance.getNumberRetainedServices());

    Map<AdWordsUser, Stub[]> allRetainedServices = instance.getAllRetainedServices();

    assertTrue(allRetainedServices.containsKey(testUser1));
    assertTrue(allRetainedServices.containsKey(testUser2));
    assertEquals(2, allRetainedServices.keySet().size());
    assertEquals(2, allRetainedServices.values().size());
    assertEquals(campaignService3, allRetainedServices.get(testUser1)[0]);
    assertEquals(campaignService4, allRetainedServices.get(testUser2)[0]);
  }

  /**
   * Tests that a service accountant can be created for a {@link Stub} object.
   */
  public void testCreateServiceAccountantStub() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(false);

    AdWordsUser testUser = new AdWordsUser("test_data/test.properties");
    CampaignServiceInterface campaignService =
        testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService.get(new CampaignSelector());

    assertNull("No service accountant should be created.", instance
        .getServiceAccountant(campaignService));

    ServiceAccountant serviceAccountant =
        ServiceAccountantManager.createServiceAccountant((Stub) campaignService);
    assertTrue("Service accountant should be empty.", serviceAccountant.getTotalUnitCount() == 0);

    campaignService.get(new CampaignSelector());

    long totalUnits = serviceAccountant.getTotalUnitCount();
    assertTrue("Service accountant not used.", totalUnits > 0);

    instance.clear();

    campaignService.get(new CampaignSelector());

    assertEquals("Service accountant still used.", totalUnits, serviceAccountant
        .getTotalUnitCount());
  }

  /**
   * Tests that a service accountant can be created for a {@link Remote} object.
   */
  public void testCreateServiceAccountantRemote() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(false);

    AdWordsUser testUser = new AdWordsUser("test_data/test.properties");
    CampaignServiceInterface campaignService =
        testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService.get(new CampaignSelector());

    assertNull("No service accountant should be created.", instance
        .getServiceAccountant(campaignService));

    ServiceAccountant serviceAccountant =
        ServiceAccountantManager.createServiceAccountant(campaignService);
    assertTrue("Service accountant should be empty.", serviceAccountant.getTotalUnitCount() == 0);

    campaignService.get(new CampaignSelector());

    long totalUnits = serviceAccountant.getTotalUnitCount();
    assertTrue("Service accountant not used.", totalUnits > 0);

    instance.clear();

    campaignService.get(new CampaignSelector());

    assertEquals("Service accountant still used.", totalUnits, serviceAccountant
        .getTotalUnitCount());
  }

  /**
   * Tests that a service accountant can be created for a {@link Service}
   * object.
   */
  public void testCreateServiceAccountantService() throws Exception {
    ServiceAccountantManager instance = ServiceAccountantManager.getInstance();
    instance.setAutoCreateAccountant(false);

    AdWordsUser testUser = new AdWordsUser("test_data/test.properties");
    CampaignServiceInterface campaignService =
        testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    campaignService.get(new CampaignSelector());

    assertNull("No service accountant should be created.", instance
        .getServiceAccountant(campaignService));

    ServiceAccountant serviceAccountant =
        ServiceAccountantManager.createServiceAccountant(((Stub) campaignService)._getService());
    assertTrue("Service accountant should be empty.", serviceAccountant.getTotalUnitCount() == 0);

    campaignService.get(new CampaignSelector());

    long totalUnits = serviceAccountant.getTotalUnitCount();
    assertTrue("Service accountant not used.", totalUnits > 0);

    instance.clear();

    campaignService.get(new CampaignSelector());

    assertEquals("Service accountant still used.", totalUnits, serviceAccountant
        .getTotalUnitCount());
  }

  /**
   * Tests the ability to collect service statistics via multiple threads.
   */
  public void testMultipleThreadAccess() {
    AdWordsServiceLogger.log();
    for (int i = 0; i < 5; i++) {
      final int threadNum = i;
      Thread t = new Thread() {
        public void run() {
          try {
            AdWordsUser testUser = new AdWordsUser("test_data/test.properties");
            ServiceAccountantManager.getInstance().setAutoCreateAccountant(true);

            CampaignServiceInterface campaignService =
                testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
            AdGroupServiceInterface adGroupService =
                testUser.getService(AdWordsService.V200909.ADGROUP_SERVICE);

            campaignService.get(new CampaignSelector());
            adGroupService.get(new AdGroupSelector());

            ServiceAccountant campaignServiceAccountant =
                ServiceAccountantManager.getInstance().getServiceAccountant(campaignService);

            ServiceAccountant adGroupServiceAccountant =
                ServiceAccountantManager.getInstance().getServiceAccountant(adGroupService);

            assertFalse("Campaign service was not registered.", campaignServiceAccountant
                .getLastServiceAccountantRecord().getEmail().equals("service_not_registered"));
            assertFalse("Campaign service was not registered.", campaignServiceAccountant
                .getLastServiceAccountantRecord().getService().equals("service_not_registered"));

            assertFalse("AdGroup service was not registered.", adGroupServiceAccountant
                .getLastServiceAccountantRecord().getEmail().equals("service_not_registered"));
            assertFalse("AdGroup service was not registered.", adGroupServiceAccountant
                .getLastServiceAccountantRecord().getService().equals("service_not_registered"));

            threadFinishedCount++;
          } catch (Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
          }
        }
      };
      t.start();
    }
    while (threadFinishedCount < 5) {
      System.out.println("Waiting on threads; threads finished: " + threadFinishedCount);
      try {
        Thread.sleep(5000L);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    System.out.println("Finished threads: " + threadFinishedCount);
  }

  /**
   * Performs some API methods via v200909.
   */
  private void performOperations() throws Exception {
    AdWordsUser testUser = new AdWordsUser("test_data/test.properties");

    CampaignServiceInterface campaignService =
        testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);
    AdGroupServiceInterface adGroupService =
        testUser.getService(AdWordsService.V200909.ADGROUP_SERVICE);

    campaignService.get(new CampaignSelector());
    adGroupService.get(new AdGroupSelector());
  }
}
TOP

Related Classes of com.google.api.adwords.lib.ServiceAccountantManagerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.