All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.distributed.internal.deadlock.DependencyGraphJUnitTest Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * 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. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.distributed.internal.deadlock;

import java.util.HashSet;
import java.util.Set;

import junit.framework.TestCase;

/**
 * @author dsmith
 *
 */
public class DependencyGraphJUnitTest extends TestCase {
  
  public void testFindCycle() {
    
    DependencyGraph graph = new DependencyGraph();
    graph.addEdge(new Dependency("A", "B"));
    graph.addEdge(new Dependency("A", "F"));
    graph.addEdge(new Dependency("B", "C"));
    graph.addEdge(new Dependency("B", "D"));
    graph.addEdge(new Dependency("B", "E"));
    graph.addEdge(new Dependency("E", "A"));
    
    Set expected = new HashSet();
    expected.add(new Dependency("A", "B"));
    expected.add(new Dependency("B", "E"));
    expected.add(new Dependency("E", "A"));
    assertEquals(expected, new HashSet(graph.findCycle()));
  }
  
  public void testSubGraph() {
    
    DependencyGraph graph = new DependencyGraph();
    graph.addEdge(new Dependency("A", "B"));
    graph.addEdge(new Dependency("B", "C"));
    graph.addEdge(new Dependency("C", "A"));
    graph.addEdge(new Dependency("E", "F"));
    graph.addEdge(new Dependency("F", "G"));
    
    DependencyGraph sub1 = graph.getSubGraph("B");
    Set expected = new HashSet();
    expected.add(new Dependency("A", "B"));
    expected.add(new Dependency("B", "C"));
    expected.add(new Dependency("C", "A"));
    assertEquals(expected, new HashSet(sub1.findCycle()));
    assertEquals(expected, new HashSet(sub1.getEdges()));
    
    DependencyGraph sub2 = graph.getSubGraph("E");
    assertEquals(null, sub2.findCycle());
  }
  
  public void testTwoPaths() {
    DependencyGraph graph = new DependencyGraph();
    graph.addEdge(new Dependency("A", "B"));
    graph.addEdge(new Dependency("A", "C"));
    graph.addEdge(new Dependency("B", "D"));
    graph.addEdge(new Dependency("C", "D"));
    
    assertEquals(null, graph.findCycle());
  }
  
  public void testEmptySet() {
    DependencyGraph graph = new DependencyGraph();
    assertEquals(null, graph.findCycle());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy