1 package org.apache.maven.plugins.enforcer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedWriter;
23 import java.io.File;
24 import java.io.FileWriter;
25 import java.io.IOException;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
29 import org.apache.maven.plugin.testing.ArtifactStubFactory;
30
31 import junit.framework.TestCase;
32
33
34
35
36
37
38 public class TestRequireFilesSize
39 extends TestCase
40 {
41 RequireFilesSize rule = new RequireFilesSize();
42
43 public void testFileExists()
44 throws EnforcerRuleException, IOException
45 {
46 File f = File.createTempFile( "enforcer", "tmp" );
47 f.deleteOnExit();
48
49 rule.files = new File[] { f };
50
51 rule.execute( EnforcerTestUtils.getHelper() );
52
53 f.delete();
54 }
55
56 public void testEmptyFile()
57 throws EnforcerRuleException, IOException
58 {
59 rule.files = new File[] { null };
60 try
61 {
62 rule.execute( EnforcerTestUtils.getHelper() );
63 fail( "Should get exception" );
64 }
65 catch ( EnforcerRuleException e )
66 {
67 assertTrue( true );
68 }
69 }
70
71 public void testEmptyFileAllowNull()
72 throws EnforcerRuleException, IOException
73 {
74 rule.files = new File[] { null };
75 rule.allowNulls = true;
76 try
77 {
78 rule.execute( EnforcerTestUtils.getHelper() );
79 }
80 catch ( EnforcerRuleException e )
81 {
82 fail( "Unexpected Exception:" + e.getLocalizedMessage() );
83 }
84 }
85
86 public void testEmptyFileList()
87 throws EnforcerRuleException, IOException
88 {
89 rule.files = new File[] {};
90
91 assertEquals( 0, rule.files.length );
92
93 MockProject project = new MockProject();
94 File f = File.createTempFile( "enforcer", "tmp" );
95 f.deleteOnExit();
96 ArtifactStubFactory factory = new ArtifactStubFactory();
97 Artifact a = factory.getReleaseArtifact();
98 a.setFile( f );
99
100 project.setArtifact(a);
101
102
103 assertSame( f, project.getArtifact().getFile() );
104
105 rule.execute( EnforcerTestUtils.getHelper(project) );
106
107 }
108
109 public void testFileDoesNotExist()
110 throws EnforcerRuleException, IOException
111 {
112 File f = File.createTempFile( "enforcer", "tmp" );
113 f.delete();
114 assertTrue( !f.exists() );
115 rule.files = new File[] { f };
116
117 try
118 {
119 rule.execute( EnforcerTestUtils.getHelper() );
120 fail( "Should get exception" );
121 }
122 catch ( EnforcerRuleException e )
123 {
124 assertTrue( true );
125 }
126 }
127
128 public void testFileTooSmall()
129 throws EnforcerRuleException, IOException
130 {
131 File f = File.createTempFile( "enforcer", "tmp" );
132 f.deleteOnExit();
133 rule.files = new File[] { f };
134 rule.minsize = 10;
135 try
136 {
137 rule.execute( EnforcerTestUtils.getHelper() );
138 fail( "Should get exception" );
139 }
140 catch ( EnforcerRuleException e )
141 {
142 assertTrue( true );
143 }
144 }
145
146 public void testFileTooBig()
147 throws EnforcerRuleException, IOException
148 {
149 File f = File.createTempFile( "enforcer", "tmp" );
150 f.deleteOnExit();
151 try
152 {
153
154 FileWriter fstream = new FileWriter( f );
155 BufferedWriter out = new BufferedWriter( fstream );
156 out.write( "123456789101112131415" );
157
158 out.close();
159 fstream.close();
160 }
161 catch ( Exception e )
162 {
163 System.err.println( "Error: " + e.getMessage() );
164 }
165
166 rule.files = new File[] { f };
167 rule.maxsize = 10;
168 assertTrue( f.length() > 10 );
169 try
170 {
171 rule.execute( EnforcerTestUtils.getHelper() );
172 fail( "Should get exception" );
173 }
174 catch ( EnforcerRuleException e )
175 {
176 assertTrue( true );
177 }
178 }
179
180
181
182
183 public void testId()
184 {
185 rule.getCacheId();
186 }
187
188 }