|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | +* @Title: QueryTest.java |
| 20 | +* @Package openthinks.libs.sql |
| 21 | +* @Description: TODO |
| 22 | +* @author dailey.yet@outlook.com |
| 23 | +* @date 2015年7月30日 |
| 24 | +* @version V1.0 |
| 25 | +*/ |
| 26 | +package openthinks.libs.sql; |
| 27 | + |
| 28 | +import java.util.List; |
| 29 | +import java.util.Locale; |
| 30 | + |
| 31 | +import openthinks.libs.sql.dhibernate.Session; |
| 32 | +import openthinks.libs.sql.dhibernate.support.SessionFactory; |
| 33 | +import openthinks.libs.sql.dhibernate.support.query.Query; |
| 34 | +import openthinks.libs.sql.dhibernate.support.query.impl.EqualsFilter; |
| 35 | +import openthinks.libs.sql.dhibernate.support.query.impl.NotEqualsFliter; |
| 36 | + |
| 37 | +import org.junit.After; |
| 38 | +import org.junit.Assert; |
| 39 | +import org.junit.Before; |
| 40 | +import org.junit.Test; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author dailey.yet@outlook.com |
| 44 | + * |
| 45 | + */ |
| 46 | +public class QueryTest { |
| 47 | + Session session; |
| 48 | + final Message testMsg = new Message(); |
| 49 | + final Message testMsg2 = new Message(); |
| 50 | + |
| 51 | + /** |
| 52 | + * @throws java.lang.Exception |
| 53 | + */ |
| 54 | + @Before |
| 55 | + public void setUp() throws Exception { |
| 56 | + session = SessionFactory.getSession(); |
| 57 | + session.disableAutoClose(); |
| 58 | + testMsg.setMessageId("QueryTest_MSG_ID_1000"); |
| 59 | + testMsg.setLocale(Locale.CHINA.toString()); |
| 60 | + testMsg.setContent("简单测试"); |
| 61 | + |
| 62 | + testMsg2.setMessageId("QueryTest_MSG_ID_1000"); |
| 63 | + testMsg2.setLocale(Locale.US.toString()); |
| 64 | + testMsg2.setContent("Simple Test"); |
| 65 | + session.save(testMsg); |
| 66 | + session.save(testMsg2); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @throws java.lang.Exception |
| 71 | + */ |
| 72 | + @After |
| 73 | + public void tearDown() throws Exception { |
| 74 | + session.delete(testMsg); |
| 75 | + session.delete(testMsg2); |
| 76 | + session.close(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void simpleQuerytest() { |
| 81 | + |
| 82 | + Query<Message> query = session.createQuery(Message.class); |
| 83 | + |
| 84 | + List<Message> msgs = query.addFilter(new EqualsFilter().filter("messageId").eq(testMsg.getMessageId())) |
| 85 | + .execute(); |
| 86 | + |
| 87 | + Assert.assertEquals(2, msgs.size()); |
| 88 | + |
| 89 | + for (Message e : msgs) { |
| 90 | + Assert.assertEquals(testMsg.getMessageId(), e.getMessageId()); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void simpleQuerytest2() { |
| 97 | + |
| 98 | + Query<Message> query = session.createQuery(Message.class); |
| 99 | + |
| 100 | + List<Message> msgs = query.addFilter(new EqualsFilter().filter("messageId").eq(testMsg.getMessageId())) |
| 101 | + .addFilter(new NotEqualsFliter().filter("locale").neq(Locale.CHINA.toString())).execute(); |
| 102 | + |
| 103 | + Assert.assertEquals(1, msgs.size()); |
| 104 | + |
| 105 | + Message msg = msgs.get(0); |
| 106 | + |
| 107 | + Assert.assertNotNull(msg); |
| 108 | + |
| 109 | + Assert.assertEquals(Locale.US.toString(), msg.getLocale()); |
| 110 | + |
| 111 | + } |
| 112 | +} |
0 commit comments