Search in sources :

Example 1 with DbResults

use of com.zimbra.cs.db.DbResults in project zm-mailbox by Zimbra.

the class FolderTest method testEmptyFolderRecursive.

/**
 * Confirms that emptying a folder removes subfolders only when requested.
 */
@Test
public void testEmptyFolderRecursive() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Folder parent = mbox.createFolder(null, "/" + "parent", new Folder.FolderOptions());
    int parentId = parent.getId();
    Folder child = mbox.createFolder(null, "child", parent.getId(), new Folder.FolderOptions());
    int childId = child.getId();
    mbox.emptyFolder(null, parent.getId(), true);
    // Look up parent by id
    mbox.getFolderById(null, parentId);
    // Look up parent by query
    String sql = "SELECT id " + "FROM " + DbMailItem.getMailItemTableName(mbox) + " WHERE mailbox_id = " + mbox.getId() + " AND id = " + parentId;
    DbResults results = DbUtil.executeQuery(sql);
    Assert.assertEquals("Parent folder query returned no data.  id=" + parentId, 1, results.size());
    // Look up child by id
    try {
        mbox.getFolderById(null, childId);
        Assert.fail("Child folder lookup by id should have not succeeded");
    } catch (NoSuchItemException e) {
    }
    // Look up child by query
    sql = "SELECT id " + "FROM " + DbMailItem.getMailItemTableName(mbox) + " WHERE mailbox_id = " + mbox.getId() + " AND id = " + childId;
    results = DbUtil.executeQuery(sql);
    Assert.assertEquals("Child folder query returned data.  id=" + childId, 0, results.size());
}
Also used : DbResults(com.zimbra.cs.db.DbResults) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) Test(org.junit.Test)

Example 2 with DbResults

use of com.zimbra.cs.db.DbResults in project zm-mailbox by Zimbra.

the class TestMailItem method testListItemIds.

@Test
public void testListItemIds() throws Exception {
    Account account = TestUtil.createAccount(USER_NAME);
    // Mailbox.ID_FOLDER_INBOX;
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    TestUtil.addMessage(mbox, testInfo.getMethodName() + "missive 1");
    TestUtil.addMessage(mbox, testInfo.getMethodName() + "missive 2");
    TestUtil.addMessage(mbox, testInfo.getMethodName() + "missive 3");
    TestUtil.addMessage(mbox, Mailbox.ID_FOLDER_DRAFTS, testInfo.getMethodName() + "draft 1", System.currentTimeMillis());
    TestUtil.addMessage(mbox, Mailbox.ID_FOLDER_DRAFTS, testInfo.getMethodName() + "draft 2", System.currentTimeMillis());
    // Get item count per folder/type
    String sql = "SELECT folder_id, type, count(*) AS item_count " + "FROM " + DbMailItem.getMailItemTableName(mbox) + " WHERE mailbox_id = " + mbox.getId() + " GROUP BY folder_id, type";
    DbResults results = DbUtil.executeQuery(sql);
    Assert.assertTrue("No results returned", results.size() > 0);
    boolean foundInDrafts = false;
    boolean foundInInbox = false;
    // Confirm that listItemIds() returns the right count for each folder/type
    while (results.next()) {
        int folderId = results.getInt("folder_id");
        MailItem.Type type = MailItem.Type.of((byte) results.getInt("type"));
        // XXX bburtin: Work around incompatibility between JDBC driver version
        // 5.0.3 and MySQL 5.0.67, where the column name returned for an alias
        // is an empty string.
        // int count = results.getInt("item_count");
        int count = results.getInt(3);
        ZimbraLog.test.debug("Confirming that folder '%s' has %s items of type %s", folderId, count, type);
        Folder folder = mbox.getFolderById(null, folderId);
        Assert.assertNotNull(String.format("Folder with ID='%s' not found", folderId), folder);
        List<Integer> ids = mbox.listItemIds(null, type, folderId);
        Assert.assertEquals(String.format("Item count does not match for Folder with ID='%s'", folderId), count, ids.size());
        if (Mailbox.ID_FOLDER_INBOX == folderId) {
            foundInInbox = true;
            Assert.assertEquals("Item count does not match for INBOX", 3, count);
        }
        if (Mailbox.ID_FOLDER_DRAFTS == folderId) {
            foundInDrafts = true;
            Assert.assertEquals("Item count does not match for DRAFTS", 2, count);
        }
    }
    Assert.assertTrue("No items reported for INBOX", foundInInbox);
    Assert.assertTrue("No items reported for DRAFTS", foundInDrafts);
}
Also used : Account(com.zimbra.cs.account.Account) MailItem(com.zimbra.cs.mailbox.MailItem) DbMailItem(com.zimbra.cs.db.DbMailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) DbResults(com.zimbra.cs.db.DbResults) Folder(com.zimbra.cs.mailbox.Folder) Test(org.junit.Test)

Example 3 with DbResults

use of com.zimbra.cs.db.DbResults in project zm-mailbox by Zimbra.

the class TestUnread method verifyUnreadFlag.

private void verifyUnreadFlag(MailItem item) throws Exception {
    String flagString = item.getFlagString();
    if (item.isUnread()) {
        Assert.assertTrue("unread bit test: " + item.getFlagBitmask(), (item.getFlagBitmask() & Flag.BITMASK_UNREAD) > 0);
        Assert.assertTrue("unread flag string: " + flagString, flagString.indexOf(Flag.toChar(Flag.ID_UNREAD)) >= 0);
    } else {
        Assert.assertTrue("read bit test: " + item.getFlagBitmask(), (item.getFlagBitmask() & Flag.BITMASK_UNREAD) == 0);
        Assert.assertTrue("read flag string: " + flagString, flagString.indexOf(Flag.toChar(Flag.ID_UNREAD)) == -1);
    }
    // if (item.getType() == MailItem.TYPE_MESSAGE || item.getType() == MailItem.TYPE_INVITE) {
    if (item.getType() == MailItem.Type.MESSAGE) {
        DbResults results = DbUtil.executeQuery("SELECT unread " + "FROM " + DbMailItem.getMailItemTableName(item) + " WHERE mailbox_id = " + item.getMailboxId() + " AND id = " + item.getId());
        Assert.assertEquals("Verify unread flag in the database", item.isUnread(), results.getBoolean(1));
    }
}
Also used : DbResults(com.zimbra.cs.db.DbResults)

Example 4 with DbResults

use of com.zimbra.cs.db.DbResults in project zm-mailbox by Zimbra.

the class TestScheduledTaskManager method checkNumPersistedTasks.

private void checkNumPersistedTasks(int expected) throws Exception {
    DbResults results = DbUtil.executeQuery("SELECT COUNT(*) FROM " + DbScheduledTask.TABLE_SCHEDULED_TASK + " WHERE class_name = '" + TestTask.class.getName() + "'");
    assertEquals("Unexpected number of persisted tasks", expected, results.getInt(1));
}
Also used : DbResults(com.zimbra.cs.db.DbResults)

Example 5 with DbResults

use of com.zimbra.cs.db.DbResults in project zm-mailbox by Zimbra.

the class TestAccount method testDeleteAccount.

public void testDeleteAccount() throws Exception {
    ZimbraLog.test.debug("testDeleteAccount()");
    // Get the account and mailbox
    Account account = TestUtil.getAccount(USER_NAME);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    String dbName = DbMailbox.getDatabaseName(mbox);
    ZimbraLog.test.debug("Account=" + account.getId() + ", mbox=" + mbox.getId());
    // Confirm that the mailbox database exists
    DbResults results = DbUtil.executeQuery("SELECT COUNT(*) FROM mailbox WHERE id = " + mbox.getId());
    assertEquals("Could not find row in mailbox table", 1, results.getInt(1));
    results = DbUtil.executeQuery("SHOW DATABASES LIKE '" + dbName + "'");
    assertEquals("Could not find mailbox database", 1, results.size());
    // Add a message to the account and confirm that the message directory exists
    TestUtil.addMessage(mbox, "TestAccount testDeleteAccount");
    String storePath = VolumeManager.getInstance().getCurrentMessageVolume().getMessageRootDir(mbox.getId());
    File storeDir = new File(storePath);
    if (TestUtil.checkLocalBlobs()) {
        assertTrue(storePath + " does not exist", storeDir.exists());
        assertTrue(storePath + " is not a directory", storeDir.isDirectory());
    }
    // Delete the account
    LmcSession session = TestUtil.getAdminSoapSession();
    LmcDeleteAccountRequest req = new LmcDeleteAccountRequest(account.getId());
    req.setSession(session);
    req.invoke(TestUtil.getAdminSoapUrl());
    // Confirm that the mailbox was deleted
    results = DbUtil.executeQuery("SELECT COUNT(*) FROM mailbox WHERE id = " + mbox.getId());
    assertEquals("Unexpected row in mailbox table", 0, results.getInt(1));
    if (TestUtil.checkLocalBlobs()) {
        // Confirm that the message directory was deleted
        assertFalse(storePath + " exists", storeDir.exists());
    }
}
Also used : Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) DbMailbox(com.zimbra.cs.db.DbMailbox) LmcSession(com.zimbra.cs.client.LmcSession) DbResults(com.zimbra.cs.db.DbResults) File(java.io.File) LmcDeleteAccountRequest(com.zimbra.cs.client.soap.LmcDeleteAccountRequest)

Aggregations

DbResults (com.zimbra.cs.db.DbResults)9 Test (org.junit.Test)5 Account (com.zimbra.cs.account.Account)3 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)2 Mailbox (com.zimbra.cs.mailbox.Mailbox)2 LmcSession (com.zimbra.cs.client.LmcSession)1 LmcDeleteAccountRequest (com.zimbra.cs.client.soap.LmcDeleteAccountRequest)1 DbMailItem (com.zimbra.cs.db.DbMailItem)1 DbMailbox (com.zimbra.cs.db.DbMailbox)1 Folder (com.zimbra.cs.mailbox.Folder)1 MailItem (com.zimbra.cs.mailbox.MailItem)1 ParsedContact (com.zimbra.cs.mime.ParsedContact)1 File (java.io.File)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1