/*
 * Copyright (c) 2010. All rights reserved.
 */
package ro.isdc.wro.model;

import static org.junit.Assert.assertEquals;

import java.io.InputStream;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import ro.isdc.wro.config.Context;
import ro.isdc.wro.model.factory.WroModelFactory;
import ro.isdc.wro.model.factory.XmlModelFactory;
import ro.isdc.wro.model.group.Group;
import ro.isdc.wro.model.group.InvalidGroupNameException;
import ro.isdc.wro.model.resource.Resource;


/**
 * Test class for WroModel..
 *
 * @author Alex Objelean
 * @created Created on Jan 6, 2010
 */
public class TestWroModel {
  private WroModel victim;
  private WroModelFactory factory;

  @Before
  public void setUp() {
    final Context context = Context.standaloneContext();
    Context.set(context);
    victim = buildValidModel();
  }

  @After
  public void tearDown() {
    factory.destroy();
  }

  @Test
  public void testGetExistingGroup() {
    Assert.assertFalse(victim.getGroups().isEmpty());
    final Group group = victim.getGroupByName("g1");
    // create a copy of original list
    Assert.assertEquals(1, group.getResources().size());
  }


  @Test(expected = InvalidGroupNameException.class)
  public void testGetInvalidGroup() {
    Assert.assertFalse(victim.getGroups().isEmpty());
    victim.getGroupByName("INVALID_GROUP");
  }

  /**
   * @return a valid {@link WroModel} pre populated with some valid resources.
   */
  private WroModel buildValidModel() {
    factory = new XmlModelFactory() {
      @Override
      protected InputStream getModelResourceAsStream() {
        return getClass().getResourceAsStream("wro.xml");
      }
    };
    // the uriLocator factory doesn't have any locators set...
    final WroModel model = factory.create();
    return model;
  }

  @Test
  public void shouldNotReturnDuplicatedResources() {
    final WroModel model = new WroModel();

    assertEquals(0, new WroModelInspector(model).getAllUniqueResources().size());

    model.addGroup(new Group("one").addResource(Resource.create("/one.js"))).addGroup(
        new Group("two").addResource(Resource.create("/one.js")));
    assertEquals(1, new WroModelInspector(model).getAllUniqueResources().size());
  }

}

@@ -145,16 +145,41 @@
 tStream;
+%0Aimport java.util.Arrays;
 %0A%0Aimport
@@ -1210,17 +1210,16 @@
 );%0A  %7D%0A%0A
-%0A
   @Test(
@@ -2277,10 +2277,496 @@
 );%0A  %7D%0A%0A
+  @Test(expected = NullPointerException.class)%0A  public void cannotMergeNullModel() %7B%0A    victim.merge(null);%0A  %7D%0A%0A  @Test%0A  public void shouldMergeEmptyModel() %7B%0A    victim.merge(new WroModel());%0A    assertEquals(buildValidModel(), victim);%0A  %7D%0A%0A  @Test%0A  public void shouldMergeNotEmptyModel() %7B%0A    victim.merge(new WroModel().addGroup(new Group(%22anEmptyGroup%22)));%0A    assertEquals(Arrays.asList(%22anEmptyGroup%22, %22g1%22, %22g2%22, %22g3%22), new WroModelInspector(victim).getGroupNames());%0A  %7D%0A
 %7D%0A

add a couple of new tests for WroModel class