<commit_before>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors. 
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.dna.spi.graph.impl;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import org.jboss.dna.common.text.TextEncoder;
import org.jboss.dna.spi.graph.Name;
import org.jboss.dna.spi.graph.Path;
import org.jboss.dna.spi.graph.ValueFactory;
import org.junit.Before;
import org.junit.Test;

/**
 * @author <NAME>
 */
public class NameValueFactoryTest {

    public static final TextEncoder NO_OP_ENCODER = Path.NO_OP_ENCODER;

    private BasicNamespaceRegistry registry;
    private ValueFactory<String> stringValueFactory;
    private NameValueFactory factory;
    private TextEncoder encoder;
    private Name name;

    @Before
    public void beforeEach() throws Exception {
        this.registry = new BasicNamespaceRegistry();
        this.registry.register("dna", "http://www.jboss.org/dna/namespace");
        this.encoder = Path.DEFAULT_ENCODER;
        this.stringValueFactory = new StringValueFactory(encoder);
        this.factory = new NameValueFactory(registry, encoder, stringValueFactory);
    }

    @Test
    public void shouldCreateNameFromSingleStringInPrefixedNamespaceFormatWithoutPrefix() {
        name = factory.create("a");
        assertThat(name.getLocalName(), is("a"));
        assertThat(name.getNamespaceUri(), is(this.registry.getNamespaceForPrefix("")));
    }

    @Test
    public void shouldCreateNameFromSingleStringInPrefixedNamespaceFormat() {
        name = factory.create("dna:something");
        assertThat(name.getLocalName(), is("something"));
        assertThat(name.getNamespaceUri(), is("http://www.jboss.org/dna/namespace"));
        assertThat(name.getString(NO_OP_ENCODER), is("{http://www.jboss.org/dna/namespace}something"));
    }

    @Test
    public void shouldCreateNameFromSingleEncodedStringInPrefixedNamespaceFormat() {
        name = factory.create(encoder.encode("dna") + ":" + encoder.encode("some/thing"));
        assertThat(name.getLocalName(), is("some/thing"));
        assertThat(name.getNamespaceUri(), is("http://www.jboss.org/dna/namespace"));
        assertThat(name.getString(NO_OP_ENCODER), is("{http://www.jboss.org/dna/namespace}some/thing"));
    }

    @Test
    public void shouldCreateNameFromSingleStringInStandardFullNamespaceFormat() {
        name = factory.create("{http://www.jboss.org/dna/namespace}something");
        assertThat(name.getLocalName(), is("something"));
        assertThat(name.getNamespaceUri(), is("http://www.jboss.org/dna/namespace"));
        assertThat(name.getString(NO_OP_ENCODER), is("{http://www.jboss.org/dna/namespace}something"));
    }

    @Test
    public void shouldCreateNameFromSingleEncodedStringInStandardFullNamespaceFormat() {
        name = factory.create("{" + encoder.encode("http://www.jboss.org/dna/namespace") + "}" + encoder.encode("some/thing"));
        assertThat(name.getLocalName(), is("some/thing"));
        assertThat(name.getNamespaceUri(), is("http://www.jboss.org/dna/namespace"));
        assertThat(name.getString(NO_OP_ENCODER), is("{http://www.jboss.org/dna/namespace}some/thing"));
    }

}<commit_msg>DNA-89: Added getNamespaceRegistry() to NameFactory, SequencerOutput, and impls, as well as constant for "jcr:primaryType" to NameFactory DNA-94: Changed to throw IAE if name is null or empty<commit_after>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.dna.spi.graph.impl;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import org.jboss.dna.common.text.TextEncoder;
import org.jboss.dna.spi.graph.Name;
import org.jboss.dna.spi.graph.NamespaceRegistry;
import org.jboss.dna.spi.graph.Path;
import org.jboss.dna.spi.graph.ValueFactory;
import org.junit.Before;
import org.junit.Test;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class NameValueFactoryTest {

	public static final TextEncoder NO_OP_ENCODER = Path.NO_OP_ENCODER;

	private NamespaceRegistry registry;
	private ValueFactory<String> stringValueFactory;
	private NameValueFactory factory;
	private TextEncoder encoder;
	private Name name;

	@Before
	public void beforeEach() throws Exception {
		this.registry = new BasicNamespaceRegistry();
		this.registry.register("dna", "http://www.jboss.org/dna/namespace");
		this.encoder = Path.DEFAULT_ENCODER;
		this.stringValueFactory = new StringValueFactory(encoder);
		this.factory = new NameValueFactory(registry, encoder, stringValueFactory);
	}

	@Test
	public void shouldCreateNameFromSingleStringInPrefixedNamespaceFormatWithoutPrefix() {
		name = factory.create("a");
		assertThat(name.getLocalName(), is("a"));
		assertThat(name.getNamespaceUri(), is(this.registry.getNamespaceForPrefix("")));
	}

	@Test
	public void shouldCreateNameFromSingleStringInPrefixedNamespaceFormat() {
		name = factory.create("dna:something");
		assertThat(name.getLocalName(), is("something"));
		assertThat(name.getNamespaceUri(), is("http://www.jboss.org/dna/namespace"));
		assertThat(name.getString(NO_OP_ENCODER), is("{http://www.jboss.org/dna/namespace}something"));
	}

	@Test
	public void shouldCreateNameFromSingleEncodedStringInPrefixedNamespaceFormat() {
		name = factory.create(encoder.encode("dna") + ":" + encoder.encode("some/thing"));
		assertThat(name.getLocalName(), is("some/thing"));
		assertThat(name.getNamespaceUri(), is("http://www.jboss.org/dna/namespace"));
		assertThat(name.getString(NO_OP_ENCODER), is("{http://www.jboss.org/dna/namespace}some/thing"));
	}

	@Test
	public void shouldCreateNameFromSingleStringInStandardFullNamespaceFormat() {
		name = factory.create("{http://www.jboss.org/dna/namespace}something");
		assertThat(name.getLocalName(), is("something"));
		assertThat(name.getNamespaceUri(), is("http://www.jboss.org/dna/namespace"));
		assertThat(name.getString(NO_OP_ENCODER), is("{http://www.jboss.org/dna/namespace}something"));
	}

	@Test
	public void shouldCreateNameFromSingleEncodedStringInStandardFullNamespaceFormat() {
		name = factory.create("{" + encoder.encode("http://www.jboss.org/dna/namespace") + "}" + encoder.encode("some/thing"));
		assertThat(name.getLocalName(), is("some/thing"));
		assertThat(name.getNamespaceUri(), is("http://www.jboss.org/dna/namespace"));
		assertThat(name.getString(NO_OP_ENCODER), is("{http://www.jboss.org/dna/namespace}some/thing"));
	}

	@Test
	public void shouldProvideAccessToNamespaceRegistryPassedInConstructor() {
		assertThat(factory.getNamespaceRegistry(), is(registry));
	}

	@Test( expected = IllegalArgumentException.class )
	public void shouldNotAllowNullLocalName() {
		factory.create("a", (String)null);
	}

	@Test( expected = IllegalArgumentException.class )
	public void shouldNotAllowNullLocalNameWithEncoder() {
		factory.create("a", null, encoder);
	}

	@Test( expected = IllegalArgumentException.class )
	public void shouldNotAllowEmptyLocalName() {
		factory.create("a", "");
	}

	@Test( expected = IllegalArgumentException.class )
	public void shouldNotAllowEmptyLocalNameWithEncoder() {
		factory.create("a", "", encoder);
	}
}
{'fraction_non_alphanumeric': 0.1242662531841843, 'fraction_numerical': 0.004208660981282534, 'mean_word_length': 4.7847533632287, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 24, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5530106', 'n_tokens_mistral': 2721, 'n_tokens_neox': 2608, 'n_words': 638}