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

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.transformer.jdom.handler.XmlFieldStatsTest.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 java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

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

import de.fhg.iais.asc.transformer.jdom.handler.util.FieldStatsMap;
import de.fhg.iais.cortex.model.aip.util.XmlProcessor;

public class XmlFieldStatsTest extends AbstractXmlFieldHandlerTest {
    private static final String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><root><header/><body><listing>"
            + "<record no=\"1\"><creator>Baal</creator></record>"
            + "<record no=\"2\"><creator>Meier</creator></record>"
            + "<record no=\"3\"><creator>Schulze, Meier [Hgs.]</creator></record>"
            + "<record no=\"4\"><creator></creator></record>" + "<record no=\"5\"><creator>Meier</creator></record>"
            + "</listing></body></root>";
    private static final String FIELDPATTERN = null;

    private static final Map<String, Integer> expected = new HashMap<String, Integer>();

    @Before
    public void setUp() {
        expected.put("creator", 5);
        expected.put("record", 5);
        expected.put("root", 1);
        expected.put("header", 1);
        expected.put("body", 1);
        expected.put("listing", 1);
    }

    @Test
    public void testSampler() throws Exception {

        FieldStatsMap stats = new FieldStatsMap();
        Document xdoc = XmlProcessor.buildDocumentFrom(XML);
        XmlFieldStats s = new XmlFieldStats(stats, FIELDPATTERN);
        xdoc = this.transform(s, xdoc);

        String output = stats.toString();
        final int p = StringUtils.countMatches(output, "test");

        Assert.assertTrue("Expected 0 strings 'test', found " + p, p == 0);
        System.out.println(output);

        int numberOfDifferentTags = 0;
        int absoluteNumberOfTags = 0;
        Map<String, Integer> result = new HashMap<String, Integer>();
        while (!output.isEmpty()) {
            int value = Integer.parseInt(output.charAt(output.indexOf(':') + 2) + "");
            absoluteNumberOfTags += value;
            result.put(output.substring(0, output.indexOf(':')), value);
            output = output.substring(output.indexOf('\n') + 1);
            numberOfDifferentTags++;
        }

        //Test, whether the number of different tags is correct
        Assert.assertEquals("Expected 6 different types of tags, found " + numberOfDifferentTags, 6,
                numberOfDifferentTags);

        //Test, whether the number of absolute tags is correct
        Assert.assertEquals("Expected 14 tags, found " + absoluteNumberOfTags, 14, absoluteNumberOfTags);

        //Tests, whether the same key tags are found, containing the same values then expected
        for (Entry<String, Integer> es : result.entrySet()) {
            Assert.assertEquals(es.getValue(), expected.get(es.getKey()));
        }
    }

}