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.net.URI;
 import java.util.UUID;
 
 import static com.ursaj.hfs.HfsTestUtils.assertThrows;
 import static com.ursaj.hfs.server.HfsServletAdapter.*;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertSame;

HfsServletAdapter test cases.
 
     @Override
     protected HfsServletAdapter<HfsFileInfocreateServlet() {
         return new TestServlet();
     }

    
Test HFS servlet initialization and config setup.

Throws:
java.lang.Exception In case of any exception.
 
     @Test
     public void testInitialization() throws Exception {
         final HfsFileService<HfsFileInfofileService = getFileService();
         final MockServletContext context = new MockServletContext();
         final TestServlet servlet = new TestServlet();
 
         final MockServletConfig config = new MockServletConfig(context"Test servlet.");
         assertThrows(ServletException.class"Missed required initial parameter"servlet"init"config);
 
         config.addInitParameter("");
         context.addInitParameter("");
         assertThrows(ServletException.class"Missed required initial parameter"servlet"init"config);
 
 
         context.setAttribute(null);
         assertThrows(ServletException.class"Missed required servlet context attribute"servlet"init"config);
 
         context.setAttribute(fileService);
         servlet.init(config);
 
         assertSame(servlet.getServletConfig(), config);
         assertSame(servlet.getServletContext(), context);
         assertSame(servlet.getFileService(), fileService);
         assertSame(servlet.getHfsClient(), HfsFactory.getClient());
     }

    
Test token parsing.

Throws:
java.lang.Exception In case of any exception.
 
     @Test
     public void testTokenParsing() throws Exception {
         HfsServletAdapter<HfsFileInfoservlet = getServlet();
         MockHttpServletRequest request = new MockHttpServletRequest();
 
         for (int i = 0; i < 100; i++) {
             request.removeParameter();
             assertThrows(IOException.class"Missed request parameter"servlet"getToken"request);
 
             String token = UUID.randomUUID().toString();
             request.setParameter(token);
             assertSame(servlet.getToken(request), token);
         }
     }

    
Test application URI recovery.

Throws:
java.lang.Exception In case of any exception.
 
    @Test
    public void testApplicationUri() throws Exception {
        for (String context : new String[] { """/ctx""/application", }) {
            validateUriResolving("http""host", 0, null"http://host");
            validateUriResolving("http""host", 0, context"http://host" + context);
            validateUriResolving("http""host", 80, context"http://host" + context);
            validateUriResolving("http""host", 443, context"http://host:443" + context);
            validateUriResolving("https""host", 0, context"https://host" + context);
            validateUriResolving("https""host", 80, context"https://host:80" + context);
            validateUriResolving("https""host", 443, context"https://host" + context);
            validateUriResolving(null"host", 0, context"://host" + context);
            validateUriResolving("""host", 80, context"://host:80" + context);
            validateUriResolving("""host", 443, context"://host:443" + context);
            validateUriResolving("http"null, 0, context"http://null" + context);
            validateUriResolving("http"null, 80, context"http://null" + context);
            validateUriResolving("http"null, 443, context"http://null:443" + context);
        }
    }

    
Validate application URI resolving.

Parameters:
schema Request schema.
serverName Server name or IP.
port Server port.
context Application context.
expected Expected resolved absolute URI.
    private void validateUriResolving(String schemaString serverNameint portString contextString expected) {
        MockHttpServletRequest req = new MockHttpServletRequest("GET""/some/uri?test");
        req.setScheme(schema);
        req.setServerName(serverName);
        req.setServerPort(port);
        req.setContextPath(context);
        assertEquals(HfsServletAdapter.getApplicationUri(req), expected);
        assertEquals(URI.create("not-empty-schema-" + expected).toString(), "not-empty-schema-" + expected);
    }

    
Servlet adapter test implementation.
    private static final class TestServlet extends HfsServletAdapter<HfsFileInfo> {
    }
New to GrepCode? Check out our FAQ X