use of java.util.Arrays in project vert.x by eclipse.
the class Http2ServerTest method testRequestResponseLifecycle.
@Test
public void testRequestResponseLifecycle() throws Exception {
waitFor(2);
server.requestHandler(req -> {
req.endHandler(v -> {
assertIllegalStateException(() -> req.setExpectMultipart(false));
assertIllegalStateException(() -> req.handler(buf -> {
}));
assertIllegalStateException(() -> req.uploadHandler(upload -> {
}));
assertIllegalStateException(() -> req.endHandler(v2 -> {
}));
complete();
});
HttpServerResponse resp = req.response();
resp.setChunked(true).write(Buffer.buffer("whatever"));
assertTrue(resp.headWritten());
assertIllegalStateException(() -> resp.setChunked(false));
assertIllegalStateException(() -> resp.setStatusCode(100));
assertIllegalStateException(() -> resp.setStatusMessage("whatever"));
assertIllegalStateException(() -> resp.putHeader("a", "b"));
assertIllegalStateException(() -> resp.putHeader("a", (CharSequence) "b"));
assertIllegalStateException(() -> resp.putHeader("a", (Iterable<String>) Arrays.asList("a", "b")));
assertIllegalStateException(() -> resp.putHeader("a", (Arrays.<CharSequence>asList("a", "b"))));
assertIllegalStateException(resp::writeContinue);
resp.end();
assertIllegalStateException(() -> resp.write("a"));
assertIllegalStateException(() -> resp.write("a", "UTF-8"));
assertIllegalStateException(() -> resp.write(Buffer.buffer("a")));
assertIllegalStateException(resp::end);
assertIllegalStateException(() -> resp.end("a"));
assertIllegalStateException(() -> resp.end("a", "UTF-8"));
assertIllegalStateException(() -> resp.end(Buffer.buffer("a")));
assertIllegalStateException(() -> resp.sendFile("the-file.txt"));
assertIllegalStateException(() -> resp.closeHandler(v -> {
}));
assertIllegalStateException(() -> resp.endHandler(v -> {
}));
assertIllegalStateException(() -> resp.drainHandler(v -> {
}));
assertIllegalStateException(() -> resp.exceptionHandler(err -> {
}));
assertIllegalStateException(resp::writeQueueFull);
assertIllegalStateException(() -> resp.setWriteQueueMaxSize(100));
assertIllegalStateException(() -> resp.putTrailer("a", "b"));
assertIllegalStateException(() -> resp.putTrailer("a", (CharSequence) "b"));
assertIllegalStateException(() -> resp.putTrailer("a", (Iterable<String>) Arrays.asList("a", "b")));
assertIllegalStateException(() -> resp.putTrailer("a", (Arrays.<CharSequence>asList("a", "b"))));
assertIllegalStateException(() -> resp.push(HttpMethod.GET, "/whatever", ar -> {
}));
complete();
});
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
request.encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of java.util.Arrays in project vert.x by eclipse.
the class Http2ClientTest method testHeaders.
@Test
public void testHeaders() throws Exception {
AtomicInteger reqCount = new AtomicInteger();
server.requestHandler(req -> {
assertEquals("https", req.scheme());
assertEquals(HttpMethod.GET, req.method());
assertEquals("/somepath", req.path());
assertEquals(DEFAULT_HTTPS_HOST_AND_PORT, req.host());
assertEquals("foo_request_value", req.getHeader("Foo_request"));
assertEquals("bar_request_value", req.getHeader("bar_request"));
assertEquals(2, req.headers().getAll("juu_request").size());
assertEquals("juu_request_value_1", req.headers().getAll("juu_request").get(0));
assertEquals("juu_request_value_2", req.headers().getAll("juu_request").get(1));
assertEquals(new HashSet<>(Arrays.asList("foo_request", "bar_request", "juu_request")), new HashSet<>(req.headers().names()));
reqCount.incrementAndGet();
HttpServerResponse resp = req.response();
resp.putHeader("content-type", "text/plain");
resp.putHeader("Foo_response", "foo_value");
resp.putHeader("bar_response", "bar_value");
resp.putHeader("juu_response", (List<String>) Arrays.asList("juu_value_1", "juu_value_2"));
resp.end();
});
startServer();
client.request(new RequestOptions().setPort(DEFAULT_HTTPS_PORT).setHost(DEFAULT_HTTPS_HOST).setURI("/somepath")).onComplete(onSuccess(req -> {
req.putHeader("Foo_request", "foo_request_value").putHeader("bar_request", "bar_request_value").putHeader("juu_request", Arrays.<String>asList("juu_request_value_1", "juu_request_value_2"));
req.send(onSuccess(resp -> {
Context ctx = vertx.getOrCreateContext();
assertOnIOContext(ctx);
assertEquals(1, resp.request().streamId());
assertEquals(1, reqCount.get());
assertEquals(HttpVersion.HTTP_2, resp.version());
assertEquals(200, resp.statusCode());
assertEquals("OK", resp.statusMessage());
assertEquals("text/plain", resp.getHeader("content-type"));
assertEquals("foo_value", resp.getHeader("foo_response"));
assertEquals("bar_value", resp.getHeader("bar_response"));
assertEquals(2, resp.headers().getAll("juu_response").size());
assertEquals("juu_value_1", resp.headers().getAll("juu_response").get(0));
assertEquals("juu_value_2", resp.headers().getAll("juu_response").get(1));
assertEquals(new HashSet<>(Arrays.asList("content-type", "content-length", "foo_response", "bar_response", "juu_response")), new HashSet<>(resp.headers().names()));
resp.endHandler(v -> {
assertOnIOContext(ctx);
testComplete();
});
}));
}));
await();
}
use of java.util.Arrays in project immutables by immutables.
the class Mappings method of.
/**
* Reflectively build <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html">elastic mapping</a>
* for a class. Currently works only for immutable interfaces and is
* very limited in its functionality.
*/
static Mapping of(Class<?> clazz) {
Objects.requireNonNull(clazz, "clazz");
Preconditions.checkArgument(clazz.isInterface(), "Expected %s to be an interface", clazz);
Map<String, String> map = new LinkedHashMap<>();
Stream<Method> methods = Arrays.stream(clazz.getDeclaredMethods()).filter(m -> m.getParameterCount() == 0).filter(m -> m.getReturnType() != Void.class).filter(m -> Modifier.isPublic(m.getModifiers())).filter(m -> !Modifier.isStatic(m.getModifiers())).filter(m -> m.getDeclaringClass() != Object.class);
for (Method method : methods.collect(Collectors.toSet())) {
Class<?> returnType = method.getReturnType();
// skip arrays and iterables (we don't handle them yet)
if (returnType.isArray() || Iterable.class.isAssignableFrom(returnType)) {
continue;
}
Type type = method.getGenericReturnType();
map.put(method.getName(), elasticType(type));
}
return Mapping.ofElastic(map);
}
use of java.util.Arrays in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method addToString.
private void addToString(JDefinedClass jclass) {
Map<String, JFieldVar> fields = jclass.fields();
JMethod toString = jclass.method(JMod.PUBLIC, String.class, "toString");
Set<String> excludes = new HashSet<>(Arrays.asList(ruleFactory.getGenerationConfig().getToStringExcludes()));
JBlock body = toString.body();
// The following toString implementation roughly matches the commons ToStringBuilder for
// backward compatibility
JClass stringBuilderClass = jclass.owner().ref(StringBuilder.class);
JVar sb = body.decl(stringBuilderClass, "sb", JExpr._new(stringBuilderClass));
// Write the header, e.g.: example.domain.MyClass@85e382a7[
body.add(sb.invoke("append").arg(jclass.dotclass().invoke("getName")).invoke("append").arg(JExpr.lit('@')).invoke("append").arg(jclass.owner().ref(Integer.class).staticInvoke("toHexString").arg(jclass.owner().ref(System.class).staticInvoke("identityHashCode").arg(JExpr._this()))).invoke("append").arg(JExpr.lit('[')));
// If this has a parent class, include its toString()
if (!jclass._extends().fullName().equals(Object.class.getName())) {
JVar baseLength = body.decl(jclass.owner().INT, "baseLength", sb.invoke("length"));
JVar superString = body.decl(jclass.owner().ref(String.class), "superString", JExpr._super().invoke("toString"));
JBlock superToStringBlock = body._if(superString.ne(JExpr._null()))._then();
// If super.toString() is in the Clazz@2ee6529d[field=10] format, extract the fields
// from the wrapper
JVar contentStart = superToStringBlock.decl(jclass.owner().INT, "contentStart", superString.invoke("indexOf").arg(JExpr.lit('[')));
JVar contentEnd = superToStringBlock.decl(jclass.owner().INT, "contentEnd", superString.invoke("lastIndexOf").arg(JExpr.lit(']')));
JConditional superToStringInnerConditional = superToStringBlock._if(contentStart.gte(JExpr.lit(0)).cand(contentEnd.gt(contentStart)));
superToStringInnerConditional._then().add(sb.invoke("append").arg(superString).arg(contentStart.plus(JExpr.lit(1))).arg(contentEnd));
// Otherwise, just append super.toString()
superToStringInnerConditional._else().add(sb.invoke("append").arg(superString));
// Append a comma if needed
body._if(sb.invoke("length").gt(baseLength))._then().add(sb.invoke("append").arg(JExpr.lit(',')));
}
// For each included instance field, add to the StringBuilder in the field=value format
for (JFieldVar fieldVar : fields.values()) {
if (excludes.contains(fieldVar.name()) || (fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
continue;
}
body.add(sb.invoke("append").arg(fieldVar.name()));
body.add(sb.invoke("append").arg(JExpr.lit('=')));
if (fieldVar.type().isPrimitive()) {
body.add(sb.invoke("append").arg(JExpr.refthis(fieldVar.name())));
} else if (fieldVar.type().isArray()) {
// Only primitive arrays are supported
if (!fieldVar.type().elementType().isPrimitive()) {
throw new UnsupportedOperationException("Only primitive arrays are supported");
}
// Leverage Arrays.toString()
body.add(sb.invoke("append").arg(JOp.cond(JExpr.refthis(fieldVar.name()).eq(JExpr._null()), JExpr.lit("<null>"), jclass.owner().ref(Arrays.class).staticInvoke("toString").arg(JExpr.refthis(fieldVar.name())).invoke("replace").arg(JExpr.lit('[')).arg(JExpr.lit('{')).invoke("replace").arg(JExpr.lit(']')).arg(JExpr.lit('}')).invoke("replace").arg(JExpr.lit(", ")).arg(JExpr.lit(",")))));
} else {
body.add(sb.invoke("append").arg(JOp.cond(JExpr.refthis(fieldVar.name()).eq(JExpr._null()), JExpr.lit("<null>"), JExpr.refthis(fieldVar.name()))));
}
body.add(sb.invoke("append").arg(JExpr.lit(',')));
}
// Add the trailer
JConditional trailerConditional = body._if(sb.invoke("charAt").arg(sb.invoke("length").minus(JExpr.lit(1))).eq(JExpr.lit(',')));
trailerConditional._then().add(sb.invoke("setCharAt").arg(sb.invoke("length").minus(JExpr.lit(1))).arg(JExpr.lit(']')));
trailerConditional._else().add(sb.invoke("append").arg(JExpr.lit(']')));
body._return(sb.invoke("toString"));
toString.annotate(Override.class);
}
use of java.util.Arrays in project jsonschema2pojo by joelittlejohn.
the class DefaultRule method getDefaultSet.
/**
* Creates a default value for a set property by:
* <ol>
* <li>Creating a new {@link LinkedHashSet} with the correct generic type
* <li>Using {@link Arrays#asList(Object...)} to initialize the set with the
* correct default values
* </ol>
*
* @param fieldType
* the java type that applies for this field ({@link Set} with
* some generic type argument)
* @param node
* the node containing default values for this set
* @return an expression that creates a default value that can be assigned
* to this field
*/
private JExpression getDefaultSet(JType fieldType, JsonNode node) {
JClass setGenericType = ((JClass) fieldType).getTypeParameters().get(0);
JClass setImplClass = fieldType.owner().ref(LinkedHashSet.class);
setImplClass = setImplClass.narrow(setGenericType);
JInvocation newSetImpl = JExpr._new(setImplClass);
if (node instanceof ArrayNode && node.size() > 0) {
JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
for (JsonNode defaultValue : node) {
invokeAsList.arg(getDefaultValue(setGenericType, defaultValue));
}
newSetImpl.arg(invokeAsList);
} else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) {
return null;
}
return newSetImpl;
}
Aggregations