/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.spring.services.dwr.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.internna.iwebmvc.core.dao.AbstractDAO;
import org.directwebremoting.io.FileTransfer;
import org.internna.iwebmvc.core.IWebMvcException;
import org.internna.iwebmvc.model.core.Document;
import org.internna.iwebmvc.repository.FileRepository;
import org.internna.iwebmvc.utils.IO;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.FileCopyUtils;
import static org.junit.Assert.*;
/**
* @author Jose Noheda
* @since 1.0
*/
public class UploadMonitorImplTest {
private UploadMonitorImpl monitor;
private static File root = new File("root");
private FileTransfer transfer;
private static EntityManagerFactory factory;
protected static EntityManager entityManager;
@Before
public void setup() throws Exception {
monitor = new UploadMonitorImpl();
monitor.dao = new AbstractDAO() {
@Override
protected EntityManager getEntityManager() {
return UploadMonitorImplTest.entityManager;
}
};
FileRepository repository = new FileRepository();
repository.setDocumentRoot(root);
monitor.documentRepository = repository;
File upload = new File("web/images/i18n/globe.gif");
FileInputStream stream = new FileInputStream(upload);
ByteArrayOutputStream out = new ByteArrayOutputStream();
FileCopyUtils.copy(stream, out);
ByteArrayInputStream bytes = new ByteArrayInputStream(out.toByteArray());
transfer = new FileTransfer("globe.gif", "image/gif", bytes);
factory = Persistence.createEntityManagerFactory("MODEL");
entityManager = factory.createEntityManager();
entityManager.getTransaction().begin();
}
@Test
public void upload() throws Exception {
Document doc = monitor.upload(transfer);
assertTrue("Size set", doc.getSizeInBytes() > 1000);
assertTrue("Uri set", doc.getUri().indexOf("globe") > 0);
assertEquals("Mime set", "image/gif", doc.getMimeType());
}
@After
public void closeTrans() {
entityManager.getTransaction().commit();
}
@AfterClass
public static void tearDown() {
IO.delete(root);
if (root.exists()) throw new IWebMvcException("Please manually delete " + root.getAbsolutePath());
}
}