|
6 | 6 | * %% |
7 | 7 | * Redistribution and use in source and binary forms, with or without |
8 | 8 | * modification, are permitted provided that the following conditions are met: |
9 | | - * |
| 9 | + * |
10 | 10 | * 1. Redistributions of source code must retain the above copyright notice, |
11 | 11 | * this list of conditions and the following disclaimer. |
12 | 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
13 | 13 | * this list of conditions and the following disclaimer in the documentation |
14 | 14 | * and/or other materials provided with the distribution. |
15 | | - * |
| 15 | + * |
16 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
17 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
56 | 56 | import java.io.IOException; |
57 | 57 | import java.net.URISyntaxException; |
58 | 58 | import java.net.URL; |
| 59 | +import java.nio.charset.Charset; |
59 | 60 |
|
60 | 61 | import org.apache.commons.io.IOUtils; |
61 | 62 | import org.apache.maven.enforcer.rule.api.EnforcerRuleException; |
62 | 63 | import org.junit.Before; |
63 | 64 | import org.junit.Test; |
64 | 65 |
|
65 | 66 | public class NoSubpackageDependenceRuleIntegrationTest { |
66 | | - private static final URL FITNESSE_TARGET_FOLDER = getResource("fitnesse-target"); |
67 | | - private static final URL FITNESSE_EXPECTED_OUTPUT = getResource("fitnesse-expected-output-subpackage-dependence.txt"); |
68 | | - private static final URL JUNIT_TARGET_FOLDER = getResource("junit-target"); |
69 | | - private static final URL JUNIT_EXPECTED_OUTPUT = getResource("junit-expected-output-subpackage-dependence.txt"); |
70 | | - |
71 | | - private NoSubpackageDependenceRule rule; |
72 | | - private EnforcerRuleHelperMock helper; |
| 67 | + // Subpackage dependence test files |
| 68 | + private static final URL SUBPACKAGE_DEPENDENT_TARGET_FOLDER = // |
| 69 | + getResource("subpackage-dependent-target"); |
| 70 | + private static final URL SUBPACKAGE_DEPENDENT_EXPECTED_OUTPUT = // |
| 71 | + getResource("subpackage-dependent-expected-output.txt"); |
| 72 | + // Control test files |
| 73 | + private static final URL CONTROL_TARGET_FOLDER = getResource("control-target"); |
73 | 74 |
|
74 | | - @Before |
75 | | - public void setUp() throws Exception { |
76 | | - rule = new NoSubpackageDependenceRule(); |
77 | | - helper = new EnforcerRuleHelperMock(); |
78 | | - } |
| 75 | + private NoSubpackageDependenceRule rule; |
| 76 | + private EnforcerRuleHelperMock helper; |
79 | 77 |
|
80 | | - @Test |
81 | | - public void fitnesseIntegrationTest() throws Exception { |
82 | | - assertPackageCycles(FITNESSE_TARGET_FOLDER, FITNESSE_EXPECTED_OUTPUT); |
83 | | - } |
| 78 | + @Before |
| 79 | + public void setUp() throws Exception { |
| 80 | + rule = new NoSubpackageDependenceRule(); |
| 81 | + helper = new EnforcerRuleHelperMock(); |
| 82 | + } |
84 | 83 |
|
85 | | - @Test |
86 | | - public void junitIntegrationTest() throws Exception { |
87 | | - assertPackageCycles(JUNIT_TARGET_FOLDER, JUNIT_EXPECTED_OUTPUT); |
88 | | - } |
| 84 | + /** |
| 85 | + * Test that a subpackage dependence throws an error |
| 86 | + */ |
| 87 | + @Test |
| 88 | + public void subpackageDependentIntegrationTest() throws URISyntaxException, IOException { |
| 89 | + helper.setTestClassesDir(new File("non-existent")); |
| 90 | + helper.setClassesDir(new File(SUBPACKAGE_DEPENDENT_TARGET_FOLDER.toURI())); |
| 91 | + try { |
| 92 | + rule.execute(helper); |
| 93 | + fail("expected EnforcerRuleException"); |
| 94 | + } catch (EnforcerRuleException e) { |
| 95 | + String expected = IOUtils.toString(SUBPACKAGE_DEPENDENT_EXPECTED_OUTPUT.openStream(), (Charset) null) // |
| 96 | + .replaceAll("\r", "") // |
| 97 | + .trim(); |
| 98 | + String actual = e.getMessage().trim(); |
| 99 | + assertEquals(expected, actual); |
| 100 | + } |
| 101 | + } |
89 | 102 |
|
90 | | - private void assertPackageCycles(URL targetFolder, URL expectedOutput) throws URISyntaxException, IOException { |
91 | | - helper.setTestClassesDir(new File("non-existent")); |
92 | | - helper.setClassesDir(new File(targetFolder.toURI())); |
93 | | - try { |
94 | | - rule.execute(helper); |
95 | | - fail("expected EnforcerRuleException"); |
96 | | - } catch (EnforcerRuleException e) { |
97 | | - // using assertEquals to get a nice comparison editor in eclipse |
98 | | - assertEquals(getExpectedOutput(expectedOutput), e.getMessage()); |
99 | | - } |
100 | | - } |
| 103 | + /** |
| 104 | + * Test no error thrown for no subpackage dependence |
| 105 | + */ |
| 106 | + @Test |
| 107 | + public void controlIntegrationTest() throws URISyntaxException { |
| 108 | + helper.setTestClassesDir(new File("non-existent")); |
| 109 | + helper.setClassesDir(new File(CONTROL_TARGET_FOLDER.toURI())); |
| 110 | + try { |
| 111 | + rule.execute(helper); |
| 112 | + } catch (EnforcerRuleException e) { |
| 113 | + fail("expected EnforcerRuleException"); |
| 114 | + } |
| 115 | + } |
101 | 116 |
|
102 | | - private String getExpectedOutput(URL expectedOutput) throws IOException { |
103 | | - return IOUtils.toString(expectedOutput.openStream()).replaceAll("\r", ""); |
104 | | - } |
105 | | - |
106 | | - private static URL getResource(String path) { |
107 | | - return Thread.currentThread().getContextClassLoader().getResource(path); |
108 | | - } |
| 117 | + private static URL getResource(String path) { |
| 118 | + return Thread.currentThread().getContextClassLoader().getResource(path); |
| 119 | + } |
109 | 120 | } |
| 121 | + |
0 commit comments