701N/A/*
701N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
701N/A *
701N/A * Copyright (c) 2009-2015 Oracle and/or its affiliates. All rights reserved.
701N/A *
701N/A * The contents of this file are subject to the terms of either the GNU
701N/A * General Public License Version 2 only ("GPL") or the Common Development
701N/A * and Distribution License("CDDL") (collectively, the "License"). You
701N/A * may not use this file except in compliance with the License. You can
701N/A * obtain a copy of the License at
701N/A * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
701N/A * or packager/legal/LICENSE.txt. See the License for the specific
701N/A * language governing permissions and limitations under the License.
701N/A *
701N/A * When distributing the software, include this License Header Notice in each
701N/A * file and include the License file at packager/legal/LICENSE.txt.
701N/A *
701N/A * GPL Classpath Exception:
701N/A * Oracle designates this particular file as subject to the "Classpath"
701N/A * exception as provided by Oracle in the GPL Version 2 section of the License
701N/A * file that accompanied this code.
701N/A *
701N/A * Modifications:
701N/A * If applicable, add the following below the License Header, with the fields
701N/A * enclosed by brackets [] replaced by your own identifying information:
701N/A * "Portions Copyright [year] [name of copyright owner]"
701N/A *
701N/A * Contributor(s):
701N/A * If you wish your version of this file to be governed by only the CDDL or
701N/A * only the GPL Version 2, indicate your decision by adding "[Contributor]
701N/A * elects to include this software in this distribution under the [CDDL or GPL
701N/A * Version 2] license." If you don't indicate a single choice of license, a
701N/A * recipient has the option to distribute your version of this file under
701N/A * either the CDDL, the GPL Version 2 or to extend the choice of license to
701N/A * its licensees as provided above. However, if you add GPL Version 2 code
701N/A * and therefore, elected the GPL Version 2 license, then the option applies
701N/A * only if the new code is made subject to such option by the copyright
701N/A * holder.
701N/A */
701N/A
701N/Apackage com.sun.mail.imap;
701N/A
701N/Aimport java.io.IOException;
701N/Aimport java.util.Properties;
701N/A
701N/Aimport javax.mail.Folder;
701N/Aimport javax.mail.Session;
701N/Aimport javax.mail.Store;
701N/Aimport javax.mail.Message;
701N/Aimport javax.mail.Flags;
701N/Aimport javax.mail.search.FlagTerm;
701N/A
701N/Aimport com.sun.mail.test.TestServer;
701N/A
701N/Aimport org.junit.Test;
701N/Aimport org.junit.Rule;
701N/Aimport org.junit.rules.Timeout;
701N/Aimport static org.junit.Assert.assertEquals;
701N/Aimport static org.junit.Assert.fail;
701N/A
701N/A/**
701N/A * Test FETCH and SEARCH responses with a message number that's out of range.
701N/A */
701N/Apublic final class IMAPMessageNumberOutOfRangeTest {
701N/A
701N/A // timeout the test in case of deadlock
701N/A @Rule
785N/A public Timeout deadlockTimeout = Timeout.seconds(20);
701N/A
701N/A @Test
701N/A public void test() {
701N/A TestServer server = null;
701N/A try {
701N/A final IMAPHandlerBad handler = new IMAPHandlerBad();
701N/A server = new TestServer(handler);
701N/A server.start();
701N/A
701N/A final Properties properties = new Properties();
701N/A properties.setProperty("mail.imap.host", "localhost");
701N/A properties.setProperty("mail.imap.port", "" + server.getPort());
701N/A final Session session = Session.getInstance(properties);
701N/A //session.setDebug(true);
701N/A
701N/A final Store store = session.getStore("imap");
701N/A Folder folder = null;
701N/A try {
701N/A store.connect("test", "test");
701N/A folder = store.getFolder("INBOX");
701N/A folder.open(Folder.READ_ONLY);
701N/A Message msg = folder.getMessage(1);
701N/A Flags f = msg.getFlags();
701N/A Message[] msgs = folder.search(
701N/A new FlagTerm(new Flags(Flags.Flag.RECENT), true));
701N/A assertEquals(1, msgs.length);
701N/A assertEquals(msg, msgs[0]);
701N/A } catch (Exception ex) {
701N/A System.out.println(ex);
701N/A //ex.printStackTrace();
701N/A fail(ex.toString());
701N/A } finally {
701N/A if (folder != null)
701N/A folder.close(false);
701N/A store.close();
701N/A }
701N/A } catch (final Exception e) {
701N/A e.printStackTrace();
701N/A fail(e.getMessage());
701N/A } finally {
701N/A if (server != null) {
701N/A server.quit();
701N/A }
701N/A }
701N/A }
701N/A
701N/A /**
701N/A * Custom handler. Returns responses for messages that don't
701N/A * exist in the folder.
701N/A */
701N/A private static final class IMAPHandlerBad extends IMAPHandler {
701N/A
701N/A @Override
701N/A public void examine() throws IOException {
701N/A numberOfMessages = 1;
701N/A numberOfRecentMessages = 1;
701N/A super.examine();
701N/A }
701N/A
701N/A @Override
701N/A public void search(String line) throws IOException {
701N/A untagged("SEARCH 1 2");
701N/A ok();
701N/A }
701N/A
701N/A @Override
701N/A public void fetch(String line) throws IOException {
701N/A untagged("1 FETCH (FLAGS (\\Recent))");
701N/A untagged("2 FETCH (FLAGS (\\Deleted))");
701N/A ok();
701N/A }
701N/A }
701N/A}