/*
 * Copyright 2010 Henry Coles
 * 
 * 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.
 */
package org.pitest.junit;

import java.util.Arrays;

import org.pitest.CompoundTestSuiteFinder;
import org.pitest.extension.Configuration;
import org.pitest.extension.TestClassIdentifier;
import org.pitest.extension.TestSuiteFinder;
import org.pitest.extension.TestUnitFinder;
import org.pitest.functional.Option;
import org.pitest.help.Help;
import org.pitest.help.PitHelpError;

public class JUnitCompatibleConfiguration implements Configuration {

  public TestUnitFinder testUnitFinder() {
    return new CompoundTestUnitFinder(Arrays.asList(
        new JUnitCustomRunnerTestUnitFinder(),
        new ParameterisedJUnitTestFinder()));
  }

  public TestSuiteFinder testSuiteFinder() {
    return new CompoundTestSuiteFinder(Arrays.<TestSuiteFinder> asList(
        new JUnit4SuiteFinder(), new RunnerSuiteFinder()));
  }

  public TestClassIdentifier testClassIdentifier() {
    return new JUnitTestClassIdentifier();
  }

  public Option<PitHelpError> verifyEnvironment() {
    try {
      final String version = junit.runner.Version.id();

      final String[] parts = (version.contains("SNAPSHOT")) ?
          version.split("-SNAPSHOT")[0].split("\\.") :
          version.split("\\.");

      final int major = Integer.parseInt(parts[0]);
      final int minor = Integer.parseInt(parts[1]);
      if ((major < 4) || ((major == 4) && (minor < 6))) {
        return Option.some(new PitHelpError(Help.WRONG_JUNIT_VERSION, version));
      }
    } catch (final NoClassDefFoundError er) {
      return Option.some(new PitHelpError(Help.NO_JUNIT));
    }

    return Option.none();
  }

}

@@ -635,16 +635,80 @@
 .Arrays;
+%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0Aimport
@@ -1099,16 +1099,156 @@
 tion %7B%0A%0A
+  private static final Pattern VERSION_PATTERN = Pattern%0A                                                   .compile(%22(%5C%5Cd+)%5C%5C.(%5C%5Cd+).*%22);%0A%0A
   public
@@ -1838,545 +1838,620 @@
 ();%0A
-%0A
       
-final String%5B%5D parts = (version.contains(%22SNAPSHOT%22)) ?%0A          version.split(%22-SNAPSHOT%22)%5B0%5D.split(%22%5C%5C.%22) :%0A          version.split(%22%5C%5C.%22);%0A%0A      final int major = Integer.parseInt(parts%5B0%5D);%0A      final int minor = Integer.parseInt(parts%5B1%5D);%0A      if ((major %3C 4) %7C%7C ((major == 4) && (minor %3C 6))) %7B%0A        return Option.some(new PitHelpError(Help.WRONG_JUNIT_VERSION, version));%0A      %7D%0A    %7D catch (final NoClassDefFoundError er) %7B%0A      return Option.some(new PitHelpError(Help.NO_JUNIT));%0A    %7D%0A%0A    return Option.none(
+if (isInvalidVersion(version)) %7B%0A        return Option.some(new PitHelpError(Help.WRONG_JUNIT_VERSION, version));%0A      %7D%0A    %7D catch (final NoClassDefFoundError er) %7B%0A      return Option.some(new PitHelpError(Help.NO_JUNIT));%0A    %7D%0A%0A    return Option.none();%0A  %7D%0A%0A  boolean isInvalidVersion(final String version) %7B%0A    final Matcher matcher = VERSION_PATTERN.matcher(version);%0A%0A    if (!matcher.matches()) %7B%0A      return true;%0A    %7D%0A%0A    final int major = Integer.parseInt(matcher.group(1));%0A    final int minor = Integer.parseInt(matcher.group(2));%0A%0A    return (major %3C 4) %7C%7C ((major == 4) && (minor %3C 6)
 );%0A 

patch from peter mahlen for junit version parsing