/*
 * 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 com.intellij.ide.updates;


import com.intellij.openapi.updateSettings.impl.*;
import com.intellij.openapi.util.BuildNumber;
import junit.framework.Assert;
import junit.framework.TestCase;


public class UpdateStrategyTest extends TestCase {

  //could be if somebody used before previous version of IDEA
  public void testWithUndefinedSelection() {
    final TestUpdateSettings settings = new TestUpdateSettings(ChannelStatus.EAP);
    //first time load
    UpdateStrategy strategy = new UpdateStrategy(9, BuildNumber.fromString("IU-98.520"), UpdatesInfoXppParserTest.InfoReader.read("idea-same.xml"), settings);

    final CheckForUpdateResult result1 = strategy.checkForUpdates();
    Assert.assertEquals(UpdateStrategy.State.LOADED, result1.getState());
    Assert.assertNull(result1.getNewBuildInSelectedChannel());
  }


  public void testWithUserSelection() {
    //assume user has version 9 eap - and used eap channel - we want to introduce new eap
    final TestUpdateSettings settings = new TestUpdateSettings(ChannelStatus.EAP);
    //first time load
    UpdateStrategy strategy = new UpdateStrategy(9, BuildNumber.fromString("IU-95.429"), UpdatesInfoXppParserTest.InfoReader.read("idea-new9eap.xml"), settings);

    final CheckForUpdateResult result = strategy.checkForUpdates();
    Assert.assertEquals(UpdateStrategy.State.LOADED, result.getState());
    final BuildInfo update = result.getNewBuildInSelectedChannel();
    Assert.assertNotNull(update);
    Assert.assertEquals("95.627", update.getNumber().toString());
  }

  public void testIgnore() {
    //assume user has version 9 eap - and used eap channel - we want to introduce new eap
    final TestUpdateSettings settings = new TestUpdateSettings(ChannelStatus.EAP);
    settings.addIgnoredBuildNumber("95.627");
    //first time load
    UpdateStrategy strategy = new UpdateStrategy(9, BuildNumber.fromString("IU-95.429"), UpdatesInfoXppParserTest.InfoReader.read("idea-new9eap.xml"), settings);

    final CheckForUpdateResult result = strategy.checkForUpdates();
    Assert.assertEquals(UpdateStrategy.State.LOADED, result.getState());
    final BuildInfo update = result.getNewBuildInSelectedChannel();
    Assert.assertNull(update);
  }

  public void testNewChannelAppears() {
    // assume user has version 9 eap subscription (default or selected)
    // and new channel appears - eap of version 10 is there
    final TestUpdateSettings settings = new TestUpdateSettings(ChannelStatus.EAP);
    //first time load
    UpdateStrategy strategy = new UpdateStrategy(9, BuildNumber.fromString("IU-95.627"), UpdatesInfoXppParserTest.InfoReader.read("idea-newChannel.xml"), settings);


    final CheckForUpdateResult result = strategy.checkForUpdates();
    Assert.assertEquals(UpdateStrategy.State.LOADED, result.getState());
    final BuildInfo update = result.getNewBuildInSelectedChannel();
    Assert.assertNull(update);

    final UpdateChannel newChannel = result.getChannelToPropose();
    Assert.assertNotNull(newChannel);
    Assert.assertEquals("IDEA10EAP", newChannel.getId());
    Assert.assertEquals("IntelliJ IDEA X EAP", newChannel.getName());
  }

  public void testNewChannelAndNewBuildAppear() {
    //assume user has version 9 eap subscription (default or selected)
    //and new channels appears - eap of version 10 is there
    //and new build withing old channel appears also
    //we need to show only one dialog
    final TestUpdateSettings settings = new TestUpdateSettings(ChannelStatus.EAP);
    //first time load
    UpdateStrategy strategy = new UpdateStrategy(9, BuildNumber.fromString("IU-95.429"), UpdatesInfoXppParserTest.InfoReader.read("idea-newChannel.xml"), settings);

    final CheckForUpdateResult result = strategy.checkForUpdates();
    Assert.assertEquals(UpdateStrategy.State.LOADED, result.getState());
    final BuildInfo update = result.getNewBuildInSelectedChannel();
    Assert.assertNotNull(update);
    Assert.assertEquals("95.627", update.getNumber().toString());

    final UpdateChannel newChannel = result.getChannelToPropose();
    Assert.assertNotNull(newChannel);
    Assert.assertEquals("IDEA10EAP", newChannel.getId());
    Assert.assertEquals("IntelliJ IDEA X EAP", newChannel.getName());
  }
}

@@ -3734,32 +3734,699 @@
 etName());%0A  %7D%0A%0A
+  public void testNewChannelWithOlderBuild() %7B%0A    final TestUpdateSettings settings = new TestUpdateSettings(ChannelStatus.EAP);%0A    //first time load%0A    UpdateStrategy strategy = new UpdateStrategy(10, BuildNumber.fromString(%22IU-107.80%22), UpdatesInfoXppParserTest.InfoReader.read(%22idea-newChannel.xml%22), settings);%0A%0A    final CheckForUpdateResult result = strategy.checkForUpdates();%0A    Assert.assertEquals(UpdateStrategy.State.LOADED, result.getState());%0A    final BuildInfo update = result.getNewBuildInSelectedChannel();%0A    Assert.assertNull(update);%0A%0A    final UpdateChannel newChannel = result.getChannelToPropose();%0A    Assert.assertNull(newChannel);%0A  %7D%0A%0A
   public void te

test for IDEA-69430