1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.file.dao;
16
17 import org.apache.commons.io.IOUtils;
18 import org.apache.log4j.Logger;
19 import org.junit.AfterClass;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.kuali.mobility.file.entity.File;
25 import org.unitils.UnitilsJUnit4TestClassRunner;
26 import org.unitils.database.annotations.Transactional;
27 import org.unitils.database.util.TransactionMode;
28 import org.unitils.orm.jpa.JpaUnitils;
29 import org.unitils.orm.jpa.annotation.JpaEntityManagerFactory;
30
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.PersistenceUnit;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.sql.Timestamp;
36 import java.util.Calendar;
37 import java.util.List;
38
39 import static org.junit.Assert.assertFalse;
40 import static org.junit.Assert.assertTrue;
41
42
43
44
45 @RunWith(UnitilsJUnit4TestClassRunner.class)
46 @JpaEntityManagerFactory(persistenceUnit="mdot")
47 public class FileDaoImplTest {
48 private static final Logger LOG = Logger.getLogger(FileDaoImplTest.class);
49 private static final String FILE_NAME = "file.test.properties";
50 private static final String CONTENT_TYPE = "text/plain";
51
52 @PersistenceUnit
53 private EntityManagerFactory entityManagerFactory;
54
55 private FileDaoImpl dao;
56
57 @BeforeClass
58 public static void setUpClass() throws Exception {
59 }
60
61 @AfterClass
62 public static void tearDownClass() throws Exception {
63 }
64
65 @Before
66 public void preTest() {
67 setDao(new FileDaoImpl());
68 JpaUnitils.injectEntityManagerInto(getDao());
69 }
70
71 @Test
72 @Transactional(TransactionMode.ROLLBACK)
73 public void testFileDao() {
74 File file = new File();
75 try {
76 InputStream in = this.getClass().getClassLoader().getResourceAsStream(FILE_NAME);
77 byte[] inputFile = IOUtils.toByteArray(in);
78 file.setBytes(inputFile);
79 file.setFileSize(inputFile.length);
80 } catch( IOException ioe ) {
81 LOG.error( ioe.getLocalizedMessage(), ioe );
82 }
83 file.setFileName(FILE_NAME);
84 file.setContentType(CONTENT_TYPE);
85 file.setPostedTimestamp(new Timestamp( Calendar.getInstance().getTimeInMillis() ) );
86
87 assertTrue("File has an ID and should not have.",file.getId()==null);
88
89 Long fileId = getDao().saveFile(file);
90
91 LOG.debug("New file id is: "+fileId);
92
93 assertTrue("Could not save file.", fileId != null && fileId.intValue() > 0);
94
95 file.setContentType("text/xml");
96 Long fileId2 = getDao().saveFile(file);
97
98 assertTrue("File was inserted again, not updated.", fileId.compareTo(fileId2) == 0);
99
100 File lookupFile = getDao().findFileById(fileId);
101
102 assertTrue("Failed to find file for ID " + fileId, lookupFile != null);
103
104 List<File> listOfFiles = getDao().findFilesByName(FILE_NAME);
105
106 assertTrue("Failed to find files for name "+FILE_NAME, listOfFiles != null && listOfFiles.size() == 1 );
107
108 List<File> allFiles = getDao().findAllFiles();
109
110 assertTrue("Failed to find all files.", allFiles != null && allFiles.size() == 1 );
111
112 File fileToRemove = allFiles.get(0);
113 boolean didRemove = getDao().removeFile(fileToRemove);
114
115 assertTrue("Failed to remove file ID "+fileToRemove.getId(),didRemove);
116
117 allFiles = getDao().findAllFiles();
118
119 assertTrue("Found files and should not have.", allFiles == null || allFiles.size() == 0 );
120
121 didRemove = getDao().removeFile(null);
122 assertFalse("Removed a null file. How is that possible?", didRemove);
123
124 Long nullId = getDao().saveFile(null);
125 assertTrue("Saved a null file. How is that possible?",nullId == null);
126 }
127
128 public EntityManagerFactory getEntityManagerFactory() {
129 return entityManagerFactory;
130 }
131
132 public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
133 this.entityManagerFactory = entityManagerFactory;
134 }
135
136 public FileDaoImpl getDao() {
137 return dao;
138 }
139
140 public void setDao(FileDaoImpl dao) {
141 this.dao = dao;
142 }
143 }