1   /*
2    * $Id: FileContentsMessageAdapterTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.AbstractMessageAdapterTestCase;
15  import org.mule.umo.MessagingException;
16  import org.mule.umo.provider.UMOMessageAdapter;
17  import org.mule.util.FileUtils;
18  
19  import java.io.File;
20  import java.util.Arrays;
21  
22  
23  public class FileContentsMessageAdapterTestCase extends AbstractMessageAdapterTestCase
24  {
25      private String validMessageContent = "Yabbadabbadooo!";
26      private byte[] validMessage = validMessageContent.getBytes();
27      private File messageFile;
28  
29      /*
30       * (non-Javadoc)
31       * 
32       * @see junit.framework.TestCase#setUp()
33       */
34      protected void doSetUp() throws Exception
35      {
36          super.doSetUp();
37  
38          // The working directory is deleted on tearDown
39          File dir = FileUtils.newFile(MuleManager.getConfiguration().getWorkingDirectory(), "tmp");
40          if (!dir.exists())
41          {
42              dir.mkdirs();
43          }
44  
45          messageFile = File.createTempFile("simple", ".mule", dir);
46          FileUtils.writeStringToFile(messageFile, validMessageContent, null);
47      }
48  
49      /*
50       * (non-Javadoc)
51       * 
52       * @see org.mule.tck.providers.AbstractMessageAdapterTestCase#getValidMessage()
53       */
54      public Object getValidMessage()
55      {
56          return validMessage;
57      }
58  
59      /*
60       * (non-Javadoc)
61       * 
62       * @see org.mule.tck.providers.AbstractMessageAdapterTestCase#createAdapter()
63       */
64      public UMOMessageAdapter createAdapter(Object payload) throws MessagingException
65      {
66          if (payload.equals(validMessage))
67          {
68              return new FileContentsMessageAdapter(messageFile);
69          }
70          else
71          {
72              // properly throw
73              return new FileContentsMessageAdapter(payload);
74          }
75      }
76  
77      // overridden to properly check the byte[] by content and not just by reference
78      public void doTestMessageEqualsPayload(Object message, Object payload) throws Exception
79      {
80          if (message instanceof byte[] && payload instanceof byte[])
81          {
82              assertTrue(Arrays.equals((byte[])message, (byte[])payload));
83          }
84          else
85          {
86              fail("message and payload must both be byte[]");
87          }
88      }
89  
90      public void testMessageContentsProperlyLoaded() throws Exception
91      {
92          // get new message adapter to test
93          UMOMessageAdapter adapter = new FileContentsMessageAdapter(messageFile);
94  
95          // delete the file before accessing the payload
96          assertTrue(messageFile.delete());
97  
98          // slight detour for testing :)
99          doTestMessageEqualsPayload(validMessage, adapter.getPayload());
100     }
101 
102     public void testMultipleSetMessageCalls() throws Exception
103     {
104         // get new message adapter to test
105         FileContentsMessageAdapter adapter = new FileContentsMessageAdapter(messageFile);
106 
107         // access first payload
108         doTestMessageEqualsPayload(validMessage, adapter.getPayload());
109 
110         // create another source file
111         String secondMessageContent = "Hooray";
112         byte[] secondMessage = secondMessageContent.getBytes();
113         File secondFile = File.createTempFile("simple2", ".mule", messageFile.getParentFile());
114         FileUtils.writeStringToFile(secondFile, secondMessageContent, null);
115 
116         // replace the first message content
117         adapter.setMessage(secondFile);
118 
119         // make sure the file was properly read
120         doTestMessageEqualsPayload(secondMessage, adapter.getPayload());
121     }
122 
123 }