|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2012 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.orion.server.tests.servlets.git; |
| 12 |
|
| 13 |
import static org.junit.Assert.assertEquals; |
| 14 |
|
| 15 |
import java.io.UnsupportedEncodingException; |
| 16 |
import java.net.HttpURLConnection; |
| 17 |
import java.net.URI; |
| 18 |
import java.util.Arrays; |
| 19 |
|
| 20 |
import org.eclipse.orion.internal.server.core.IOUtilities; |
| 21 |
import org.eclipse.orion.internal.server.servlets.ProtocolConstants; |
| 22 |
import org.eclipse.orion.server.git.GitConstants; |
| 23 |
import org.eclipse.orion.server.git.objects.Command; |
| 24 |
import org.json.JSONException; |
| 25 |
import org.json.JSONObject; |
| 26 |
import org.junit.Test; |
| 27 |
|
| 28 |
import com.meterware.httpunit.PostMethodWebRequest; |
| 29 |
import com.meterware.httpunit.WebRequest; |
| 30 |
import com.meterware.httpunit.WebResponse; |
| 31 |
|
| 32 |
public class GitCommandTest extends GitTest { |
| 33 |
|
| 34 |
@Test |
| 35 |
public void command() throws Exception { |
| 36 |
URI workspaceLocation = createWorkspace(getMethodName()); |
| 37 |
JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), gitDir.toString()); |
| 38 |
JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); |
| 39 |
String gitCommandUri = gitSection.getString(GitConstants.KEY_COMMAND); |
| 40 |
|
| 41 |
WebRequest request = getPosGitCommandRequest(gitCommandUri, new String[] {"branch"}); |
| 42 |
WebResponse response = webConversation.getResponse(request); |
| 43 |
assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); |
| 44 |
String commandResult = response.getText(); |
| 45 |
|
| 46 |
String expectedResult = "* master\r\n"; |
| 47 |
assertEquals(expectedResult, commandResult); |
| 48 |
} |
| 49 |
|
| 50 |
@Test |
| 51 |
public void commandWithArgs() throws Exception { |
| 52 |
URI workspaceLocation = createWorkspace(getMethodName()); |
| 53 |
JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), gitDir.toString()); |
| 54 |
JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); |
| 55 |
String gitCommandUri = gitSection.getString(GitConstants.KEY_COMMAND); |
| 56 |
|
| 57 |
WebRequest request = getPosGitCommandRequest(gitCommandUri, new String[] {"commit", "--amend", "-m", "'Amended commit'"}); |
| 58 |
WebResponse response = webConversation.getResponse(request); |
| 59 |
assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); |
| 60 |
String commandResult = response.getText(); |
| 61 |
|
| 62 |
String expectedResult = "[master ecf5161e231e3f798ec9ae0910264bd5c85a4c37] 'Amended commit'\r\n"; |
| 63 |
assertEquals(expectedResult, commandResult); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void notImplementedCommand() throws Exception { |
| 68 |
URI workspaceLocation = createWorkspace(getMethodName()); |
| 69 |
JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), gitDir.toString()); |
| 70 |
JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); |
| 71 |
String gitCommandUri = gitSection.getString(GitConstants.KEY_COMMAND); |
| 72 |
|
| 73 |
WebRequest request = getPosGitCommandRequest(gitCommandUri, new String[] {"status"}); |
| 74 |
WebResponse response = webConversation.getResponse(request); |
| 75 |
assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode()); |
| 76 |
} |
| 77 |
|
| 78 |
private static WebRequest getPosGitCommandRequest(String location, String[] args) throws UnsupportedEncodingException, JSONException { |
| 79 |
String requestURI; |
| 80 |
if (location.startsWith("http://")) |
| 81 |
requestURI = location; |
| 82 |
else if (location.startsWith("/")) |
| 83 |
requestURI = SERVER_LOCATION + location; |
| 84 |
else |
| 85 |
requestURI = SERVER_LOCATION + GIT_SERVLET_LOCATION + Command.RESOURCE + location; |
| 86 |
JSONObject body = new JSONObject(); |
| 87 |
body.put(GitConstants.KEY_COMMAND_ARGS, Arrays.asList(args)); |
| 88 |
|
| 89 |
WebRequest request = new PostMethodWebRequest(requestURI, IOUtilities.toInputStream(body.toString()), "application/json"); |
| 90 |
request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1"); |
| 91 |
setAuthentication(request); |
| 92 |
return request; |
| 93 |
} |
| 94 |
} |