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.james.mpt.ant;
21  
22  import java.io.File;
23  import java.io.FileWriter;
24  import java.io.InputStream;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.james.mpt.DiscardProtocol;
29  import org.apache.tools.ant.Project;
30  import org.apache.tools.ant.types.Resource;
31  import org.apache.tools.ant.types.resources.StringResource;
32  import org.apache.tools.ant.types.resources.Union;
33  
34  public class TestRunScripts extends TestCase {
35  
36      private static final String SCRIPT = "A script";
37  
38      private static final int PORT = 10001;
39      
40      Union stubResourceCollection;
41      Resource stubResource;
42      
43      DiscardProtocol fakeServer;
44      DiscardProtocol.Record record;
45      
46      MailProtocolTestTask subject;
47      
48      protected void setUp() throws Exception {
49          super.setUp();
50          fakeServer = new DiscardProtocol(PORT);
51          fakeServer.start();
52          record = fakeServer.recordNext();
53          
54          stubResourceCollection = new Union();        
55          stubResource = new StringResource("C: " + SCRIPT);
56          stubResourceCollection.add(stubResource);
57          
58          subject = new MailProtocolTestTask();
59          subject.setHost("127.0.0.1");
60          subject.setPort(PORT);
61          subject.setProject(new Project());
62      }
63  
64      protected void tearDown() throws Exception {
65          super.tearDown();
66          fakeServer.stop();
67      }
68  
69      public void testIgnoreUnsupportedResource() throws Exception {
70          final Resource unsupportedResource = new StringResource() {
71              public InputStream getInputStream() {
72                  throw new UnsupportedOperationException();
73              }
74          };
75          stubResourceCollection.add(unsupportedResource);
76          subject.add(stubResourceCollection);
77          subject.execute();
78          assertEquals(SCRIPT +"\r\n", record.complete());
79      }
80      
81      public void testRunOneScriptFromCollection() throws Exception {
82          subject.add(stubResourceCollection);
83          subject.execute();
84          assertEquals(SCRIPT +"\r\n", record.complete());
85      }
86      
87      public void testRunOneScriptFromAttribute() throws Exception {
88          final File file = File.createTempFile("Test", "mpt");
89          file.deleteOnExit();
90          final FileWriter writer = new FileWriter(file);
91          writer.write("C: " + SCRIPT);
92          writer.close();
93          subject.setScript(file);
94          subject.execute();
95          assertEquals(SCRIPT +"\r\n", record.complete());
96      }
97  }