Java Code Examples for org.litepal.LitePal#find()

The following examples show how to use org.litepal.LitePal#find() . 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: TransactionTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactionForUpdate() {
    Teacher teacher = new Teacher();
    teacher.setTeacherName("Tony");
    teacher.setTeachYears(3);
    teacher.setAge(23);
    teacher.setSex(false);
    Assert.assertTrue(teacher.save());
    LitePal.beginTransaction();
    ContentValues values = new ContentValues();
    values.put("TeachYears", 13);
    int rows = LitePal.update(Teacher.class, values, teacher.getId());
    Assert.assertEquals(1, rows);
    Teacher teacherFromDb = LitePal.find(Teacher.class, teacher.getId());
    Assert.assertEquals(13, teacherFromDb.getTeachYears());
    // not set transaction successful
    LitePal.endTransaction();
    teacherFromDb = LitePal.find(Teacher.class, teacher.getId());
    Assert.assertEquals(3, teacherFromDb.getTeachYears());
}
 
Example 2
Source File: UpdateUsingSaveMethodTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateM2OAssociationsOnMSide() {
	init();
	s1.setClassroom(c1);
	s2.setClassroom(c1);
	assertTrue(c1.save());
	assertTrue(c2.save());
	assertTrue(s1.save());
	assertTrue(s2.save());
	s1.setClassroom(c2);
	s2.setClassroom(c2);
	Calendar calendar = Calendar.getInstance();
	calendar.clear();
	calendar.set(1989, 7, 7, 0, 0, 0);
	s2.setBirthday(calendar.getTime());
	assertTrue(s1.save());
	assertTrue(s2.save());
	assertEquals(c2.get_id(), getForeignKeyValue(studentTable, classroomTable, s1.getId()));
	assertEquals(c2.get_id(), getForeignKeyValue(studentTable, classroomTable, s2.getId()));
	Student student2 = LitePal.find(Student.class, s2.getId());
	calendar.clear();
	calendar.set(1989, 7, 7, 0, 0, 0);
	assertEquals(calendar.getTimeInMillis(), student2.getBirthday().getTime());
}
 
Example 3
Source File: UpdateUsingSaveMethodTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateGenericData() {
    Classroom classroom = new Classroom();
    classroom.setName("Classroom origin");
    classroom.getNews().add("n");
    classroom.getNews().add("e");
    classroom.getNews().add("w");
    classroom.getNumbers().add(1);
    classroom.getNumbers().add(2);
    classroom.getNumbers().add(3);
    classroom.save();
    classroom.setName("Classroom update");
    classroom.getNews().add("s");
    classroom.getNumbers().clear();
    classroom.save();
    Classroom c = LitePal.find(Classroom.class, classroom.get_id());
    assertEquals("Classroom update", c.getName());
    assertEquals(4, classroom.getNews().size());
    assertEquals(0, classroom.getNumbers().size());
    StringBuilder builder = new StringBuilder();
    for (String s : classroom.getNews()) {
        builder.append(s);
    }
    assertEquals("news", builder.toString());
}
 
Example 4
Source File: UpdateUsingUpdateMethodTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateGenericData() {
    Classroom c = new Classroom();
    c.setName("Math room");
    c.getNews().add("news");
    c.getNews().add("paper");
    c.update(classroom.get_id());
    Classroom result = LitePal.find(Classroom.class, classroom.get_id());
    assertEquals("Math room", result.getName());
    StringBuilder builder = new StringBuilder();
    for (String s : result.getNews()) {
        builder.append(s);
    }
    assertEquals("newspaper", builder.toString());
    assertEquals(2, result.getNumbers().size());
    Classroom c2 = new Classroom();
    c2.setToDefault("numbers");
    c2.update(classroom.get_id());
    result = LitePal.find(Classroom.class, classroom.get_id());
    assertEquals("Math room", result.getName());
    assertEquals(2, result.getNews().size());
    assertEquals(0, result.getNumbers().size());
}
 
Example 5
Source File: UpdateUsingUpdateMethodTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateToDefaultValueWithInstanceUpdate() {
	Student s = new Student();
	s.setToDefault("age");
	s.setToDefault("name");
	s.setToDefault("birthday");
	int affectedStudent = s.update(student.getId());
	assertEquals(1, affectedStudent);
	Student newStudent = LitePal.find(Student.class, student.getId());
	assertNull(newStudent.getBirthday());
	assertNull(newStudent.getName());
	assertEquals(0, newStudent.getAge());
	Teacher t = new Teacher();
	t.setAge(45);
	t.setTeachYears(5);
	t.setTeacherName("John");
	t.setToDefault("teacherName");
	t.setToDefault("age");
	int affectedTeacher = t.update(teacher.getId());
	assertEquals(1, affectedTeacher);
	Teacher newTeacher = getTeacher(teacher.getId());
	assertEquals(22, newTeacher.getAge());
	assertEquals("", newTeacher.getTeacherName());
	assertEquals(5, newTeacher.getTeachYears());
}
 
Example 6
Source File: QueryBasicTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testFind() {
	short isbn = 30013;
	Book book = new Book();
	book.setArea(10.5f);
	book.setBookName("Android First Line");
	book.setIsbn(isbn);
	book.setLevel('A');
	book.setPages(450);
	book.setPrice(49.99);
	book.setPublished(false);
	book.save();
	Book b = LitePal.find(Book.class, book.getId());
	assertEquals(book.getId(), b.getId());
	assertEquals(10.5f, b.getArea());
	assertEquals("Android First Line", b.getBookName());
	assertEquals(isbn, b.getIsbn());
	assertEquals('A', b.getLevel());
	assertTrue(450 == b.getPages());
	assertEquals(49.99, b.getPrice());
	assertFalse(b.isPublished());
	assertTrue(b.isSaved());
}
 
Example 7
Source File: SaveTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testSaveGenericData() {
    Classroom classroom = new Classroom();
    classroom.setName("classroom1");
    classroom.getNews().add("news1");
    classroom.getNews().add("news2");
    classroom.getNews().add("news3");
    List<Integer> numbers = new ArrayList<>();
    numbers.add(1);
    numbers.add(2);
    numbers.add(3);
    numbers.add(4);
    classroom.setNumbers(numbers);
    classroom.save();
    Classroom c = LitePal.find(Classroom.class, classroom.get_id());
    assertEquals("classroom1", c.getName());
    assertEquals(3, c.getNews().size());
    assertEquals(4, c.getNumbers().size());
    for (String news : c.getNews()) {
        assertTrue(news.equals("news1") || news.equals("news2") || news.equals("news3"));
    }
    for (int number : c.getNumbers()) {
        assertTrue(number == 1 || number == 2 || number == 3 || number == 4);
    }
}
 
Example 8
Source File: SaveTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testSaveInheritModels() {
    WeChatMessage weChatMessage = new WeChatMessage();
    weChatMessage.setFriend("Tom");
    weChatMessage.setContent("Hello nice to meet you");
    weChatMessage.setTitle("Greeting message");
    weChatMessage.setType(1);
    assertTrue(weChatMessage.save());
    assertTrue(weChatMessage.getId() > 0);
    WeChatMessage message1 = LitePal.find(WeChatMessage.class, weChatMessage.getId());
    assertEquals("Tom", message1.getFriend());
    assertEquals("Hello nice to meet you", message1.getContent());
    assertNull(message1.getTitle());
    assertEquals(1, message1.getType());

    WeiboMessage weiboMessage = new WeiboMessage();
    weiboMessage.setType(2);
    weiboMessage.setTitle("Following message");
    weiboMessage.setContent("Something big happens");
    weiboMessage.setFollower("Jimmy");
    weiboMessage.setNumber(123456);
    assertTrue(weiboMessage.save());
    assertTrue(weiboMessage.getId() > 0);
}
 
Example 9
Source File: SaveTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testSaveWithConstructors() {
	Computer computer = new Computer("asus", 699.00);
	assertTrue(computer.save());
	assertTrue(isDataExists(getTableName(computer), computer.getId()));
	Computer c = getComputer(computer.getId());
	assertEquals("asus", c.getBrand());
	assertEquals(699.00, c.getPrice());
	Computer cc = LitePal.find(Computer.class, computer.getId());
	assertEquals("asus", cc.getBrand());
	assertEquals(699.00, cc.getPrice());
	Product p = new Product(null);
	p.setBrand("apple");
	p.setPrice(1222.33);
	p.save();
}
 
Example 10
Source File: TransactionTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactionForDelete() {
    Student tony = new Student();
    tony.setName("Tony");
    tony.setAge(23);
    tony.save();
    int studentId = tony.getId();
    LitePal.beginTransaction();
    int rowsAffected = tony.delete();
    Assert.assertEquals(1, rowsAffected);
    Student studentFromDb = LitePal.find(Student.class, studentId);
    Assert.assertNull(studentFromDb);
    // not set transaction successful
    LitePal.endTransaction();
    studentFromDb = LitePal.find(Student.class, studentId);
    Assert.assertNotNull(studentFromDb);
    Assert.assertEquals("Tony", studentFromDb.getName());
    Assert.assertEquals(23, studentFromDb.getAge());
}
 
Example 11
Source File: SaveTest.java    From LitePal with Apache License 2.0 5 votes vote down vote up
@Test
   public void testSaveLongMaximumNumber() {
   	IdCard idCard = new IdCard();
   	idCard.setSerial(Long.MAX_VALUE);
   	idCard.setAddress("abczyx");
   	assertTrue(idCard.save());
   	IdCard idCardFromDB = LitePal.find(IdCard.class, idCard.getId());
   	assertEquals(Long.MAX_VALUE, idCardFromDB.getSerial());
}
 
Example 12
Source File: QueryDateTest.java    From LitePal with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryDate() {
	Calendar calendar = Calendar.getInstance();
	calendar.clear();
	calendar.set(1990, 9, 16, 0, 0, 0);
	Student student1 = new Student();
	student1.setName("Student 1");
	student1.setBirthday(calendar.getTime());
	student1.save();
	Student studentFromDB = LitePal.find(Student.class, student1.getId());
	assertEquals("Student 1", studentFromDB.getName());
	assertEquals(calendar.getTimeInMillis(), studentFromDB.getBirthday().getTime());
}
 
Example 13
Source File: QueryDateTest.java    From LitePal with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryDateBefore1970() {
	Calendar calendar = Calendar.getInstance();
	calendar.clear();
	calendar.set(1920, 6, 3, 0, 0, 0);
	Student student1 = new Student();
	student1.setName("Student 2");
	student1.setBirthday(calendar.getTime());
	student1.save();
	Student studentFromDB = LitePal.find(Student.class, student1.getId());
	assertEquals("Student 2", studentFromDB.getName());
	assertEquals(calendar.getTimeInMillis(), studentFromDB.getBirthday().getTime());
}
 
Example 14
Source File: QueryDateTest.java    From LitePal with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryDateWithDefaultValue() {
	Student student = new Student();
	student.setName("School Student");
	assertTrue(student.save());
	Student studentFromDB = LitePal.find(Student.class, student.getId());
	assertEquals(1589203961859L, studentFromDB.getSchoolDate().getTime());
}
 
Example 15
Source File: TransactionTest.java    From LitePal with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransactionSuccessfulForCRUD() {
    LitePal.beginTransaction();
    Student tony = new Student();
    tony.setName("Tony");
    tony.setAge(23);
    tony.save();
    int studentId = tony.getId();
    Student studentFromDb = LitePal.find(Student.class, studentId);
    Assert.assertNotNull(studentFromDb);
    Assert.assertEquals("Tony", studentFromDb.getName());
    Assert.assertEquals(23, studentFromDb.getAge());
    Student updateModel = new Student();
    updateModel.setAge(25);
    int rowsAffected = updateModel.update(studentId);
    Assert.assertEquals(1, rowsAffected);
    studentFromDb = LitePal.find(Student.class, studentId);
    Assert.assertEquals(25, studentFromDb.getAge());
    rowsAffected = tony.delete();
    Assert.assertEquals(1, rowsAffected);
    studentFromDb = LitePal.find(Student.class, studentId);
    Assert.assertNull(studentFromDb);
    Assert.assertTrue(tony.save());
    studentFromDb = LitePal.find(Student.class, tony.getId());
    Assert.assertNotNull(studentFromDb);
    LitePal.setTransactionSuccessful();
    LitePal.endTransaction();
    studentFromDb = LitePal.find(Student.class, tony.getId());
    Assert.assertNotNull(studentFromDb);
    Assert.assertEquals("Tony", studentFromDb.getName());
    Assert.assertEquals(23, studentFromDb.getAge());
}
 
Example 16
Source File: UpdateUsingUpdateMethodTest.java    From LitePal with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateAllWithInstanceUpdate() {
	Student s;
	int[] ids = new int[5];
	for (int i = 0; i < 5; i++) {
		s = new Student();
		s.setName("Jessica");
		s.setAge(i + 10);
		s.save();
		ids[i] = s.getId();
	}
	Date date = new Date();
	Student toUpdate = new Student();
	toUpdate.setAge(24);
	toUpdate.setBirthday(date);
	int affectedRows = toUpdate.updateAll(new String[] { "name = ? and age = ?", "Jessica", "13" });
	assertEquals(1, affectedRows);
	Student updatedStu = LitePal.find(Student.class, ids[3]);
	assertEquals(24, updatedStu.getAge());
	assertEquals(date.getTime(), updatedStu.getBirthday().getTime());
	toUpdate.setAge(18);
	toUpdate.setName("Jess");
	affectedRows = toUpdate.updateAll(new String[] { "name = ?", "Jessica" });
	assertEquals(5, affectedRows);
	List<Student> students = getStudents(ids);
	for (Student updatedStudent : students) {
		assertEquals("Jess", updatedStudent.getName());
		assertEquals(18, updatedStudent.getAge());
	}
}
 
Example 17
Source File: TransactionTest.java    From LitePal with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransactionForCRUD() {
    LitePal.beginTransaction();
    Student tony = new Student();
    tony.setName("Tony");
    tony.setAge(23);
    tony.save();
    int studentId = tony.getId();
    Student studentFromDb = LitePal.find(Student.class, studentId);
    Assert.assertNotNull(studentFromDb);
    Assert.assertEquals("Tony", studentFromDb.getName());
    Assert.assertEquals(23, studentFromDb.getAge());
    Student updateModel = new Student();
    updateModel.setAge(25);
    int rowsAffected = updateModel.update(studentId);
    Assert.assertEquals(1, rowsAffected);
    studentFromDb = LitePal.find(Student.class, studentId);
    Assert.assertEquals(25, studentFromDb.getAge());
    rowsAffected = tony.delete();
    Assert.assertEquals(1, rowsAffected);
    studentFromDb = LitePal.find(Student.class, studentId);
    Assert.assertNull(studentFromDb);
    Assert.assertTrue(tony.save());
    studentFromDb = LitePal.find(Student.class, tony.getId());
    Assert.assertNotNull(studentFromDb);
    // not set transaction successful
    LitePal.endTransaction();
    studentFromDb = LitePal.find(Student.class, tony.getId());
    Assert.assertNull(studentFromDb);
}
 
Example 18
Source File: TransactionTest.java    From LitePal with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransactionForSaveAll() {
    LitePal.beginTransaction();
    String serial = UUID.randomUUID().toString();
    WeiboMessage weiboMessage = new WeiboMessage();
    try {
        weiboMessage.setFollower("nobody");
        boolean saveResult = weiboMessage.save();
        List<Cellphone> cellphones = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Cellphone cellphone = new Cellphone();
            cellphone.setBrand("Apple");
            cellphone.setSerial(serial + (i % 10)); // serial is unique, so this should save failed
            cellphone.getMessages().add(weiboMessage);
            cellphones.add(cellphone);
        }
        boolean saveAllResult = LitePal.saveAll(cellphones);
        if (saveResult && saveAllResult) {
            LitePal.setTransactionSuccessful();
        }
    } finally {
        LitePal.endTransaction();
    }
    Assert.assertTrue(weiboMessage.isSaved());
    WeiboMessage messageFromDb = LitePal.find(WeiboMessage.class, weiboMessage.getId());
    Assert.assertNull(messageFromDb);
    List<Cellphone> list = LitePal.where("serial like ?", serial + "%").find(Cellphone.class);
    assertTrue(list.isEmpty());
}
 
Example 19
Source File: SaveTest.java    From LitePal with Apache License 2.0 4 votes vote down vote up
@Test
public void testSaveInheritModelsWithAssociations() {
    Cellphone cellphone = new Cellphone();
    cellphone.setBrand("iPhone 7");
    cellphone.setInStock('N');
    cellphone.setPrice(6999.99);
    cellphone.setSerial(UUID.randomUUID().toString());
    cellphone.setMac("ff:3d:4a:99:76");
    cellphone.save();

    WeChatMessage weChatMessage = new WeChatMessage();
    weChatMessage.setFriend("Tom");
    weChatMessage.setContent("Hello nice to meet you");
    weChatMessage.setTitle("Greeting message");
    weChatMessage.setType(1);
    assertTrue(weChatMessage.save());
    assertTrue(weChatMessage.getId() > 0);
    WeChatMessage message1 = LitePal.find(WeChatMessage.class, weChatMessage.getId());
    assertEquals("Tom", message1.getFriend());
    assertEquals("Hello nice to meet you", message1.getContent());
    assertNull(message1.getTitle());
    assertEquals(1, message1.getType());

    WeiboMessage weiboMessage = new WeiboMessage();
    weiboMessage.setType(2);
    weiboMessage.setTitle("Following message");
    weiboMessage.setContent("Something big happens");
    weiboMessage.setFollower("Jimmy");
    weiboMessage.setNumber(123456);
    weiboMessage.setCellphone(cellphone);
    assertTrue(weiboMessage.save());
    assertTrue(weiboMessage.getId() > 0);
    WeiboMessage message2 = LitePal.find(WeiboMessage.class, weiboMessage.getId(), true);
    Cellphone result = message2.getCellphone();
    assertEquals(cellphone.getId(), result.getId());
    assertEquals(cellphone.getBrand(), result.getBrand());
    assertEquals(cellphone.getInStock(), result.getInStock());
    assertEquals(cellphone.getPrice(), result.getPrice());
    assertEquals(cellphone.getSerial(), result.getSerial());
    assertEquals(cellphone.getMac(), result.getMac());
}