1 /**************************************************************** 2 * Licensed to the Apache Software Foundation (ASF) under one * 3 * or more contributor license agreements. See the NOTICE file * 4 * distributed with this work for additional information * 5 * regarding copyright ownership. The ASF licenses this file * 6 * to you under the Apache License, Version 2.0 (the * 7 * "License"); you may not use this file except in compliance * 8 * with the License. You may obtain a copy of the License at * 9 * * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, * 13 * software distributed under the License is distributed on an * 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 15 * KIND, either express or implied. See the License for the * 16 * specific language governing permissions and limitations * 17 * under the License. * 18 ****************************************************************/ 19 20 package org.apache.jsieve.utils; 21 22 import java.io.ByteArrayInputStream; 23 24 import javax.mail.MessagingException; 25 import javax.mail.Session; 26 import javax.mail.internet.MimeMessage; 27 28 import org.apache.jsieve.ConfigurationManager; 29 import org.apache.jsieve.exception.SieveException; 30 import org.apache.jsieve.mail.MailAdapter; 31 import org.apache.jsieve.parser.generated.Node; 32 import org.apache.jsieve.parser.generated.ParseException; 33 34 /** 35 * Class JUnitUtils implements utility methods used during unit testing. 36 */ 37 public class JUnitUtils { 38 /** 39 * Method interpret parses a script and evaluates it against a MailAdapter. 40 * 41 * @param mail 42 * @param script 43 * @throws SieveException 44 * @throws ParseException 45 */ 46 static public void interpret(MailAdapter mail, String script) 47 throws SieveException, ParseException { 48 new ConfigurationManager().build().interpret(mail, 49 new ByteArrayInputStream(script.getBytes())); 50 } 51 52 /** 53 * Method interpret parses a script and evaluates it against a MailAdapter. 54 * 55 * @param mail 56 * @param script 57 * @throws SieveException 58 * @throws ParseException 59 */ 60 static public Node parse(String script) throws SieveException, 61 ParseException { 62 return new ConfigurationManager().build().parse( 63 new ByteArrayInputStream(script.getBytes())); 64 } 65 66 /** 67 * Method createMimeMessage answers an empty MimeMessage. 68 * 69 * @return MimeMessage 70 */ 71 static public MimeMessage createMimeMessage() { 72 return new MimeMessage(Session.getDefaultInstance(System 73 .getProperties())); 74 } 75 76 /** 77 * Method createMail answers a SieveMailAdapter wrapping an empty 78 * MimeMessage. 79 * 80 * @return SieveEnvelopeMailAdapter 81 */ 82 static public MailAdapter createMail() { 83 return new SieveMailAdapter(createMimeMessage()); 84 } 85 86 /** 87 * Method createEnvelopeMail answers a SieveEnvelopeMailAdapter wrapping an 88 * empty MimeMessage. 89 * 90 * @return SieveEnvelopeMailAdapter 91 */ 92 static public SieveEnvelopeMailAdapter createEnvelopeMail() { 93 return new SieveEnvelopeMailAdapter(createMimeMessage()); 94 } 95 96 /** 97 * Method copyMail answers a copy of our mock MailAdapter. 98 * 99 * @param mail 100 * @return MailAdapter 101 * @throws MessagingException 102 */ 103 static public MailAdapter copyMail(SieveMailAdapter mail) 104 throws MessagingException { 105 MimeMessage message = new MimeMessage(mail.getMessage()); 106 return new SieveMailAdapter(message); 107 } 108 109 /** 110 * Constructor for JUnitUtils. 111 */ 112 private JUnitUtils() { 113 super(); 114 } 115 116 }