All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.dagere.peass.dependency.analysis.data.TestSet Maven / Gradle / Ivy

There is a newer version: 0.4.0
Show newest version
/**
 *     This file is part of PerAn.
 *
 *     PerAn is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     PerAn 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 General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with PerAn.  If not, see .
 */
package de.dagere.peass.dependency.analysis.data;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.KeyDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

/**
 * Represents a set of tests which are executed for one version by its class and its list of methods.
 * 
 * @author reichelt
 *
 */
public class TestSet {

   private static final Logger LOG = LogManager.getLogger(TestSet.class);

   public static class ChangedEntitityDeserializer extends KeyDeserializer {

      public ChangedEntitityDeserializer() {
      }

      @Override
      public ChangedEntity deserializeKey(final String key, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
         String value = key;
         final ChangedEntity entity;

         String method = "";
         if (value.contains(ChangedEntity.METHOD_SEPARATOR)) {
            method = value.substring(value.indexOf(ChangedEntity.METHOD_SEPARATOR) + 1);
            value = value.substring(0, value.indexOf(ChangedEntity.METHOD_SEPARATOR));
         }

         if (value.contains(ChangedEntity.MODULE_SEPARATOR)) {
            final String clazz = value.substring(value.indexOf(ChangedEntity.MODULE_SEPARATOR) + 1);
            final String module = value.substring(0, value.indexOf(ChangedEntity.MODULE_SEPARATOR));
            entity = new ChangedEntity(clazz, module, method);
         } else {
            entity = new ChangedEntity(value, "", method);
         }

         return entity;
      }
   }

   @JsonInclude(JsonInclude.Include.NON_EMPTY)
   @JsonDeserialize(keyUsing = ChangedEntitityDeserializer.class, contentAs = TreeSet.class, as = TreeMap.class)
   private final Map> testcases = new TreeMap<>();
   private String predecessor;

   public TestSet() {
   }

   public TestSet(final String testcase) {
      addTest(new TestCase(testcase));
   }

   public TestSet(final TestCase testcase) {
      addTest(testcase);
   }

   public TestSet(final List testcases) {
      for (String testcase : testcases) {
         addTest(new TestCase(testcase));
      }
   }

   @JsonIgnore
   public void addTest(final TestCase classname) {
      final ChangedEntity entity = new ChangedEntity(classname.getClazz(), classname.getModule());
      addTest(entity, classname.getMethod());
   }

   /**
    * Adds a test to the TestSet. If the method is null, and no further method is added, the TestSet contains the all methods of the test; if one method is added, only the method
    * (and perhaps future added methods) are included.
    * 
    * @param classname
    * @param methodname
    */
   public void addTest(final ChangedEntity classname, final String methodname) {
      if (classname.getMethod() != null && classname.getMethod() != "") {
         throw new RuntimeException("A testset should only get Changed Entities with empty method, but was " + classname.getMethod());
      }
      Set methods = testcases.get(classname);
      if (methods == null) {
         methods = new TreeSet<>();
         testcases.put(classname.copy(), methods);
      }
      if (methodname != null) {
         final String internalizedMethodName = methodname.intern();
         if (!methods.contains(internalizedMethodName)) {
            methods.add(internalizedMethodName);
         }
      }
   }

   @JsonIgnore
   public void addTestSet(final TestSet testSet) {
      for (final Map.Entry> newTestEntry : testSet.entrySet()) {
         Set methods = testcases.get(newTestEntry.getKey());
         if (methods == null) {
            methods = new TreeSet<>();
            methods.addAll(newTestEntry.getValue());
            testcases.put(newTestEntry.getKey().copy(), methods);
         } else {
            if (newTestEntry.getValue().size() != 0 && methods.size() != 0) {
               methods.addAll(newTestEntry.getValue());
            } else {
               // If List is empty, all methods are changed -> Should be
               // remembered
               methods.clear();
            }
         }
      }
   }

   @JsonIgnore
   public Set>> entrySet() {
      return testcases.entrySet();
   }

   @JsonIgnore
   public int classCount() {
      return testcases.size();
   }

   @JsonIgnore
   public Set getClasses() {
      return testcases.keySet();
   }

   @JsonIgnore
   public Set getTests() {
      final Set testcases = new LinkedHashSet<>();
      for (final Entry> classTests : getTestcases().entrySet()) {
         if (classTests.getValue().size() > 0) {
            for (final String method : classTests.getValue()) {
               final TestCase testcase = new TestCase(classTests.getKey().getClazz(), method, classTests.getKey().getModule());
               testcases.add(testcase);
            }
         } else {
            testcases.add(new TestCase(classTests.getKey().getClazz(), "", classTests.getKey().getModule()));
         }
      }
      return testcases;
   }

   public Map> getTestcases() {
      return testcases;
   }

   public void removeTest(final ChangedEntity testClassName) {
      if (testClassName.getMethod() != null && testClassName.getMethod() != "") {
         throw new RuntimeException("Testset class removal should only be done with empty method of ChangedEntity!");
      }
      testcases.remove(testClassName);
   }

   @JsonIgnore
   public void removeTest(final ChangedEntity testClassName, final String testMethodName) {
      if (testClassName.getMethod() != null && testClassName.getMethod() != "") {
         throw new RuntimeException("Testset class removal should only be done with empty method of ChangedEntity!");
      }
      final Set testMethods = testcases.get(testClassName);
      if (testMethods != null) {
         removeMethod(testClassName, testMethodName, testMethods);
      } else {
         ChangedEntity other = null;
         for (ChangedEntity entity : testcases.keySet()) {
            if (entity.getClazz().equals(testClassName.getClazz())) {
               other = entity;
            }
         }
         if (other != null) {
            LOG.warn("{} not found - found {} instead. Modules should be saved in ChangedEntity-instances!", testClassName, other);
            removeMethod(other, testMethodName, testcases.get(other));
         } else {
            LOG.error("Testclass " + testClassName + " missing");
         }

      }
   }

   private void removeMethod(final ChangedEntity testClassName, final String testMethodName, final Set testMethods) {
      LOG.debug("Removing: " + testClassName + "#" + testMethodName);
      if (!testMethods.remove(testMethodName)) {
         LOG.error("Problem: " + testMethodName + " can not be removed.");
         // throw new RuntimeException("Test " + testMethodName + " was not in TestSet!");
      }
      if (testMethods.size() == 0) {
         testcases.remove(testClassName);
      }
   }

   @Override
   public String toString() {
      return testcases.toString();
   }

   @JsonIgnore
   public Set getMethods(final ChangedEntity clazz) {
      return testcases.get(clazz);
   }

   @JsonInclude(Include.NON_EMPTY)
   public String getPredecessor() {
      return predecessor;
   }

   public void setPredecessor(final String predecessor) {
      this.predecessor = predecessor;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy