Added: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageAssert.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageAssert.java?rev=1682388&view=auto ============================================================================== --- james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageAssert.java (added) +++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageAssert.java Fri May 29 07:56:38 2015 @@ -0,0 +1,108 @@ +/**************************************************************** + * 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 org.apache.commons.io.IOUtils; +import org.apache.james.mailbox.store.mail.MessageMapper; +import org.assertj.core.api.AbstractAssert; + +import javax.mail.Flags; +import java.io.IOException; + +public class MessageAssert extends AbstractAssert<MessageAssert, Message<?>> { + + public MessageAssert(Message<?> actual) { + super(actual, MessageAssert.class); + } + + public static MessageAssert assertThat(Message<?> actual) { + return new MessageAssert(actual); + } + + public MessageAssert isEqualTo(Message<?> expected, MessageMapper.FetchType usedFetchType) throws IOException { + isNotNull(); + if (!equals(actual.getMailboxId(), expected.getMailboxId())) { + failWithMessage("Expected Mailbox ID to be <%s> but was <%s>", expected.getMailboxId().toString(), actual.getMailboxId().toString()); + } + if (!equals(actual.getUid(), expected.getUid())) { + failWithMessage("Expected UID to be <%s> but was <%s>", expected.getUid(), actual.getUid()); + } + if (!equals(actual.getInternalDate(), expected.getInternalDate())) { + failWithMessage("Expected Internal Date to be <%s> but was <%s>", expected.getInternalDate(), actual.getInternalDate()); + } + if (!equals(actual.getBodyOctets(), expected.getBodyOctets())) { + failWithMessage("Expected Body octet to be <%s> but was <%s>", expected.getBodyOctets(), actual.getBodyOctets()); + } + if (!equals(actual.getMediaType(), expected.getMediaType())) { + failWithMessage("Expected Media type to be <%s> but was <%s>", expected.getBodyOctets(), actual.getBodyOctets()); + } + if (!equals(actual.getSubType(), expected.getSubType())) { + failWithMessage("Expected Sub type to be <%s> but was <%s>", expected.getBodyOctets(), actual.getBodyOctets()); + } + if (usedFetchType == MessageMapper.FetchType.Full) { + if (!equals(actual.getFullContentOctets(), expected.getFullContentOctets())) { + failWithMessage("Expected Message size to be <%s> but was <%s>", expected.getFullContentOctets(), actual.getFullContentOctets()); + } + if (!equals(IOUtils.toString(actual.getFullContent()), IOUtils.toString(expected.getFullContent()))) { + failWithMessage("Expected Full content to be <%s> but was <%s>", IOUtils.toString(actual.getFullContent()), IOUtils.toString(expected.getFullContent())); + } + } + if (usedFetchType == MessageMapper.FetchType.Full || usedFetchType == MessageMapper.FetchType.Headers) { + if (!equals(IOUtils.toString(actual.getHeaderContent()), IOUtils.toString(expected.getHeaderContent()))) { + failWithMessage("Expected Header content to be <%s> but was <%s>", IOUtils.toString(actual.getHeaderContent()), IOUtils.toString(expected.getHeaderContent())); + } + } + if (usedFetchType == MessageMapper.FetchType.Full || usedFetchType == MessageMapper.FetchType.Body) { + if (!equals(IOUtils.toString(actual.getBodyContent()), IOUtils.toString(expected.getBodyContent()))) { + failWithMessage("Expected Body content to be <%s> but was <%s>", IOUtils.toString(actual.getBodyContent()), IOUtils.toString(expected.getBodyContent())); + } + } + return this; + } + + public MessageAssert hasFlags(Flags flags) { + if (flags.contains(Flags.Flag.ANSWERED) != actual.isAnswered()) { + failWithMessage("Expected ANSWERED flag to be <%s> but was <%>", flags.contains(Flags.Flag.ANSWERED), actual.isAnswered()); + } + if (flags.contains(Flags.Flag.DELETED) != actual.isDeleted()) { + failWithMessage("Expected DELETED flag to be <%s> but was <%>", flags.contains(Flags.Flag.DELETED), actual.isDeleted()); + } + if (flags.contains(Flags.Flag.DRAFT) != actual.isDraft()) { + failWithMessage("Expected DRAFT flag to be <%s> but was <%>", flags.contains(Flags.Flag.DRAFT), actual.isDraft()); + } + if (flags.contains(Flags.Flag.FLAGGED) != actual.isFlagged()) { + failWithMessage("Expected FLAGGED flag to be <%s> but was <%>", flags.contains(Flags.Flag.FLAGGED), actual.isFlagged()); + } + if (flags.contains(Flags.Flag.SEEN) != actual.isSeen()) { + failWithMessage("Expected SEEN flag to be <%s> but was <%>", flags.contains(Flags.Flag.SEEN), actual.isSeen()); + } + if (flags.contains(Flags.Flag.RECENT) != actual.isRecent()) { + failWithMessage("Expected RECENT flag to be <%s> but was <%>", flags.contains(Flags.Flag.RECENT), actual.isRecent()); + } + return this; + } + + private boolean equals(Object object1, Object object2) { + if ( object1 == null && object2 == null ) { + return true; + } + return ( object1 != null ) && object1.equals(object2); + } +} \ No newline at end of file
Added: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageAssertTest.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageAssertTest.java?rev=1682388&view=auto ============================================================================== --- james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageAssertTest.java (added) +++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageAssertTest.java Fri May 29 07:56:38 2015 @@ -0,0 +1,81 @@ +/**************************************************************** + * 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 org.apache.james.mailbox.store.mail.MessageMapper; +import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder; +import org.apache.james.mailbox.store.mail.model.impl.SimpleMessage; +import org.junit.Test; + +import javax.mail.Flags; +import javax.mail.util.SharedByteArrayInputStream; +import java.io.IOException; +import java.util.Date; + +public class MessageAssertTest { + + public static final long MAILBOX_ID = 42L; + public static final long UID = 24L; + + @Test + public void messageAssertShouldSucceedWithTwoEqualsMessages() throws IOException { + String headerString = "name: headerName\n\n"; + String bodyString = "body\n.\n"; + Date date = new Date(); + SimpleMessage<Long> message1 = new SimpleMessage<Long>(date, headerString.length() + bodyString.length(), + headerString.length(), new SharedByteArrayInputStream((headerString + bodyString).getBytes()), new Flags(), new PropertyBuilder(), MAILBOX_ID); + message1.setUid(UID); + SimpleMessage<Long> message2 = new SimpleMessage<Long>(date, headerString.length() + bodyString.length(), + headerString.length(), new SharedByteArrayInputStream((headerString + bodyString).getBytes()), new Flags(), new PropertyBuilder(), MAILBOX_ID); + message2.setUid(UID); + MessageAssert.assertThat(message1).isEqualTo(message2, MessageMapper.FetchType.Full); + } + + @Test + public void messageAssertShouldSucceedWhenBodyMismatchInFetchHeaderMode() throws IOException { + String headerString = "name: headerName\n\n"; + String bodyString = "body\n.\n"; + Date date = new Date(); + SimpleMessage<Long> message1 = new SimpleMessage<Long>(date, headerString.length() + bodyString.length(), + headerString.length(), new SharedByteArrayInputStream((headerString + bodyString).getBytes()), new Flags(), new PropertyBuilder(), MAILBOX_ID); + message1.setUid(UID); + bodyString = "work\n.\n"; + SimpleMessage<Long> message2 = new SimpleMessage<Long>(date, headerString.length() + bodyString.length(), + headerString.length(), new SharedByteArrayInputStream((headerString + bodyString).getBytes()), new Flags(), new PropertyBuilder(), MAILBOX_ID); + message2.setUid(UID); + MessageAssert.assertThat(message1).isEqualTo(message2, MessageMapper.FetchType.Headers); + } + + @Test(expected = AssertionError.class) + public void messageAssertShouldFailWhenBodyMismatchInFetchBodyMode() throws IOException { + String headerString = "name: headerName\n\n"; + String bodyString = "body\n.\n"; + Date date = new Date(); + SimpleMessage<Long> message1 = new SimpleMessage<Long>(date, headerString.length() + bodyString.length(), + headerString.length(), new SharedByteArrayInputStream((headerString + bodyString).getBytes()), new Flags(), new PropertyBuilder(), MAILBOX_ID); + message1.setUid(UID); + bodyString = "work\n.\n"; + SimpleMessage<Long> message2 = new SimpleMessage<Long>(date, headerString.length() + bodyString.length(), + headerString.length(), new SharedByteArrayInputStream((headerString + bodyString).getBytes()), new Flags(), new PropertyBuilder(), MAILBOX_ID); + message2.setUid(UID); + MessageAssert.assertThat(message1).isEqualTo(message2, MessageMapper.FetchType.Body); + } + +} Added: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MetadataMapAssert.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MetadataMapAssert.java?rev=1682388&view=auto ============================================================================== --- james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MetadataMapAssert.java (added) +++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MetadataMapAssert.java Fri May 29 07:56:38 2015 @@ -0,0 +1,58 @@ +/**************************************************************** + * 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 org.apache.james.mailbox.model.MessageMetaData; +import org.assertj.core.api.AbstractAssert; + +import java.util.Map; + +public class MetadataMapAssert extends AbstractAssert<MetadataMapAssert, Map<Long, MessageMetaData>> { + + public MetadataMapAssert(Map<Long, MessageMetaData> actual) { + super(actual, MetadataMapAssert.class); + } + + public static MetadataMapAssert assertThat(Map<Long, MessageMetaData> actual) { + return new MetadataMapAssert(actual); + } + + public MetadataMapAssert hasSize(int expectedSize) { + if(actual.size() != expectedSize) { + failWithMessage("Expecting size to be <%s> but is <%s>", expectedSize, actual.size()); + } + return this; + } + + public MetadataMapAssert containsMetadataForMessages(Message... messages) { + for(Message message : messages) { + if (actual.get(message.getUid()).getUid() != message.getUid()) { + failWithMessage("Expected UID stored in MessageMetadata to be <%s> but was <%s>", actual.get(message.getUid()).getUid(), message.getUid()); + } + if (!actual.get(message.getUid()).getInternalDate().equals(message.getInternalDate())) { + failWithMessage("Expected Internal date in MessageMetadata to be <%s> but was <%s>", actual.get(message.getUid()).getInternalDate(), message.getInternalDate()); + } + if (actual.get(message.getUid()).getSize() != message.getFullContentOctets()) { + failWithMessage("Expected Size stored in MessageMetadata to be <%s> but was <%s>", actual.get(message.getUid()).getSize(), message.getFullContentOctets()); + } + } + return this; + } +} Added: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MetadataMapAssertTest.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MetadataMapAssertTest.java?rev=1682388&view=auto ============================================================================== --- james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MetadataMapAssertTest.java (added) +++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/mail/model/MetadataMapAssertTest.java Fri May 29 07:56:38 2015 @@ -0,0 +1,85 @@ +/**************************************************************** + * 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 org.apache.james.mailbox.model.MessageMetaData; +import org.apache.james.mailbox.store.SimpleMessageMetaData; +import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder; +import org.apache.james.mailbox.store.mail.model.impl.SimpleMessage; +import org.junit.Before; +import org.junit.Test; + +import javax.mail.Flags; +import javax.mail.util.SharedByteArrayInputStream; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +public class MetadataMapAssertTest { + + private static final Long UID = 18L; + private static final Long MODSEQ = 24L; + private static final Date DATE = new Date(); + private static final String HEADER_STRING = "name: headerName\n\n"; + private static final String BODY_STRING = "body\\n.\\n"; + private static final Long MAILBOX_ID = 12L; + + private SimpleMessage<Long> message1; + + @Before + public void setUp() { + message1 = new SimpleMessage<Long>(DATE, HEADER_STRING.length() + BODY_STRING.length(), + HEADER_STRING.length(), new SharedByteArrayInputStream((HEADER_STRING + BODY_STRING).getBytes()), new Flags(), new PropertyBuilder(), MAILBOX_ID); + message1.setUid(UID); + message1.setModSeq(MODSEQ); + } + + @Test + public void metadataMapAssertShouldSucceedWhenContainingRightMetadata() { + Map<Long, MessageMetaData> metaDataMap = new HashMap<Long, MessageMetaData>(); + metaDataMap.put(18L, new SimpleMessageMetaData(UID, MODSEQ, new Flags(), HEADER_STRING.length() + BODY_STRING.length(), DATE)); + MetadataMapAssert.assertThat(metaDataMap).containsMetadataForMessages(message1); + } + + @Test(expected = AssertionError.class) + public void metadataMapAssertShouldFailWhenUidMismatch() { + Map<Long, MessageMetaData> metaDataMap = new HashMap<Long, MessageMetaData>(); + metaDataMap.put(18L, new SimpleMessageMetaData(UID + 1, MODSEQ, new Flags(), HEADER_STRING.length() + BODY_STRING.length(), DATE)); + MetadataMapAssert.assertThat(metaDataMap).containsMetadataForMessages(message1); + } + + @Test(expected = AssertionError.class) + public void metadataMapAssertShouldFailWhenDateMismatch() { + Map<Long, MessageMetaData> metaDataMap = new HashMap<Long, MessageMetaData>(); + Date date = new Date(); + date.setTime(DATE.getTime() + 100L); + metaDataMap.put(18L, new SimpleMessageMetaData(UID, MODSEQ, new Flags(), HEADER_STRING.length() + BODY_STRING.length(), date)); + MetadataMapAssert.assertThat(metaDataMap).containsMetadataForMessages(message1); + } + + @Test(expected = AssertionError.class) + public void metadataMapAssertShouldFailWhenSizeMismatch() { + Map<Long, MessageMetaData> metaDataMap = new HashMap<Long, MessageMetaData>(); + metaDataMap.put(18L, new SimpleMessageMetaData(UID , MODSEQ, new Flags(), HEADER_STRING.length() + BODY_STRING.length() + 1, DATE)); + MetadataMapAssert.assertThat(metaDataMap).containsMetadataForMessages(message1); + } + + +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org