HierarchicalSpringUnitContextTest.java :  » Testing » springunit-0.6 » org » springunit » framework » Java Open Source

Java Open Source » Testing » springunit 0.6 
springunit 0.6 » org » springunit » framework » HierarchicalSpringUnitContextTest.java
/*
 * Copyright 2006,2007 the original author or authors.
 *
 * 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 org.springunit.framework;

import junit.framework.TestCase;

import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import org.springunit.framework.tests.OneDeepTransactionalTest;
import org.springunit.framework.tests.OneDeepUnitTest;
import org.springunit.framework.tests.ThreeDeepTransactionalTest;
import org.springunit.framework.tests.ThreeDeepUnitTest;
import org.springunit.framework.tests.TwoDeepTransactionalTest;
import org.springunit.framework.tests.TwoDeepUnitTest;

public class HierarchicalSpringUnitContextTest extends TestCase {
  
  public void testConstructor() {
    HierarchicalSpringUnitContext subject = createSubject(1, "unit");
  }
  
  public void testGetConfigLocations1() throws Exception {
    HierarchicalSpringUnitContext subject = createSubject(1, "unit");
    String[] actual = subject.getConfigLocations();
    String[] expected = new String[]{
        "classpath:OneDeepUnitTest.xml"
    };
    assertTrue(areEqual(expected, actual));
  }
  
  public void testGetConfigLocations2() throws Exception {
    HierarchicalSpringUnitContext subject = createSubject(2, "unit");
    String[] actual = subject.getConfigLocations();
    String[] expected = new String[]{
        "classpath:org/springunit/framework/tests/TwoDeepUnitTest.xml", "classpath:OneDeepUnitTest.xml"
    };
    assertTrue(areEqual(expected, actual));
  }
  
  public void testGetConfigLocations3() throws Exception {
    HierarchicalSpringUnitContext subject = createSubject(3, "unit");
    String[] actual = subject.getConfigLocations();
    String[] expected = new String[]{
        "classpath:org/springunit/framework/tests/ThreeDeepUnitTest.xml", "classpath:org/springunit/framework/tests/TwoDeepUnitTest.xml", "classpath:OneDeepUnitTest.xml"
    };
    assertTrue(areEqual(expected, actual));
  }
  
  public void testGetConfigLocations4() throws Exception {
    HierarchicalSpringUnitContext subject = createSubject(1, "transaction");
    String[] actual = subject.getConfigLocations();
    String[] expected = new String[]{
        "classpath:OneDeepTransactionalTest.xml"
    };
    assertTrue(areEqual(expected, actual));
  }
  
  public void testGetConfigLocations5() throws Exception {
    HierarchicalSpringUnitContext subject = createSubject(2, "transaction");
    String[] actual = subject.getConfigLocations();
    String[] expected = new String[]{
        "classpath:org/springunit/framework/tests/TwoDeepTransactionalTest.xml",
        "classpath:OneDeepTransactionalTest.xml"
    };
    assertTrue(areEqual(expected, actual));
  }
  
  public void testGetConfigLocations6() throws Exception {
    HierarchicalSpringUnitContext subject = createSubject(3, "transaction");
    String[] actual = subject.getConfigLocations();
    String[] expected = new String[]{
        "classpath:org/springunit/framework/tests/ThreeDeepTransactionalTest.xml",
        "classpath:org/springunit/framework/tests/TwoDeepTransactionalTest.xml",
        "classpath:OneDeepTransactionalTest.xml"
    };
    assertTrue(areEqual(expected, actual));
  }
  
  protected HierarchicalSpringUnitContext createSubject(int depth, String type) {
    AbstractDependencyInjectionSpringContextTests test = createTest(depth, type);
    HierarchicalSpringUnitContext result = new HierarchicalSpringUnitContext(test.getClass());
    return result;
  }
  
  protected class ClassTest extends SpringUnitTest {
    public ClassTest() {
    }
    public ClassTest(String fName) {
      super(fName);
    }
  }
  
  protected AbstractDependencyInjectionSpringContextTests createTest(int depth, String type) {
    if ("unit".equals(type)) {
      switch(depth) {
      case 1:
        return new OneDeepUnitTest();
      case 2:
        return new TwoDeepUnitTest();
      case 3:
        return new ThreeDeepUnitTest();
      default:
        throw new IllegalArgumentException("depth must be an integer between 1 and 3");
      }
    }
    else if ("transaction".equals(type)) {
      switch(depth) {
      case 1:
        return new OneDeepTransactionalTest();
      case 2:
        return new TwoDeepTransactionalTest();
      case 3:
        return new ThreeDeepTransactionalTest();
      default:
        throw new IllegalArgumentException("depth must be an integer between 1 and 3");
      }
    }
    else {
      throw new IllegalArgumentException("type must be either \"unit\" or \"transaction\"");
    }
  }
  
  protected boolean areEqual(String[] left, String[] right) {
    boolean result = left.length == right.length;
    if (result) {
      for (int i = 0; i < left.length; i++) {
        result &= ((left[i] == null && right[i] == null) ||
            left[i] != null && left[i].equals(right[i]));
        if (!result) {
          break;
        }
      }
    }
    return result;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.