/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * 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 jetbrains.buildServer.tools;

import jetbrains.buildServer.core.runtime.osgx.Version;
import junit.framework.Assert;

import org.junit.Test;

public class VersionTest {

  @Test
  public void version_pattern_jdk() throws Exception {
    final Version version = new Version("1.6.0_23");
    Assert.assertNotNull(version);
    Assert.assertEquals("1", version.getMajor());
    Assert.assertEquals("6", version.getMinor());
    Assert.assertEquals("0", version.getMicro());
    Assert.assertEquals("23", version.getUpdate());
  }
  
  @Test
  public void version_pattern_wide_build() throws Exception {
    final Version version = new Version("1.6.0_23-b05");
    Assert.assertNotNull(version);
    Assert.assertEquals("1", version.getMajor());
    Assert.assertEquals("6", version.getMinor());
    Assert.assertEquals("0", version.getMicro());
    Assert.assertEquals("23", version.getUpdate());
    Assert.assertEquals("-b05", version.getBuild());    
  }
  
  @Test
  public void version_pattern_small_build() throws Exception {
    final Version version = new Version("2.4.b05");
    Assert.assertNotNull(version);
    Assert.assertEquals("2", version.getMajor());
    Assert.assertEquals("4", version.getMinor());
    Assert.assertEquals("", version.getMicro());
    Assert.assertEquals("", version.getUpdate());
    Assert.assertEquals("b05", version.getBuild());    
  }
  
  @Test
  public void version_pattern_small_build2() throws Exception {
    final Version version = new Version("2.4-b05");
    Assert.assertNotNull(version);
    Assert.assertEquals("2", version.getMajor());
    Assert.assertEquals("4", version.getMinor());
    Assert.assertEquals("", version.getMicro());
    Assert.assertEquals("", version.getUpdate());
    Assert.assertEquals("-b05", version.getBuild());    
  }
  

  @Test
  public void version_pattern_ant() throws Exception {
    final Version version = new Version("1.8.2");
    Assert.assertNotNull(version);
    Assert.assertEquals("1", version.getMajor());
    Assert.assertEquals("8", version.getMinor());
    Assert.assertEquals("2", version.getMicro());
    Assert.assertEquals("", version.getUpdate());
  }

  @Test
  public void version_pattern_2() throws Exception {
    final Version version = new Version("2.16");
    Assert.assertNotNull(version);
    Assert.assertEquals("2", version.getMajor());
    Assert.assertEquals("16", version.getMinor());
    Assert.assertEquals("", version.getMicro());
    Assert.assertEquals("", version.getUpdate());
  }

  @Test
  public void version_pattern_1() throws Exception {
    final Version version = new Version("3");
    Assert.assertNotNull(version);
    Assert.assertEquals("3", version.getMajor());
    Assert.assertEquals("", version.getMinor());
    Assert.assertEquals("", version.getMicro());
    Assert.assertEquals("", version.getUpdate());
  }

  @Test
  public void version_compare_jdk_update() throws Exception {
    final Version version16023 = new Version("1.6.0_23");
    final Version version16021 = new Version("1.6.0_21");
    Assert.assertTrue(version16023.compareTo(version16021) > 0);
  }

  @Test
  public void version_compare_jdk_micro() throws Exception {
    final Version version16121 = new Version("1.6.1_21");
    final Version version16021 = new Version("1.6.0_21");
    Assert.assertTrue(version16121.compareTo(version16021) > 0);
  }

  @Test
  public void version_compare_jdk_minor() throws Exception {
    final Version version1701 = new Version("1.7.0_1");
    final Version version16525 = new Version("1.6.5_21");
    Assert.assertTrue(version1701.compareTo(version16525) > 0);
  }

  @Test
  public void version_compare_jdk_major() throws Exception {
    final Version version1701 = new Version("1.7.0_1");
    final Version version26525 = new Version("2.6.5_21");
    Assert.assertTrue(version1701.compareTo(version26525) < 0);
  }

  @Test
  public void version_matcher() throws Exception {
    //match exactly
    final Version version16023 = new Version("1.6.0_23");
    Assert.assertTrue(version16023.matches(version16023));
    //match pattern
    final Version version160 = new Version("1.6.0");
    final Version version16021 = new Version("1.6.0_21");
    Assert.assertTrue(version160.matches(version16021));
    //reverse match 
    Assert.assertTrue(version16021.matches(version160));
    //doesn't match
    final Version version15422 = new Version("1.5.4_22");
    final Version version16020 = new Version("1.6.0_20");
    Assert.assertFalse(version15422.matches(version16020));
  }
  
  @Test
  public void version_eclipse_style() throws Exception {
    final Version version = new Version("2.2.0.v20100429");
    Assert.assertNotNull(version);
    Assert.assertEquals("2", version.getMajor());
    Assert.assertEquals("2", version.getMinor());
    Assert.assertEquals("0", version.getMicro());
    Assert.assertEquals("", version.getUpdate());    
    Assert.assertEquals("v20100429", version.getBuild());
  }

  @Test
  public void version_eclipse_style2() throws Exception {
    final Version version = new Version("2.0.1.R36x_v20100823");
    Assert.assertNotNull(version);
    Assert.assertEquals("2", version.getMajor());
    Assert.assertEquals("0", version.getMinor());
    Assert.assertEquals("1", version.getMicro());
    Assert.assertEquals("", version.getUpdate());    
    Assert.assertEquals("R36x_v20100823", version.getBuild());
  }
  
}
