86N/A/*
86N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
86N/A *
292N/A * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
86N/A *
86N/A * The contents of this file are subject to the terms of either the GNU
86N/A * General Public License Version 2 only ("GPL") or the Common Development
86N/A * and Distribution License("CDDL") (collectively, the "License"). You
292N/A * may not use this file except in compliance with the License. You can
292N/A * obtain a copy of the License at
292N/A * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
292N/A * or packager/legal/LICENSE.txt. See the License for the specific
86N/A * language governing permissions and limitations under the License.
86N/A *
86N/A * When distributing the software, include this License Header Notice in each
292N/A * file and include the License file at packager/legal/LICENSE.txt.
292N/A *
292N/A * GPL Classpath Exception:
292N/A * Oracle designates this particular file as subject to the "Classpath"
292N/A * exception as provided by Oracle in the GPL Version 2 section of the License
292N/A * file that accompanied this code.
292N/A *
292N/A * Modifications:
292N/A * If applicable, add the following below the License Header, with the fields
292N/A * enclosed by brackets [] replaced by your own identifying information:
292N/A * "Portions Copyright [year] [name of copyright owner]"
86N/A *
86N/A * Contributor(s):
86N/A * If you wish your version of this file to be governed by only the CDDL or
86N/A * only the GPL Version 2, indicate your decision by adding "[Contributor]
86N/A * elects to include this software in this distribution under the [CDDL or GPL
86N/A * Version 2] license." If you don't indicate a single choice of license, a
86N/A * recipient has the option to distribute your version of this file under
86N/A * either the CDDL, the GPL Version 2 or to extend the choice of license to
86N/A * its licensees as provided above. However, if you add GPL Version 2 code
86N/A * and therefore, elected the GPL Version 2 license, then the option applies
86N/A * only if the new code is made subject to such option by the copyright
86N/A * holder.
86N/A */
86N/A
86N/Aimport java.util.*;
86N/Aimport java.io.*;
86N/Aimport javax.mail.*;
86N/Aimport javax.mail.event.*;
86N/Aimport javax.mail.internet.*;
86N/Aimport javax.activation.*;
86N/A
86N/A/*
86N/A * Test IMAP message cache.
86N/A *
86N/A * @author Bill Shannon
86N/A */
86N/A
86N/Apublic class messagecachetest {
86N/A
86N/A static String protocol;
86N/A static String host = null;
86N/A static String user = null;
86N/A static String password = null;
86N/A static String mbox = null;
86N/A static String url = null;
86N/A static int port = -1;
86N/A static boolean verbose = false;
86N/A static boolean debug = false;
86N/A static Session session;
86N/A
86N/A public static void main(String argv[]) {
86N/A int nummsg = 256;
86N/A int optind;
86N/A
86N/A for (optind = 0; optind < argv.length; optind++) {
86N/A if (argv[optind].equals("-T")) {
86N/A protocol = argv[++optind];
86N/A } else if (argv[optind].equals("-H")) {
86N/A host = argv[++optind];
86N/A } else if (argv[optind].equals("-U")) {
86N/A user = argv[++optind];
86N/A } else if (argv[optind].equals("-P")) {
86N/A password = argv[++optind];
86N/A } else if (argv[optind].equals("-v")) {
86N/A verbose = true;
86N/A } else if (argv[optind].equals("-D")) {
86N/A debug = true;
86N/A } else if (argv[optind].equals("-f")) {
86N/A mbox = argv[++optind];
86N/A } else if (argv[optind].equals("-L")) {
86N/A url = argv[++optind];
86N/A } else if (argv[optind].equals("-p")) {
86N/A port = Integer.parseInt(argv[++optind]);
86N/A } else if (argv[optind].equals("--")) {
86N/A optind++;
86N/A break;
86N/A } else if (argv[optind].startsWith("-")) {
86N/A System.out.println(
86N/A"Usage: messagecachetest [-L url] [-T protocol] [-H host] [-p port] [-U user]");
86N/A System.out.println(
86N/A"\t[-P password] [-f mailbox] [msgnum] [-v] [-D]");
86N/A System.exit(1);
86N/A } else {
86N/A break;
86N/A }
86N/A }
86N/A
86N/A try {
86N/A if (optind < argv.length)
86N/A nummsg = Integer.parseInt(argv[optind]);
86N/A
86N/A // Get a Properties object
86N/A Properties props = System.getProperties();
86N/A
86N/A // Get a Session object
86N/A session = Session.getInstance(props, null);
86N/A session.setDebug(debug);
86N/A
86N/A // Get a Store object
86N/A Store store = null;
86N/A if (url != null) {
86N/A URLName urln = new URLName(url);
86N/A store = session.getStore(urln);
86N/A store.connect();
86N/A } else {
86N/A if (protocol != null)
86N/A store = session.getStore(protocol);
86N/A else
86N/A store = session.getStore();
86N/A
86N/A // Connect
86N/A if (host != null || user != null || password != null)
86N/A store.connect(host, port, user, password);
86N/A else
86N/A store.connect();
86N/A }
86N/A
86N/A
86N/A // Open the Folder
86N/A
86N/A Folder folder = store.getDefaultFolder();
86N/A if (folder == null) {
86N/A System.out.println("No default folder");
86N/A System.exit(1);
86N/A }
86N/A
86N/A if (mbox == null)
86N/A mbox = "messagecachetest";
86N/A folder = folder.getFolder(mbox);
86N/A if (folder == null) {
86N/A System.out.println("Invalid folder");
86N/A System.exit(1);
86N/A }
86N/A
86N/A Message[] msgs = createMessages(nummsg);
86N/A if (folder.exists())
86N/A folder.delete(false);
86N/A folder.create(Folder.HOLDS_MESSAGES);
86N/A
86N/A folder.open(Folder.READ_WRITE);
86N/A if (verbose)
86N/A System.out.println("fill folder");
86N/A folder.appendMessages(msgs);
86N/A folder.close(false);
86N/A
86N/A folder.open(Folder.READ_WRITE);
86N/A if (verbose)
86N/A System.out.println("test message number");
86N/A testMessageNumber(folder);
86N/A folder.close(false);
86N/A folder.open(Folder.READ_WRITE);
86N/A if (verbose)
86N/A System.out.println("test expunge forward");
86N/A testExpungeForward(folder);
86N/A folder.close(false);
86N/A
86N/A folder.open(Folder.READ_WRITE);
86N/A folder.appendMessages(msgs);
86N/A folder.close(false);
86N/A folder.open(Folder.READ_WRITE);
86N/A if (verbose)
86N/A System.out.println("test expunge reverse");
86N/A testExpungeReverse(folder);
86N/A folder.close(false);
86N/A
86N/A folder.open(Folder.READ_WRITE);
86N/A folder.appendMessages(msgs);
86N/A folder.close(false);
86N/A folder.open(Folder.READ_WRITE);
86N/A if (verbose)
86N/A System.out.println("test expunge random");
86N/A testExpungeRandom(folder);
86N/A folder.close(false);
86N/A
86N/A folder.open(Folder.READ_WRITE);
86N/A folder.appendMessages(msgs);
86N/A folder.close(false);
86N/A folder.open(Folder.READ_WRITE);
86N/A if (verbose)
86N/A System.out.println("test expunge other");
86N/A testExpungeOther(folder);
86N/A folder.close(false);
86N/A store.close();
86N/A } catch (Exception ex) {
86N/A System.out.println("Oops, got exception! " + ex.getMessage());
86N/A ex.printStackTrace();
86N/A System.exit(1);
86N/A }
86N/A System.exit(0);
86N/A }
86N/A
86N/A private static Message[] createMessages(int num) throws MessagingException {
86N/A Message[] msgs = new Message[num];
86N/A for (int i = 1; i <= num; i++) {
86N/A MimeMessage msg = new MimeMessage(session);
86N/A msg.setSentDate(new Date());
86N/A msg.setFrom();
86N/A msg.setRecipients(Message.RecipientType.TO, "nobody@nowhere.com");
86N/A msg.setSubject(Integer.toString(i));
86N/A msg.setText(i + "\n");
86N/A msg.saveChanges();
86N/A msgs[i-1] = msg;
86N/A }
86N/A return msgs;
86N/A }
86N/A
86N/A private static void testMessageNumber(Folder folder)
86N/A throws MessagingException {
86N/A int nummsg = folder.getMessageCount();
86N/A Message msgs[] = new Message[nummsg];
86N/A for (int i = 1; i <= nummsg; i++) {
86N/A Message msg = folder.getMessage(i);
86N/A msgs[i-1] = msg;
86N/A if (Integer.valueOf(msg.getSubject()) != i) {
86N/A System.out.println("FAIL: Wrong message! Got " +
86N/A msg.getSubject() + ", Expected " + i);
86N/A }
86N/A }
86N/A for (int i = 1; i <= nummsg; i++) {
86N/A Message msg = folder.getMessage(i);
86N/A if (msgs[i-1] != msg || msg.getMessageNumber() != i) {
86N/A System.out.println("FAIL: Wrong message! Got " +
86N/A msg.getMessageNumber() + ", Expected " + i);
86N/A }
86N/A }
86N/A }
86N/A
86N/A private static void testExpungeForward(Folder folder)
86N/A throws MessagingException {
86N/A int nummsg = folder.getMessageCount();
86N/A for (int i = 1; i <= nummsg; i++) {
86N/A Message msg = folder.getMessage(1);
86N/A msg.setFlag(Flags.Flag.DELETED, true);
86N/A folder.expunge();
86N/A for (int j = 1; j < nummsg - i; j++) {
86N/A msg = folder.getMessage(j);
86N/A if (msg.getMessageNumber() != j) {
86N/A System.out.println("FAIL: Wrong message! Got " +
86N/A msg.getMessageNumber() + ", Expected " + j);
86N/A }
86N/A }
86N/A }
86N/A }
86N/A
86N/A private static void testExpungeReverse(Folder folder)
86N/A throws MessagingException {
86N/A int nummsg = folder.getMessageCount();
86N/A for (int i = nummsg; i >= 1; i--) {
86N/A Message msg = folder.getMessage(i);
86N/A msg.setFlag(Flags.Flag.DELETED, true);
86N/A folder.expunge();
86N/A for (int j = 1; j < i; j++) {
86N/A msg = folder.getMessage(j);
86N/A if (msg.getMessageNumber() != j) {
86N/A System.out.println("FAIL: Wrong message! Got " +
86N/A msg.getMessageNumber() + ", Expected " + j);
86N/A }
86N/A }
86N/A }
86N/A }
86N/A
86N/A private static void testExpungeRandom(Folder folder)
86N/A throws MessagingException {
86N/A int nummsg = folder.getMessageCount();
86N/A Random rnd = new Random(System.currentTimeMillis());
86N/A while (nummsg > 0) {
86N/A Message msg = folder.getMessage(rnd.nextInt(nummsg) + 1);
86N/A msg.setFlag(Flags.Flag.DELETED, true);
86N/A folder.expunge();
86N/A nummsg--;
86N/A for (int j = 1; j <= nummsg; j++) {
86N/A msg = folder.getMessage(j);
86N/A if (msg.getMessageNumber() != j) {
86N/A System.out.println("FAIL: Wrong message! Got " +
86N/A msg.getMessageNumber() + ", Expected " + j);
86N/A }
86N/A }
86N/A }
86N/A }
86N/A
86N/A private static void testExpungeOther(Folder folder)
86N/A throws MessagingException {
86N/A Folder other = folder.getStore().getFolder(folder.getFullName());
86N/A other.open(Folder.READ_WRITE);
86N/A Message msg = other.getMessage(2);
86N/A msg.setFlag(Flags.Flag.DELETED, true);
86N/A other.expunge();
86N/A System.out.println("waiting for expunge notification");
86N/A try { Thread.sleep(2000); } catch (InterruptedException ex) { }
86N/A folder.isOpen();
86N/A try {
86N/A msg = folder.getMessage(2);
86N/A msg.getFlags();
86N/A if (!msg.isExpunged()) {
86N/A System.out.println("FAIL: Message 2 is not expunged!");
86N/A }
86N/A } catch (MessageRemovedException mex) {
86N/A System.out.println("SUCCESS: message 2 is removed!");
86N/A }
86N/A msg = folder.getMessage(1);
86N/A if (Integer.valueOf(msg.getSubject()) != 1) {
86N/A System.out.println("FAIL: Message 1 is wrong!");
86N/A }
86N/A msg = folder.getMessage(3);
86N/A if (Integer.valueOf(msg.getSubject()) != 3) {
86N/A System.out.println("FAIL: Message 3 is wrong!");
86N/A }
86N/A other.close(false);
86N/A }
86N/A}