javapns IOS推送

javapns IOS推送

package javapns.test;

import java.util.*;

import javapns.*;
import javapns.communication.exceptions.*;
import javapns.devices.*;

/**
 * A command-line test facility for the Feedback Service.
 * <p>Example:  <code>java -cp "[required libraries]" javapns.test.FeedbackTest keystore.p12 mypass</code></p>
 * 
 * <p>By default, this test uses the sandbox service.  To switch, add "production" as a third parameter:</p>
 * <p>Example:  <code>java -cp "[required libraries]" javapns.test.FeedbackTest keystore.p12 mypass production</code></p>
 * 
 * @author Sylvain Pedneault
 */
public class FeedbackTest extends TestFoundation {

	/**
	 * Execute this class from the command line to run tests.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		/* Verify that the test is being invoked  */
		if (!verifyCorrectUsage(FeedbackTest.class, args, "keystore-path", "keystore-password", "[production|sandbox]")) return;

		/* Initialize Log4j to print logs to console */
		configureBasicLogging();

		/* Get a list of inactive devices */
		feedbackTest(args);
	}


	private FeedbackTest() {
	}


	/**
	 * Retrieves a list of inactive devices from the Feedback service.
	 * @param args
	 */
	private static void feedbackTest(String[] args) {
		String keystore = args[0];
		String password = args[1];
		boolean production = args.length >= 3 ? args[2].equalsIgnoreCase("production") : false;
		try {
			List<Device> devices = Push.feedback(keystore, password, production);

			for (Device device : devices) {
				System.out.println("Inactive device: " + device.getToken());
			}
		} catch (CommunicationException e) {
			e.printStackTrace();
		} catch (KeystoreException e) {
			e.printStackTrace();
		}
	}

}

 

 

package javapns.test;

import java.util.*;

import javapns.*;
import javapns.communication.exceptions.*;
import javapns.devices.*;
import javapns.devices.implementations.basic.*;
import javapns.notification.*;
import javapns.notification.transmission.*;

import org.json.*;

/**
 * A command-line test facility for the Push Notification Service.
 * <p>Example:  <code>java -cp "[required libraries]" javapns.test.NotificationTest keystore.p12 mypass 2ed202ac08ea9033665e853a3dc8bc4c5e78f7a6cf8d55910df230567037dcc4</code></p>
 * 
 * <p>By default, this test uses the sandbox service.  To switch, add "production" as a fourth parameter:</p>
 * <p>Example:  <code>java -cp "[required libraries]" javapns.test.NotificationTest keystore.p12 mypass 2ed202ac08ea9033665e853a3dc8bc4c5e78f7a6cf8d55910df230567037dcc4 production</code></p>
 * 
 * <p>Also by default, this test pushes a simple alert.  To send a complex payload, add "complex" as a fifth parameter:</p>
 * <p>Example:  <code>java -cp "[required libraries]" javapns.test.NotificationTest keystore.p12 mypass 2ed202ac08ea9033665e853a3dc8bc4c5e78f7a6cf8d55910df230567037dcc4 production complex</code></p>
 * 
 * <p>To send a simple payload to a large number of fake devices, add "threads" as a fifth parameter, the number of fake devices to construct, and the number of threads to use:</p>
 * <p>Example:  <code>java -cp "[required libraries]" javapns.test.NotificationTest keystore.p12 mypass 2ed202ac08ea9033665e853a3dc8bc4c5e78f7a6cf8d55910df230567037dcc4 sandbox threads 1000 5</code></p>
 * 
 * @author Sylvain Pedneault
 */
public class NotificationTest extends TestFoundation {

	/**
	 * Execute this class from the command line to run tests.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		/* Verify that the test is being invoked  */
		if (!verifyCorrectUsage(NotificationTest.class, args, "keystore-path", "keystore-password", "device-token", "[production|sandbox]", "[complex|simple|threads]", "[#devices]", "[#threads]")) return;

		/* Initialize Log4j to print logs to console */
		configureBasicLogging();

		/* Push an alert */
		try {
			pushTest(args);
		} catch (CommunicationException e) {
			e.printStackTrace();
		} catch (KeystoreException e) {
			e.printStackTrace();
		}
	}


	private NotificationTest() {
	}


	/**
	 * Push a test notification to a device, given command-line parameters.
	 * 
	 * @param args
	 * @throws KeystoreException 
	 * @throws CommunicationException 
	 */
	private static void pushTest(String[] args) throws CommunicationException, KeystoreException {
		String keystore = args[0];
		String password = args[1];
		String token = args[2];
		boolean production = args.length >= 4 ? args[3].equalsIgnoreCase("production") : false;
		boolean simulation = args.length >= 4 ? args[3].equalsIgnoreCase("simulation") : false;
		boolean complex = args.length >= 5 ? args[4].equalsIgnoreCase("complex") : false;
		boolean threads = args.length >= 5 ? args[4].equalsIgnoreCase("threads") : false;
		int threadDevices = args.length >= 6 ? Integer.parseInt(args[5]) : 100;
		int threadThreads = args.length >= 7 ? Integer.parseInt(args[6]) : 10;
		boolean simple = !complex && !threads;

		verifyKeystore(keystore, password, production);

		if (simple) {

			/* Push a test alert */
			List<PushedNotification> notifications = Push.test(keystore, password, production, token);
			printPushedNotifications(notifications);

		} else if (complex) {

			/* Push a more complex payload */
			List<PushedNotification> notifications = Push.payload(createComplexPayload(), keystore, password, production, token);
			printPushedNotifications(notifications);

		} else if (threads) {

			/* Push a Hello World! alert repetitively using NotificationThreads */
			pushSimplePayloadUsingThreads(keystore, password, production, token, simulation, threadDevices, threadThreads);

		}
	}


	/**
	 * Create a complex payload for test purposes.
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private static Payload createComplexPayload() {
		PushNotificationPayload complexPayload = PushNotificationPayload.complex();
		try {
			// You can use addBody to add simple message, but we'll use
			// a more complex alert message so let's comment it
			complexPayload.addCustomAlertBody("My alert message");
			complexPayload.addCustomAlertActionLocKey("Open App");
			complexPayload.addCustomAlertLocKey("javapns rocks %@ %@%@");
			ArrayList parameters = new ArrayList();
			parameters.add("Test1");
			parameters.add("Test");
			parameters.add(2);
			complexPayload.addCustomAlertLocArgs(parameters);
			complexPayload.addBadge(45);
			complexPayload.addSound("default");
			complexPayload.addCustomDictionary("acme", "foo");
			complexPayload.addCustomDictionary("acme2", 42);
			ArrayList values = new ArrayList();
			values.add("value1");
			values.add(2);
			complexPayload.addCustomDictionary("acme3", values);
		} catch (JSONException e) {
			System.out.println("Error creating complex payload:");
			e.printStackTrace();
		}
		return complexPayload;
	}


	protected static void pushSimplePayloadUsingThreads(String keystore, String password, boolean production, String token, boolean simulation, int devices, int threads) {
		try {

			System.out.println("Creating PushNotificationManager and AppleNotificationServer");
			AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production);
			System.out.println("Creating payload (simulation mode)");
			//			Payload payload = PushNotificationPayload.alert("Hello World!");
			Payload payload = PushNotificationPayload.test();

			System.out.println("Generating " + devices + " fake devices");
			List<Device> deviceList = new ArrayList<Device>(devices);
			for (int i = 0; i < devices; i++) {
				String tokenToUse = token;
				if (tokenToUse == null || tokenToUse.length() != 64) {
					tokenToUse = "123456789012345678901234567890123456789012345678901234567" + (1000000 + i);
				}
				deviceList.add(new BasicDevice(tokenToUse));
			}

			System.out.println("Creating " + threads + " notification threads");
			NotificationThreads work = new NotificationThreads(server, simulation ? payload.asSimulationOnly() : payload, deviceList, threads);
			//work.setMaxNotificationsPerConnection(10000);
			System.out.println("Linking notification work debugging listener");
			work.setListener(DEBUGGING_PROGRESS_LISTENER);

			System.out.println("Starting all threads...");
			long timestamp1 = System.currentTimeMillis();
			work.start();
			System.out.println("All threads started, waiting for them...");
			work.waitForAllThreads();
			long timestamp2 = System.currentTimeMillis();
			System.out.println("All threads finished in " + (timestamp2 - timestamp1) + " milliseconds");

			printPushedNotifications(work.getPushedNotifications());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * A NotificationProgressListener you can use to debug NotificationThreads.
	 */
	public static final NotificationProgressListener DEBUGGING_PROGRESS_LISTENER = new NotificationProgressListener() {

		public void eventThreadStarted(NotificationThread notificationThread) {
			System.out.println("   [EVENT]: thread #" + notificationThread.getThreadNumber() + " started with " + notificationThread.getDevices().size() + " devices beginning at message id #" + notificationThread.getFirstMessageIdentifier());
		}


		public void eventThreadFinished(NotificationThread thread) {
			System.out.println("   [EVENT]: thread #" + thread.getThreadNumber() + " finished: pushed messages #" + thread.getFirstMessageIdentifier() + " to " + thread.getLastMessageIdentifier() + " toward " + thread.getDevices().size() + " devices");
		}


		public void eventConnectionRestarted(NotificationThread thread) {
			System.out.println("   [EVENT]: connection restarted in thread #" + thread.getThreadNumber() + " because it reached " + thread.getMaxNotificationsPerConnection() + " notifications per connection");
		}


		public void eventAllThreadsStarted(NotificationThreads notificationThreads) {
			System.out.println("   [EVENT]: all threads started: " + notificationThreads.getThreads().size());
		}


		public void eventAllThreadsFinished(NotificationThreads notificationThreads) {
			System.out.println("   [EVENT]: all threads finished: " + notificationThreads.getThreads().size());
		}


		public void eventCriticalException(NotificationThread notificationThread, Exception exception) {
			System.out.println("   [EVENT]: critical exception occurred: " + exception);
		}
	};


	/**
	 * Print to the console a comprehensive report of all pushed notifications and results.
	 * @param notifications a raw list of pushed notifications
	 */
	public static void printPushedNotifications(List<PushedNotification> notifications) {
		List<PushedNotification> failedNotifications = PushedNotification.findFailedNotifications(notifications);
		List<PushedNotification> successfulNotifications = PushedNotification.findSuccessfulNotifications(notifications);
		int failed = failedNotifications.size();
		int successful = successfulNotifications.size();

		if (successful > 0 && failed == 0) {
			printPushedNotifications("All notifications pushed successfully (" + successfulNotifications.size() + "):", successfulNotifications);
		} else if (successful == 0 && failed > 0) {
			printPushedNotifications("All notifications failed (" + failedNotifications.size() + "):", failedNotifications);
		} else if (successful == 0 && failed == 0) {
			System.out.println("No notifications could be sent, probably because of a critical error");
		} else {
			printPushedNotifications("Some notifications failed (" + failedNotifications.size() + "):", failedNotifications);
			printPushedNotifications("Others succeeded (" + successfulNotifications.size() + "):", successfulNotifications);
		}
	}


	/**
	 * Print to the console a list of pushed notifications.
	 * @param description a title for this list of notifications
	 * @param notifications a list of pushed notifications to print
	 */
	public static void printPushedNotifications(String description, List<PushedNotification> notifications) {
		System.out.println(description);
		for (PushedNotification notification : notifications) {
			try {
				System.out.println("  " + notification.toString());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

 

 

 

package javapns.test;

import java.io.*;
import java.util.*;

import javapns.*;
import javapns.communication.exceptions.*;
import javapns.devices.*;
import javapns.devices.implementations.basic.*;
import javapns.notification.*;
import javapns.notification.transmission.*;

import org.json.*;

/**
 * Specific test cases intended for the project's developers.
 * 
 * @author Sylvain Pedneault
 */
public class SpecificNotificationTests extends TestFoundation {

	/**
	 * Execute this class from the command line to run tests.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		/* Verify that the test is being invoked  */
		if (!verifyCorrectUsage(NotificationTest.class, args, "keystore-path", "keystore-password", "device-token", "[production|sandbox]", "[test-name]")) return;

		/* Initialize Log4j to print logs to console */
		configureBasicLogging();

		/* Push an alert */
		runTest(args);
	}


	private SpecificNotificationTests() {
	}


	/**
	 * Push a test notification to a device, given command-line parameters.
	 * 
	 * @param args
	 */
	private static void runTest(String[] args) {
		String keystore = args[0];
		String password = args[1];
		String token = args[2];
		boolean production = args.length >= 4 ? args[3].equalsIgnoreCase("production") : false;
		boolean simulation = args.length >= 4 ? args[3].equalsIgnoreCase("simulation") : false;

		String testName = args.length >= 5 ? args[4] : null;
		if (testName == null || testName.length() == 0) testName = "default";

		try {
			SpecificNotificationTests.class.getDeclaredMethod("test_" + testName, String.class, String.class, String.class, boolean.class).invoke(null, keystore, password, token, production);
		} catch (NoSuchMethodException e) {
			System.out.println(String.format("Error: test '%s' not found.  Test names are case-sensitive", testName));
		} catch (Exception e) {
			(e.getCause() != null ? e.getCause() : e).printStackTrace();
		}
	}


	private static void test_PushHelloWorld(String keystore, String password, String token, boolean production) throws CommunicationException, KeystoreException {
		List<PushedNotification> notifications = Push.alert("Hello World!", keystore, password, production, token);
		NotificationTest.printPushedNotifications(notifications);
	}


	private static void test_Issue74(String keystore, String password, String token, boolean production) {
		try {
			System.out.println("");
			System.out.println("TESTING 257-BYTES PAYLOAD WITH SIZE ESTIMATION ENABLED");
			/* Expected result: PayloadMaxSizeProbablyExceededException when the alert is added to the payload */
			pushSpecificPayloadSize(keystore, password, token, production, true, 257);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			System.out.println("");
			System.out.println("TESTING 257-BYTES PAYLOAD WITH SIZE ESTIMATION DISABLED");
			/* Expected result: PayloadMaxSizeExceededException when the payload is pushed */
			pushSpecificPayloadSize(keystore, password, token, production, false, 257);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			System.out.println("");
			System.out.println("TESTING 256-BYTES PAYLOAD");
			/* Expected result: no exception */
			pushSpecificPayloadSize(keystore, password, token, production, false, 256);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	private static void test_Issue75(String keystore, String password, String token, boolean production) {
		try {
			System.out.println("");
			System.out.println("TESTING 257-BYTES PAYLOAD WITH SIZE ESTIMATION ENABLED");
			NewsstandNotificationPayload payload = NewsstandNotificationPayload.contentAvailable();
			debugPayload(payload);

			List<PushedNotification> notifications = Push.payload(payload, keystore, password, production, token);
			NotificationTest.printPushedNotifications(notifications);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	private static void test_Issue82(String keystore, String password, String token, boolean production) {
		try {
			System.out.println("");
			Payload payload = PushNotificationPayload.test();

			System.out.println("TESTING ISSUE #82 PART 1");
			List<PushedNotification> notifications = Push.payload(payload, keystore, password, production, 1, token);
			NotificationTest.printPushedNotifications(notifications);
			System.out.println("ISSUE #82 PART 1 TESTED");

			System.out.println("TESTING ISSUE #82 PART2");
			AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production);
			NotificationThread thread = new NotificationThread(new PushNotificationManager(), server, payload, token);
			thread.setListener(NotificationTest.DEBUGGING_PROGRESS_LISTENER);
			thread.start();
			System.out.println("ISSUE #82 PART 2 TESTED");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	private static void test_Issue87(String keystore, String password, String token, boolean production) {
		try {
			System.out.println("TESTING ISSUES #87 AND #88");

			InputStream ks = new BufferedInputStream(new FileInputStream(keystore));
			PushQueue queue = Push.queue(ks, password, false, 3);
			queue.start();
			queue.add(PushNotificationPayload.test(), token);
			queue.add(PushNotificationPayload.test(), token);
			queue.add(PushNotificationPayload.test(), token);
			queue.add(PushNotificationPayload.test(), token);
			Thread.sleep(10000);
			List<Exception> criticalExceptions = queue.getCriticalExceptions();
			for (Exception exception : criticalExceptions) {
				exception.printStackTrace();
			}
			Thread.sleep(10000);

			List<PushedNotification> pushedNotifications = queue.getPushedNotifications();
			NotificationTest.printPushedNotifications("BEFORE CLEAR:", pushedNotifications);

			queue.clearPushedNotifications();

			pushedNotifications = queue.getPushedNotifications();
			NotificationTest.printPushedNotifications("AFTER CLEAR:", pushedNotifications);

			Thread.sleep(50000);
			System.out.println("ISSUES #87 AND #88 TESTED");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	private static void test_Issue88(String keystore, String password, String token, boolean production) {
		try {
			System.out.println("TESTING ISSUES #88");

			//			List<String> devices = new Vector<String>();
			//			for (int i = 0; i < 5; i++) {
			//				devices.add(token);
			//			}
			//			PushedNotifications notifications = Push.payload(PushNotificationPayload.test(), keystore, password, false, devices);
			PushQueue queue = Push.queue(keystore, password, false, 1);
			queue.start();
			queue.add(PushNotificationPayload.test(), token);
			queue.add(PushNotificationPayload.test(), token);
			queue.add(PushNotificationPayload.test(), token);
			queue.add(PushNotificationPayload.test(), token);
			Thread.sleep(10000);

			PushedNotifications notifications = queue.getPushedNotifications();
			NotificationTest.printPushedNotifications(notifications);

			Thread.sleep(5000);
			System.out.println("ISSUES #88 TESTED");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	private static void test_Issue99(String keystore, String password, String token, boolean production) {
		try {
			System.out.println("");
			System.out.println("TESTING ISSUE #99");
			PushNotificationPayload payload = PushNotificationPayload.complex();
			payload.addCustomAlertBody("Hello World!");
			payload.addCustomAlertActionLocKey(null);
			debugPayload(payload);

			List<PushedNotification> notifications = Push.payload(payload, keystore, password, production, token);
			NotificationTest.printPushedNotifications(notifications);
			System.out.println("ISSUE #99 TESTED");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	private static void test_Issue102(String keystore, String password, String token, boolean production) {
		try {
			System.out.println("");
			System.out.println("TESTING ISSUE #102");
			int devices = 10000;
			int threads = 20;
			boolean simulation = false;
			String realToken = token;
			token = null;

			try {

				System.out.println("Creating PushNotificationManager and AppleNotificationServer");
				AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production);
				System.out.println("Creating payload (simulation mode)");
				//Payload payload = PushNotificationPayload.alert("Hello World!");
				Payload payload = PushNotificationPayload.test();

				System.out.println("Generating " + devices + " fake devices");
				List<Device> deviceList = new ArrayList<Device>(devices);
				for (int i = 0; i < devices; i++) {
					String tokenToUse = token;
					if (tokenToUse == null || tokenToUse.length() != 64) {
						tokenToUse = "123456789012345678901234567890123456789012345678901234567" + (1000000 + i);
					}
					deviceList.add(new BasicDevice(tokenToUse));
				}
				deviceList.add(new BasicDevice(realToken));
				System.out.println("Creating " + threads + " notification threads");
				NotificationThreads work = new NotificationThreads(server, simulation ? payload.asSimulationOnly() : payload, deviceList, threads);
				//work.setMaxNotificationsPerConnection(10000);
				//System.out.println("Linking notification work debugging listener");
				//work.setListener(DEBUGGING_PROGRESS_LISTENER);

				System.out.println("Starting all threads...");
				long timestamp1 = System.currentTimeMillis();
				work.start();
				System.out.println("All threads started, waiting for them...");
				work.waitForAllThreads();
				long timestamp2 = System.currentTimeMillis();
				System.out.println("All threads finished in " + (timestamp2 - timestamp1) + " milliseconds");

				NotificationTest.printPushedNotifications(work.getSuccessfulNotifications());

			} catch (Exception e) {
				e.printStackTrace();
			}

			//			List<PushedNotification> notifications = Push.payload(payload, keystore, password, production, token);
			//			NotificationTest.printPushedNotifications(notifications);
			System.out.println("ISSUE #102 TESTED");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	private static void test_ThreadPoolFeature(String keystore, String password, String token, boolean production) throws Exception {
		try {
			System.out.println("");
			System.out.println("TESTING THREAD POOL FEATURE");

			AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production);
			NotificationThreads pool = new NotificationThreads(server, 3).start();
			Device device = new BasicDevice(token);

			System.out.println("Thread pool started and waiting...");

			System.out.println("Sleeping 5 seconds before queuing payloads...");
			Thread.sleep(5 * 1000);

			for (int i = 1; i <= 4; i++) {
				Payload payload = PushNotificationPayload.alert("Test " + i);
				NotificationThread threadForPayload = (NotificationThread) pool.add(new PayloadPerDevice(payload, device));
				System.out.println("Queued payload " + i + " to " + threadForPayload.getThreadNumber());
				System.out.println("Sleeping 10 seconds before queuing another payload...");
				Thread.sleep(10 * 1000);
			}
			System.out.println("Sleeping 10 more seconds let threads enough times to push the latest payload...");
			Thread.sleep(10 * 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	private static void pushSpecificPayloadSize(String keystore, String password, String token, boolean production, boolean checkWhenAdding, int targetPayloadSize) throws CommunicationException, KeystoreException, JSONException {
		StringBuilder buf = new StringBuilder();
		for (int i = 0; i < targetPayloadSize - 20; i++)
			buf.append('x');

		String alertMessage = buf.toString();
		PushNotificationPayload payload = PushNotificationPayload.complex();
		if (checkWhenAdding) payload.setPayloadSizeEstimatedWhenAdding(true);
		debugPayload(payload);

		boolean estimateValid = payload.isEstimatedPayloadSizeAllowedAfterAdding("alert", alertMessage);
		System.out.println("Payload size estimated to be allowed: " + (estimateValid ? "yes" : "no"));
		payload.addAlert(alertMessage);
		debugPayload(payload);

		List<PushedNotification> notifications = Push.payload(payload, keystore, password, production, token);
		NotificationTest.printPushedNotifications(notifications);
	}


	private static void debugPayload(Payload payload) {
		System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
		try {
			System.out.println("Payload size: " + payload.getPayloadSize());
		} catch (Exception e) {
		}
		try {
			System.out.println("Payload representation: " + payload);
		} catch (Exception e) {
		}
		System.out.println(payload.isPayloadSizeEstimatedWhenAdding() ? "Payload size is estimated when adding properties" : "Payload size is only checked when it is complete");
		System.out.println("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
	}

}

 

 

 

 

你可能感兴趣的:(javap)