@Test public void totalOrder() throws RuleBaseException { SortBlock block = define("<sort by='ascending' priority='5'>@foo</sort>"); final OrderGraph graph = mockery.mock(OrderGraph.class); mockery.checking( new Expectations() { { one(graph).order("x2", "x1", 5); one(graph).order("x2", "x3", 5); one(graph).order("x2", "x4", 5); one(graph).order("x1", "x4", 5); one(graph).order("x3", "x4", 5); } }); List<Pair<String, Integer>> list = new ArrayList<Pair<String, Integer>>(); list.add(Pair.of("x1", 23)); list.add(Pair.of("x2", 18)); list.add(Pair.of("x3", 23)); list.add(Pair.of("x4", 35)); Comparator<Pair<String, Integer>> comparator = new Comparator<Pair<String, Integer>>() { public int compare(Pair<String, Integer> o1, Pair<String, Integer> o2) { return o1.second.compareTo(o2.second); } }; block.totalOrder(list, comparator, graph); }
@SuppressWarnings("unchecked") @Test public void sortAfter() throws RuleBaseException { final Node comp1 = content.query().single("/id('comp1')").node(); final Node comp2 = content.query().single("/id('comp2')").node(); final Node cname = content.query().single("/id('cname')").node(); SortBySiblingBlock block = define("<sort after='sibling'>uml:name</sort>"); SortBySiblingBlock.SortBySiblingSeg seg1 = (SortBySiblingBlock.SortBySiblingSeg) block.createSeg(mod); SortBySiblingBlock.SortBySiblingSeg seg2 = (SortBySiblingBlock.SortBySiblingSeg) block.createSeg(mod); seg1.siblingsByTarget = Arrays.<Pair<Node, List<Node>>>asList( new Pair[] { Pair.of(comp1, Arrays.asList(cname, comp2)), Pair.of(comp2, Arrays.asList(cname)) }); seg2.siblingsByTarget = Arrays.<Pair<Node, List<Node>>>asList(new Pair[] {Pair.of(cname, Arrays.asList(comp2))}); final OrderGraph graph = mockery.mock(OrderGraph.class); mockery.checking( new Expectations() { { one(graph).order(cname, comp1, 0); one(graph).order(comp2, comp1, 0); one(graph).order(cname, comp2, 0); one(graph).order(comp2, cname, 0); } }); block.sort(Arrays.asList(seg1, seg2), graph); }
@Test public void restore() throws RuleBaseException, TransformException { Node comp1 = content.query().single("/id('comp1')").node(); Node comp2 = content.query().single("/id('comp2')").node(); Node cname = content.query().single("/id('cname')").node(); setModNearestAncestorImplementing( NodeTarget.class, new NodeTarget() { public ItemList targets() throws TransformException { return content.query().all("/id('comp1 comp2')"); } }); setModData("(<sort-siblings run-length='2'/>, <sort-siblings run-length='1'/>)"); setModReferences(cname, comp2, cname); SortBySiblingBlock block = define("<sort before='sibling'>uml:name</sort>"); SortBySiblingBlock.SortBySiblingSeg seg = (SortBySiblingBlock.SortBySiblingSeg) block.createSeg(mod); seg.restore(); assertEquals( Arrays.asList( new Pair[] { Pair.of(comp1, Arrays.asList(cname, comp2)), Pair.of(comp2, Arrays.asList(cname)) }), seg.siblingsByTarget); }
@Test(expected = TransformException.class) public void verifyBad() throws RuleBaseException, TransformException { SortByValueBlock block = define("<sort by='ascending'>@name</sort>"); SortByValueBlock.SortByValueSeg seg = (SortByValueBlock.SortByValueSeg) block.createSeg(mod); setModData( "(<sort-value refid='m1'>start</sort-value>, <sort-value refid='m2'>foo</sort-value>)"); seg.values = new ArrayList<Pair<String, Item>>(); seg.values.add(Pair.of("m1", content.query().single("/id('m1')/@name").toAtomicItem())); seg.values.add(Pair.of("m2", content.query().single("/id('m2')/@name").toAtomicItem())); seg.verify(); }
@SuppressWarnings("unchecked") @Test(expected = TransformException.class) public void verifyMismatch() throws RuleBaseException, TransformException { Node uc1 = content.query().single("/id('uc1')").node(); Node comp1 = content.query().single("/id('comp1')").node(); Node comp2 = content.query().single("/id('comp2')").node(); Node cname = content.query().single("/id('cname')").node(); setModScope(uc1.query()); SortBySiblingBlock block = define("<sort before='sibling'>uml:name</sort>"); SortBySiblingBlock.SortBySiblingSeg seg = (SortBySiblingBlock.SortBySiblingSeg) block.createSeg(mod); seg.siblingsByTarget = Arrays.<Pair<Node, List<Node>>>asList( new Pair[] { Pair.of(comp1, Arrays.asList(comp2)), Pair.of(comp2, Arrays.asList(cname)) }); seg.verify(); }
@Test public void sortDescending() throws RuleBaseException { SortByValueBlock block = define("<sort by='descending'>@name</sort>"); SortByValueBlock.SortByValueSeg seg1 = (SortByValueBlock.SortByValueSeg) block.createSeg(mod); SortByValueBlock.SortByValueSeg seg2 = (SortByValueBlock.SortByValueSeg) block.createSeg(mod); seg1.values = new ArrayList<Pair<String, Item>>(); seg1.values.add(Pair.of("m1", content.query().single("/id('m1')/@name").toAtomicItem())); seg2.values = new ArrayList<Pair<String, Item>>(); seg2.values.add(Pair.of("m2", content.query().single("/id('m2')/@name").toAtomicItem())); final OrderGraph graph = mockery.mock(OrderGraph.class); mockery.checking( new Expectations() { { one(graph).order("m1", "m2", 0); } }); block.sort(Arrays.asList(seg1, seg2), graph); }
@Override public void restore() throws TransformException { List<Node> references = mod.references(); ItemList targets = mod.nearest(NodeTarget.class).targets(); proxies = new ArrayList<Pair<String, Node>>(references.size()); for (int i = 0; i < references.size(); i++) { proxies.add(Pair.of(targets.get(i).query().single("@xml:id").value(), references.get(i))); } }
@Test public void sort() throws RuleBaseException { SortByProxyBlock block = define("<sort as='corresponding'>$source</sort>"); SortByProxyBlock.SortByProxySeg seg1 = (SortByProxyBlock.SortByProxySeg) block.createSeg(mod); SortByProxyBlock.SortByProxySeg seg2 = (SortByProxyBlock.SortByProxySeg) block.createSeg(mod); seg1.proxies = new ArrayList<Pair<String, Node>>(); seg2.proxies = new ArrayList<Pair<String, Node>>(); seg1.proxies.add(Pair.of("um1", content.query().single("/id('m1')").node())); seg2.proxies.add(Pair.of("uf1", content.query().single("/id('f1')").node())); seg1.proxies.add(Pair.of("um2", content.query().single("/id('m2')").node())); seg2.proxies.add(Pair.of("uf2", content.query().single("/id('f1')").node())); final OrderGraph graph = mockery.mock(OrderGraph.class); mockery.checking( new Expectations() { { one(graph).order("um1", "um2", 0); } }); block.sort(Arrays.asList(seg1, seg2), graph); }
@Override public void restore() throws TransformException { ItemList runLengths = mod.supplementQuery().all("sort-siblings/@run-length"); ItemList targets = mod.nearest(NodeTarget.class).targets(); assert runLengths.size() == targets.size(); for (int i = 0, j = 0; i < targets.size(); i++) { int runLength = runLengths.get(i).intValue(); siblingsByTarget.add( Pair.of(targets.get(i).node(), mod.references().subList(j, j + runLength))); j += runLength; } }
@Override public void restore() throws TransformException { ItemList targets = mod.nearest(NodeTarget.class).targets(); values = new ArrayList<Pair<String, Item>>(targets.size()); for (Node node : targets.nodes()) { ItemList items = query.runOn(mod.scope(node.query())); if (items.size() != 1) throw new TransformException( "sort by value query did not select exactly one item: " + items); values.add(Pair.of(node.query().single("@xml:id").value(), items.get(0).toAtomicItem())); } }
@Test public void restore() throws RuleBaseException, TransformException { setModNearestAncestorImplementing( NodeTarget.class, new NodeTarget() { public ItemList targets() throws TransformException { return content.query().all("/id('um1 um2')"); } }); setModData("(<sort-proxy position='1'/>, <sort-proxy position='2'/>)"); setModReferences( content.query().single("/id('m1')").node(), content.query().single("/id('m2')").node()); SortByProxyBlock block = define("<sort as='corresponding'>$source</sort>"); SortByProxyBlock.SortByProxySeg seg = (SortByProxyBlock.SortByProxySeg) block.createSeg(mod); seg.restore(); assertEquals( Arrays.asList( new Pair[] { Pair.of("um1", content.query().single("/id('m1')").node()), Pair.of("um2", content.query().single("/id('m2')").node()) }), seg.proxies); }
@Test public void restore() throws RuleBaseException, TransformException { setModNearestAncestorImplementing( NodeTarget.class, new NodeTarget() { public ItemList targets() throws TransformException { return content.query().all("/id('m1 m2')"); } }); setModScope( content.query().single("/id('m1')").node().query(), content.query().single("/id('m2')").node().query()); SortByValueBlock block = define("<sort by='ascending'>@name</sort>"); SortByValueBlock.SortByValueSeg seg = (SortByValueBlock.SortByValueSeg) block.createSeg(mod); seg.restore(); assertEquals( Arrays.asList( new Pair[] { Pair.of("m1", content.query().single("/id('m1')/@name").toAtomicItem()), Pair.of("m2", content.query().single("/id('m2')/@name").toAtomicItem()) }), seg.values); }
private void runVerifyScenario(String proxyid, String modData) throws TransformException, RuleBaseException { Node um1 = content.query().single("/id('um1')").node(); Node m1 = content.query().single("/id('m1')").node(); setModNearestAncestorImplementing( NodeTarget.class, new NodeTarget() { public ItemList targets() throws TransformException { return content.query().all("/id('um1')"); } }); setModData(modData); setModReferences(content.query().single("/id($_1)", proxyid).node()); setModScope(um1.query().let("$source", m1)); SortByProxyBlock block = define("<sort as='corresponding'>$source</sort>"); SortByProxyBlock.SortByProxySeg seg = (SortByProxyBlock.SortByProxySeg) block.createSeg(mod); seg.proxies = new ArrayList<Pair<String, Node>>(); seg.proxies.add(Pair.of("um1", mod.references().get(0))); seg.verify(); }