/*
* 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.repository;
import java.io.File;
import java.io.FileInputStream;
import org.internna.iwebmvc.model.DocumentHolder;
import org.internna.iwebmvc.utils.IO;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Jose Noheda
* @since 1.0
*/
public class FileRepositoryTest {
private static FileRepository fileRepository;
private static File root;
private DocumentHolder doc;
@BeforeClass
public static void setup() {
fileRepository = new FileRepository();
root = new File("root");
fileRepository.setDocumentRoot(root);
fileRepository.init();
}
@Before
public void save() throws Exception {
doc = new DocumentHolder();
doc.setIdentifier("web/images/i18n/globe.gif");
doc.setStream(new FileInputStream(new File(doc.getIdentifier())));
doc.setMimeType("image/jpg");
fileRepository.store(doc);
}
@Test
public void store() throws Exception{
assertNotNull("URI generated", doc.getUri());
assertTrue("Repository content exists", new File(root, doc.getUri()).exists());
}
@Test
public void retrieveByUri() throws Exception{
DocumentHolder ret = fileRepository.retrieve(doc.getUri());
assertNotNull("File was retrieved", ret);
assertNotNull("Stream is available", ret.getStream());
}
@Test(expected = org.internna.iwebmvc.core.IWebMvcException.class)
public void delete() throws Exception{
assertTrue("File was deleted", fileRepository.delete(doc.getUri()));
assertNull(fileRepository.retrieve(doc.getUri()));
}
@AfterClass
public static void tearDown() {
IO.delete(root);
}
}