// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.google.dart.compiler;

import junit.framework.TestCase;

import java.net.URI;

public class SystemLibraryManagerTest extends TestCase {
  /**
   * For FS based {@link URI} the path is not <code>null</code>, for JAR {@link URI} the scheme
   * specific part is not <code>null</code>.
   * 
   * @return the scheme specific path.
   */
  private static String getPath(URI uri) {
    if (uri.getPath() != null) {
      return uri.getPath();
    }
    return uri.getSchemeSpecificPart();
  }

  SystemLibraryManager systemLibraryManager = new SystemLibraryManager();

  public void testExpand1() throws Exception {
    URI shortUri = new URI("dart:core");
    URI fullUri = systemLibraryManager.expandRelativeDartUri(shortUri);
    assertNotNull(fullUri);
    assertEquals("dart", fullUri.getScheme());
    assertEquals("core", fullUri.getHost());
    assertTrue(getPath(fullUri).endsWith("/core_runtime.dart"));
  }

  public void testExpand2() throws Exception {
    URI shortUri = new URI("dart:coreimpl");
    URI fullUri = systemLibraryManager.expandRelativeDartUri(shortUri);
    assertNotNull(fullUri);
    assertEquals("dart", fullUri.getScheme());
    assertEquals("coreimpl", fullUri.getHost());
    assertTrue(getPath(fullUri).endsWith("/coreimpl_runtime.dart"));
  }

  public void testExpand3() throws Exception {
    URI shortUri = new URI("dart:coreimpl");
    URI fullUri1 = systemLibraryManager.expandRelativeDartUri(shortUri);
    URI fullUri2 = systemLibraryManager.expandRelativeDartUri(fullUri1);
    assertNotNull(fullUri2);
    assertEquals("dart", fullUri2.getScheme());
    assertEquals("coreimpl", fullUri2.getHost());
    assertTrue(getPath(fullUri2).endsWith("/coreimpl_runtime.dart"));
  }

  public void testExpand4() throws Exception {
    URI shortUri = new URI("dart:doesnotexist");
    URI fullUri = systemLibraryManager.expandRelativeDartUri(shortUri);
    assertNull(fullUri);
  }

  public void testTranslate1() throws Exception {
    URI shortUri = new URI("dart:core");
    URI fullUri = systemLibraryManager.expandRelativeDartUri(shortUri);
    URI translatedURI = systemLibraryManager.resolveDartUri(fullUri);
    assertNotNull(translatedURI);
    String scheme = translatedURI.getScheme();
    assertTrue(scheme.equals("file"));
    assertTrue(getPath(translatedURI).endsWith("/core_runtime.dart"));
  }

  public void testTranslate2() throws Exception {
    URI shortUri = new URI("dart:coreimpl");
    URI fullUri = systemLibraryManager.expandRelativeDartUri(shortUri);
    URI translatedURI = systemLibraryManager.resolveDartUri(fullUri);
    assertNotNull(translatedURI);
    String scheme = translatedURI.getScheme();
    assertTrue(scheme.equals("file"));
    assertTrue(getPath(translatedURI).endsWith("/coreimpl_runtime.dart"));
  }

  public void testTranslate3() throws Exception {
    URI fullUri = new URI("dart://doesnotexist/some/file.dart");
    URI translatedURI = systemLibraryManager.resolveDartUri(fullUri);
    assertNotNull(translatedURI);
    String scheme = translatedURI.getScheme();
    assertTrue(scheme.equals("file"));
    assertTrue(getPath(translatedURI).endsWith("some/file.dart"));
  }
}

@@ -626,18 +626,48 @@
  != null
-)
  
+&& uri.getPath().length() != 0)
 %7B%0A      
@@ -3406,10 +3406,1256 @@
 ));%0A  %7D%0A
+  %0A  public void testPackageExpand1() throws Exception %7B%0A    URI shortUri = new URI(%22package:test.dart%22);%0A    URI fullUri = systemLibraryManager.expandRelativeDartUri(shortUri);%0A    assertNotNull(fullUri);%0A    assertEquals(%22package%22, fullUri.getScheme());%0A    assertEquals(%22test.dart%22, fullUri.getHost());%0A    assertTrue(getPath(fullUri).endsWith(%22/test.dart%22));%0A  %7D%0A%0A  public void testPackageExpand2() throws Exception %7B%0A    URI shortUri = new URI(%22package:test.dart%22);%0A    URI fullUri1 = systemLibraryManager.expandRelativeDartUri(shortUri);%0A    URI fullUri2 = systemLibraryManager.expandRelativeDartUri(fullUri1);%0A    assertNotNull(fullUri2);%0A    assertEquals(%22package%22, fullUri2.getScheme());%0A    assertEquals(%22test.dart%22, fullUri2.getHost());%0A    assertTrue(getPath(fullUri2).endsWith(%22/test.dart%22));%0A  %7D%0A%0A  public void testPackageTranslate1() throws Exception %7B%0A    URI shortUri = new URI(%22package:test.dart%22);%0A    URI fullUri = systemLibraryManager.expandRelativeDartUri(shortUri);%0A    URI translatedURI = systemLibraryManager.resolveDartUri(fullUri);%0A    assertNotNull(translatedURI);%0A    String scheme = translatedURI.getScheme();%0A    assertTrue(scheme.equals(%22file%22));%0A    assertTrue(getPath(translatedURI).endsWith(%22/test.dart%22));%0A  %7D%0A
 %7D%0A

add tests for package uri's Review URL: http://codereview.chromium.org//10826123