Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 Ursa Project LLC (http://ursaj.com) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.ursaj.hfs.server;
 
 
 import java.util.*;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNull;

 
 public class HfsUploadServletAdapterTest extends
Test upload request.

Throws:
java.lang.Exception In case of any exception.
 
     @Test
     public void testUploadRequest() throws Exception {
         TestServlet servlet = (TestServletgetServlet();
 
         Collection<HfsUploadInfoinfos = new ArrayList<HfsUploadInfo>();
 
         infos.add(new HfsUploadInfoImpl(."No files."));
         infos.add(new HfsUploadInfoImpl(."Parsing failed."));
         infos.add(new HfsUploadInfoImpl(Arrays.<HfsFileInfo>asList(
             new HfsFileInfoImpl(UUID.randomUUID(), "file.jpg""image/jpg"new Date(), 200000),
             new HfsFileInfoImpl(UUID.randomUUID(), "text.doc""text/doc"new Date(), 10000))));
 
         // Test valid tokens.
         for (HfsUploadInfo info : infos) {
             servlet.lastStored = null;
             MockHttpServletResponse resp = doRequest("/upload"info);
 
             if (info.getStatus() == .)
                 assertEquals(servlet.lastStoredinfo.getFiles());
             else
                 assertNull(servlet.lastStored);
 
             validateUploadResponse(inforesp);
         }
 
         servlet.lastStored = null;
 
         // Test missed token.
         for (int i = 0; i < 10; i++) {
             HfsUploadInfo expected = new StoringFailedInfo("Failed to store uploaded files information ("
                 + "Missed request parameter 'token' with encoded communication message from HFS server.)");
 
             MockHttpServletResponse resp = doRequest("/upload"null);
             assertNull(servlet.lastStored);
 
             validateUploadResponse(expectedresp);
         }
 
         // Test invalid token.
         for (int i = 0; i < 10; i++) {
             HfsGcRequest gcReq = new HfsGcRequestImpl(UUID.randomUUID());
             HfsUploadInfo expected = new StoringFailedInfo("Failed to store uploaded files information ("
                 + "Unexpected response type: " + gcReq + ")");
 
             MockHttpServletResponse resp = doRequest("/upload"gcReq);
             assertNull(servlet.lastStored);
 
             validateUploadResponse(expectedresp);
         }
     }

    
Do upload request.

Parameters:
servletPath Servlet path to send request to.
message Message to send as a token or null to skip token sending.
Returns:
Response mock object.
Throws:
java.lang.Exception In case of any exception.
    private MockHttpServletResponse doRequest(String servletPath, @Nullable Object messagethrows Exception {
        HfsUploadServletAdapter servlet = getServlet();
        MockServletContext context = (MockServletContextservlet.getServletContext();
        MockHttpServletRequest req = new MockHttpServletRequest(context"GET",
            context.getContextPath() + servletPath);
        req.setContextPath(context.getContextPath());
        if (message != null) {
            req.addParameter("token"createToken(message));
        }
        MockHttpServletResponse resp = new MockHttpServletResponse();
        servlet.service(reqresp);
        return resp;
    }

    
Validate upload response.

Parameters:
info Upload info to validate response against.
resp Response mock object.
Throws:
java.lang.Exception In case of any exception.
    private void validateUploadResponse(HfsUploadInfo infoMockHttpServletResponse respthrows Exception {
        assertEquals(resp.getHeader("Content-Type"), "text/html");
        assertEquals(resp.getContentAsString(), HfsUploadLibrary.renderUploadFrame(
            info.getStatus().ordinal(), getServlet().renderJsResponse(nullrespinfo)));
    }
    @Override
        return new TestServlet();
    }

    
Upload servlet adapter test implementation.
    private static final class TestServlet extends HfsUploadServletAdapter<HfsFileInfo> {
        
Last stored upload info.
        private Collection<HfsFileInfolastStored;
        @Override
        protected void storeUploadedFiles(HttpServletRequest reqHttpServletResponse resp,
            Collection<HfsFileInfouploadedFilesthrows ServletExceptionIOException {
             = uploadedFiles;
        }
    }
New to GrepCode? Check out our FAQ X