/** * Create a delivery options from JSON * * @param json the JSON */ public DeliveryOptions(JsonObject json) { this.timeout = json.getLong("timeout", DEFAULT_TIMEOUT); this.codecName = json.getString("codecName", null); JsonObject hdrs = json.getJsonObject("headers", null); if (hdrs != null) { headers = new CaseInsensitiveHeaders(); for (Map.Entry<String, Object> entry: hdrs) { if (!(entry.getValue() instanceof String)) { throw new IllegalStateException("Invalid type for message header value " + entry.getValue().getClass()); } headers.set(entry.getKey(), (String)entry.getValue()); } } this.localOnly = json.getBoolean("localOnly", DEFAULT_LOCAL_ONLY); }
private void testGetResourceAsStream(long ver, ClassLoader cl) throws Exception { try (InputStream is = cl.getResourceAsStream(resourceName)) { assertNotNull(is); try (Scanner scanner = new Scanner(is, "UTF-8").useDelimiter("\\A")) { assertTrue(scanner.hasNext()); JsonObject json = new JsonObject(scanner.next()); assertEquals(ver, json.getLong("ver", -1L).longValue()); } } }
@Test public void testPutLong() { assertSame(jsonObject, jsonObject.put("foo", 123l)); assertEquals(Long.valueOf(123l), jsonObject.getLong("foo")); jsonObject.put("quux", 321l); assertEquals(Long.valueOf(321l), jsonObject.getLong("quux")); assertEquals(Long.valueOf(123l), jsonObject.getLong("foo")); jsonObject.put("foo", 456l); assertEquals(Long.valueOf(456l), jsonObject.getLong("foo")); jsonObject.put("foo", (Long) null); assertTrue(jsonObject.containsKey("foo")); try { jsonObject.put(null, 123l); fail(); } catch (NullPointerException e) { // OK } }
@Test public void testGetLong() { jsonObject.put("foo", 123l); assertEquals(Long.valueOf(123l), jsonObject.getLong("foo")); jsonObject.put("bar", "hello"); try { jsonObject.getLong("bar"); fail(); } catch (ClassCastException e) { assertEquals(Long.valueOf(123l), jsonObject.getLong("foo")); jsonObject.put("foo", 123d); assertEquals(Long.valueOf(123l), jsonObject.getLong("foo")); jsonObject.put("foo", 123f); assertEquals(Long.valueOf(123l), jsonObject.getLong("foo")); jsonObject.put("foo", Long.MAX_VALUE); assertEquals(Long.valueOf(Long.MAX_VALUE), jsonObject.getLong("foo")); assertNull(jsonObject.getLong("foo")); assertNull(jsonObject.getLong("absent")); jsonObject.getLong(null); fail(); } catch (NullPointerException e) {
@Test public void testGetLongDefault() { jsonObject.put("foo", 123l); assertEquals(Long.valueOf(123l), jsonObject.getLong("foo", 321l)); assertEquals(Long.valueOf(123), jsonObject.getLong("foo", null)); jsonObject.put("bar", "hello"); try { jsonObject.getLong("bar", 123l); fail(); } catch (ClassCastException e) { assertEquals(Long.valueOf(123l), jsonObject.getLong("foo", 321l)); jsonObject.put("foo", 123d); assertEquals(Long.valueOf(123l), jsonObject.getLong("foo", 321l)); jsonObject.put("foo", 123f); assertEquals(Long.valueOf(123l), jsonObject.getLong("foo", 321l)); jsonObject.put("foo", Long.MAX_VALUE); assertEquals(Long.valueOf(Long.MAX_VALUE), jsonObject.getLong("foo", 321l)); assertNull(jsonObject.getLong("foo", 321l)); assertEquals(Long.valueOf(321l), jsonObject.getLong("absent", 321l)); assertNull(jsonObject.getLong("foo", null)); assertNull(jsonObject.getLong("absent", null)); jsonObject.getLong(null, null); fail(); } catch (NullPointerException e) {
@Test public void testWithConfProvidedInline() throws IOException { long someNumber = new Random().nextLong(); setManifest("MANIFEST-Launcher-Http-Verticle.MF"); cli.dispatch(new Launcher(), new String[] {"--conf={\"random\":" + someNumber + "}"}); assertWaitUntil(() -> { try { return getHttpCode() == 200; } catch (IOException e) { return false; } }); assertThat(getContent().getJsonObject("conf").getLong("random")).isEqualTo(someNumber); }
@Test public void testRedeploymentWithSlash() throws IOException { cli.dispatch(new Launcher(), new String[]{"run", // Use "/", on windows it gets replaced. HttpTestVerticle.class.getName(), "--redeploy=**" + "/" + "*.txt", "--launcher-class=" + Launcher.class.getName(), ExecUtils.isWindows() ? "--redeploy-termination-period=3000" : "" }); assertWaitUntil(() -> { try { return RunCommandTest.getHttpCode() == 200; } catch (IOException e) { return false; } }); long start1 = RunCommandTest.getContent().getLong("startTime"); File file = new File("target/test-classes/foo.txt"); if (file.exists()) { file.delete(); } file.createNewFile(); assertWaitUntil(() -> { try { return RunCommandTest.getHttpCode() == 200 && start1 != RunCommandTest.getContent().getLong("startTime"); } catch (IOException e) { return false; } }, 20000); }
@Test public void testRedeployment() throws IOException { cli.dispatch(new Launcher(), new String[]{"run", HttpTestVerticle.class.getName(), "--redeploy=**" + File.separator + "*.txt", "--launcher-class=" + Launcher.class.getName(), ExecUtils.isWindows() ? "--redeploy-termination-period=3000" : "" }); assertWaitUntil(() -> { try { return RunCommandTest.getHttpCode() == 200; } catch (IOException e) { return false; } }); long start1 = RunCommandTest.getContent().getLong("startTime"); File file = new File("target/test-classes/foo.txt"); if (file.exists()) { file.delete(); } file.createNewFile(); assertWaitUntil(() -> { try { return RunCommandTest.getHttpCode() == 200 && start1 != RunCommandTest.getContent().getLong("startTime"); } catch (IOException e) { return false; } }, 20000); }
@Test public void testThatCustomLauncherCanUpdateConfigurationWhenNoneArePassed() throws IOException { long time = System.nanoTime(); Launcher myLauncher = new Launcher() { @Override protected String getMainVerticle() { return HttpTestVerticle.class.getName(); } @Override public void afterStartingVertx(Vertx vertx) { LauncherExtensibilityTest.this.vertx = vertx; } @Override public void afterConfigParsed(JsonObject config) { config.put("time", time); } }; myLauncher.dispatch(new String[0]); assertWaitUntil(() -> { try { return RunCommandTest.getHttpCode() == 200; } catch (IOException e) { return false; } }); assertThat(RunCommandTest.getContent().getJsonObject("conf").getLong("time")).isEqualTo(time); }
@Test public void testThatCustomLauncherCanUpdateConfiguration() throws IOException { long time = System.nanoTime(); Launcher myLauncher = new Launcher() { @Override protected String getMainVerticle() { return HttpTestVerticle.class.getName(); } @Override public void afterStartingVertx(Vertx vertx) { LauncherExtensibilityTest.this.vertx = vertx; } @Override public void afterConfigParsed(JsonObject config) { config.put("time", time); } }; myLauncher.dispatch(new String[] {"-conf=\"{\"time\":345667}"}); assertWaitUntil(() -> { try { return RunCommandTest.getHttpCode() == 200; } catch (IOException e) { return false; } }); assertThat(RunCommandTest.getContent().getJsonObject("conf").getLong("time")).isEqualTo(time); }
assertEquals("bar", jsonObject.getString("str")); assertEquals(Integer.valueOf(123), jsonObject.getInteger("int")); assertEquals(Long.valueOf(123l), jsonObject.getLong("long")); assertEquals(Float.valueOf(1.23f), jsonObject.getFloat("float")); assertEquals(Double.valueOf(1.23d), jsonObject.getDouble("double"));
@Test public void testDecode() throws Exception { byte[] bytes = TestUtils.randomByteArray(10); String strBytes = Base64.getEncoder().encodeToString(bytes); Instant now = Instant.now(); String strInstant = ISO_INSTANT.format(now); String json = "{\"mystr\":\"foo\",\"myint\":123,\"mylong\":1234,\"myfloat\":1.23,\"mydouble\":2.34,\"" + "myboolean\":true,\"mybinary\":\"" + strBytes + "\",\"myinstant\":\"" + strInstant + "\",\"mynull\":null,\"myobj\":{\"foo\":\"bar\"},\"myarr\":[\"foo\",123]}"; JsonObject obj = new JsonObject(json); assertEquals(json, obj.encode()); assertEquals("foo", obj.getString("mystr")); assertEquals(Integer.valueOf(123), obj.getInteger("myint")); assertEquals(Long.valueOf(1234), obj.getLong("mylong")); assertEquals(Float.valueOf(1.23f), obj.getFloat("myfloat")); assertEquals(Double.valueOf(2.34d), obj.getDouble("mydouble")); assertTrue(obj.getBoolean("myboolean")); assertArrayEquals(bytes, obj.getBinary("mybinary")); assertEquals(Base64.getEncoder().encodeToString(bytes), obj.getValue("mybinary")); assertEquals(now, obj.getInstant("myinstant")); assertEquals(now.toString(), obj.getValue("myinstant")); assertTrue(obj.containsKey("mynull")); JsonObject nestedObj = obj.getJsonObject("myobj"); assertEquals("bar", nestedObj.getString("foo")); JsonArray nestedArr = obj.getJsonArray("myarr"); assertEquals("foo", nestedArr.getString(0)); assertEquals(Integer.valueOf(123), Integer.valueOf(nestedArr.getInteger(1))); }
@Override public SessionStore init(Vertx vertx, JsonObject options) { this.vertx = vertx; this.sessionMapName = options.getString("mapName", DEFAULT_SESSION_MAP_NAME); this.retryTimeout = options.getLong("retryTimeout", DEFAULT_RETRY_TIMEOUT); this.random = new PRNG(vertx); return this; }
public SockJSHandlerOptions(JsonObject json) { this.sessionTimeout = json.getLong("sessionTimeout", DEFAULT_SESSION_TIMEOUT); this.insertJSESSIONID = json.getBoolean("insertJSESSIONID", DEFAULT_INSERT_JSESSIONID); this.heartbeatInterval = json.getLong("heartbeatInterval", DEFAULT_HEARTBEAT_INTERVAL); this.maxBytesStreaming = json.getInteger("maxBytesStreaming", DEFAULT_MAX_BYTES_STREAMING); this.libraryURL = json.getString("libraryURL", DEFAULT_LIBRARY_URL); JsonArray arr = json.getJsonArray("disabledTransports"); if (arr != null) { for (Object str : arr) { if (str instanceof String) { String sstr = (String) str; disabledTransports.add(sstr); } else { throw new IllegalArgumentException("Invalid type " + str.getClass() + " in disabledTransports array"); } } } }
/** * Create a delivery options from JSON * * @param json the JSON */ public DeliveryOptions(JsonObject json) { this.timeout = json.getLong("timeout", DEFAULT_TIMEOUT); this.codecName = json.getString("codecName", null); JsonObject hdrs = json.getJsonObject("headers", null); if (hdrs != null) { headers = new CaseInsensitiveHeaders(); for (Map.Entry<String, Object> entry: hdrs) { if (!(entry.getValue() instanceof String)) { throw new IllegalStateException("Invalid type for message header value " + entry.getValue().getClass()); } headers.set(entry.getKey(), (String)entry.getValue()); } } this.localOnly = json.getBoolean("localOnly", DEFAULT_LOCAL_ONLY); }
@Override public SessionStore init(Vertx vertx, JsonObject options) { // initialize a secure random this.random = new PRNG(vertx); this.vertx = vertx; this.reaperInterval = options.getLong("reaperInterval", DEFAULT_REAPER_INTERVAL); localMap = vertx.sharedData().getLocalMap(options.getString("mapName", DEFAULT_SESSION_MAP_NAME)); setTimer(); return this; }
private void testGetResourceAsStream(long ver, ClassLoader cl) throws Exception { try (InputStream is = cl.getResourceAsStream(resourceName)) { assertNotNull(is); try (Scanner scanner = new Scanner(is, "UTF-8").useDelimiter("\\A")) { assertTrue(scanner.hasNext()); JsonObject json = new JsonObject(scanner.next()); assertEquals(ver, json.getLong("ver", -1L).longValue()); } } }
@Test public void testPutLong() { assertSame(jsonObject, jsonObject.put("foo", 123l)); assertEquals(Long.valueOf(123l), jsonObject.getLong("foo")); jsonObject.put("quux", 321l); assertEquals(Long.valueOf(321l), jsonObject.getLong("quux")); assertEquals(Long.valueOf(123l), jsonObject.getLong("foo")); jsonObject.put("foo", 456l); assertEquals(Long.valueOf(456l), jsonObject.getLong("foo")); jsonObject.put("foo", (Long) null); assertTrue(jsonObject.containsKey("foo")); try { jsonObject.put(null, 123l); fail(); } catch (NullPointerException e) { // OK } }
@Test public void testWithConfProvidedInline() throws IOException { long someNumber = new Random().nextLong(); setManifest("MANIFEST-Launcher-Http-Verticle.MF"); cli.dispatch(new Launcher(), new String[] {"--conf={\"random\":" + someNumber + "}"}); assertWaitUntil(() -> { try { return getHttpCode() == 200; } catch (IOException e) { return false; } }); assertThat(getContent().getJsonObject("conf").getLong("random")).isEqualTo(someNumber); }
@Test public void testThatCustomLauncherCanUpdateConfiguration() throws IOException { long time = System.nanoTime(); Launcher myLauncher = new Launcher() { @Override protected String getMainVerticle() { return HttpTestVerticle.class.getName(); } @Override public void afterStartingVertx(Vertx vertx) { LauncherExtensibilityTest.this.vertx = vertx; } @Override public void afterConfigParsed(JsonObject config) { config.put("time", time); } }; myLauncher.dispatch(new String[] {"-conf=\"{\"time\":345667}"}); assertWaitUntil(() -> { try { return RunCommandTest.getHttpCode() == 200; } catch (IOException e) { return false; } }); assertThat(RunCommandTest.getContent().getJsonObject("conf").getLong("time")).isEqualTo(time); }