Newer
Older
MagnetRON / src / tests / MagnetRONFrameTest.java
package tests;

import static org.junit.jupiter.api.Assertions.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.ntlab.deltaExtractor.DeltaExtractorJSON;
import org.ntlab.deltaExtractor.ExtractedStructure;
import org.ntlab.deltaExtractor.IAliasCollector;
import org.ntlab.deltaExtractor.IAliasTracker;
import org.ntlab.deltaViewer.DeltaAliasCollector;
import org.ntlab.trace.MethodExecution;
import org.ntlab.trace.ObjectReference;
import org.ntlab.trace.Reference;
import org.ntlab.trace.ThreadInstance;
import org.ntlab.trace.TraceJSON;
import org.ntlab.trace.TracePoint;

class MagnetRONFrameTest {
	// Delta Extract Type
	protected static final String CONTAINER_COMPONENT = "Container-Component";
	protected static final String CONTAINER_COMPONENT_COLLECTION = "Container-Component(Collection)";


	public MagnetRONFrameTest() {
	}

	public Map<ExtractedStructure, IAliasCollector> extractPluralDelta(String key) {
		Map<ExtractedStructure, IAliasCollector> extractedPluralDelta = new HashMap<>();
		Map<String, String[]> argsMap = new HashMap<>();
		setArgmentsForDeltaExtract(argsMap);
		Map<String, List<String>> argsKeysMap = new HashMap<>();
		setArgsKeysForPluralDeltaExtract(argsKeysMap, argsMap);
		List<String> argsKeys = argsKeysMap.get(key);
		TraceJSON trace = new TraceJSON(argsMap.get(argsKeys.get(0))[4]);
		DeltaExtractorJSON s = new DeltaExtractorJSON(trace);

		for (String argsKey: argsKeys) {
			List<ExtractedStructure> eList = new ArrayList<>();
			List<IAliasCollector> dacList = new ArrayList<>();
			if (argsMap.get(argsKey)[5] == CONTAINER_COMPONENT || argsMap.get(argsKey)[5] == CONTAINER_COMPONENT_COLLECTION) {
				HashMap<String, ThreadInstance> threads = trace.getAllThreads();

				if (threads.values().size() == 1) {
					ThreadInstance thread = threads.values().iterator().next(); // 最後のスレッドを見ているだけ…
					TracePoint tp = thread.getRoot().get(thread.getRoot().size() - 1).getExitPoint();
					Reference reference = new Reference(argsMap.get(argsKey)[0], argsMap.get(argsKey)[1], argsMap.get(argsKey)[2], argsMap.get(argsKey)[3]);
					IAliasTracker dac = new DeltaAliasCollector();
					ExtractedStructure e = s.extract(reference, tp, dac);
					eList.add(e);
					dacList.add(dac);
				} else {
					for (ThreadInstance thread: threads.values()) {
						TracePoint tp = thread.getRoot().get(thread.getRoot().size() - 1).getExitPoint();
						Reference reference = new Reference(argsMap.get(argsKey)[0], argsMap.get(argsKey)[1], argsMap.get(argsKey)[2], argsMap.get(argsKey)[3]);
						if (argsMap.get(argsKey)[5] == CONTAINER_COMPONENT_COLLECTION) {
							reference.setCollection(true);						
						}
//						reference.setArray(true);
//						reference.setFinalLocal(true);
//						reference.setCreation(true);
						IAliasTracker dac = new DeltaAliasCollector();
						ExtractedStructure e = s.extract(reference, tp, dac);
						System.out.println(e);
						if (e != null) {
							eList.add(e);
							dacList.add(dac);
							System.out.println("add" + eList.size() + ", " + dacList.size());
						}
						System.out.println("---------------------------");
					}
				}
			} else {
				IAliasTracker dac = new DeltaAliasCollector();
				MethodExecution me = trace.getLastMethodExecution(argsMap.get(argsKey)[2]);
				Map<ObjectReference, TracePoint> refs = me.getObjectReferences(argsMap.get(argsKey)[3]);
				ObjectReference ref = refs.keySet().iterator().next();
				ExtractedStructure e = s.extract(refs.get(ref), ref, dac);
				eList.add(e);
				dacList.add(dac);
			}
			extractedPluralDelta.put(eList.get(0), dacList.get(0));
		}
		return extractedPluralDelta;
	}
	
	private void setArgmentsForDeltaExtract(Map<String, String[]> map){
		// Samples of Plural Delta
		String[] getterOverlap1 = {null, null, "getterOverlap.F","getterOverlap.D", "traces/getterOverlap.txt", CONTAINER_COMPONENT_COLLECTION}; map.put("getterOverlap1", getterOverlap1);
		String[] getterOverlap2 = {null, null, "getterOverlap.G","getterOverlap.D", "traces/getterOverlap.txt", CONTAINER_COMPONENT_COLLECTION}; map.put("getterOverlap2", getterOverlap2);
		String[] setterOverlap1 = {null, null, "setterOverlap.F","setterOverlap.C", "traces/setterOverlap.txt", CONTAINER_COMPONENT_COLLECTION}; map.put("setterOverlap1", setterOverlap1);
		String[] setterOverlap2 = {null, null, "setterOverlap.G","setterOverlap.C", "traces/setterOverlap.txt", CONTAINER_COMPONENT_COLLECTION}; map.put("setterOverlap2", setterOverlap2);
	}
	
	private void setArgsKeysForPluralDeltaExtract(Map<String, List<String>> argsKeysMap, Map<String, String[]> argsMap){
		// Samples of Plural Delta
		argsKeysMap.put("getterOverlap", new ArrayList<String>(Arrays.asList("getterOverlap1", "getterOverlap2")));
		argsKeysMap.put("setterOverlap", new ArrayList<String>(Arrays.asList("setterOverlap1", "setterOverlap2")));
	}
}