Search in sources :

Example 36 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class WikiEngineTest method testAttachmentRefs.

/**
 *  Checks, if ReferenceManager is informed of new attachments.
 */
@Test
public void testAttachmentRefs() throws Exception {
    ReferenceManager refMgr = m_engine.getReferenceManager();
    AttachmentManager attMgr = m_engine.getAttachmentManager();
    m_engine.saveText(NAME1, "fooBar");
    Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    try {
        // and check post-conditions
        Collection c = refMgr.findUncreated();
        Assert.assertTrue("attachment exists: " + c, c == null || c.size() == 0);
        c = refMgr.findUnreferenced();
        Assert.assertEquals("unreferenced count", 2, c.size());
        Iterator<String> i = c.iterator();
        String first = i.next();
        String second = i.next();
        Assert.assertTrue("unreferenced", (first.equals(NAME1) && second.equals(NAME1 + "/TestAtt.txt")) || (first.equals(NAME1 + "/TestAtt.txt") && second.equals(NAME1)));
    } finally {
        // do cleanup
        String files = props.getProperty(FileSystemProvider.PROP_PAGEDIR);
        TestEngine.deleteAll(new File(files, NAME1 + BasicAttachmentProvider.DIR_EXTENSION));
    }
}
Also used : Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) File(java.io.File) Test(org.junit.Test)

Example 37 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class WikiEngineTest method testAttachmentRefs4.

/**
 *  Checks, if ReferenceManager is informed if a third page references an attachment.
 */
@Test
public void testAttachmentRefs4() throws Exception {
    ReferenceManager refMgr = m_engine.getReferenceManager();
    AttachmentManager attMgr = m_engine.getAttachmentManager();
    m_engine.saveText(NAME1, "[TestPage2]");
    Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    m_engine.saveText("TestPage2", "[" + NAME1 + "/TestAtt.txt]");
    try {
        // and check post-conditions
        Collection c = refMgr.findUncreated();
        Assert.assertTrue("attachment exists", c == null || c.size() == 0);
        c = refMgr.findUnreferenced();
        Assert.assertEquals("unreferenced count", c.size(), 1);
        Assert.assertTrue("unreferenced", ((String) c.iterator().next()).equals(NAME1));
    } finally {
        // do cleanup
        String files = props.getProperty(FileSystemProvider.PROP_PAGEDIR);
        TestEngine.deleteAll(new File(files, NAME1 + BasicAttachmentProvider.DIR_EXTENSION));
        new File(files, "TestPage2" + FileSystemProvider.FILE_EXT).delete();
    }
}
Also used : Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) File(java.io.File) Test(org.junit.Test)

Example 38 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class WikiEngineTest method testAttachmentRefs2.

/**
 *  Is ReferenceManager updated properly if a page references
 *  its own attachments?
 */
/*
      FIXME: This is a deep problem.  The real problem is that the reference
      manager cannot know when it encounters a link like "testatt.txt" that it
      is really a link to an attachment IF the link is created before
      the attachment.  This means that when the attachment is created,
      the link will stay in the "uncreated" list.

      There are two issues here: first of all, TranslatorReader should
      able to return the proper attachment references (which I think
      it does), and second, the ReferenceManager should be able to
      remove any links that are not referred to, nor they are created.

      However, doing this in a relatively sane timeframe can be a problem.
    */
@Test
public void testAttachmentRefs2() throws Exception {
    ReferenceManager refMgr = m_engine.getReferenceManager();
    AttachmentManager attMgr = m_engine.getAttachmentManager();
    m_engine.saveText(NAME1, "[TestAtt.txt]");
    // check a few pre-conditions
    Collection c = refMgr.findReferrers("TestAtt.txt");
    Assert.assertTrue("normal, unexisting page", c != null && ((String) c.iterator().next()).equals(NAME1));
    c = refMgr.findReferrers(NAME1 + "/TestAtt.txt");
    Assert.assertTrue("no attachment", c == null || c.size() == 0);
    c = refMgr.findUncreated();
    Assert.assertTrue("unknown attachment", c != null && c.size() == 1 && ((String) c.iterator().next()).equals("TestAtt.txt"));
    // now we create the attachment
    Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    try {
        // and check post-conditions
        c = refMgr.findUncreated();
        Assert.assertTrue("attachment exists: ", c == null || c.size() == 0);
        c = refMgr.findReferrers("TestAtt.txt");
        Assert.assertTrue("no normal page", c == null || c.size() == 0);
        c = refMgr.findReferrers(NAME1 + "/TestAtt.txt");
        Assert.assertTrue("attachment exists now", c != null && ((String) c.iterator().next()).equals(NAME1));
        c = refMgr.findUnreferenced();
        Assert.assertTrue("unreferenced", c.size() == 1 && ((String) c.iterator().next()).equals(NAME1));
    } finally {
        // do cleanup
        String files = props.getProperty(FileSystemProvider.PROP_PAGEDIR);
        TestEngine.deleteAll(new File(files, NAME1 + BasicAttachmentProvider.DIR_EXTENSION));
    }
}
Also used : Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) File(java.io.File) Test(org.junit.Test)

Example 39 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class AuthorizationManagerTest method testInheritedAclPermissions.

@Test
public void testInheritedAclPermissions() throws Exception {
    // Create test page & attachment
    String src = "[{ALLOW view Alice}] ";
    m_engine.saveText("Test", src);
    File f = m_engine.makeAttachmentFile();
    Attachment att = new Attachment(m_engine, "Test", "test1.txt");
    att.setAuthor("FirstPost");
    m_engine.getAttachmentManager().storeAttachment(att, f);
    Attachment p = (Attachment) m_engine.getPage("Test/test1.txt");
    Permission view = PermissionFactory.getPagePermission(p, "view");
    Permission edit = PermissionFactory.getPagePermission(p, "edit");
    // Create session with user 'Alice', who can read (in ACL)
    WikiSession session;
    session = WikiSessionTest.authenticatedSession(m_engine, Users.ALICE, Users.ALICE_PASS);
    Assert.assertTrue("Foo view Test", m_auth.checkPermission(session, view));
    Assert.assertFalse("Foo !edit Test", m_auth.checkPermission(session, edit));
    // Create session with user 'Bob', who can't read or edit (not in ACL)
    session = WikiSessionTest.authenticatedSession(m_engine, Users.BOB, Users.BOB_PASS);
    Assert.assertFalse("Bar !view Test", m_auth.checkPermission(session, view));
    Assert.assertFalse("Bar !edit Test", m_auth.checkPermission(session, view));
    // Delete test page & attachment
    m_engine.getAttachmentManager().deleteAttachment(att);
    m_engine.deletePage("Test");
}
Also used : WikiSession(org.apache.wiki.WikiSession) WikiPermission(org.apache.wiki.auth.permissions.WikiPermission) PagePermission(org.apache.wiki.auth.permissions.PagePermission) AllPermission(org.apache.wiki.auth.permissions.AllPermission) Permission(java.security.Permission) Attachment(org.apache.wiki.attachment.Attachment) File(java.io.File) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 40 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class AuthorizationManagerTest method testInheritedPermissions.

@Test
public void testInheritedPermissions() throws Exception {
    // Create test page & attachment
    String src = "[{ALLOW edit Alice}] ";
    m_engine.saveText("Test", src);
    File f = m_engine.makeAttachmentFile();
    Attachment att = new Attachment(m_engine, "Test", "test1.txt");
    att.setAuthor("FirstPost");
    m_engine.getAttachmentManager().storeAttachment(att, f);
    Attachment p = (Attachment) m_engine.getPage("Test/test1.txt");
    Permission view = PermissionFactory.getPagePermission(p, "view");
    Permission edit = PermissionFactory.getPagePermission(p, "edit");
    // Create authenticated session with user 'Alice', who can read & edit (in ACL)
    WikiSession session;
    session = WikiSessionTest.authenticatedSession(m_engine, Users.ALICE, Users.ALICE_PASS);
    Assert.assertTrue("Alice view Test/test1.txt", m_auth.checkPermission(session, view));
    Assert.assertTrue("Alice edit Test/test1.txt", m_auth.checkPermission(session, edit));
    // Create authenticated session with user 'Bob', who can't read or edit (not in ACL)
    session = WikiSessionTest.authenticatedSession(m_engine, Users.BOB, Users.BOB_PASS);
    Assert.assertFalse("Bob !view Test/test1.txt", m_auth.checkPermission(session, view));
    Assert.assertFalse("Bob !edit Test/test1.txt", m_auth.checkPermission(session, edit));
    // Delete test page & attachment
    m_engine.getAttachmentManager().deleteAttachment(att);
    m_engine.deletePage("Test");
}
Also used : WikiSession(org.apache.wiki.WikiSession) WikiPermission(org.apache.wiki.auth.permissions.WikiPermission) PagePermission(org.apache.wiki.auth.permissions.PagePermission) AllPermission(org.apache.wiki.auth.permissions.AllPermission) Permission(java.security.Permission) Attachment(org.apache.wiki.attachment.Attachment) File(java.io.File) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Aggregations

Attachment (org.apache.wiki.attachment.Attachment)62 WikiPage (org.apache.wiki.WikiPage)26 Test (org.junit.Test)23 File (java.io.File)20 ProviderException (org.apache.wiki.api.exceptions.ProviderException)17 Collection (java.util.Collection)13 Date (java.util.Date)11 Iterator (java.util.Iterator)10 WikiEngine (org.apache.wiki.WikiEngine)10 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)7 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)6 List (java.util.List)6 Vector (java.util.Vector)6 WikiContext (org.apache.wiki.WikiContext)5 Hashtable (java.util.Hashtable)4 Element (net.sf.ehcache.Element)4 PagePermission (org.apache.wiki.auth.permissions.PagePermission)4 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)3