public ArrayList<Tag> Get_Tags() { try { SQLiteDatabase db = this.getReadableDatabase(); SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); builder.setTables(NotesContract.Tag.FTS_TABLE_NAME); String[] projection = NotesContract.Tag.getTagColumns(); Cursor cur = builder.query(db, projection, null, null, null, null, null, null); if (cur != null) { cur.moveToFirst(); do { Tag tag = new Tag(); tag.setTagId(Integer.valueOf(cur.getString(1))); tag.setTagName(cur.getString(2)); // add tag to list tags_list.add(tag); } while (cur.moveToNext()); } cur.close(); return tags_list; } catch (Exception e) { Log.e("SELECT All tags", " " + e); } return tags_list; }
@Test public void testComponentSpec() { em.getTransaction().begin(); long time = System.currentTimeMillis(); final Map<String, String> initialViews = Collections.singletonMap("v1", "v1value"); Tag t1 = new Tag(); t1.setTagId("tag1"); Tag t2 = new Tag(); t2.setTagId("tag2"); final List<Tag> tags = Arrays.asList(t1, t2); for (Tag t : tags) { em.persist(t); } ComponentSpec cs = createComponentSpec( Long.toString(time), "chris", "cs", "type1", "chris", tags, initialViews); em.persist(cs); ComponentSpec cs1 = createComponentSpec( Long.toString(time + 1), "chris", "cs1", "type2", "chris", Collections.<Tag>emptyList(), initialViews); em.persist(cs1); cs.getReferencedComponents().add(cs1); em.getTransaction().commit(); em.clear(); cs = em.find(ComponentSpec.class, Long.toString(time)); checkComponentSpec(cs, "chris", "type1", "chris", "cs", tags, initialViews); Assert.assertEquals(cs.getReferencedComponents().size(), 1); Assert.assertEquals( cs.getReferencedComponents().iterator().next().getComponentId(), Long.toString(time + 1)); checkComponentSpec( cs.getReferencedComponents().iterator().next(), "chris", "type2", "chris", "cs1", Collections.<Tag>emptyList(), initialViews); em.getTransaction().begin(); ComponentSpec cs2 = createComponentSpec( Long.toString(time + 2), "chris", "cs2", "type3", "chris", Collections.<Tag>emptyList(), Collections.<String, String>emptyMap()); cs = em.find(ComponentSpec.class, Long.toString(time)); cs.getReferencedComponents().add(0, cs2); em.getTransaction().commit(); em.clear(); // check cascade persist and relationship ordering Assert.assertNotNull(em.find(ComponentSpec.class, Long.toString(time + 2))); cs = em.find(ComponentSpec.class, Long.toString(time)); Assert.assertEquals(cs.getReferencedComponents().size(), 2); Iterator<ComponentSpec> it = cs.getReferencedComponents().iterator(); Assert.assertEquals(it.next().getComponentId(), Long.toString(time + 2)); Assert.assertEquals(it.next().getComponentId(), Long.toString(time + 1)); }