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