QueryTree extends CompositeItem { public Item and(Item item) { Item result = and(getRoot(), item); setRoot(result); return result; } QueryTree(Item root); void setIndexName(String index); ItemType getItemType(); String getName(); int encode(ByteBuffer buffer); Item getRoot(); final void setRoot(Item root); @Override boolean equals(Object o); @Override QueryTree clone(); @Override void addItem(Item item); @Override void addItem(int index, Item item); boolean isEmpty(); Item and(Item item); static List<IndexedItem> getPositiveTerms(Item item);  }
@Test public void testAddQueryItemWithRoot() { Assert.assertEquals("AND a b", new QueryTree(new WordItem("a")).and(new WordItem("b")).toString()); NotItem not = new NotItem(); not.addNegativeItem(new WordItem("b")); assertEquals("+a -b", new QueryTree(new WordItem("a")).and(not).toString()); }
@Test public void addNotToNot() { NotItem not1 = new NotItem(); not1.addPositiveItem(new WordItem("p1")); not1.addNegativeItem(new WordItem("n1.1")); not1.addNegativeItem(new WordItem("n1.2")); NotItem not2 = new NotItem(); not2.addPositiveItem(new WordItem("p2")); not2.addNegativeItem(new WordItem("n2.1")); not2.addNegativeItem(new WordItem("n2.2")); QueryTree tree = new QueryTree(not1); tree.and(not2); assertEquals("+(AND p1 p2) -n1.1 -n1.2 -n2.1 -n2.2", tree.toString()); }