View Javadoc

1   /*
2    * $Id:EncryptionTransformerTestCase.java 5937 2007-04-09 22:35:04Z rossmason $
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.transformer.encryption;
12  
13  import org.mule.api.lifecycle.InitialisationException;
14  import org.mule.api.security.CryptoFailureException;
15  import org.mule.api.transformer.Transformer;
16  import org.mule.security.PasswordBasedEncryptionStrategy;
17  import org.mule.transformer.AbstractTransformerTestCase;
18  import org.mule.transformer.types.DataTypeFactory;
19  
20  import java.util.Arrays;
21  
22  public class EncryptionTransformerTestCase extends AbstractTransformerTestCase
23  {
24      private static final String TEST_DATA = "the quick brown fox jumped over the lazy dog the quick brown fox jumped over the lazy dog the quick brown fox jumped over the lazy dog";
25      
26      private PasswordBasedEncryptionStrategy strat;
27  
28      @Override
29      protected void doSetUp() throws Exception
30      {
31          strat = new PasswordBasedEncryptionStrategy();
32          strat.setPassword("mule");
33          strat.initialise();
34      }
35  
36      @Override
37      public Object getResultData()
38      {
39          try
40          {
41              return strat.encrypt(TEST_DATA.getBytes(), null);
42          }
43          catch (CryptoFailureException e)
44          {
45              fail(e.getMessage());
46              return null;
47          }
48      }
49  
50      @Override
51      public Object getTestData()
52      {
53          return TEST_DATA;
54      }
55  
56      @Override
57      public Transformer getTransformer()
58      {
59          EncryptionTransformer transformer = new EncryptionTransformer();
60          transformer.setStrategy(strat);
61          try
62          {
63              transformer.initialise();
64          }
65          catch (InitialisationException e)
66          {
67              fail(e.getMessage());
68          }
69          return transformer;
70      }
71  
72      @Override
73      public Transformer getRoundTripTransformer()
74      {
75          DecryptionTransformer transformer = new DecryptionTransformer();
76          transformer.setStrategy(strat);
77          transformer.setReturnDataType(DataTypeFactory.STRING);
78          try
79          {
80              transformer.initialise();
81          }
82          catch (InitialisationException e)
83          {
84              fail(e.getMessage());
85          }
86          return transformer;
87      }
88  
89      @Override
90      public boolean compareResults(Object src, Object result)
91      {
92          if (src == null && result == null)
93          {
94              return true;
95          }
96  
97          if (src == null || result == null)
98          {
99              return false;
100         }
101 
102         if (src instanceof byte[] && result instanceof byte[])
103         {
104             return Arrays.equals((byte[]) src, (byte[]) result);
105         }
106         else
107         {
108             return super.compareResults(src, result);
109         }
110     }
111 }