/* * Copyright 2010 Google Inc. * * 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.google.jstestdriver.server.handlers; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.Collection; import java.util.HashSet; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.easymock.EasyMock; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.google.jstestdriver.FileInfo; import com.google.jstestdriver.FileSetCacheStrategy; /** * Tests for the file cahcing handler. * * @author rdionne@google.com (Robert Dionne) */ public class FileCacheHandlerTest extends junit.framework.TestCase { private final Gson gson = new Gson(); public void testFileCache() throws Exception { HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class); HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class); FileCacheHandler handler = new FileCacheHandler( request, response, gson, new HashSet<FileInfo>(), new FileSetCacheStrategy()); ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(out); FileInfo info = new FileInfo("asdf", 1, -1, false, true, "data", "asdf"); EasyMock.expect(request.getParameter("fileSet")) .andReturn(gson.toJson(new FileInfo[] {info})); EasyMock.expect(response.getWriter()).andReturn(writer); EasyMock.replay(request, response); handler.handleIt(); writer.flush(); Collection<FileInfo> infos = gson.fromJson(out.toString(), new TypeToken<Collection<FileInfo>>() {}.getType()); assertEquals(info, infos.iterator().next()); EasyMock.verify(request, response); } }