1   /*
2    * $Id: FileConnectorTestCase.java 10483 2008-01-23 11:57:53Z holger $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.file;
12  
13  import org.mule.MuleManager;
14  import org.mule.tck.providers.AbstractConnectorTestCase;
15  import org.mule.umo.UMOComponent;
16  import org.mule.umo.endpoint.UMOEndpoint;
17  import org.mule.umo.endpoint.UMOImmutableEndpoint;
18  import org.mule.umo.provider.UMOConnector;
19  import org.mule.umo.provider.UMOMessageReceiver;
20  import org.mule.util.FileUtils;
21  
22  import java.io.File;
23  import java.util.Properties;
24  
25  public class FileConnectorTestCase extends AbstractConnectorTestCase
26  {
27      static final long POLLING_FREQUENCY = 1234;
28      static final long POLLING_FREQUENCY_OVERRIDE = 4321;
29  
30      private File validMessage;
31  
32      // @Override
33      protected void doSetUp() throws Exception
34      {
35          super.doSetUp();
36  
37          // The working directory is deleted on tearDown
38          File tempDir = FileUtils.newFile(MuleManager.getConfiguration().getWorkingDirectory(), "tmp");
39          if (!tempDir.exists())
40          {
41              tempDir.mkdirs();
42          }
43  
44          validMessage = File.createTempFile("simple", ".mule", tempDir);
45          assertNotNull(validMessage);
46          FileUtils.writeStringToFile(validMessage, "validMessage");
47      }
48  
49      // @Override
50      protected void doTearDown() throws Exception
51      {
52          // TestConnector dispatches events via the test: protocol to test://test
53          // endpoints, which seems to end up in a directory called "test" :(
54          FileUtils.deleteTree(FileUtils.newFile(getTestConnector().getProtocol()));
55          super.doTearDown();
56      }
57  
58      /*
59       * (non-Javadoc)
60       * 
61       * @see org.mule.tck.providers.AbstractConnectorTestCase#createConnector()
62       */
63      public UMOConnector createConnector() throws Exception
64      {
65          UMOConnector connector = new FileConnector();
66          connector.setName("testFile");
67          connector.initialise();
68          return connector;
69      }
70  
71      public String getTestEndpointURI()
72      {
73          return "file://" + MuleManager.getConfiguration().getWorkingDirectory();
74      }
75  
76      public Object getValidMessage() throws Exception
77      {
78          return validMessage;
79      }
80  
81      /**
82       * Test polling frequency set on a connector.
83       */
84      public void testConnectorPollingFrequency() throws Exception
85      {
86          FileConnector connector = (FileConnector) getConnector();
87          connector.setPollingFrequency(POLLING_FREQUENCY);
88  
89          UMOEndpoint endpoint = getTestEndpoint("simple", UMOImmutableEndpoint.ENDPOINT_TYPE_RECEIVER);
90          UMOComponent component = getTestComponent(getDescriptor());
91          UMOMessageReceiver receiver = connector.createReceiver(component, endpoint);
92          assertEquals("Connector's polling frequency must not be ignored.", POLLING_FREQUENCY,
93              ((FileMessageReceiver) receiver).getFrequency());
94      }
95  
96      /**
97       * Test polling frequency overridden at an endpoint level.
98       */
99      public void testPollingFrequencyEndpointOverride() throws Exception
100     {
101         FileConnector connector = (FileConnector) getConnector();
102         // set some connector-level value which we are about to override
103         connector.setPollingFrequency(-1);
104 
105         UMOEndpoint endpoint = getTestEndpoint("simple", UMOImmutableEndpoint.ENDPOINT_TYPE_RECEIVER);
106 
107         Properties props = new Properties();
108         // Endpoint wants String-typed properties
109         props.put(FileConnector.PROPERTY_POLLING_FREQUENCY, String.valueOf(POLLING_FREQUENCY_OVERRIDE));
110         endpoint.setProperties(props);
111 
112         UMOComponent component = getTestComponent(getDescriptor());
113         UMOMessageReceiver receiver = connector.createReceiver(component, endpoint);
114         assertEquals("Polling frequency endpoint override must not be ignored.", POLLING_FREQUENCY_OVERRIDE,
115             ((FileMessageReceiver) receiver).getFrequency());
116     }
117 
118     public void testOnlySingleDispatcherPerEndpoint()
119     {
120         // MULE-1773 implies that we must only have one dispatcher per endpoint
121         FileConnector connector = (FileConnector) getConnector();
122 
123         assertEquals(1, connector.getMaxDispatchersActive());
124 
125         try
126         {
127             connector.setMaxDispatchersActive(2);
128             fail("expected IllegalArgumentException");
129         }
130         catch (IllegalArgumentException iax)
131         {
132             // OK - expected
133         }
134 
135         // value must be unchanged
136         assertEquals(1, connector.getMaxDispatchersActive());        
137     }
138 }