/**
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * 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.appengine.demos.guestbook;

import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;

public class GuestbookServletTest {

  private GuestbookServlet guestbookServlet;

  private final LocalServiceTestHelper helper =
      new LocalServiceTestHelper(new LocalUserServiceTestConfig())
          .setEnvIsLoggedIn(true)
          .setEnvAuthDomain("localhost")
          .setEnvEmail("test@localhost");

  @Before
  public void setupGuestBookServlet() {
    helper.setUp();
    guestbookServlet = new GuestbookServlet();
  }

  @After
  public void tearDownHelper() {
    helper.tearDown();
  }

  @Test
  public void testDoGet() throws IOException {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);

    StringWriter stringWriter = new StringWriter();

    when(response.getWriter()).thenReturn(new PrintWriter(stringWriter));

    guestbookServlet.doGet(request, response);

    User currentUser = UserServiceFactory.getUserService().getCurrentUser();

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(os);
    ps.println(currentUser.getNickname());
    assertEquals("Hello, " +  os.toString("UTF8"), stringWriter.toString());
  }

}

@@ -797,580 +797,580 @@
 ort 
-java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.PrintStream;%0Aimport java.io.PrintWriter;%0Aimport java.io.StringWriter;%0A%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0Aimport com.google.appengine.api.users.User;%0Aimport com.google.appengine.api.users.UserServiceFactory;%0Aimport com.google.appengine.tools.development.testing.LocalServiceTestHelper;%0Aimport com.google.appengine.tools.development.testing.LocalUserServiceTestConfig
+com.google.appengine.api.users.User;%0Aimport com.google.appengine.api.users.UserServiceFactory;%0Aimport com.google.appengine.tools.development.testing.LocalServiceTestHelper;%0Aimport com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;%0A%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.PrintStream;%0Aimport java.io.PrintWriter;%0Aimport java.io.StringWriter;%0A%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse
 ;%0A%0Ap

Rollback reordering import