Java Code Examples for org.rapidoid.setup.App#beans()

The following examples show how to use org.rapidoid.setup.App#beans() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpBeanParamsTest.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@Test
@ExpectErrors
public void testBeanParams() {
	App.beans(new Object() {

		@GET
		@SuppressWarnings("unchecked")
		public List<Object> pers(String name, Person person, int id, Integer xx) {
			return U.list(id, name, person, xx);
		}

	});

	onlyGet("/pers?name=Einstein&id=1000");
	onlyGet("/pers?name=Mozart");
	onlyGet("/pers?id=200");
}
 
Example 2
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test4() {
	App.beans(new Object() {
		@PUT
		public int test4() {
			return 12345;
		}
	});

	notFound("/");
	onlyPut("/test4");
}
 
Example 3
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test12() {
	App.beans(new Ctrl2());

	notFound("/");

	onlyGet("/x");
	onlyPost("/y");
	getAndPost("/p");

	notFound("/z");
	notFound("/w");
}
 
Example 4
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test11() {
	App.beans(Ctrl1.class);

	notFound("/");

	onlyGet("/x");
	onlyPost("/y");
	getAndPost("/p");

	notFound("/z");
	notFound("/w");
}
 
Example 5
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test10() {
	App.beans(new Ctrl1());

	notFound("/");

	onlyGet("/x");
	onlyPost("/y");
	getAndPost("/p");

	notFound("/z");
	notFound("/w");
}
 
Example 6
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test9() {
	App.beans(new Object() {
		@DELETE
		public Date test9(Req req, Resp resp) {
			return new Date(50505050);
		}
	});

	notFound("/");
	onlyDelete("/test9");
}
 
Example 7
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test8() {
	App.beans(new Object() {
		@PUT
		public Object test8(Req req, Resp resp) {
			return U.set("b", 0, false);
		}
	});

	notFound("/");
	onlyPut("/test8");
}
 
Example 8
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test7() {
	App.beans(new Object() {
		@POST
		public Object test7(Req req, Resp resp) {
			return U.list("a", 123, true);
		}
	});

	notFound("/");
	onlyPost("/test7");
}
 
Example 9
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test6() {
	App.beans(new Object() {
		@GET
		public Object test6(Req req, Resp resp) {
			return U.map("a", 1, "b", 2);
		}
	});

	notFound("/");
	onlyGet("/test6");
}
 
Example 10
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test5() {
	App.beans(new Object() {
		@DELETE
		public boolean test5() {
			return true;
		}
	});

	notFound("/");
	onlyDelete("/test5");
}
 
Example 11
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test3() {
	App.beans(new Object() {
		@POST
		public Object test3() {
			return "ABC DE";
		}
	});

	notFound("/");
	onlyPost("/test3");
}
 
Example 12
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test2() {
	App.beans(new Object() {
		@GET
		public Object test2() {
			return 123.456;
		}
	});

	notFound("/");
	onlyGet("/test2");
}
 
Example 13
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test1() {
	App.beans(new Object() {
		public String test1() {
			return "not annotated";
		}
	});

	notFound("/");
	notFound("/test1");
}
 
Example 14
Source File: HttpBeanParamsTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrimitiveLambdaParams() {
	App.beans(new Object() {
		@GET
		public Object foo(double a, double b, double c) {
			return U.join(":", a, b, c);
		}
	});

	onlyGet("/foo?a=10&b=20&c=30");
}
 
Example 15
Source File: HttpReregistrationTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testControllerReregistration() {
	notFound("/inc");
	notFound("/dec");

	App.beans(ctrl1("nextA"));
	verifyRoutes("ctrl1");

	onlyGet("/inc?x=100");

	App.beans(ctrl1("nextB"));
	verifyRoutes("ctrl1");

	onlyGet("/inc?x=200");

	App.beans(ctrl1("nextC"));
	verifyRoutes("ctrl1");

	onlyGet("/inc?x=300");

	// can deregister with other instance, only the class matters for deregistration, not the instance
	App.setup().deregister(ctrl1("invisible"));
	verifyNoRoutes();

	notFound("/inc");
	notFound("/dec");
}
 
Example 16
Source File: HttpReregistrationTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testControllerDeregistration() {
	Object ctrl1 = ctrl1("next");
	Object ctrl2 = ctrl2();

	notFound("/inc");
	notFound("/dec");

	App.beans(ctrl1);
	verifyRoutes("ctrl1");

	onlyGet("/inc?x=5");
	notFound("/dec");

	App.setup().deregister(ctrl1);
	verifyNoRoutes();

	App.beans(ctrl2);
	verifyRoutes("ctrl2");

	onlyPost("/dec?x=12");
	notFound("/inc");

	App.setup().deregister(ctrl2.getClass());
	verifyNoRoutes();

	notFound("/inc");
	notFound("/dec");
}
 
Example 17
Source File: TxErrorHandlerTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void txWrapperShouldNotDisruptCustomErrorHandling() {
	JPA.bootstrap(path());

	App.beans(new TxCtrl());

	On.error(IllegalArgumentException.class).handler((req, resp, e) -> {
		resp.code(400);
		return U.map("error", "Invalid data!");
	});

	onlyGet("/x");
}
 
Example 18
Source File: HttpPojoControllerTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testPojoHandlers() {
	App.beans(new Object() {

		@GET(uri = "/a")
		public Object theFoo() {
			return "foo";
		}

		@POST(uri = "/x")
		public Object x(Req req, Resp resp) {
			return "x";
		}
	});

	onlyGet("/a");
	onlyPost("/x");
	notFound("/b");

	List<String> ctrls = Scan.annotated(MyTestController.class).in("pkg1", "pkg2").getAll();
	isTrue(ctrls.isEmpty());

	List<String> ctrls2 = Scan.annotated(MyTestController.class).in("non-existing-pkg", "").getAll();
	eq(ctrls2, U.list(Ff.class.getName()));

	Scan.annotated(MyTestController.class, MyTestController.class).in(App.path()).forEach(App::beans);

	onlyGet("/a");
	onlyGet("/b");
	onlyPost("/x");
}
 
Example 19
Source File: HttpCachingTest.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Test
public void testHttpCachingWithAnnotations() {
	App.beans(CachingCtrl.class);

	exerciseCaching();
}
 
Example 20
Source File: CustomizationTest.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Test
public void customErrorHandlerByType() {

	App.beans(new Object() {
		@GET
		public void err5() {
			throw new SecurityException("INTENTIONAL - Access denied!");
		}
	});

	My.error(NullPointerException.class).handler((req1, resp, e) -> "MY NPE");
	My.error(RuntimeException.class).handler((req1, resp, e) -> e instanceof NotFound ? null : "MY RTE");

	On.error(SecurityException.class).handler((req1, resp, e) -> {
		resp.code(403);
		return U.map("error", "Access denied!");
	});

	My.error(SecurityException.class).handler((req1, resp, e) -> "MY SEC");

	On.get("/err1").json(req -> {
		throw new NullPointerException();
	});

	On.post("/err2").json(req -> {
		throw new RuntimeException();
	});

	On.get("/err3").json(req -> {
		throw new SecurityException("INTENTIONAL - Access denied!");
	});

	On.get("/err4").json(req -> {
		throw new OutOfMemoryError("INTENTIONAL - Out of memory!");
	});

	onlyGet("/err1");
	onlyPost("/err2");
	onlyGet("/err3");
	onlyGet("/err4");
	onlyGet("/err5");
}