/*
 * 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.apache.solr.util;

import org.apache.solr.SolrTestCase;
import org.semver4j.SemverException;

public class TestSolrVersion extends SolrTestCase {
  private static final SolrVersion SOLR_9_0_1 = SolrVersion.valueOf("9.0.1");

  public void testToString() {
    assertEquals("9.0.0", SolrVersion.forIntegers(9, 0, 0).toString());
  }

  public void testVersionComponents() {
    SolrVersion v9_0_1_rc1 = SolrVersion.valueOf("9.0.1-rc1.0+b123");
    assertEquals("9.0.1-rc1.0+b123", v9_0_1_rc1.toString());
    assertEquals(9, v9_0_1_rc1.getMajorVersion());
    assertEquals(0, v9_0_1_rc1.getMinorVersion());
    assertEquals(1, v9_0_1_rc1.getPatchVersion());
    assertEquals("rc1.0", v9_0_1_rc1.getPrereleaseVersion());
  }

  public void testLatestInitialized() {
    assertTrue(SolrVersion.LATEST.greaterThanOrEqualTo(SolrVersion.valueOf("9.0.0")));
  }

  public void testForwardsCompatibility() {
    assertTrue(
        SolrVersion.valueOf("9.10.20").greaterThanOrEqualTo(SolrVersion.forIntegers(9, 0, 0)));
  }

  public void testParseExceptions() {
    expectThrows(SemverException.class, () -> SolrVersion.valueOf("SOLR_7_0_0"));
  }

  public void testSatisfies() {
    assertTrue(SOLR_9_0_1.satisfies("~9.0"));
    assertTrue(SOLR_9_0_1.satisfies("9.x"));
    assertTrue(SOLR_9_0_1.satisfies("9"));
    assertTrue(SOLR_9_0_1.satisfies("<=9"));
    assertTrue(SOLR_9_0_1.satisfies("<9.1"));
    assertFalse(SOLR_9_0_1.satisfies(">9.0"));
    assertFalse(SOLR_9_0_1.satisfies("8.x"));
    assertTrue(SOLR_9_0_1.satisfies("8 - 9"));
  }
}

/*
 * 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.apache.solr.util;

import java.util.Locale;
import org.apache.solr.SolrTestCase;
import org.apache.solr.SolrTestCaseJ4;
import org.junit.BeforeClass;
import org.semver4j.SemverException;

public class TestSolrVersion extends SolrTestCase {
  private static final SolrVersion SOLR_9_0_1 = SolrVersion.valueOf("9.0.1");

  @SuppressWarnings("AssertionFailureIgnored")
  @BeforeClass
  public static void beforeClass() {
    // Checks that semver4j works with configured locale */
    try {
      assertTrue(SOLR_9_0_1.satisfies("~9.0"));
    } catch (AssertionError e) {
      SolrTestCaseJ4.assumeNoException(
          "See SOLR-16467 - semver4j doesn't work with " + Locale.getDefault().toLanguageTag(),
          new Exception(e));
    }
  }

  public void testToString() {
    assertEquals("9.0.0", SolrVersion.forIntegers(9, 0, 0).toString());
  }

  public void testVersionComponents() {
    SolrVersion v9_0_1_rc1 = SolrVersion.valueOf("9.0.1-rc1.0+b123");
    assertEquals("9.0.1-rc1.0+b123", v9_0_1_rc1.toString());
    assertEquals(9, v9_0_1_rc1.getMajorVersion());
    assertEquals(0, v9_0_1_rc1.getMinorVersion());
    assertEquals(1, v9_0_1_rc1.getPatchVersion());
    assertEquals("rc1.0", v9_0_1_rc1.getPrereleaseVersion());
  }

  public void testLatestInitialized() {
    assertTrue(SolrVersion.LATEST.greaterThanOrEqualTo(SolrVersion.valueOf("9.0.0")));
  }

  public void testForwardsCompatibility() {
    assertTrue(
        SolrVersion.valueOf("9.10.20").greaterThanOrEqualTo(SolrVersion.forIntegers(9, 0, 0)));
  }

  public void testParseExceptions() {
    expectThrows(SemverException.class, () -> SolrVersion.valueOf("SOLR_7_0_0"));
  }

  public void testSatisfies() {
    assertTrue(SOLR_9_0_1.satisfies("~9.0"));
    assertTrue(SOLR_9_0_1.satisfies("9.x"));
    assertTrue(SOLR_9_0_1.satisfies("9"));
    assertTrue(SOLR_9_0_1.satisfies("<=9"));
    assertTrue(SOLR_9_0_1.satisfies("<9.1"));
    assertFalse(SOLR_9_0_1.satisfies(">9.0"));
    assertFalse(SOLR_9_0_1.satisfies("8.x"));
    assertTrue(SOLR_9_0_1.satisfies("8 - 9"));
  }
}

Add check for working locale in TestSolrVersion (#1083)