@Test
 public void test_invoke_toCorrectContentMime_method_only_once() {
   PowerMockito.mockStatic(MimeTypeUtil.class);
   MimeTypeUtil.toCorrectContentMime(configurationMock, "abc");
   PowerMockito.verifyStatic(Mockito.times(1));
   MimeTypeUtil.toCorrectContentMime(configurationMock, "abc");
 }
  @Test
  public void should_return_correct_content_mime() {
    mockStatic(MimeTypeUtil.class);
    expect(MimeTypeUtil.toCorrectContentMime(configurationMock, collectionMime))
        .andReturn("application/collection+json");
    replay(MimeTypeUtil.class);

    MimeTypeUtil.toCorrectContentMime(configurationMock, collectionMime);
    verify(MimeTypeUtil.class);
  }
Exemplo n.º 3
0
 /**
  * Returns the extension for a URL to a resource based on a predefined value (can be null or '').
  * The result is always not 'null' and can be added without check; it starts with a '.' if not
  * blank.
  *
  * @param resource the referenced resource
  * @param extension the predefined extension (can be 'null' or blank for determination)
  * @param detectMimeTypeExtension if 'true' an extension according to the mime type will be
  *     detected
  * @return the string which has to add to the resources path; '' if nothing should add
  */
 public static String getExtension(
     ResourceHandle resource, String extension, boolean detectMimeTypeExtension) {
   if (StringUtils.isBlank(extension) && detectMimeTypeExtension) {
     if (resource.isFile()) {
       MimeType mimeType = MimeTypeUtil.getMimeType(resource);
       if (mimeType != null) {
         extension = mimeType.getExtension();
       } else {
         String name = resource.getName();
         int lastDot = name.lastIndexOf('.');
         if (lastDot > 0) {
           extension = name.substring(lastDot + 1);
         }
       }
     }
   }
   if (StringUtils.isNotBlank(extension)) {
     String name = resource.getName();
     if (name.toLowerCase().endsWith(extension.toLowerCase())) {
       extension = ""; // no extension necessary to add
     }
   }
   if (extension == null) {
     String resourceType = resource.getResourceType();
     if (resourceType != null && !resource.getPrimaryType().equals(resourceType)) {
       extension = EXT_HTML; // use '.html' by default if a real resource type is present
     } else {
       ResourceHandle content = resource.getContentResource();
       if (content.isValid() && !ResourceUtil.isNonExistingResource(content)) {
         resourceType = content.getResourceType();
         if (resourceType != null && !content.getPrimaryType().equals(resourceType)) {
           extension = EXT_HTML; // use '.html' by default if a content resource exists with a real
           // resource type
         }
       }
     }
   }
   if (StringUtils.isNotBlank(extension)) {
     if (!extension.startsWith(".")) {
       extension = "." + extension;
     }
   }
   return extension != null ? extension : "";
 }
Exemplo n.º 4
0
 /** Creates a new instance with the specified {@code path}. */
 protected AbstractEntry(String path) {
   this(path, MimeTypeUtil.guessFromPath(path));
 }
 @Test
 public void should_return_default_accept_type() {
   String retrieved = MimeTypeUtil.toCorrectAcceptMime(configurationMock, "application/xml");
   assertEquals(retrieved, "application/xml");
 }
 @Test
 public void should_return_proper_accept_mime_type() {
   when(configurationMock.getAcceptMimeType()).thenReturn(MimeType.XML);
   String retrieved = MimeTypeUtil.toCorrectAcceptMime(configurationMock, jobMime);
   assertEquals(retrieved, "application/job+xml");
 }
 @Test
 public void should_return_default_content_type() {
   String retrieved = MimeTypeUtil.toCorrectContentMime(configurationMock, "application/json");
   assertEquals(retrieved, "application/json");
 }
 @Test
 public void should_return_proper_content_mime_type() {
   when(configurationMock.getContentMimeType()).thenReturn(MimeType.JSON);
   String retrieved = MimeTypeUtil.toCorrectContentMime(configurationMock, collectionMime);
   assertEquals(retrieved, "application/collection+json");
 }