/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.kitesdk.morphline.solr;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;

import org.apache.commons.lang.SystemUtils;
import org.apache.lucene.LucenePackage;
import org.apache.lucene.util.Version;
import org.apache.solr.core.SolrCore;
import org.junit.Assert;
import org.junit.Test;

/** Print and verify some info about the environment in which the unit tests are running */
public class EnvironmentTest extends Assert {

  // see pom.xml, example: "4.2.0"
  private static final String EXPECTED_SOLR_VERSION = System.getProperty("solr.expected.version");

  @Test
  public void testEnvironment() throws UnknownHostException {
    System.out.println("EXPECTED_SOLR_VERSION: " + EXPECTED_SOLR_VERSION);

    System.out.println("Running test suite with java version: " + SystemUtils.JAVA_VERSION + " "
        + SystemUtils.JAVA_VM_NAME + " on " + SystemUtils.OS_NAME + " " + SystemUtils.OS_VERSION + "/"
        + SystemUtils.OS_ARCH + " on host: " + InetAddress.getLocalHost().getHostName());

    Package p = SolrCore.class.getPackage();
    System.out.println("Running test suite with solr-spec-version: " + p.getSpecificationVersion()
        + ", solr-impl-version: " + p.getImplementationVersion());
    if (EXPECTED_SOLR_VERSION != null) {
      assertTrue("unexpected version: " + p.getSpecificationVersion(),
          p.getSpecificationVersion().startsWith(EXPECTED_SOLR_VERSION));
      assertTrue("unexpected version: " + p.getImplementationVersion(),
          p.getImplementationVersion().startsWith(EXPECTED_SOLR_VERSION));
    }

    p = LucenePackage.class.getPackage();
    System.out.println("Running test suite with lucene-spec-version: " + p.getSpecificationVersion()
        + ", lucene-impl-version: " + p.getImplementationVersion());
    if (EXPECTED_SOLR_VERSION != null) {
      assertTrue("unexpected version: " + p.getSpecificationVersion(),
          p.getSpecificationVersion().startsWith(EXPECTED_SOLR_VERSION));
      assertTrue("unexpected version: " + p.getImplementationVersion(),
          p.getImplementationVersion().startsWith(EXPECTED_SOLR_VERSION));

      Version expectedMinorLuceneVersion = getMinorLuceneVersion(EXPECTED_SOLR_VERSION);
      System.out.println("expectedMinorLuceneVersion: " + expectedMinorLuceneVersion);
      assertTrue(Version.LATEST.onOrAfter(expectedMinorLuceneVersion));
    }
  }

  private static Version getMinorLuceneVersion(String version) {
    try {
      return Version.parseLeniently(version.replaceFirst("^(\\d)\\.(\\d).*", "LUCENE_$1$2"));
    } catch (ParseException e) {
      throw new RuntimeException(e);
    }
  }

}

@@ -1368,16 +1368,50 @@
 _VERSION
+;%0A  %0A  static %7B%0A    String version
  = Syste
@@ -1449,16 +1449,277 @@
 rsion%22);
+%0A    if (version != null) %7B%0A      int i = version.indexOf('-');%0A      if (i %3E= 0) %7B%0A        // e.g. strip off %22-cdh5.4.0-SNAPSHOT%22 from %224.10.3-cdh5.4.0-SNAPSHOT%22%0A        version = version.substring(0, i); %0A      %7D%0A    %7D%0A    EXPECTED_SOLR_VERSION = version;%0A  %7D
 %0A%0A  @Tes

make solr version string used by EnvironmentTest more lenient to accomodate various solr flavours