diff options
author | Irina Iancu <elenairina@google.com> | 2017-01-10 08:32:24 +0000 |
---|---|---|
committer | Marcel Hlopko <hlopko@google.com> | 2017-01-10 10:23:38 +0000 |
commit | 1a2ab00bfecafc66724f637cf58bf0066175cdba (patch) | |
tree | 77876c83c1b1d5dff2854a2d33e7604014d3e775 /src/test/junitrunner/javatests/com/google/testing/junit/runner/util | |
parent | b603417c0ba3c048cf8ea8d5d2bae04518a5d820 (diff) |
Open sourcing junitrunner tests.
--
PiperOrigin-RevId: 144053696
MOS_MIGRATED_REVID=144053696
Diffstat (limited to 'src/test/junitrunner/javatests/com/google/testing/junit/runner/util')
3 files changed, 285 insertions, 0 deletions
diff --git a/src/test/junitrunner/javatests/com/google/testing/junit/runner/util/GoogleTestSecurityManagerTest.java b/src/test/junitrunner/javatests/com/google/testing/junit/runner/util/GoogleTestSecurityManagerTest.java new file mode 100644 index 0000000000..ab0c704a2e --- /dev/null +++ b/src/test/junitrunner/javatests/com/google/testing/junit/runner/util/GoogleTestSecurityManagerTest.java @@ -0,0 +1,160 @@ +// Copyright 2002 The Bazel Authors. 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. + +package com.google.testing.junit.runner.util; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.security.Permission; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Test the GoogleTestSecurityManager. Most of the tests only works if the + * security manager is actually installed; otherwise they just + * pass without testing anything. + */ +@RunWith(JUnit4.class) +public class GoogleTestSecurityManagerTest { + private SecurityManager previousSecurityManager; + private GoogleTestSecurityManager installedSecurityManager; + + @Before + public void setUp() throws Exception { + previousSecurityManager = System.getSecurityManager(); + + // These tests assume that there isn't already a GoogleTestSecurityManager + // running. + GoogleTestSecurityManager.uninstallIfInstalled(); + } + + @After + public void tearDown() { + if (installedSecurityManager != null) { + installedSecurityManager.setEnabled(false); + } + if (System.getSecurityManager() != previousSecurityManager) { + System.setSecurityManager(previousSecurityManager); + } + } + + private void installTestSecurityManager() { + SecurityManager previousSecurityManager = System.getSecurityManager(); + assertNull(previousSecurityManager); + + installedSecurityManager = new GoogleTestSecurityManager(); + System.setSecurityManager(installedSecurityManager); + } + + @Test + public void testExit() { + installTestSecurityManager(); + + try { + System.exit(1); + fail("exit() have thrown exception; how come it didn't exit?!"); + } catch (SecurityException se) { + // passed + } + } + + @Test + public void testSetSecurityManager() { + installTestSecurityManager(); + + try { + System.setSecurityManager(new SecurityManager()); + fail("setSecurityManager() should have thrown exception."); + } catch (SecurityException se) { + // passed + } + } + + /** + * test enabling/disabling the security manager. This test does not require + * that a GoogleTestSecurityManager be installed. + */ + @Test + public void testSecurityManagerEnabled() { + // create a security manager to use, but don't install it. + GoogleTestSecurityManager sm = new GoogleTestSecurityManager(); + + assertTrue(sm.isEnabled()); + try { + sm.checkExit(0); + fail("GoogleTestSecurityManager allowed exit while enabled."); + } catch (SecurityException ex) { + // passed + } + + sm.setEnabled(false); + assertTrue(!sm.isEnabled()); + + sm.checkExit(0); // should allow + + sm.setEnabled(true); + assertTrue(sm.isEnabled()); + try { + sm.checkExit(0); + fail("GoogleTestSecurityManager allowed exit while enabled."); + } catch (SecurityException ex) { + // passed + } + } + + @Test + public void testUninstallIfInstalled_whenInstalled() { + installTestSecurityManager(); + GoogleTestSecurityManager.uninstallIfInstalled(); + + assertTrue("Security manager should be enabled", installedSecurityManager.isEnabled()); + assertNull("Security manager should be uninstalled", System.getSecurityManager()); + } + + @Test + public void testUninstallIfInstalled_whenNoSecurityManagerInstalled() { + GoogleTestSecurityManager.uninstallIfInstalled(); + + assertNull("No security manager should be uninstalled", System.getSecurityManager()); + } + + @Test + public void testUninstallIfInstalled_whenOtherSecurityManagerInstalled() { + PermissiveSecurityManager otherSecurityManager = new PermissiveSecurityManager(); + System.setSecurityManager(otherSecurityManager); + GoogleTestSecurityManager.uninstallIfInstalled(); + + assertSame(otherSecurityManager, System.getSecurityManager()); + System.setSecurityManager(null); + } + + /** + * Security manager that allows anything. + */ + private static class PermissiveSecurityManager extends SecurityManager { + @Override public void checkPermission(Permission p) { + return; + } + + @Override public void checkPermission(Permission p, java.lang.Object o) { + return; + } + } +} diff --git a/src/test/junitrunner/javatests/com/google/testing/junit/runner/util/TestIntegrationsExporterTest.java b/src/test/junitrunner/javatests/com/google/testing/junit/runner/util/TestIntegrationsExporterTest.java new file mode 100644 index 0000000000..01ffb641ad --- /dev/null +++ b/src/test/junitrunner/javatests/com/google/testing/junit/runner/util/TestIntegrationsExporterTest.java @@ -0,0 +1,58 @@ +// Copyright 2011 The Bazel Authors. 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. + +package com.google.testing.junit.runner.util; + +import static org.mockito.Mockito.verify; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/** Tests for {@link TestIntegrationsExporter}. */ +@RunWith(MockitoJUnitRunner.class) +public class TestIntegrationsExporterTest { + @Mock private TestIntegrationsExporter.Callback mockCallback; + private TestIntegrationsExporter.Callback previousCallback; + + @Before + public void setThreadCallback() throws Exception { + previousCallback = TestIntegrationsRunnerIntegration.setTestCaseForThread(mockCallback); + } + + @After + public void restorePreviousThreadCallback() { + TestIntegrationsRunnerIntegration.setTestCaseForThread(previousCallback); + } + + @Test + public void testExportTestIntegration() { + final TestIntegration testIntegration = + TestIntegration.builder() + .setContactEmail("test@testmail.com") + .setComponentId("1234") + .setName("Test") + .setUrl("testurl") + .setDescription("Test description.") + .setForegroundColor("white") + .setBackgroundColor("rgb(47, 122, 243)") + .build(); + + TestIntegrationsExporter.INSTANCE.newTestIntegration(testIntegration); + verify(mockCallback).exportTestIntegration(testIntegration); + } +} diff --git a/src/test/junitrunner/javatests/com/google/testing/junit/runner/util/TestPropertyExporterTest.java b/src/test/junitrunner/javatests/com/google/testing/junit/runner/util/TestPropertyExporterTest.java new file mode 100644 index 0000000000..b467e263dc --- /dev/null +++ b/src/test/junitrunner/javatests/com/google/testing/junit/runner/util/TestPropertyExporterTest.java @@ -0,0 +1,67 @@ +// Copyright 2011 The Bazel Authors. 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. + +package com.google.testing.junit.runner.util; + +import static org.mockito.Mockito.verify; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * Tests for {@link TestPropertyExporter}. + */ +@RunWith(MockitoJUnitRunner.class) +public class TestPropertyExporterTest { + @Mock private TestPropertyExporter.Callback mockCallback; + private TestPropertyExporter.Callback previousCallback; + + @Before + public void setThreadCallback() throws Exception { + previousCallback = TestPropertyRunnerIntegration.setTestCaseForThread(mockCallback); + } + + @After + public void restorePreviousThreadCallback() throws Exception { + TestPropertyRunnerIntegration.setTestCaseForThread(previousCallback); + } + + @Test + public void testExportProperty() { + TestPropertyExporter.INSTANCE.exportProperty("propertyName", "value"); + verify(mockCallback).exportProperty("propertyName", "value"); + } + + @Test + public void testExportRepeatedProperty() { + TestPropertyExporter.INSTANCE.exportRepeatedProperty("propertyName", "value"); + verify(mockCallback).exportRepeatedProperty("propertyName", "value"); + } + + @Test + public void testExportProperty_emptyNameIsValid() { + TestPropertyExporter.INSTANCE.exportProperty(" ", "value"); + verify(mockCallback).exportProperty(" ", "value"); + } + + @Test + public void testExportRepeatedProperty_emptyNameIsValid() { + TestPropertyExporter.INSTANCE.exportRepeatedProperty(" ", "value"); + verify(mockCallback).exportRepeatedProperty(" ", "value"); + } +} |