Example usage for org.json JSONObject getJSONObject

List of usage examples for org.json JSONObject getJSONObject

Introduction

In this page you can find the example usage for org.json JSONObject getJSONObject.

Prototype

public JSONObject getJSONObject(String key) throws JSONException 

Source Link

Document

Get the JSONObject value associated with a key.

Usage

From source file:de.btobastian.javacord.entities.permissions.impl.ImplBan.java

public ImplBan(ImplDiscordAPI api, ImplServer server, JSONObject data) {
    this.server = server;
    this.user = api.getOrCreateUser(data.getJSONObject("user"));
    this.reason = data.isNull("reason") ? null : data.getString("reason");
}

From source file:com.domuslink.elements.Alias.java

public Alias(JSONObject jsonElement) throws Exception {
    super();//  w w w  .jav  a 2s.  c om
    setState(0);
    setDimLevel(0);
    try {
        setLabel(jsonElement.getString("label"));
        setAliasMapElement(new AliasMap(jsonElement.getJSONObject("aliasMapElement")));
        if (!this.aliasMapElement.getElementType().contentEquals("Scene")) {
            setHouseCode(jsonElement.getString("houseCode"));
            setDevices(jsonElement.getString("devices"));
            setModuleType(jsonElement.getString("moduleType"));
            setModuleOptions(jsonElement.getString("moduleOptions"));
            setElementType(jsonElement.getString("elementType"));
            setElementLine(jsonElement.getString("elementLine"));
            setLineNum(jsonElement.getInt("lineNum"));
            setArrayNum(jsonElement.getInt("arrayNum"));
            setEnabled(jsonElement.getBoolean("enabled"));
            this.isScene = false;
            if (devices.indexOf(",") > 0 || devices.indexOf("-") > 0)
                this.isMultiAlias = true;
            else
                this.isMultiAlias = false;
        } else {
            this.isScene = true;
            this.isMultiAlias = false;
        }
    } catch (Exception e) {
        Log.e(TAG, "Error getting alias from JSONObject", e);
        throw e;
    }

}

From source file:org.eclipse.orion.server.tests.servlets.git.GitDiffTest.java

@Test
public void testNoDiff() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());

    String projectName = getMethodName();
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
    String gitDiffUri = gitSection.getString(GitConstants.KEY_DIFF);

    String[] parts = getDiff(gitDiffUri);
    assertEquals("", parts[1]);
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitDiffTest.java

@Test
public void testDiffAlreadyModified() throws Exception {
    Writer w = new OutputStreamWriter(new FileOutputStream(testFile), "UTF-8");
    try {//  www  .  ja  v a2  s  .  co  m
        w.write("hello");
    } finally {
        w.close();
    }

    URI workspaceLocation = createWorkspace(getMethodName());

    String projectName = getMethodName();
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
    String gitDiffUri = gitSection.getString(GitConstants.KEY_DIFF);

    String[] parts = getDiff(gitDiffUri);

    StringBuilder sb = new StringBuilder();
    sb.append("diff --git a/test.txt b/test.txt").append("\n");
    sb.append("index 30d74d2..b6fc4c6 100644").append("\n");
    sb.append("--- a/test.txt").append("\n");
    sb.append("+++ b/test.txt").append("\n");
    sb.append("@@ -1 +1 @@").append("\n");
    sb.append("-test").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    sb.append("+hello").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    assertEquals(sb.toString(), parts[1]);
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitDiffTest.java

@Test
public void testDiffModifiedByOrion() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());

    String projectName = getMethodName();
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject testTxt = getChild(project, "test.txt");
    modifyFile(testTxt, "hello");

    JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
    String gitDiffUri = gitSection.getString(GitConstants.KEY_DIFF);

    String[] parts = getDiff(gitDiffUri);
    StringBuilder sb = new StringBuilder();
    sb.append("diff --git a/test.txt b/test.txt").append("\n");
    sb.append("index 30d74d2..b6fc4c6 100644").append("\n");
    sb.append("--- a/test.txt").append("\n");
    sb.append("+++ b/test.txt").append("\n");
    sb.append("@@ -1 +1 @@").append("\n");
    sb.append("-test").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    sb.append("+hello").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    assertEquals(sb.toString(), parts[1]);
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitDiffTest.java

@Test
public void testDiffFilter() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());

    String projectName = getMethodName();
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject testTxt = getChild(project, "test.txt");
    modifyFile(testTxt, "hi");

    JSONObject folder1 = getChild(project, "folder");
    JSONObject folderTxt = getChild(folder1, "folder.txt");
    modifyFile(folderTxt, "folder change");

    String[] parts = getDiff(folder1.getJSONObject(GitConstants.KEY_GIT).getString(GitConstants.KEY_DIFF));
    StringBuilder sb = new StringBuilder();
    sb.append("diff --git a/folder/folder.txt b/folder/folder.txt").append("\n");
    sb.append("index 0119635..95c4c65 100644").append("\n");
    sb.append("--- a/folder/folder.txt").append("\n");
    sb.append("+++ b/folder/folder.txt").append("\n");
    sb.append("@@ -1 +1 @@").append("\n");
    sb.append("-folder").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    sb.append("+folder change").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    assertEquals(sb.toString(), parts[1]);

    String gitDiffUri = testTxt.getJSONObject(GitConstants.KEY_GIT).getString(GitConstants.KEY_DIFF);
    parts = getDiff(gitDiffUri);//from www.j  a  va 2  s  . c o m

    assertDiffUris(gitDiffUri, new String[] { "test", "hi", "test" }, new JSONObject(parts[0]));

    sb.setLength(0);
    sb.append("diff --git a/test.txt b/test.txt").append("\n");
    sb.append("index 30d74d2..32f95c0 100644").append("\n");
    sb.append("--- a/test.txt").append("\n");
    sb.append("+++ b/test.txt").append("\n");
    sb.append("@@ -1 +1 @@").append("\n");
    sb.append("-test").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    sb.append("+hi").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    assertEquals(sb.toString(), parts[1]);
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitDiffTest.java

@Test
public void testDiffPaths() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());

    String projectName = getMethodName();
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject testTxt = getChild(project, "test.txt");
    modifyFile(testTxt, "hi");

    JSONObject folder1 = getChild(project, "folder");
    JSONObject folderTxt = getChild(folder1, "folder.txt");
    modifyFile(folderTxt, "folder change");

    WebRequest request = getGetGitDiffRequest(
            project.getJSONObject(GitConstants.KEY_GIT).getString(GitConstants.KEY_DIFF) + "?parts=diff",
            new String[] { "folder/folder.txt" });
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    StringBuilder sb1 = new StringBuilder();
    sb1.append("diff --git a/folder/folder.txt b/folder/folder.txt").append("\n");
    sb1.append("index 0119635..95c4c65 100644").append("\n");
    sb1.append("--- a/folder/folder.txt").append("\n");
    sb1.append("+++ b/folder/folder.txt").append("\n");
    sb1.append("@@ -1 +1 @@").append("\n");
    sb1.append("-folder").append("\n");
    sb1.append("\\ No newline at end of file").append("\n");
    sb1.append("+folder change").append("\n");
    sb1.append("\\ No newline at end of file").append("\n");
    assertEquals(sb1.toString(), response.getText());

    request = getGetGitDiffRequest(/*  w ww. j  a v  a 2s.  c o m*/
            project.getJSONObject(GitConstants.KEY_GIT).getString(GitConstants.KEY_DIFF) + "?parts=diff",
            new String[] { "test.txt" });
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    StringBuilder sb2 = new StringBuilder();
    sb2.append("diff --git a/test.txt b/test.txt").append("\n");
    sb2.append("index 30d74d2..32f95c0 100644").append("\n");
    sb2.append("--- a/test.txt").append("\n");
    sb2.append("+++ b/test.txt").append("\n");
    sb2.append("@@ -1 +1 @@").append("\n");
    sb2.append("-test").append("\n");
    sb2.append("\\ No newline at end of file").append("\n");
    sb2.append("+hi").append("\n");
    sb2.append("\\ No newline at end of file").append("\n");
    assertEquals(sb2.toString(), response.getText());

    request = getGetGitDiffRequest(
            project.getJSONObject(GitConstants.KEY_GIT).getString(GitConstants.KEY_DIFF) + "?parts=diff",
            new String[] { "folder/folder.txt", "test.txt" });
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    sb1.append(sb2);
    assertEquals(sb1.toString(), response.getText());
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitDiffTest.java

@Test
public void testDiffCommits() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());

    String projectName = getMethodName();
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject testTxt = getChild(project, "test.txt");
    modifyFile(testTxt, "first change");
    addFile(testTxt);//www  . j a v a2 s .  com

    JSONObject commit1 = commitFile(testTxt, "commit1");

    // modify again
    modifyFile(testTxt, "second change");
    addFile(testTxt);

    // commit2
    JSONObject commit2 = commitFile(testTxt, "commit2");

    String testTxtHeadLocation = testTxt.getJSONObject(GitConstants.KEY_GIT).getString(GitConstants.KEY_HEAD);
    JSONObject testTxtInitialCommit = findCommitByMessage(testTxtHeadLocation, "Initial commit");
    String gitDiffUri = getDiffLocation(testTxtInitialCommit.getString(GitConstants.KEY_DIFF),
            commit1.getString(ProtocolConstants.KEY_NAME));
    String[] parts = getDiff(gitDiffUri);
    StringBuilder sb = new StringBuilder();
    sb.append("diff --git a/test.txt b/test.txt").append("\n");
    sb.append("index 30d74d2..3c26ed4 100644").append("\n");
    sb.append("--- a/test.txt").append("\n");
    sb.append("+++ b/test.txt").append("\n");
    sb.append("@@ -1 +1 @@").append("\n");
    sb.append("-test").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    sb.append("+first change").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    assertEquals(sb.toString(), parts[1]);

    gitDiffUri = getDiffLocation(testTxtInitialCommit.getString(GitConstants.KEY_DIFF),
            commit2.getString(ProtocolConstants.KEY_NAME));
    parts = getDiff(gitDiffUri);
    sb.setLength(0);
    sb.append("diff --git a/test.txt b/test.txt").append("\n");
    sb.append("index 30d74d2..58bcb48 100644").append("\n");
    sb.append("--- a/test.txt").append("\n");
    sb.append("+++ b/test.txt").append("\n");
    sb.append("@@ -1 +1 @@").append("\n");
    sb.append("-test").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    sb.append("+second change").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    assertEquals(sb.toString(), parts[1]);
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitDiffTest.java

@Test
public void testDiffPost() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());

    String projectName = getMethodName();
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);

    JSONObject testTxt = getChild(project, "test.txt");
    modifyFile(testTxt, "change");
    addFile(testTxt);//from   w  w w . j av a2s . c  o  m

    commitFile(testTxt, "commit1", false);

    String testTxtHeadLocation = testTxt.getJSONObject(GitConstants.KEY_GIT).getString(GitConstants.KEY_HEAD);
    JSONObject testTxtInitialCommit = findCommitByMessage(testTxtHeadLocation, "Initial commit");
    String location = getDiffLocation(testTxtInitialCommit.getString(GitConstants.KEY_DIFF), Constants.HEAD);

    String diffScope = URLEncoder.encode(
            testTxtInitialCommit.getString(ProtocolConstants.KEY_NAME) + ".." + Constants.HEAD, "UTF-8");
    String expectedLocation = gitSection.getString(GitConstants.KEY_DIFF)
            .replaceAll(GitConstants.KEY_DIFF_DEFAULT, diffScope);
    expectedLocation += "test.txt";
    assertEquals(expectedLocation, location);

    WebRequest request = getGetRequest(location);
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    String[] parts = parseMultiPartResponse(response);

    assertDiffUris(expectedLocation, new String[] { "test", "change", "test" }, new JSONObject(parts[0]));

    StringBuilder sb = new StringBuilder();
    sb.append("diff --git a/test.txt b/test.txt").append("\n");
    sb.append("index 30d74d2..8013df8 100644").append("\n");
    sb.append("--- a/test.txt").append("\n");
    sb.append("+++ b/test.txt").append("\n");
    sb.append("@@ -1 +1 @@").append("\n");
    sb.append("-test").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    sb.append("+change").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    assertEquals(sb.toString(), parts[1]);
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitDiffTest.java

@Test
public void testDiffParts() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());

    String projectName = getMethodName();
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject testTxt = getChild(project, "test.txt");
    modifyFile(testTxt, "change");
    addFile(testTxt);/*from ww  w  . ja v  a2s  . c om*/

    JSONObject commit = commitFile(testTxt, "commit");

    String testTxtHeadLocation = testTxt.getJSONObject(GitConstants.KEY_GIT).getString(GitConstants.KEY_HEAD);
    JSONObject testTxtInitialCommit = findCommitByMessage(testTxtHeadLocation, "Initial commit");
    String location = getDiffLocation(testTxtInitialCommit.getString(GitConstants.KEY_DIFF),
            commit.getString(ProtocolConstants.KEY_NAME));

    String[] parts = getDiff(location + "?parts=uris,diff");

    assertDiffUris(location, new String[] { "test", "change", "test" }, new JSONObject(parts[0]));

    StringBuilder sb = new StringBuilder();
    sb.append("diff --git a/test.txt b/test.txt").append("\n");
    sb.append("index 30d74d2..8013df8 100644").append("\n");
    sb.append("--- a/test.txt").append("\n");
    sb.append("+++ b/test.txt").append("\n");
    sb.append("@@ -1 +1 @@").append("\n");
    sb.append("-test").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    sb.append("+change").append("\n");
    sb.append("\\ No newline at end of file").append("\n");
    assertEquals(sb.toString(), parts[1]);

    parts = getDiff(location + "?parts=diff,uris");

    assertDiffUris(location, new String[] { "test", "change", "test" }, new JSONObject(parts[0]));
    assertEquals(sb.toString(), parts[1]);

    WebRequest request = getGetRequest(location + "?parts=diff");
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    assertEquals(sb.toString(), response.getText());

    request = getGetRequest(location + "?parts=uris");
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    assertDiffUris(location, new String[] { "test", "change", "test" }, new JSONObject(response.getText()));
}