@Test public void testLeftJoin() throws Exception { Dao<Bar, Integer> barDao = createDao(Bar.class, true); Dao<Baz, Integer> bazDao = createDao(Baz.class, true); Bar bar1 = new Bar(); bar1.val = 2234; assertEquals(1, barDao.create(bar1)); Bar bar2 = new Bar(); bar2.val = 324322234; assertEquals(1, barDao.create(bar2)); Baz baz1 = new Baz(); baz1.bar = bar1; assertEquals(1, bazDao.create(baz1)); Baz baz2 = new Baz(); baz2.bar = bar2; assertEquals(1, bazDao.create(baz2)); Baz baz3 = new Baz(); // no bar assertEquals(1, bazDao.create(baz3)); QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder(); List<Baz> results = bazDao.queryBuilder().query(); assertEquals(3, results.size()); results = bazDao.queryBuilder().join(barQb).query(); assertEquals(2, results.size()); results = bazDao.queryBuilder().leftJoin(barQb).query(); assertEquals(3, results.size()); results = bazDao.queryBuilder().join(barQb, JoinType.LEFT, JoinWhereOperation.AND).query(); assertEquals(3, results.size()); }
@Test public void testGeneric() { // Check that normal bean injection is working correctly! assertNotNull(baz2.getCorge()); assertEquals(baz2.getCorge().getName(), "fred"); // Test that the generic configuration injection wiring is working for bar assertNotNull(bar1.getInjectedMessage()); assertEquals(bar1.getInjectedMessage().value(), "hello1"); assertNotNull(bar2.getInjectedMessage()); assertEquals(bar2.getInjectedMessage().value(), "hello2"); // Check that the generic configuration injection wiring is working for baz assertNotNull(baz1.getMessage()); assertEquals(baz1.getMessage().value(), "hello1"); assertNotNull(baz2.getMessage()); assertEquals(baz2.getMessage().value(), "hello2"); assertNotNull(baz5.getMessage()); assertEquals(baz5.getMessage().value(), "hello5"); // Check that this isn't affecting annotations on the generic bean without @Inject assertNull(baz1.getBar().getMessage()); assertNull(baz2.getBar().getMessage()); }
@Test public void testJoinOrder() throws Exception { Dao<Bar, Integer> barDao = createDao(Bar.class, true); Dao<Baz, Integer> bazDao = createDao(Baz.class, true); Bar bar1 = new Bar(); bar1.val = 2234; assertEquals(1, barDao.create(bar1)); Bar bar2 = new Bar(); bar2.val = 324322234; assertEquals(1, barDao.create(bar2)); Baz baz1 = new Baz(); baz1.bar = bar1; assertEquals(1, bazDao.create(baz1)); Baz baz2 = new Baz(); baz2.bar = bar2; assertEquals(1, bazDao.create(baz2)); QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder(); barQb.orderBy(Bar.VAL_FIELD, true); List<Baz> results = bazDao.queryBuilder().join(barQb).query(); assertEquals(2, results.size()); assertEquals(bar1.id, results.get(0).bar.id); assertEquals(bar2.id, results.get(1).bar.id); // reset the query to change the order direction barQb.reset(); barQb.orderBy(Bar.VAL_FIELD, false); results = bazDao.queryBuilder().join(barQb).query(); assertEquals(2, results.size()); assertEquals(bar2.id, results.get(0).bar.id); assertEquals(bar1.id, results.get(1).bar.id); }
@Test public void testJoinGroup() throws Exception { Dao<Bar, Integer> barDao = createDao(Bar.class, true); Dao<Baz, Integer> bazDao = createDao(Baz.class, true); Bar bar1 = new Bar(); bar1.val = 2234; assertEquals(1, barDao.create(bar1)); Bar bar2 = new Bar(); bar2.val = 324322234; assertEquals(1, barDao.create(bar2)); Baz baz1 = new Baz(); baz1.bar = bar1; assertEquals(1, bazDao.create(baz1)); Baz baz2 = new Baz(); baz2.bar = bar2; assertEquals(1, bazDao.create(baz2)); QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder(); barQb.where().eq(Bar.VAL_FIELD, bar1.val); barQb.groupBy(Bar.ID_FIELD); List<Baz> results = bazDao.queryBuilder().query(); assertEquals(2, results.size()); results = bazDao.queryBuilder().groupBy(Baz.ID_FIELD).join(barQb).query(); assertEquals(1, results.size()); assertEquals(bar1.id, results.get(0).bar.id); }
@Test public void testBaseClassComparison() throws Exception { Dao<Bar, String> barDao = createDao(Bar.class, true); Dao<Baz, String> bazDao = createDao(Baz.class, true); BarSuperClass bar1 = new BarSuperClass(); bar1.val = 10; assertEquals(1, barDao.create(bar1)); Baz baz1 = new Baz(); baz1.bar = bar1; assertEquals(1, bazDao.create(baz1)); List<Baz> results = bazDao.queryBuilder().where().eq(Baz.BAR_FIELD, bar1).query(); assertEquals(1, results.size()); assertEquals(bar1.id, results.get(0).bar.id); try { // we allow a super class of the field but _not_ a sub class results = bazDao.queryBuilder().where().eq(Baz.BAR_FIELD, new Object()).query(); fail("Should have thrown"); } catch (SQLException e) { // expected } }
public void testCustomizeAssemblerFactory() throws Exception { configure("AssemblerFactory.dicon"); S2Container container = S2ContainerFactory.create(getClass().getName().replace('.', '/') + ".foo.dicon"); container.init(); Baz baz = (Baz) container.getComponent("baz"); assertNull("1", baz.getFoo()); }
@Test public void testScope() { assertNotSame(baz1.getCorge(), baz2.getCorge()); assertNotNull(baz3); assertNotNull(baz3a); assertEquals(baz3.getCorge(), baz3a.getCorge()); assertNotNull(baz4); assertNotNull(baz4a); assertEquals(baz4.getCorge(), baz4a.getCorge()); assertNotSame(baz3.getCorge(), baz4.getCorge()); }
@Test public void testMultipleJoin() throws Exception { Dao<Bar, Integer> barDao = createDao(Bar.class, true); Dao<Baz, Integer> bazDao = createDao(Baz.class, true); Dao<Bing, Integer> bingDao = createDao(Bing.class, true); Bar bar1 = new Bar(); bar1.val = 2234; assertEquals(1, barDao.create(bar1)); Bar bar2 = new Bar(); bar2.val = 324322234; assertEquals(1, barDao.create(bar2)); Baz baz1 = new Baz(); baz1.bar = bar1; assertEquals(1, bazDao.create(baz1)); Baz baz2 = new Baz(); baz2.bar = bar2; assertEquals(1, bazDao.create(baz2)); Bing bing1 = new Bing(); bing1.baz = baz1; assertEquals(1, bingDao.create(bing1)); Bing bing2 = new Bing(); bing2.baz = baz1; assertEquals(1, bingDao.create(bing2)); QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder(); barQb.where().eq(Bar.VAL_FIELD, bar1.val); List<Baz> results = bazDao.queryBuilder().query(); assertEquals(2, results.size()); QueryBuilder<Bing, Integer> bingQb = bingDao.queryBuilder(); bingQb.where().eq(Bing.ID_FIELD, bing2.id); List<Baz> bingResults = bazDao.queryBuilder().query(); assertEquals(2, bingResults.size()); results = bazDao.queryBuilder().join(barQb).join(bingQb).query(); assertEquals(1, results.size()); assertEquals(bar1.id, results.get(0).bar.id); }
/** * Verify RobustReflectionConverter can handle missing fields in a class extending * Throwable/Exception (default ThrowableConverter registered by XStream calls ReflectionConverter * directly, rather than our RobustReflectionConverter replacement). (HUDSON-5769) */ public void testUnmarshalThrowableMissingField() { Baz baz = new Baz(); baz.myFailure = new Exception("foo"); XStream2 xs = new XStream2(); String xml = xs.toXML(baz); baz = (Baz) xs.fromXML(xml); assertEquals("foo", baz.myFailure.getMessage()); baz = (Baz) xs.fromXML( "<hudson.util.XStream2Test_-Baz><myFailure>" + "<missingField>true</missingField>" + "<detailMessage>hoho</detailMessage>" + "<stackTrace><trace>" + "hudson.util.XStream2Test.testUnmarshalThrowableMissingField(XStream2Test.java:97)" + "</trace></stackTrace>" + "</myFailure></hudson.util.XStream2Test_-Baz>"); // Object should load, despite "missingField" in XML above assertEquals("hoho", baz.myFailure.getMessage()); }
@Test public void testSimpleJoinColumnArg() throws Exception { Dao<Bar, Integer> barDao = createDao(Bar.class, true); Dao<Baz, Integer> bazDao = createDao(Baz.class, true); Bar bar1 = new Bar(); int val = 1313123; bar1.val = val; assertEquals(1, barDao.create(bar1)); Bar bar2 = new Bar(); bar2.val = val; assertEquals(1, barDao.create(bar2)); Baz baz1 = new Baz(); baz1.bar = bar1; baz1.val = val; assertEquals(1, bazDao.create(baz1)); Baz baz2 = new Baz(); baz2.bar = bar2; baz2.val = val + 1; assertEquals(1, bazDao.create(baz2)); Baz baz3 = new Baz(); baz1.bar = bar2; baz1.val = val; assertEquals(1, bazDao.create(baz3)); Baz baz4 = new Baz(); baz2.bar = bar1; baz2.val = val; assertEquals(1, bazDao.create(baz4)); QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder(); barQb.where().eq(Bar.VAL_FIELD, new ColumnArg("baz", Baz.VAL_FIELD)); List<Baz> results = bazDao.queryBuilder().query(); assertEquals(4, results.size()); results = bazDao.queryBuilder().join(barQb).query(); assertEquals(1, results.size()); assertEquals(bar1.id, results.get(0).bar.id); }
@Test public void testSimpleJoinOr() throws Exception { Dao<Bar, Integer> barDao = createDao(Bar.class, true); Dao<Baz, Integer> bazDao = createDao(Baz.class, true); Bar bar1 = new Bar(); bar1.val = 2234; assertEquals(1, barDao.create(bar1)); Bar bar2 = new Bar(); bar2.val = 324322234; assertEquals(1, barDao.create(bar2)); Baz baz1 = new Baz(); baz1.bar = bar1; baz1.val = 423423; assertEquals(1, bazDao.create(baz1)); Baz baz2 = new Baz(); baz2.bar = bar2; baz2.val = 9570423; assertEquals(1, bazDao.create(baz2)); QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder(); barQb.where().eq(Bar.VAL_FIELD, bar1.val); List<Baz> results = bazDao.queryBuilder().query(); assertEquals(2, results.size()); QueryBuilder<Baz, Integer> bazQb = bazDao.queryBuilder(); bazQb.where().eq(Baz.VAL_FIELD, baz2.val); results = bazQb.joinOr(barQb).query(); assertEquals(2, results.size()); assertEquals(bar1.id, results.get(0).bar.id); assertEquals(bar2.id, results.get(1).bar.id); bazQb.reset(); bazQb.where().eq(Baz.VAL_FIELD, baz2.val); results = bazQb.join(barQb, JoinType.INNER, JoinWhereOperation.OR).query(); assertEquals(2, results.size()); assertEquals(bar1.id, results.get(0).bar.id); assertEquals(bar2.id, results.get(1).bar.id); // now do join which should be an AND bazQb.reset(); bazQb.where().eq(Baz.VAL_FIELD, baz2.val); results = bazQb.join(barQb).query(); // should find no results assertEquals(0, results.size()); }
public void testBaz() throws Exception { Baz baz = SerializableObjects.baz; roundTrip(baz, Baz.getSchema(), Baz.getPipeSchema()); }
@Test(description = "A simple test to check conversation replication") public void testConversationReplication() throws Exception { TestContainer container1 = bootstrapContainer(1, Collections.singletonList(Baz.class)); BeanManagerImpl beanManager1 = getBeanManager(container1); TestContainer container2 = bootstrapContainer(2, Collections.singletonList(Baz.class)); BeanManagerImpl beanManager2 = getBeanManager(container2); use(1); // Set up the conversation context BoundRequest request1 = new BoundRequestImpl(container1.getSessionStore()); BoundConversationContext conversationContext1 = Utils.getReference(beanManager1, BoundConversationContext.class); conversationContext1.associate(request1); conversationContext1.activate(); // Set a value into Baz1 Baz baz1 = Utils.getReference(beanManager1, Baz.class); baz1.setName("pete"); // Begin the conversation Conversation conversation1 = Utils.getReference(beanManager1, Conversation.class); conversation1.begin(); // refetch the test bean and check it has the right value baz1 = Utils.getReference(beanManager1, Baz.class); assert baz1.getName().equals("pete"); // Simulate ending the request (from the POV of the conversation only!) assert !conversation1.isTransient(); String cid = conversation1.getId(); conversationContext1.invalidate(); conversationContext1.deactivate(); conversationContext1.dissociate(request1); // and start another, propagating the conversation request1 = new BoundRequestImpl(container1.getSessionStore()); conversationContext1.associate(request1); conversationContext1.activate(cid); // refetch the test bean and check it has the right value baz1 = Utils.getReference(beanManager1, Baz.class); assert baz1.getName().equals("pete"); assert !conversation1.isTransient(); replicateSession(1, container1, 2, container2); use(2); // Set up the conversation context BoundRequest request2 = new BoundRequestImpl(container2.getSessionStore()); BoundConversationContext conversationContext2 = Utils.getReference(beanManager2, BoundConversationContext.class); conversationContext2.associate(request2); conversationContext2.activate(cid); Baz baz2 = Utils.getReference(beanManager2, Baz.class); assert baz2.getName().equals("pete"); Conversation conversation2 = Utils.getReference(beanManager2, Conversation.class); assert !conversation2.isTransient(); use(2); container2.stopContainer(); use(1); container1.stopContainer(); }