Skip to content

Commit

Permalink
MAILBOX-261 Message now have a unique MessageId
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/james/project/trunk@1724222 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mbaechler committed Jan 12, 2016
1 parent 697853b commit ba37cbd
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 49 deletions.
5 changes: 5 additions & 0 deletions mailbox/pom.xml
Expand Up @@ -449,6 +449,11 @@
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>1.7.6</version>
</dependency>
<!--
END Testing
-->
Expand Down
5 changes: 5 additions & 0 deletions mailbox/store/pom.xml
Expand Up @@ -106,6 +106,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock</artifactId>
Expand Down
Expand Up @@ -118,6 +118,9 @@ public InputStream getFullContent() throws IOException {
return new SequenceInputStream(getHeaderContent(), getBodyContent());
}


@Override
public DefaultMessageId getMessageId() {
return new DefaultMessageId(getMailboxId(), getUid());
}

}
@@ -0,0 +1,55 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you 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 org.apache.james.mailbox.store.mail.model;

import com.google.common.base.Objects;
import com.google.common.base.Preconditions;

public class DefaultMessageId implements MessageId {

private final MailboxId mailboxId;
private final long messageUid;

public DefaultMessageId(MailboxId mailboxId, long messageUid) {
Preconditions.checkNotNull(mailboxId);
this.mailboxId = mailboxId;
this.messageUid = messageUid;
}

@Override
public String serialize() {
return String.format("%s-%d", mailboxId.serialize(), messageUid);
}

@Override
public final boolean equals(Object obj) {
if (obj instanceof DefaultMessageId) {
DefaultMessageId other = (DefaultMessageId) obj;
return Objects.equal(mailboxId, other.mailboxId) &&
Objects.equal(messageUid, other.messageUid);

}
return false;
}

@Override
public final int hashCode() {
return Objects.hashCode(mailboxId, messageUid);
}
}
Expand Up @@ -32,6 +32,8 @@
*/
public interface Message<Id extends MailboxId> extends Comparable<Message<Id>>{

MessageId getMessageId();

Date getInternalDate();

/**
Expand Down
@@ -0,0 +1,23 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you 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 org.apache.james.mailbox.store.mail.model;

public interface MessageId {
String serialize();
}
Expand Up @@ -35,6 +35,7 @@

import javax.mail.Flags;

import org.apache.james.mailbox.store.mail.model.DefaultMessageId;
import org.apache.james.mailbox.store.mail.model.Message;
import org.apache.james.mailbox.store.mail.model.Property;

Expand Down Expand Up @@ -71,79 +72,46 @@ public SimpleMailboxMembership(TestId mailboxId, long uid, long modSeq, Date int
setFlags(flags);
}

/**
* @see org.apache.james.imap.Message.mail.model.Document#getInternalDate()
*/
public Date getInternalDate() {
return internalDate;
}

/**
* @see org.apache.james.imap.Message.mail.model.Document#getMailboxId()
*/
public TestId getMailboxId() {
return mailboxId;
}

/**
* @see org.apache.james.imap.Message.mail.model.Document#getUid()
*/
public long getUid() {
return uid;
}

/**
* @see org.apache.james.imap.Message.mail.model.Document#isAnswered()
*/
public boolean isAnswered() {
return answered;
}

/**
* @see org.apache.james.imap.Message.mail.model.Document#isDeleted()
*/
public boolean isDeleted() {
return deleted;
}

/**
* @see org.apache.james.imap.Message.mail.model.Document#isDraft()
*/
public boolean isDraft() {
return draft;
}

/**
* @see org.apache.james.imap.Message.mail.model.Document#isFlagged()
*/
public boolean isFlagged() {
return flagged;
}

/**
* @see org.apache.james.imap.Message.mail.model.Document#isRecent()
*/
public boolean isRecent() {
return recent;
}

/**
* @see org.apache.james.imap.Message.mail.model.Document#isSeen()
*/
public boolean isSeen() {
return seen;
}

/**
* @see org.apache.james.imap.Message.mail.model.Document#unsetRecent()
*/
public void unsetRecent() {
recent = false;
}

/**
* @see org.apache.james.imap.Message.mail.model.Document#setFlags(javax.mail.Flags)
*/
public void setFlags(Flags flags) {
answered = flags.contains(Flags.Flag.ANSWERED);
deleted = flags.contains(Flags.Flag.DELETED);
Expand All @@ -153,9 +121,6 @@ public void setFlags(Flags flags) {
seen = flags.contains(Flags.Flag.SEEN);
}

/**
* @see org.apache.james.imap.Message.mail.model.Document#createFlags()
*/
public Flags createFlags() {
final Flags flags = new Flags();

Expand Down Expand Up @@ -240,17 +205,10 @@ public String toString()
private long modSeq;


/**
* @throws IOException
* @see org.apache.james.imap.Message.mail.model.Document#getBodyContent()
*/
public InputStream getBodyContent() throws IOException {
return new ByteArrayInputStream(body);
}

/**
* @see org.apache.james.mailbox.store.mail.model.Message#getHeaderContent()
*/
public InputStream getHeaderContent() throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Writer writer = new OutputStreamWriter(baos, "us-ascii");
Expand Down Expand Up @@ -293,9 +251,6 @@ public long getFullContentOctets() {
return size;
}

/**
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(Message<TestId> other) {
return (int) (getUid() - other.getUid());
}
Expand All @@ -316,7 +271,10 @@ public void setUid(long uid) {
public InputStream getFullContent() throws IOException {
return new SequenceInputStream(getHeaderContent(), getBodyContent());
}



@Override
public DefaultMessageId getMessageId() {
return new DefaultMessageId(getMailboxId(), getUid());
}

}
@@ -0,0 +1,46 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you 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 org.apache.james.mailbox.store.mail.model;

import static org.assertj.core.api.Assertions.assertThat;

import org.apache.james.mailbox.store.TestId;
import org.junit.Test;

import nl.jqno.equalsverifier.EqualsVerifier;

public class DefaultMessageIdTest {

@Test(expected=NullPointerException.class)
public void constructorShouldThrowWhenNullMailboxId() {
new DefaultMessageId(null, 1);
}

@Test
public void serializeShouldFormatMailboxIdAndUid() {
DefaultMessageId id = new DefaultMessageId(TestId.of(12l), 1);
assertThat(id.serialize()).isEqualTo("12-1");
}

@Test
public void shouldRespectJavaBeanContract() {
EqualsVerifier.forClass(DefaultMessageId.class).verify();
}

}

0 comments on commit ba37cbd

Please sign in to comment.