Example #1
0
  /** Test creation of media file. */
  public void testCreateMediaFile() throws Exception {

    User testUser = null;
    Weblog testWeblog = null;
    testUser = TestUtils.setupUser("mediaFileTestUser3");
    testWeblog = TestUtils.setupWeblog("mediaFileTestWeblog3", testUser);

    MediaFileManager mfMgr = WebloggerFactory.getWeblogger().getMediaFileManager();

    // no need to create root directory, that is done automatically now
    MediaFileDirectory rootDirectory = mfMgr.getMediaFileRootDirectory(testWeblog);

    // MediaFileDirectory rootDirectory = new MediaFileDirectory(null, "root", "root d",
    // testWeblog);
    // mfMgr.createMediaFileDirectory(rootDirectory);

    TestUtils.endSession(true);

    testWeblog = TestUtils.getManagedWebsite(testWeblog);
    rootDirectory = mfMgr.getMediaFileDirectory(rootDirectory.getId());

    MediaFile mediaFile = new MediaFile();
    mediaFile.setName("test.jpg");
    mediaFile.setDescription("This is a test image");
    mediaFile.setCopyrightText("test copyright text");
    mediaFile.setSharedForGallery(true);
    mediaFile.setLength(2000);
    mediaFile.setDirectory(rootDirectory);
    mediaFile.setWeblog(testWeblog);
    mediaFile.setInputStream(getClass().getResourceAsStream(TEST_IMAGE));
    mediaFile.setContentType("image/jpeg");
    rootDirectory.getMediaFiles().add(mediaFile);

    MediaFileTag tag1 = new MediaFileTag("work", mediaFile);
    MediaFileTag tag2 = new MediaFileTag("home", mediaFile);
    Set<MediaFileTag> tags = new HashSet<MediaFileTag>();
    tags.add(tag1);
    tags.add(tag2);
    mediaFile.setTags(tags);

    mfMgr.createMediaFile(testWeblog, mediaFile, new RollerMessages());
    TestUtils.endSession(true);
    assertNotNull(mediaFile.getId());
    assertNotNull(mediaFile.getId().length() > 0);

    testWeblog = TestUtils.getManagedWebsite(testWeblog);
    MediaFile mediaFile1 = mfMgr.getMediaFile(mediaFile.getId());
    assertEquals("test.jpg", mediaFile1.getName());
    assertEquals("This is a test image", mediaFile1.getDescription());
    assertEquals("test copyright text", mediaFile1.getCopyrightText());
    assertTrue(mediaFile1.isSharedForGallery());
    assertEquals(2000, mediaFile1.getLength());

    TestUtils.endSession(true);
    TestUtils.teardownWeblog(testWeblog.getId());
    TestUtils.teardownUser(testUser.getUserName());
  }
Example #2
0
  /** Test searching media file. */
  public void testSearchMediaFile() throws Exception {
    User testUser = null;
    Weblog testWeblog = null;
    testUser = TestUtils.setupUser("mediaFileTestUser7");
    testWeblog = TestUtils.setupWeblog("mediaFileTestWeblog7", testUser);

    MediaFileManager mfMgr = WebloggerFactory.getWeblogger().getMediaFileManager();

    testWeblog = TestUtils.getManagedWebsite(testWeblog);
    MediaFileDirectory rootDirectory = mfMgr.getMediaFileRootDirectory(testWeblog);

    try {

      String id1 = null;
      {
        MediaFile mf = new MediaFile();
        mf.setName("test_work.jpg");
        mf.setDescription("This is a test image");
        mf.setCopyrightText("test copyright text");
        mf.setSharedForGallery(true);
        mf.setLength(2000);
        mf.setDirectory(rootDirectory);
        mf.setWeblog(testWeblog);
        mf.setInputStream(getClass().getResourceAsStream(TEST_IMAGE));
        mf.setContentType("image/jpeg");
        rootDirectory.getMediaFiles().add(mf);

        mfMgr.createMediaFile(testWeblog, mf, new RollerMessages());

        MediaFileTag tag = new MediaFileTag("work", mf);
        Set<MediaFileTag> tags = new HashSet<MediaFileTag>();
        tags.add(tag);
        mf.setTags(tags);
        mfMgr.updateMediaFile(testWeblog, mf);

        mfMgr.createMediaFile(testWeblog, mf, new RollerMessages());
        TestUtils.endSession(true);
        id1 = mf.getId();
        assertNotNull(mf.getId());
        assertNotNull(mf.getId().length() > 0);
      }

      String id2 = null;
      {
        testWeblog = TestUtils.getManagedWebsite(testWeblog);
        rootDirectory = mfMgr.getMediaFileDirectory(rootDirectory.getId());

        MediaFile mf = new MediaFile();
        mf = new MediaFile();
        mf.setName("test_home.jpg");
        mf.setDescription("This is a test image");
        mf.setCopyrightText("test copyright text");
        mf.setSharedForGallery(true);
        mf.setLength(3000);
        mf.setDirectory(rootDirectory);
        mf.setWeblog(testWeblog);
        mf.setInputStream(getClass().getResourceAsStream(TEST_IMAGE));
        mf.setContentType("image/jpeg");
        rootDirectory.getMediaFiles().add(mf);

        mfMgr.createMediaFile(testWeblog, mf, new RollerMessages());

        MediaFileTag tag = new MediaFileTag("home", mf);
        Set<MediaFileTag> tags = new HashSet<MediaFileTag>();
        tags.add(tag);
        mf.setTags(tags);
        mfMgr.updateMediaFile(testWeblog, mf);

        TestUtils.endSession(true);
        id2 = mf.getId();
        assertNotNull(mf.getId());
        assertNotNull(mf.getId().length() > 0);
      }

      String id3 = null;
      {
        testWeblog = TestUtils.getManagedWebsite(testWeblog);
        rootDirectory = mfMgr.getMediaFileDirectory(rootDirectory.getId());

        MediaFile mf = new MediaFile();
        mf = new MediaFile();
        mf.setName("test_pers.jpg");
        mf.setDescription("This is a personal test image");
        mf.setCopyrightText("test pers copyright text");
        mf.setSharedForGallery(true);
        mf.setLength(4000);
        mf.setWeblog(testWeblog);
        mf.setDirectory(rootDirectory);
        mf.setInputStream(getClass().getResourceAsStream(TEST_IMAGE));
        mf.setContentType("image/jpeg");
        rootDirectory.getMediaFiles().add(mf);

        mfMgr.createMediaFile(testWeblog, mf, new RollerMessages());

        MediaFileTag tag = new MediaFileTag("home", mf);
        Set<MediaFileTag> tags = new HashSet<MediaFileTag>();
        tags.add(tag);
        mf.setTags(tags);
        mfMgr.updateMediaFile(testWeblog, mf);

        TestUtils.endSession(true);
        id3 = mf.getId();
        assertNotNull(mf.getId());
        assertNotNull(mf.getId().length() > 0);
      }

      testWeblog = TestUtils.getManagedWebsite(testWeblog);

      List<MediaFile> searchResults;

      // search by name

      MediaFileFilter filter1 = new MediaFileFilter();
      filter1.setName("mytest.jpg");
      searchResults = mfMgr.searchMediaFiles(testWeblog, filter1);
      assertTrue(searchResults.isEmpty());

      MediaFileFilter filter2 = new MediaFileFilter();
      filter2.setName("test_home.jpg");
      searchResults = mfMgr.searchMediaFiles(testWeblog, filter2);
      assertFalse(searchResults.isEmpty());
      assertEquals(id2, ((MediaFile) searchResults.get(0)).getId());
      assertNotNull(((MediaFile) searchResults.get(0)).getDirectory());
      assertEquals("/", ((MediaFile) searchResults.get(0)).getDirectory().getPath());

      MediaFileFilter filter3 = new MediaFileFilter();
      filter3.setName("test_work.jpg");
      searchResults = mfMgr.searchMediaFiles(testWeblog, filter3);
      assertFalse(searchResults.isEmpty());
      assertEquals(id1, ((MediaFile) searchResults.get(0)).getId());

      // search by tag

      // must be tickling an OpenJPA bug. this tag query works the
      // first time and then fails the second time it is run

      //            MediaFileFilter filter5 = new MediaFileFilter();
      //            filter5.setTags(Arrays.asList("home"));
      //            searchResults = mfMgr.searchMediaFiles(testWeblog, filter5);
      //            assertFalse(searchResults.isEmpty());
      //            assertEquals(2, searchResults.size());
      //
      //            MediaFileFilter filter51 = new MediaFileFilter();
      //            filter51.setTags(Arrays.asList("home"));
      //            searchResults = mfMgr.searchMediaFiles(testWeblog, filter51);
      //            assertFalse(searchResults.isEmpty());
      //            assertEquals(2, searchResults.size());

      MediaFileFilter filter4 = new MediaFileFilter();
      filter4.setTags(Arrays.asList("work"));
      searchResults = mfMgr.searchMediaFiles(testWeblog, filter4);
      assertFalse(searchResults.isEmpty());
      assertEquals(1, searchResults.size());
      assertEquals("test_work.jpg", ((MediaFile) searchResults.get(0)).getName());

      // search by size

      MediaFileFilter filter6 = new MediaFileFilter();
      filter6.setSize(3000);
      filter6.setSizeFilterType(MediaFileFilter.SizeFilterType.LT);
      searchResults = mfMgr.searchMediaFiles(testWeblog, filter6);
      assertFalse(searchResults.isEmpty());
      assertEquals(1, searchResults.size());
      assertEquals("test_work.jpg", ((MediaFile) searchResults.get(0)).getName());

      MediaFileFilter filter7 = new MediaFileFilter();
      filter7.setSize(3000);
      filter7.setSizeFilterType(MediaFileFilter.SizeFilterType.EQ);
      searchResults = mfMgr.searchMediaFiles(testWeblog, filter7);
      assertFalse(searchResults.isEmpty());
      assertEquals(1, searchResults.size());
      assertEquals("test_home.jpg", ((MediaFile) searchResults.get(0)).getName());

      MediaFileFilter filter8 = new MediaFileFilter();
      filter8.setSize(3000);
      filter8.setSizeFilterType(MediaFileFilter.SizeFilterType.GT);
      searchResults = mfMgr.searchMediaFiles(testWeblog, filter8);
      assertFalse(searchResults.isEmpty());
      assertEquals(1, searchResults.size());
      assertEquals("test_pers.jpg", ((MediaFile) searchResults.get(0)).getName());

      MediaFileFilter filter9 = new MediaFileFilter();
      filter9.setSize(3000);
      filter9.setSizeFilterType(MediaFileFilter.SizeFilterType.LTE);
      searchResults = mfMgr.searchMediaFiles(testWeblog, filter9);
      assertFalse(searchResults.isEmpty());
      assertEquals(2, searchResults.size());

      MediaFileFilter filter10 = new MediaFileFilter();
      filter10.setSize(3000);
      filter10.setSizeFilterType(MediaFileFilter.SizeFilterType.GTE);
      searchResults = mfMgr.searchMediaFiles(testWeblog, filter10);
      assertFalse(searchResults.isEmpty());
      assertEquals(2, searchResults.size());

      // search by type

      MediaFileFilter filter11 = new MediaFileFilter();
      filter11.setType(MediaFileType.IMAGE);
      searchResults = mfMgr.searchMediaFiles(testWeblog, filter11);
      assertFalse(searchResults.isEmpty());
      assertEquals(3, searchResults.size());

      MediaFileFilter filter12 = new MediaFileFilter();
      filter12.setType(MediaFileType.IMAGE);
      filter12.setTags(Arrays.asList("home"));
      searchResults = mfMgr.searchMediaFiles(testWeblog, filter12);
      assertFalse(searchResults.isEmpty());
      assertEquals(2, searchResults.size());

    } finally {
      TestUtils.endSession(true);
      TestUtils.teardownWeblog(testWeblog.getId());
      TestUtils.teardownUser(testUser.getUserName());
    }
  }
Example #3
0
  /** Test deletion of media file */
  public void testDeleteMediaFile() throws Exception {
    User testUser = null;
    Weblog testWeblog = null;
    testUser = TestUtils.setupUser("mediaFileTestUser4");
    testWeblog = TestUtils.setupWeblog("mediaFileTestWeblog4", testUser);

    MediaFileManager mfMgr = WebloggerFactory.getWeblogger().getMediaFileManager();

    // no need to create root directory, that is done automatically now
    MediaFileDirectory rootDirectory = mfMgr.getMediaFileRootDirectory(testWeblog);

    // MediaFileDirectory rootDirectory = new MediaFileDirectory(null, "root", "root d",
    // testWeblog);
    // mfMgr.createMediaFileDirectory(rootDirectory);

    TestUtils.endSession(true);

    testWeblog = TestUtils.getManagedWebsite(testWeblog);
    rootDirectory = mfMgr.getMediaFileDirectory(rootDirectory.getId());

    MediaFile mediaFile = new MediaFile();
    mediaFile.setName("test4.jpg");
    mediaFile.setDescription("This is a test image 4");
    mediaFile.setCopyrightText("test 4 copyright text");
    mediaFile.setSharedForGallery(false);
    mediaFile.setLength(3000);
    mediaFile.setDirectory(rootDirectory);
    mediaFile.setWeblog(testWeblog);
    mediaFile.setContentType("image/jpeg");
    mediaFile.setInputStream(getClass().getResourceAsStream(TEST_IMAGE));

    MediaFileTag tag1 = new MediaFileTag("tst4work", mediaFile);
    MediaFileTag tag2 = new MediaFileTag("tst4home", mediaFile);
    Set<MediaFileTag> tags = new HashSet<MediaFileTag>();
    tags.add(tag1);
    tags.add(tag2);
    mediaFile.setTags(tags);

    mfMgr.createMediaFile(testWeblog, mediaFile, new RollerMessages());
    String id = mediaFile.getId();
    TestUtils.endSession(true);
    assertNotNull(id);
    assertNotNull(id.length() > 0);

    testWeblog = TestUtils.getManagedWebsite(testWeblog);
    MediaFile mediaFile1 = mfMgr.getMediaFile(id);

    assertEquals("test4.jpg", mediaFile1.getName());
    assertNotNull(mediaFile1.getTags());
    assertEquals(2, mediaFile1.getTags().size());

    try {
      mfMgr.removeMediaFile(testWeblog, mediaFile1);
    } catch (Exception ignorable) {
      log.debug("ERROR removing media file", ignorable);
    }
    TestUtils.endSession(true);

    MediaFile mediaFile2 = mfMgr.getMediaFile(id);
    assertNull(mediaFile2);

    TestUtils.endSession(true);
    TestUtils.teardownWeblog(testWeblog.getId());
    TestUtils.teardownUser(testUser.getUserName());

    String uploadsDirName = WebloggerConfig.getProperty("uploads.dir");
    File flag = new File(uploadsDirName + File.separator + "migration-status.properties");
    flag.delete();
  }