1   /*
2    * $Id: MessagePropertiesTransformerTestCase.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.transformers.simple;
12  
13  import org.mule.impl.MuleMessage;
14  import org.mule.tck.AbstractMuleTestCase;
15  import org.mule.umo.UMOEventContext;
16  import org.mule.umo.UMOMessage;
17  
18  import java.util.Collections;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  public class MessagePropertiesTransformerTestCase extends AbstractMuleTestCase
23  {
24  
25      public void testOverwriteFlagEnabledByDefault() throws Exception
26      {
27          MessagePropertiesTransformer t = new MessagePropertiesTransformer();
28          Map add = new HashMap();
29          add.put("addedProperty", "overwrittenValue");
30          t.setAddProperties(add);
31  
32          UMOMessage msg = new MuleMessage("message");
33          msg.setProperty("addedProperty", "originalValue");
34          UMOEventContext ctx = getTestEventContext(msg);
35          // context clones message
36          msg = ctx.getMessage();
37          MuleMessage transformed = (MuleMessage) t.transform(msg, null, ctx);
38          assertSame(msg, transformed);
39          assertEquals(msg.getUniqueId(), transformed.getUniqueId());
40          assertEquals(msg.getPayload(), transformed.getPayload());
41          // property values will be different
42          assertEquals(msg.getPropertyNames(), transformed.getPropertyNames());
43  
44          assertEquals("overwrittenValue", transformed.getProperty("addedProperty"));
45      }
46  
47      public void testOverwriteFalsePreservesOriginal() throws Exception
48      {
49          MessagePropertiesTransformer t = new MessagePropertiesTransformer();
50          Map add = new HashMap();
51          add.put("addedProperty", "overwrittenValue");
52          t.setAddProperties(add);
53          t.setOverwrite(false);
54  
55          MuleMessage msg = new MuleMessage("message");
56          msg.setProperty("addedProperty", "originalValue");
57          UMOEventContext ctx = getTestEventContext(msg);
58          MuleMessage transformed = (MuleMessage) t.transform(msg, null, ctx);
59          assertSame(msg, transformed);
60          assertEquals(msg.getUniqueId(), transformed.getUniqueId());
61          assertEquals(msg.getPayload(), transformed.getPayload());
62          assertEquals(msg.getPropertyNames(), transformed.getPropertyNames());
63  
64          assertEquals("originalValue", transformed.getProperty("addedProperty"));
65      }
66  
67      public void testDelete() throws Exception
68      {
69          MessagePropertiesTransformer t = new MessagePropertiesTransformer();
70          t.setDeleteProperties(Collections.singletonList("badProperty"));
71  
72          MuleMessage msg = new MuleMessage("message");
73          msg.setProperty("badProperty", "badValue");
74          UMOEventContext ctx = getTestEventContext(msg);
75          MuleMessage transformed = (MuleMessage) t.transform(msg, null, ctx);
76          assertSame(msg, transformed);
77          assertEquals(msg.getUniqueId(), transformed.getUniqueId());
78          assertEquals(msg.getPayload(), transformed.getPayload());
79          assertEquals(msg.getPropertyNames(), transformed.getPropertyNames());
80          assertFalse(transformed.getPropertyNames().contains("badValue"));
81      }
82  
83  }