Android Open Source - adventure.datetime Web Storage Test






From Project

Back to project page adventure.datetime.

License

The source code is released under:

MIT License

If you think the Android project adventure.datetime listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ca.cmput301f13t03.adventure_datetime.model;
//  ww  w  .j av  a 2 s  . co  m
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.test.AndroidTestCase;
import ca.cmput301f13t03.adventure_datetime.R;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class WebStorageTest extends AndroidTestCase {

  WebStorage es;
  Bitmap bitmap;
  
  protected void setUp() throws Exception {
    super.setUp();
    es = new WebStorage();
    es.setIndex("testing");
    bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.grumpy_cat);
  }

  protected void tearDown() throws Exception {
    super.tearDown();
  }

  public void testDeleteComment() throws Exception {
    Comment c = new Comment();
    c.setAuthor("lolcat");
    c.setContent("Can I haz ID?");
    c.setTargetId(UUID.randomUUID());
    c.setId(UUID.randomUUID());
    
    boolean result = es.putComment(c);
    assertTrue(es.getErrorMessage(), result);
    
    System.out.println("Comment ID: " + c.getId());
    
    result = es.deleteComment(c.getId());
    assertTrue(es.getErrorMessage(), result);
  }
  
  public void testGetComments() throws Exception {
    List<Comment> comments = new ArrayList<Comment>();
    List<Comment> returned = new ArrayList<Comment>();
    UUID targetId = UUID.randomUUID();
    
    for (int i = 0; i < 5; i++) {
      comments.add(createComment(i, targetId));
    }

    for (Comment c : comments) {
      boolean result = es.putComment(c);
      assertTrue(es.getErrorMessage(), result);
    }
    
    // give elasticsearch some time to sort out its life issues
    Thread.sleep(4000);
    
    returned = es.getComments(targetId, 0, 10);
    
    assertEquals("Lists different size!, " + es.getErrorMessage(), comments.size(), returned.size());
    
    for (Comment c : returned) {
      try {
        es.deleteComment(c.getId());
      }
      catch (Exception e) {
        
      }
    }
    
    for (Comment c : comments) {
      assertTrue("Comment missing from results", returned.contains(c));
    }
    
    for (Comment c : returned) {
        assertEquals(c.getId(), c.getImage().getId());
        assertNotNull(c.getImage().getEncodedBitmap());
    }

  }
  
  public void testGetAllStories() throws Exception {
    List<Story> stories = new ArrayList<Story>();
    for (int i = 0; i < 5; i++) {
      Story story = createStory(i);
      stories.add(story);
      boolean result = es.publishStory(story, null);
      assertTrue(es.getErrorMessage(), result);
    }
    
    // Give elasticsearch some time, its a bit slow. In the head.
    Thread.sleep(4000);
    
    List<Story> result = es.getStories(0, 10);  
    
    for (Story s : stories) {
      es.deleteStory(s.getId());
    }
    
    for (Story s : stories) {
      assertTrue("Story missing from results", result.contains(s));
    }
    
    for (Story s : result) {
      assertEquals(s.getId(), s.getThumbnail().getId());
      assertNotNull(s.getThumbnail().getEncodedBitmap());
    }
    
  }
  
  public void testQueryStories() throws Exception {
    List<Story> stories = new ArrayList<Story>();
    for (int i = 0; i < 5; i++) {
      Story story = createStory(i);
      stories.add(story);
    }
    
    stories.get(0).setAuthor("Andrew Fontaine");
    stories.get(1).setTitle("Thanks Andrew");
    stories.get(2).setSynopsis("How Andrew ruined civ.");
    
    for (Story s : stories) {
      boolean result = es.publishStory(s, null);
      assertTrue(es.getErrorMessage(), result);
    }
    
    // Give elasticsearch some time, it is having a mid life crisis
    // about if it wants to be a server anymore.
    Thread.sleep(4000);
    
    String filter = "Andrew Fontaine";
    List<Story> result = es.queryStories(filter, 0, 10);  
    
    for (Story s : stories) {
      try {
        es.deleteStory(s.getId());
      }
      catch (Exception e) {
        // keep on trucking
      }
    }
    
    assertTrue(result.size() >= 3);
    assertTrue("Story missing from results", result.contains(stories.get(0)));
    assertTrue("Story missing from results", result.contains(stories.get(1)));
    assertTrue("Story missing from results", result.contains(stories.get(2)));
    
    for (Story s : result) {
      assertEquals(s.getId(), s.getThumbnail().getId());
      assertNotNull(s.getThumbnail().getEncodedBitmap());
    }
  }
  
  public void testGetStoryFragments() throws Exception {
    Story story = createStory(0);
    List<StoryFragment> fragments = new ArrayList<StoryFragment>();
    fragments.add(createFragment(story.getId(), 0));
    story.setHeadFragmentId(fragments.get(0));
    
    for (int i = 1; i < 5; i++) {
      fragments.add(createFragment(story.getId(), i));
      story.addFragment(fragments.get(i));
    }
    
    boolean result = es.publishStory(story, fragments);
    assertTrue(es.getErrorMessage(), result);
    
    // Elasticsearch must deeply reflect on itself every time you post to it.
    // You know, evaluate what it means to be a web server. If it is okay
    // with us just pushing random objects into it. Maybe it feels violated?
    Thread.sleep(4000);
    
    Story story2 = es.getStory(story.getId());
    List<StoryFragment> fragments2 = es.getFragmentsForStory(story.getId(), 0, 10);
    
    try {
      es.deleteStory(story.getId());
    }
    catch (Exception e){}
    
    for (StoryFragment f : fragments) {
      deleteFragment(f);
    }
    
    assertEquals(es.getErrorMessage(), story, story2);
    assertNotNull(es.getErrorMessage(), fragments2);
        
    for (int i = 0; i < fragments.size(); i++) {
      assertEquals("Image missing", fragments.get(i).getMedia(0).getEncodedBitmap(), 
          fragments2.get(i).getMedia(0).getEncodedBitmap());
    }
  }
  
  private Story createStory(int i) {
    Story story = new Story();
    
    story.setAuthor("Bad Writer " + i);
    story.setSynopsis("Bad Synopsis " + i);
    story.setTitle("Bad Story " + i);
    story.setHeadFragmentId(UUID.randomUUID());
    story.setThumbnail(bitmap);
  
    return story;
  }
  
  private StoryFragment createFragment(UUID storyId, int i) {
    StoryFragment fragment = new StoryFragment(storyId, "Fragment " + i);
    fragment.addMedia(new Image(bitmap));
    
    return fragment;
  }
  
  private Comment createComment(int i, UUID targetId) {
    Comment comment = new Comment();
    comment.setTargetId(targetId);
    comment.setAuthor("Pretentious Douchebag " + i);
    comment.setContent("This test sucks. 0/5 would not test again.");
    comment.setImage(bitmap);
    
    return comment;
  }
  
  private void deleteFragment(StoryFragment fragment) {
    try {
      for (UUID id : fragment.getMediaIds()) {
        es.deleteImage(id);
      }
      
      es.deleteFragment(fragment.getFragmentID());
    }
    catch (Exception e) {}
  }
}




Java Source Code List

ca.cmput301f13t03.adventure_datetime.controller.AuthorControllerTest.java
ca.cmput301f13t03.adventure_datetime.controller.AuthorController.java
ca.cmput301f13t03.adventure_datetime.controller.UserControllerTest.java
ca.cmput301f13t03.adventure_datetime.controller.UserController.java
ca.cmput301f13t03.adventure_datetime.model.AccountServiceTests.java
ca.cmput301f13t03.adventure_datetime.model.AccountService.java
ca.cmput301f13t03.adventure_datetime.model.BookmarkTest.java
ca.cmput301f13t03.adventure_datetime.model.Bookmark.java
ca.cmput301f13t03.adventure_datetime.model.ChoiceTest.java
ca.cmput301f13t03.adventure_datetime.model.Choice.java
ca.cmput301f13t03.adventure_datetime.model.CommentTest.java
ca.cmput301f13t03.adventure_datetime.model.Comment.java
ca.cmput301f13t03.adventure_datetime.model.ES.java
ca.cmput301f13t03.adventure_datetime.model.ImageTest.java
ca.cmput301f13t03.adventure_datetime.model.Image.java
ca.cmput301f13t03.adventure_datetime.model.StoryDBTest.java
ca.cmput301f13t03.adventure_datetime.model.StoryDB.java
ca.cmput301f13t03.adventure_datetime.model.StoryFragmentTest.java
ca.cmput301f13t03.adventure_datetime.model.StoryFragment.java
ca.cmput301f13t03.adventure_datetime.model.StoryManager.java
ca.cmput301f13t03.adventure_datetime.model.StoryTest.java
ca.cmput301f13t03.adventure_datetime.model.Story.java
ca.cmput301f13t03.adventure_datetime.model.ThreadPool.java
ca.cmput301f13t03.adventure_datetime.model.WebStorageCleaner.java
ca.cmput301f13t03.adventure_datetime.model.WebStorageTest.java
ca.cmput301f13t03.adventure_datetime.model.WebStorage.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IAllFragmentsListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IBookmarkListListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ICommentsListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ICurrentFragmentListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ICurrentStoryListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ILocalStorage.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ILocalStoriesListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IOnlineStoriesListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IStoryModelDirector.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IStoryModelPresenter.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IWebStorage.java
ca.cmput301f13t03.adventure_datetime.serviceLocator.Locator.java
ca.cmput301f13t03.adventure_datetime.view.AuthorEdit_Edit.java
ca.cmput301f13t03.adventure_datetime.view.AuthorEdit_Overview.java
ca.cmput301f13t03.adventure_datetime.view.AuthorEdit.java
ca.cmput301f13t03.adventure_datetime.view.AuthorStories.java
ca.cmput301f13t03.adventure_datetime.view.AuthorStoryDescription.java
ca.cmput301f13t03.adventure_datetime.view.AuthorViewPager.java
ca.cmput301f13t03.adventure_datetime.view.BrowseFragment.java
ca.cmput301f13t03.adventure_datetime.view.BrowseView.java
ca.cmput301f13t03.adventure_datetime.view.CommentsView.java
ca.cmput301f13t03.adventure_datetime.view.ContinueView.java
ca.cmput301f13t03.adventure_datetime.view.FragmentViewActivity.java
ca.cmput301f13t03.adventure_datetime.view.FragmentView.java
ca.cmput301f13t03.adventure_datetime.view.FullScreen_Image.java
ca.cmput301f13t03.adventure_datetime.view.IFragmentSelected.java
ca.cmput301f13t03.adventure_datetime.view.MainViewTest.java
ca.cmput301f13t03.adventure_datetime.view.MainView.java
ca.cmput301f13t03.adventure_datetime.view.StoryDescription.java
ca.cmput301f13t03.adventure_datetime.view.treeView.Camera.java
ca.cmput301f13t03.adventure_datetime.view.treeView.ConnectionPlacer.java
ca.cmput301f13t03.adventure_datetime.view.treeView.FragmentConnection.java
ca.cmput301f13t03.adventure_datetime.view.treeView.FragmentNode.java
ca.cmput301f13t03.adventure_datetime.view.treeView.GridSegment.java
ca.cmput301f13t03.adventure_datetime.view.treeView.InputHandler.java
ca.cmput301f13t03.adventure_datetime.view.treeView.NodeGrid.java
ca.cmput301f13t03.adventure_datetime.view.treeView.NodePlacer.java
ca.cmput301f13t03.adventure_datetime.view.treeView.Region.java
ca.cmput301f13t03.adventure_datetime.view.treeView.TreeView.java