Search in sources :

Example 51 with TypeToken

use of com.google.gson.reflect.TypeToken in project sonar-java by SonarSource.

the class JavaSquidSensorTest method verify_analysis_errors_are_collected_on_parse_error.

@Test
public void verify_analysis_errors_are_collected_on_parse_error() throws Exception {
    SensorContextTester context = createParseErrorContext();
    context.settings().setProperty(SonarComponents.COLLECT_ANALYSIS_ERRORS_KEY, true);
    executeJavaSquidSensor(context);
    String feedback = context.<String>measure("projectKey", "sonarjava_feedback").value();
    Collection<AnalysisError> analysisErrors = new Gson().fromJson(feedback, new TypeToken<Collection<AnalysisError>>() {
    }.getType());
    assertThat(analysisErrors).hasSize(1);
    AnalysisError analysisError = analysisErrors.iterator().next();
    assertThat(analysisError.getMessage()).startsWith("Parse error at line 6 column 1:");
    assertThat(analysisError.getCause()).startsWith("com.sonar.sslr.api.RecognitionException: Parse error at line 6 column 1:");
    assertThat(analysisError.getFilename()).endsWith("ParseError.java");
    assertThat(analysisError.getKind()).isEqualTo(AnalysisError.Kind.PARSE_ERROR);
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) AnalysisError(org.sonar.java.AnalysisError) TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 52 with TypeToken

use of com.google.gson.reflect.TypeToken in project sonar-java by SonarSource.

the class SonarComponentsTest method sonarcloud_feedback_metric_should_not_exceed_roughly_200ko.

@Test
public void sonarcloud_feedback_metric_should_not_exceed_roughly_200ko() {
    File file = new File("src/test/files/ParseError.java");
    SensorContextTester sensorContext = SensorContextTester.create(file.getParentFile().getAbsoluteFile());
    sensorContext.settings().setProperty(SonarComponents.COLLECT_ANALYSIS_ERRORS_KEY, true);
    Measure<String> feedback = analysisWithAnError(sensorContext);
    Collection<AnalysisError> analysisErrorsDeserialized = new Gson().fromJson(feedback.value(), new TypeToken<Collection<AnalysisError>>() {
    }.getType());
    assertThat(analysisErrorsDeserialized.size()).isBetween(35, 45);
    assertThat(analysisErrorsDeserialized.iterator().next().getKind()).isEqualTo(AnalysisError.Kind.PARSE_ERROR);
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 53 with TypeToken

use of com.google.gson.reflect.TypeToken in project libSBOLj by SynBioDex.

the class SynBioHubFrontend method search.

/**
 * Search this SynBioHub instance for objects matching a search query
 *
 * @param query the search query
 *
 * @return An ArrayList of MetaData for objects that match the specified search query
 *
 * @throws SynBioHubException if there was an error communicating with the SynBioHub
 */
public ArrayList<IdentifiedMetadata> search(SearchQuery query) throws SynBioHubException {
    String url = backendUrl + "/search/";
    // query.offset = offset;
    // query.limit = limit;
    String textQuery = "";
    boolean first = true;
    for (SearchCriteria criteria : query.getCriteria()) {
        if (criteria.getKey().equals("objectType")) {
            url += encodeUri(criteria.getKey() + "=" + criteria.getValue() + "&");
            continue;
        }
        if (criteria.getKey().equals("name")) {
            if (first)
                first = false;
            else
                textQuery = " ";
            textQuery = criteria.getValue();
            continue;
        }
        if (criteria.getKey().startsWith("http")) {
            url += encodeUri("<" + criteria.getKey() + ">=");
        } else {
            url += encodeUri(criteria.getKey() + "=");
        }
        if (criteria.getValue().startsWith("http")) {
            url += encodeUri("<" + criteria.getValue() + ">&");
        } else {
            url += encodeUri("'" + criteria.getValue() + "'&");
        }
    }
    url += encodeUri(textQuery);
    if (query.getOffset() != null && query.getLimit() != null) {
        url += "/?offset=" + query.getOffset() + "&" + "limit=" + query.getLimit();
    } else if (query.getOffset() != null) {
        url += "/?offset=" + query.getOffset();
    } else if (query.getLimit() != null) {
        url += "/?limit=" + query.getLimit();
    }
    // System.out.println(url);
    Gson gson = new Gson();
    HttpGet request = new HttpGet(url);
    request.setHeader("X-authorization", user);
    request.setHeader("Accept", "text/plain");
    try {
        HttpResponse response = client.execute(request);
        checkResponseCode(response);
        InputStream inputStream = response.getEntity().getContent();
        ArrayList<IdentifiedMetadata> metadataList = gson.fromJson(new InputStreamReader(inputStream), new TypeToken<ArrayList<IdentifiedMetadata>>() {
        }.getType());
        return metadataList;
    } catch (Exception e) {
        throw new SynBioHubException(e);
    } finally {
        request.releaseConnection();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) Gson(com.google.gson.Gson) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SBOLValidationException(org.sbolstandard.core2.SBOLValidationException) TypeToken(com.google.gson.reflect.TypeToken)

Example 54 with TypeToken

use of com.google.gson.reflect.TypeToken in project libSBOLj by SynBioDex.

the class SynBioHubFrontend method getRootCollectionMetadata.

/**
 * Search the default store for Collections that are not members of any other Collections
 *
 * @return An ArrayList of CollectionMetaData objects with a summary of all matching Collections.
 *
 * @throws SynBioHubException if there was an error communicating with the SynBioHub
 */
public ArrayList<IdentifiedMetadata> getRootCollectionMetadata() throws SynBioHubException {
    String url = backendUrl + "/rootCollections";
    Gson gson = new Gson();
    HttpGet request = new HttpGet(url);
    request.setHeader("X-authorization", user);
    request.setHeader("Accept", "text/plain");
    try {
        HttpResponse response = client.execute(request);
        checkResponseCode(response);
        InputStream inputStream = response.getEntity().getContent();
        ArrayList<IdentifiedMetadata> metadataList = gson.fromJson(new InputStreamReader(inputStream), new TypeToken<ArrayList<IdentifiedMetadata>>() {
        }.getType());
        return metadataList;
    } catch (Exception e) {
        throw new SynBioHubException(e);
    } finally {
        request.releaseConnection();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TypeToken(com.google.gson.reflect.TypeToken) HttpGet(org.apache.http.client.methods.HttpGet) Gson(com.google.gson.Gson) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SBOLValidationException(org.sbolstandard.core2.SBOLValidationException)

Example 55 with TypeToken

use of com.google.gson.reflect.TypeToken in project EssayJoke by qiyei2015.

the class HttpUtil method gsonToGetParams.

/**
 * 将 httpGet请求进行参数的序列化为map对象
 * @param request
 * @param <T>
 * @return
 */
public static <T> Map<String, String> gsonToGetParams(HttpRequest<T> request) {
    // 将对象序列化成字符串
    String gsonStr = new Gson().toJson(request.getBody());
    Map<String, String> map = new Gson().fromJson(gsonStr, new TypeToken<HashMap<String, String>>() {
    }.getType());
    return map;
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson)

Aggregations

TypeToken (com.google.gson.reflect.TypeToken)418 Gson (com.google.gson.Gson)178 Test (org.junit.Test)99 IOException (java.io.IOException)83 Map (java.util.Map)71 List (java.util.List)56 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)54 ArrayList (java.util.ArrayList)53 HashMap (java.util.HashMap)52 GsonBuilder (com.google.gson.GsonBuilder)45 File (java.io.File)34 Notebook (org.apache.zeppelin.notebook.Notebook)32 Type (java.lang.reflect.Type)31 FileNotFoundException (java.io.FileNotFoundException)29 Paragraph (org.apache.zeppelin.notebook.Paragraph)27 RestResponse (com.google.gerrit.acceptance.RestResponse)24 JsonElement (com.google.gson.JsonElement)24 JsonObject (com.google.gson.JsonObject)24 OutputStreamWriter (java.io.OutputStreamWriter)22 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)21