Search in sources :

Example 46 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class AttachmentManagerTest method testSimpleStore.

@Test
public void testSimpleStore() throws Exception {
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "test1.txt");
    att.setAuthor("FirstPost");
    m_manager.storeAttachment(att, makeAttachmentFile());
    final Attachment att2 = m_manager.getAttachmentInfo(new WikiContext(m_engine, Wiki.contents().page(m_engine, NAME1)), "test1.txt");
    Assertions.assertNotNull(att2, "attachment disappeared");
    Assertions.assertEquals(att.getName(), att2.getName(), "name");
    Assertions.assertEquals(att.getAuthor(), att2.getAuthor(), "author");
    Assertions.assertEquals(c_fileContents.length(), att2.getSize(), "size");
    final InputStream in = m_manager.getAttachmentStream(att2);
    Assertions.assertNotNull(in, "stream");
    final StringWriter sout = new StringWriter();
    FileUtil.copyContents(new InputStreamReader(in), sout);
    in.close();
    sout.close();
    Assertions.assertEquals(c_fileContents, sout.toString(), "contents");
}
Also used : WikiContext(org.apache.wiki.WikiContext) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Attachment(org.apache.wiki.api.core.Attachment) Test(org.junit.jupiter.api.Test)

Example 47 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class AttachmentManagerTest method testNonexistentPage.

@Test
public void testNonexistentPage() throws Exception {
    try {
        m_engine.saveText("TestPage", "xx");
        final Attachment att = Wiki.contents().attachment(m_engine, "TestPages", "foo.bin");
        att.setAuthor("MonicaBellucci");
        m_manager.storeAttachment(att, makeAttachmentFile());
        Assertions.fail("Attachment was stored even when the page does not exist");
    } catch (final ProviderException ex) {
    // This is the intended exception
    } finally {
        m_engine.getManager(PageManager.class).deletePage("TestPage");
    }
}
Also used : PageManager(org.apache.wiki.pages.PageManager) ProviderException(org.apache.wiki.api.exceptions.ProviderException) Attachment(org.apache.wiki.api.core.Attachment) Test(org.junit.jupiter.api.Test)

Example 48 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class AttachmentManagerTest method testSimpleStoreByVersion.

@Test
public void testSimpleStoreByVersion() throws Exception {
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "test1.txt");
    att.setAuthor("FirstPost");
    m_manager.storeAttachment(att, makeAttachmentFile());
    final Attachment att2 = m_manager.getAttachmentInfo(new WikiContext(m_engine, Wiki.contents().page(m_engine, NAME1)), "test1.txt", 1);
    Assertions.assertNotNull(att2, "attachment disappeared");
    Assertions.assertEquals(1, att2.getVersion(), "version");
    Assertions.assertEquals(att.getName(), att2.getName(), "name");
    Assertions.assertEquals(att.getAuthor(), att2.getAuthor(), "author");
    Assertions.assertEquals(c_fileContents.length(), att2.getSize(), "size");
    final InputStream in = m_manager.getAttachmentStream(att2);
    Assertions.assertNotNull(in, "stream");
    final StringWriter sout = new StringWriter();
    FileUtil.copyContents(new InputStreamReader(in), sout);
    in.close();
    sout.close();
    Assertions.assertEquals(c_fileContents, sout.toString(), "contents");
}
Also used : WikiContext(org.apache.wiki.WikiContext) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Attachment(org.apache.wiki.api.core.Attachment) Test(org.junit.jupiter.api.Test)

Example 49 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class AttachmentManagerTest method testSimpleStoreSpace.

@Test
public void testSimpleStoreSpace() throws Exception {
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "test file.txt");
    att.setAuthor("FirstPost");
    m_manager.storeAttachment(att, makeAttachmentFile());
    final Attachment att2 = m_manager.getAttachmentInfo(new WikiContext(m_engine, Wiki.contents().page(m_engine, NAME1)), "test file.txt");
    Assertions.assertNotNull(att2, "attachment disappeared");
    Assertions.assertEquals(att.getName(), att2.getName(), "name");
    Assertions.assertEquals(att.getAuthor(), att2.getAuthor(), "author");
    Assertions.assertEquals(c_fileContents.length(), att2.getSize(), "size");
    final InputStream in = m_manager.getAttachmentStream(att2);
    Assertions.assertNotNull(in, "stream");
    final StringWriter sout = new StringWriter();
    FileUtil.copyContents(new InputStreamReader(in), sout);
    in.close();
    sout.close();
    Assertions.assertEquals(c_fileContents, sout.toString(), "contents");
}
Also used : WikiContext(org.apache.wiki.WikiContext) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Attachment(org.apache.wiki.api.core.Attachment) Test(org.junit.jupiter.api.Test)

Example 50 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class AttachmentManagerTest method testMultipleStore.

@Test
public void testMultipleStore() throws Exception {
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "test1.txt");
    att.setAuthor("FirstPost");
    m_manager.storeAttachment(att, makeAttachmentFile());
    att.setAuthor("FooBar");
    m_manager.storeAttachment(att, makeAttachmentFile());
    final Attachment att2 = m_manager.getAttachmentInfo(new WikiContext(m_engine, Wiki.contents().page(m_engine, NAME1)), "test1.txt");
    Assertions.assertNotNull(att2, "attachment disappeared");
    Assertions.assertEquals(att.getName(), att2.getName(), "name");
    Assertions.assertEquals(att.getAuthor(), att2.getAuthor(), "author");
    Assertions.assertEquals(2, att2.getVersion(), "version");
    final InputStream in = m_manager.getAttachmentStream(att2);
    Assertions.assertNotNull(in, "stream");
    final StringWriter sout = new StringWriter();
    FileUtil.copyContents(new InputStreamReader(in), sout);
    in.close();
    sout.close();
    Assertions.assertEquals(c_fileContents, sout.toString(), "contents");
    // 
    // Check that first author did not disappear
    // 
    final Attachment att3 = m_manager.getAttachmentInfo(new WikiContext(m_engine, Wiki.contents().page(m_engine, NAME1)), "test1.txt", 1);
    Assertions.assertEquals(1, att3.getVersion(), "version of v1");
    Assertions.assertEquals("FirstPost", att3.getAuthor(), "name of v1");
}
Also used : WikiContext(org.apache.wiki.WikiContext) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Attachment(org.apache.wiki.api.core.Attachment) Test(org.junit.jupiter.api.Test)

Aggregations

Attachment (org.apache.wiki.api.core.Attachment)76 Test (org.junit.jupiter.api.Test)37 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)35 Page (org.apache.wiki.api.core.Page)27 ProviderException (org.apache.wiki.api.exceptions.ProviderException)20 PageManager (org.apache.wiki.pages.PageManager)19 File (java.io.File)15 Date (java.util.Date)10 Engine (org.apache.wiki.api.core.Engine)10 ReferenceManager (org.apache.wiki.references.ReferenceManager)9 InputStream (java.io.InputStream)8 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)6 InputStreamReader (java.io.InputStreamReader)6 StringWriter (java.io.StringWriter)6 Vector (java.util.Vector)6 WikiContext (org.apache.wiki.WikiContext)6 Permission (java.security.Permission)4 ArrayList (java.util.ArrayList)4 Hashtable (java.util.Hashtable)4