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