/** * This file Copyright (c) 2011 Magnolia International * Ltd. (http://www.magnolia-cms.com). All rights reserved. * * * This file is dual-licensed under both the Magnolia * Network Agreement and the GNU General Public License. * You may elect to use one or the other of these licenses. * * This file is distributed in the hope that it will be * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. * Redistribution, except as permitted by whichever of the GPL * or MNA you select, is prohibited. * * 1. For the GPL license (GPL), you can redistribute and/or * modify this file under the terms of the GNU General * Public License, Version 3, as published by the Free Software * Foundation. You should have received a copy of the GNU * General Public License, Version 3 along with this program; * if not, write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * 2. For the Magnolia Network Agreement (MNA), this file * and the accompanying materials are made available under the * terms of the MNA which accompanies this distribution, and * is available at http://www.magnolia-cms.com/mna.html * * Any modifications to this file must keep this entire header * intact. * */ package info.magnolia.forum.externalindexing.util; import info.magnolia.cms.core.Content; import info.magnolia.cms.core.ItemType; import info.magnolia.nodebuilder.NodeBuilder; import info.magnolia.test.mock.MockContent; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; import org.junit.Before; import org.junit.Test; import javax.jcr.RepositoryException; import static info.magnolia.nodebuilder.Ops.addNode; import static org.junit.Assert.*; /** * * @author gjoseph * @version $Revision: $ ($Author: $) */ public class ContentUtilTest { private MockContent root; @Before public void setUp() { root = new MockContent("root", ItemType.CONTENT); new NodeBuilder(root, addNode("a", ItemType.CONTENT).then( addNode("aa", ItemType.CONTENT), addNode("ab", ItemType.CONTENTNODE).then( addNode("abb", ItemType.CONTENTNODE) ) ), addNode("b", ItemType.CONTENT), addNode("c", ItemType.CONTENTNODE) ).exec(); } @Test public void getAncestorOfTypeBasicCase() throws RepositoryException { final Content content = root.getContent("a/ab/abb"); assertEquals(root.getContent("a"), ContentUtil.getAncestorOfType(content, ItemType.CONTENT.getSystemName())); } @Test public void getAncestorOfTypeReturnsSelfIfMatch() throws RepositoryException { final Content abb = root.getContent("a/ab/abb"); final Content ab = root.getContent("a/ab"); final Content aa = root.getContent("a/aa"); final Content c = root.getContent("c"); assertEquals(abb, ContentUtil.getAncestorOfType(abb, ItemType.CONTENTNODE.getSystemName())); assertEquals(ab, ContentUtil.getAncestorOfType(ab, ItemType.CONTENTNODE.getSystemName())); assertEquals(aa, ContentUtil.getAncestorOfType(aa, ItemType.CONTENT.getSystemName())); assertEquals(c, ContentUtil.getAncestorOfType(c, ItemType.CONTENTNODE.getSystemName())); } @Test public void getAncestorOfTypeThrowsExceptionIfNotFound() throws RepositoryException { final Content content = root.getContent("a/aa"); try { ContentUtil.getAncestorOfType(content, ItemType.CONTENTNODE.getSystemName()); fail("should have failed"); } catch (Throwable t) { assertThat(t, isExceptionWithMessage(IllegalStateException.class, "No ancestor of type mgnl:contentNode found for null:/root/a/aa[mgnl:content]")); } } // TODO copied from info.magnolia.test.MgnlMatchers public static Matcher isExceptionWithMessage(final Class t, final String expectedMessage) { return new TypeSafeMatcher() { @Override protected boolean matchesSafely(Throwable item) { return item.getClass().isAssignableFrom(t) && expectedMessage.equals(item.getMessage()); } @Override public void describeTo(Description description) { description.appendText("a " + t.getSimpleName() + " with message [" + expectedMessage + "]"); } }; } }