<commit_before>/**
 * Copyright (c) 2010 IBM Corporation
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *
 * $Id$
 */
package org.openhealthtools.mdht.uml.cda.operations;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringEscapeUtils;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.junit.Test;
import org.openhealthtools.mdht.uml.cda.CDAFactory;
import org.openhealthtools.mdht.uml.cda.util.CDAUtil;
import org.openhealthtools.mdht.uml.hl7.datatypes.CD;
import org.openhealthtools.mdht.uml.hl7.datatypes.CE;
import org.openhealthtools.mdht.uml.hl7.datatypes.CS;
import org.openhealthtools.mdht.uml.hl7.datatypes.DatatypesFactory;
import org.openhealthtools.mdht.uml.hl7.datatypes.II;
import org.openhealthtools.mdht.uml.hl7.rim.InfrastructureRoot;
import org.openhealthtools.mdht.uml.hl7.rim.operations.RIMOperationTest;

/**
 * This class is the super class for all JUnit4 test cases for CDA based
 * classes.
 */
@SuppressWarnings("nls")
public abstract class CDAValidationTest extends RIMOperationTest {

	/**
	 * The template id to use for the has* tests.
	 */
	protected static final String BAD_TEMPLATE_ID = "1.2.3.4";
	protected static final II THE_BAD_II = DatatypesFactory.eINSTANCE.createII();

	static {
		THE_BAD_II.setRoot(BAD_TEMPLATE_ID);
	}

	// protected static int count = 0;

	protected static Map<Object, Object> map = new HashMap<Object, Object>();

	/**
	 * Do all validation tests defined for the objectToTest.
	 */
	@Test
	public final void testAll() {
		String testTargetDescription = "";
		try {
			for (final CDATestCase testCase : getTestCases()) {
				testTargetDescription = testCase.getTestTargetDescription();
				final EObject objectToTest = getObjectToTest();
				final BasicDiagnostic diagnostician = Diagnostician.INSTANCE.createDefaultDiagnostic(objectToTest);

				testCase.doTest(objectToTest, diagnostician, map);
			}

		} catch (final UnsupportedOperationException uoe) {
			fail(createUnsupportedOperationFailureMessage(testTargetDescription, uoe));
		}
		// finally {
		// count += getTestCases().size();
		// System.out.println("count = " + count);
		// }
	} // testAll

	protected List<CDATestCase> getTestCases() {
		return new ArrayList<CDATestCase>();
	}

	static abstract protected class CDATestCase {
		private final String testTargetDescription;

		/**
		 * @param testTargetDescription
		 */
		protected CDATestCase(final String testTargetDescription) {
			this.testTargetDescription = testTargetDescription;
		}

		abstract protected void doTest(final EObject objectToTest, final BasicDiagnostic diagnostician, final Map<Object, Object> map);

		protected String getTestTargetDescription() {
			return testTargetDescription;
		}

		@Override
		public String toString() {
			return getTestTargetDescription();
		}

	} // CDATestCase

	static abstract protected class CDAOperationsTestCase extends CDATestCase {

		protected CDAOperationsTestCase(final String testTargetDescription) {
			super(testTargetDescription);
		}

		abstract protected void doAdd(EObject objectToTest, EObject eObjectToAdd);

		abstract protected EObject getEObjectToAdd();

	} // CDAOperationsTestCase

	static abstract protected class CDAAddTestCase extends CDAOperationsTestCase {
<commit_msg>added file creation for all tests<commit_after>/**
 * Copyright (c) 2010 IBM Corporation
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *
 * $Id$
 */
package org.openhealthtools.mdht.uml.cda.operations;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringEscapeUtils;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.junit.Test;
import org.openhealthtools.mdht.uml.cda.CDAFactory;
import org.openhealthtools.mdht.uml.cda.util.CDAUtil;
import org.openhealthtools.mdht.uml.hl7.datatypes.CD;
import org.openhealthtools.mdht.uml.hl7.datatypes.CE;
import org.openhealthtools.mdht.uml.hl7.datatypes.CS;
import org.openhealthtools.mdht.uml.hl7.datatypes.DatatypesFactory;
import org.openhealthtools.mdht.uml.hl7.datatypes.II;
import org.openhealthtools.mdht.uml.hl7.rim.InfrastructureRoot;
import org.openhealthtools.mdht.uml.hl7.rim.operations.RIMOperationTest;

/**
 * This class is the super class for all JUnit4 test cases for CDA based
 * classes.
 */
@SuppressWarnings("nls")
public abstract class CDAValidationTest extends RIMOperationTest {

	/**
	 * The template id to use for the has* tests.
	 */
	protected static final String BAD_TEMPLATE_ID = "1.2.3.4";
	protected static final II THE_BAD_II = DatatypesFactory.eINSTANCE.createII();

	static {
		THE_BAD_II.setRoot(BAD_TEMPLATE_ID);
	}

	// protected static int count = 0;

	protected static Map<Object, Object> map = new HashMap<Object, Object>();

	/**
	 * Do all validation tests defined for the objectToTest.
	 */
	@Test
	public final void testAll() {
		String testTargetDescription = "";
		try {
			for (final CDATestCase testCase : getTestCases()) {
				testTargetDescription = testCase.getTestTargetDescription();
				final EObject objectToTest = getObjectToTest();
				final BasicDiagnostic diagnostician = Diagnostician.INSTANCE.createDefaultDiagnostic(objectToTest);

				testCase.doTest(objectToTest, diagnostician, map);
			}

		} catch (final UnsupportedOperationException uoe) {
			fail(createUnsupportedOperationFailureMessage(testTargetDescription, uoe));
		}
		// finally {
		// count += getTestCases().size();
		// System.out.println("count = " + count);
		// }
	} // testAll

	protected List<CDATestCase> getTestCases() {
		return new ArrayList<CDATestCase>();
	}

	static abstract protected class CDATestCase {
		private final String testTargetDescription;

		/**
		 * @param testTargetDescription
		 */
		protected CDATestCase(final String testTargetDescription) {
			this.testTargetDescription = testTargetDescription;
			
			String testLogDir = System.getProperty("testlogdir");
			try {
				if (testLogDir != null)
				{
				String logFileName = String.format("%s/test%s", testLogDir,getTestTargetDescription().substring(0, 1).toUpperCase() + getTestTargetDescription().substring(1) + ".xml");
				FileWriter fstream = new FileWriter(logFileName); 
				BufferedWriter out = new BufferedWriter(fstream);
				out.flush();
				out.write("<xmlsnippet>" +  "</xmlsnippet>");				
				out.close();
				}
			} catch (Exception e) {
				System.err.println("Error: " + e.getMessage());
			}
			
		}

		abstract protected void doTest(final EObject objectToTest, final BasicDiagnostic diagnostician, final Map<Object, Object> map);

		protected String getTestTargetDescription() {
			return testTargetDescription;
		}

		@Override
		public String toString() {
			return getTestTargetDescription();
		}

	} // CDATestCase

	static abstract protected class CDAOperationsTestCase extends CDATestCase {

		protected CDAOperationsTestCase(final String testTargetDescription) {
			super(testTargetDescription);
		}

		abstract protected void doAdd(EObject objectToTest, EObject eObjectToAdd);

		abstract protected EObject getEObjectToAdd();

	} // CDAOperationsTestCase

	static abstract protected class CDAAddTestCase extends CDAOperationsTestCase {

{'fraction_non_alphanumeric': 0.10120106761565836, 'fraction_numerical': 0.005004448398576513, 'mean_word_length': 4.7463258785942495, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 3}, 'pii_count': 2, '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': '2899332', 'n_tokens_mistral': 2935, 'n_tokens_neox': 2792, 'n_words': 628}