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);
}
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);
}
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();
}
}
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();
}
}
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;
}
Aggregations