Package com.google.gwt.dev

Source Code of com.google.gwt.dev.MinimalRebuildCacheTest

/*
* Copyright 2014 Google Inc.
*
* 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.google.gwt.dev;

import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.dev.jjs.ast.JTypeOracle;
import com.google.gwt.thirdparty.guava.common.collect.ImmutableMap;
import com.google.gwt.thirdparty.guava.common.collect.Sets;

import junit.framework.TestCase;

import java.util.Map;

/**
* Tests for {@link MinimalRebuildCache}.
*/
public class MinimalRebuildCacheTest extends TestCase {

  private MinimalRebuildCache minimalRebuildCache;

  public void testComputeAndClearStale() {
    // These three compilation units exist.
    Map<String, Long> currentModifiedBySourcePath = new ImmutableMap.Builder<String, Long>().put(
        "Foo.java", 0L).put("Bar.java", 0L).put("Baz.java", 0L).build();
    minimalRebuildCache.recordDiskSourceResources(currentModifiedBySourcePath);

    // They each contain a type and nested type.
    minimalRebuildCache.recordNestedTypeName("Foo", "Foo");
    minimalRebuildCache.recordNestedTypeName("Foo", "Foo$Inner");
    minimalRebuildCache.recordNestedTypeName("Bar", "Bar");
    minimalRebuildCache.recordNestedTypeName("Bar", "Bar$Inner");
    minimalRebuildCache.recordNestedTypeName("Baz", "Baz");
    minimalRebuildCache.recordNestedTypeName("Baz", "Baz$Inner");

    // There's some JS for each type.
    minimalRebuildCache.setJsForType(TreeLogger.NULL, "Foo", "Some Js for Foo");
    minimalRebuildCache.setJsForType(TreeLogger.NULL, "Foo$Inner", "Some Js for Foo");
    minimalRebuildCache.setJsForType(TreeLogger.NULL, "Bar", "Some Js for Bar");
    minimalRebuildCache.setJsForType(TreeLogger.NULL, "Bar$Inner", "Some Js for Bar");
    minimalRebuildCache.setJsForType(TreeLogger.NULL, "Baz", "Some Js for Baz");
    minimalRebuildCache.setJsForType(TreeLogger.NULL, "Baz$Inner", "Some Js for Baz");

    // Record that Bar references Foo and Baz subclasses Foo.
    minimalRebuildCache.addTypeReference("Bar", "Foo");
    minimalRebuildCache.getImmediateTypeRelations().getImmediateSuperclassesByClass().put("Baz",
        "Foo");

    // Record that these types reference their inner classes.
    minimalRebuildCache.addTypeReference("Foo", "Foo$Inner");
    minimalRebuildCache.addTypeReference("Bar", "Bar$Inner");
    minimalRebuildCache.addTypeReference("Baz", "Baz$Inner");

    // In the next compile only Foo is modified.
    Map<String, Long> laterModifiedBySourcePath = new ImmutableMap.Builder<String, Long>().put(
        "Foo.java", 9999L).put("Bar.java", 0L).put("Baz.java", 0L).build();
    minimalRebuildCache.recordDiskSourceResources(laterModifiedBySourcePath);

    // Ensure the types are known to be reachable.
    minimalRebuildCache.setRootTypeNames(Sets.newHashSet("Foo", "Bar", "Baz"));
    minimalRebuildCache.computeReachableTypeNames();

    // Request clearing of cache related to stale types.
    minimalRebuildCache.computeAndClearStaleTypesCache(TreeLogger.NULL,
        new JTypeOracle(null, minimalRebuildCache, true));

    // Has the expected JS been cleared?
    assertNull(minimalRebuildCache.getJs("Foo"));
    assertNull(minimalRebuildCache.getJs("Foo$Inner"));
    assertNull(minimalRebuildCache.getJs("Bar"));
    assertNull(minimalRebuildCache.getJs("Baz"));
    assertNotNull(minimalRebuildCache.getJs("Bar$Inner"));
    assertNotNull(minimalRebuildCache.getJs("Baz$Inner"));
  }

  public void testComputeDeletedTypes() {
    // These three compilation units exist.
    Map<String, Long> currentModifiedBySourcePath = new ImmutableMap.Builder<String, Long>().put(
        "Foo.java", 0L).put("Bar.java", 0L).put("Baz.java", 0L).build();
    minimalRebuildCache.recordDiskSourceResources(currentModifiedBySourcePath);

    // They each contain a type and nested type.
    minimalRebuildCache.recordNestedTypeName("Foo", "Foo");
    minimalRebuildCache.recordNestedTypeName("Foo", "Foo$Inner");
    minimalRebuildCache.recordNestedTypeName("Bar", "Bar");
    minimalRebuildCache.recordNestedTypeName("Bar", "Bar$Inner");
    minimalRebuildCache.recordNestedTypeName("Baz", "Baz");
    minimalRebuildCache.recordNestedTypeName("Baz", "Baz$Inner");

    // In the next compile it turns out there are fewer compilation units, Baz is gone.
    Map<String, Long> laterModifiedBySourcePath =
        new ImmutableMap.Builder<String, Long>().put("Foo.java", 0L).put("Bar.java", 0L).build();
    minimalRebuildCache.recordDiskSourceResources(laterModifiedBySourcePath);

    // Is the correct deleted type set calculated?
    assertEquals(Sets.newHashSet("Baz", "Baz$Inner"),
        minimalRebuildCache.computeDeletedTypeNames());
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();

    minimalRebuildCache = new MinimalRebuildCache();
  }
}
TOP

Related Classes of com.google.gwt.dev.MinimalRebuildCacheTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.