de.fhg.iais.asc.transformer.jdom.handler.XmlFieldEditorTest.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.transformer.jdom.handler.XmlFieldEditorTest.java

Source

package de.fhg.iais.asc.transformer.jdom.handler;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License.                   *
 * You may obtain a copy of the License at                                    *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 * Unless required by applicable law or agreed to in writing,                 *
 * software distributed under the License is distributed on an "AS IS" BASIS, *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

import org.apache.commons.lang.StringUtils;
import org.jdom.Document;
import org.junit.Assert;
import org.junit.Test;

import de.fhg.iais.cortex.model.aip.util.XmlProcessor;
import de.fhg.iais.cortex.model.aip.util.XmlUtils;

public class XmlFieldEditorTest extends AbstractXmlFieldHandlerTest {

    private static final String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><root><header/><body><listing>"
            + "<record no=\"1\"><creator>Mller</creator></record>"
            + "<record no=\"2\"><creator>Meier [Hg.]</creator></record>"
            + "<record no=\"3\"><creator>Schulze, Meier [Hgs.]</creator></record>" + "</listing></body></root>";
    private static final String FIELDPATTERN = "//creator";
    private static final String CONTENTPATTERN = ".*";
    private static final String[] SEARCHTERMS = new String[] { "[Hg.]", "[Hgs.]" };
    private static final String REPLACEMENT = "";
    private static final String NEWFIELDNAME = "publisher";

    @Test
    public void testSampler() throws Exception {

        Document xdoc = XmlProcessor.buildDocumentFrom(XML);
        XmlFieldEditor s = new XmlFieldEditor(FIELDPATTERN, CONTENTPATTERN, SEARCHTERMS, REPLACEMENT, NEWFIELDNAME);
        xdoc = this.transform(s, xdoc);

        String output = XmlUtils.elementToString(true, xdoc.getRootElement());
        System.out.println(output);

        final int p = StringUtils.countMatches(output, "publisher");
        final int c = StringUtils.countMatches(output, "creator");
        Assert.assertTrue("Expected 2 creator tags, found " + c, c == 2);
        Assert.assertTrue("Expected 4 publisher tags, found " + p, p == 4);
    }
}