View Javadoc

1   /*
2    * $Id: ExpressionKeyGeneratorTestCase.java 22607 2011-08-08 02:13:54Z pablo.kraan $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.cache.keygenerator;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.expression.ExpressionManager;
17  import org.mule.tck.junit4.AbstractMuleTestCase;
18  
19  import java.io.NotSerializableException;
20  import java.io.Serializable;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.mockito.Mockito.mock;
27  import static org.mockito.Mockito.when;
28  
29  public class ExpressionKeyGeneratorTestCase extends AbstractMuleTestCase
30  {
31  
32      private static final String KEY = "KEY";
33      private static final String EXPRESSION = "muleExpression";
34  
35      private ExpressionKeyGenerator keyGenerator;
36      private MuleMessage message;
37      private MuleEvent event;
38  
39      @Before
40      public void setUp() throws Exception
41      {
42          expressionManager = mock(ExpressionManager.class);
43          MuleContext context = mock(MuleContext.class);
44          when(context.getExpressionManager()).thenReturn(expressionManager);
45  
46          message = mock(MuleMessage.class);
47  
48          event = mock(MuleEvent.class);
49          when(event.getMessage()).thenReturn(message);
50          when(event.getMuleContext()).thenReturn(context);
51  
52          keyGenerator = new ExpressionKeyGenerator();
53          keyGenerator.setExpression(EXPRESSION);
54      }
55  
56      private ExpressionManager expressionManager;
57  
58      @Test
59      public void testGeneratesSerializableKey() throws Exception
60      {
61          when(expressionManager.evaluate(EXPRESSION, message)).thenReturn(KEY);
62          Serializable key = keyGenerator.generateKey(event);
63  
64          assertEquals(KEY, key);
65      }
66  
67      @Test(expected = NotSerializableException.class)
68      public void testThrowsExceptionOnNonSerializableKey() throws Exception
69      {
70          when(expressionManager.evaluate(EXPRESSION, message)).thenReturn(null);
71          keyGenerator.generateKey(event);
72      }
73  }