Home | History | Annotate | Download | only in mockitousage
      1 /*
      2  * Copyright (c) 2007 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 
      6 package org.mockitousage;
      7 
      8 import org.junit.Before;
      9 import org.junit.Test;
     10 import org.mockito.Mockito;
     11 import org.mockito.invocation.InvocationOnMock;
     12 import org.mockito.stubbing.Answer;
     13 import org.mockitoutil.TestBase;
     14 
     15 import java.util.Date;
     16 import java.util.GregorianCalendar;
     17 
     18 import static junit.framework.TestCase.assertEquals;
     19 import static org.mockito.BDDMockito.given;
     20 import static org.mockito.BDDMockito.willAnswer;
     21 import static org.mockito.Matchers.any;
     22 import static org.mockito.Mockito.verify;
     23 
     24 public class PlaygroundWithDemoOfUnclonedParametersProblemTest extends TestBase {
     25 
     26     ImportManager importManager;
     27     ImportLogDao importLogDao;
     28     IImportHandler importHandler;
     29 
     30     @Before
     31     public void setUp() throws Exception {
     32         importLogDao = Mockito.mock(ImportLogDao.class);
     33         importHandler = Mockito.mock(IImportHandler.class);
     34         importManager = new ImportManager(importLogDao);
     35     }
     36 
     37     @Test
     38     public void shouldIncludeInitialLog() {
     39         //given
     40         int importType = 0;
     41         Date currentDate = new GregorianCalendar(2009, 10, 12).getTime();
     42 
     43         ImportLogBean initialLog = new ImportLogBean(currentDate, importType);
     44         initialLog.setStatus(1);
     45 
     46         given(importLogDao.anyImportRunningOrRunnedToday(importType, currentDate)).willReturn(false);
     47         willAnswer(byCheckingLogEquals(initialLog)).given(importLogDao).include(any(ImportLogBean.class));
     48 
     49         //when
     50         importManager.startImportProcess(importType, currentDate);
     51 
     52         //then
     53         verify(importLogDao).include(any(ImportLogBean.class));
     54     }
     55 
     56     @Test
     57     public void shouldAlterFinalLog() {
     58         //given
     59         int importType = 0;
     60         Date currentDate = new GregorianCalendar(2009, 10, 12).getTime();
     61 
     62         ImportLogBean finalLog = new ImportLogBean(currentDate, importType);
     63         finalLog.setStatus(9);
     64 
     65         given(importLogDao.anyImportRunningOrRunnedToday(importType, currentDate)).willReturn(false);
     66         willAnswer(byCheckingLogEquals(finalLog)).given(importLogDao).alter(any(ImportLogBean.class));
     67 
     68         //when
     69         importManager.startImportProcess(importType, currentDate);
     70 
     71         //then
     72         verify(importLogDao).alter(any(ImportLogBean.class));
     73     }
     74 
     75     private Answer<Object> byCheckingLogEquals(final ImportLogBean status) {
     76         return new Answer<Object>() {
     77             public Object answer(InvocationOnMock invocation) throws Throwable {
     78                 ImportLogBean bean =  invocation.getArgument(0);
     79                 assertEquals(status, bean);
     80                 return null;
     81             }
     82         };
     83     }
     84 
     85     public class ImportManager {
     86 
     87         public ImportManager(ImportLogDao pImportLogDao) {
     88             super();
     89             importLogDao = pImportLogDao;
     90         }
     91 
     92         private ImportLogDao importLogDao = null;
     93 
     94         public void startImportProcess(int importType, Date date) {
     95             ImportLogBean importLogBean = null;
     96 
     97             try {
     98                 importLogBean = createResume(importType, date);
     99                 if (isOkToImport(importType, date)) {
    100                     // get the right handler
    101                     //importLogBean = ImportHandlerFactory.singleton().getImportHandler(importType).processImport(importLogBean);
    102                     // 2 = ok
    103                     importLogBean.setStatus(2);
    104                 } else {
    105                     // 5 = failed - is there a running process
    106                     importLogBean.setStatus(9);
    107                 }
    108             } catch (Exception e) {
    109                 // 9 = failed - exception
    110                 if (importLogBean != null)
    111                     importLogBean.setStatus(9);
    112             } finally {
    113                 if (importLogBean != null)
    114                     finalizeResume(importLogBean);
    115             }
    116         }
    117 
    118         private boolean isOkToImport(int importType, Date date) {
    119             return importLogDao.anyImportRunningOrRunnedToday(importType, date);
    120         }
    121 
    122         private ImportLogBean createResume(int importType, Date date) {
    123             ImportLogBean importLogBean = new ImportLogBean(date,
    124                     importType);
    125             // 1 = running
    126             importLogBean.setStatus(1);
    127             importLogDao.include(importLogBean);
    128             return importLogBean;
    129         }
    130 
    131         private void finalizeResume(ImportLogBean importLogBean) {
    132             importLogDao.alter(importLogBean);
    133         }
    134     }
    135 
    136     private interface ImportLogDao {
    137         boolean anyImportRunningOrRunnedToday(int importType, Date currentDate);
    138 
    139         void include(ImportLogBean importLogBean);
    140 
    141         void alter(ImportLogBean importLogBean);
    142     }
    143 
    144     private class IImportHandler {
    145     }
    146 
    147     private class ImportLogBean {
    148         private Date currentDate;
    149         private int importType;
    150         private int status;
    151 
    152         public ImportLogBean(Date currentDate, int importType) {
    153             this.currentDate = currentDate;
    154             this.importType = importType;
    155         }
    156 
    157         public void setStatus(int status) {
    158             this.status = status;
    159         }
    160 
    161         @Override
    162         public boolean equals(Object o) {
    163             if (this == o) return true;
    164             if (!(o instanceof ImportLogBean)) return false;
    165 
    166             ImportLogBean that = (ImportLogBean) o;
    167 
    168             if (importType != that.importType) return false;
    169             if (status != that.status) return false;
    170             if (currentDate != null ? !currentDate.equals(that.currentDate) : that.currentDate != null) return false;
    171 
    172             return true;
    173         }
    174 
    175         @Override
    176         public int hashCode() {
    177             int result = currentDate != null ? currentDate.hashCode() : 0;
    178             result = 31 * result + importType;
    179             result = 31 * result + status;
    180             return result;
    181         }
    182     }
    183 }
    184