1515package org .lesscss ;
1616
1717import org .apache .commons .io .FileUtils ;
18+ import org .apache .commons .io .IOUtils ;
1819import org .apache .commons .lang3 .reflect .FieldUtils ;
1920import org .junit .Before ;
2021import org .junit .Test ;
2122import org .junit .runner .RunWith ;
23+ import org .mockito .AdditionalMatchers ;
24+ import org .mockito .Matchers ;
2225import org .mockito .Mock ;
26+ import org .mockito .Mockito ;
2327import org .powermock .core .classloader .annotations .PrepareForTest ;
2428import org .powermock .modules .junit4 .PowerMockRunner ;
2529
2630import java .io .File ;
2731import java .io .FileInputStream ;
2832import java .io .IOException ;
33+ import java .io .InputStream ;
2934import java .net .URL ;
3035import java .nio .charset .Charset ;
3136import java .util .LinkedHashMap ;
3742import static org .junit .matchers .JUnitMatchers .containsString ;
3843import static org .powermock .api .mockito .PowerMockito .*;
3944
40- @ PrepareForTest ({FileUtils .class , LessSource .class })
45+ @ PrepareForTest ({FileUtils .class , IOUtils . class , LessSource . class , FileResource .class })
4146@ RunWith (PowerMockRunner .class )
4247public class LessSourceTest {
4348
4449 private LessSource lessSource ;
45-
50+
4651 @ Mock private File file ;
52+ File sourceFile = null ;
4753 @ Mock private FileInputStream fileInputStream ;
4854
4955 @ Mock private LessSource import1 ;
@@ -60,25 +66,29 @@ public void setUp() throws Exception {
6066 imports .put ("import1" , import1 );
6167 imports .put ("import2" , import2 );
6268 imports .put ("import3" , import3 );
69+
70+ URL sourceUrl = getClass ().getResource ("/compatibility/a_source.less" );
71+ sourceFile = new File (sourceUrl .getFile ());
6372 }
6473
6574 @ Test
6675 public void testNewLessSourceWithoutImports () throws Exception {
67- mockFile (true ,"content" ,"absolutePath" );
6876
69- lessSource = new LessSource (new FileResource (file ));
77+ FileResource fileResource = new FileResource (sourceFile );
78+
79+ lessSource = new LessSource (fileResource );
7080
71- assertEquals ("absolutePath" , lessSource .getAbsolutePath ());
81+ assertEquals (sourceFile . getAbsolutePath () , lessSource .getAbsolutePath ());
7282 assertEquals ("content" , lessSource .getContent ());
7383 assertEquals ("content" , lessSource .getNormalizedContent ());
74- assertEquals (lastModified , lessSource .getLastModified ());
75- assertEquals (lastModified , lessSource .getLastModifiedIncludingImports ());
84+ assertEquals (sourceFile . lastModified () , lessSource .getLastModified ());
85+ assertEquals (sourceFile . lastModified () , lessSource .getLastModifiedIncludingImports ());
7686 assertEquals (0 , lessSource .getImports ().size ());
7787
7888 verifyStatic ();
79- FileUtils .readFileToString (file );
89+ FileUtils .readFileToString (sourceFile );
8090 }
81-
91+
8292 @ Test (expected = IllegalArgumentException .class )
8393 public void testNewLessSourceFileNull () throws Exception {
8494 lessSource = new LessSource ((Resource )null );
@@ -133,19 +143,21 @@ public void testWithBadEncodingLessFile() throws Exception {
133143 private String readLessSourceWithEncoding (String encoding ) throws IOException , IllegalAccessException {
134144 URL sourceUrl = getClass ().getResource ("/compatibility/utf8-content.less" );
135145 File sourceFile = new File (sourceUrl .getFile ());
136- LessSource lessSource = new LessSource (new FileResource (file ), Charset .forName (encoding ));
146+ LessSource lessSource = new LessSource (new FileResource (sourceFile ), Charset .forName (encoding ));
137147 return (String ) FieldUtils .readField (lessSource , "content" , true );
138148 }
139149
140150
141- private File mockFile (boolean fileExists , String content , String absolutePath ) throws Exception , IOException {
151+ private void mockFile (boolean fileExists , String content , String absolutePath ) throws Exception , IOException {
142152 when (file .exists ()).thenReturn (fileExists );
143153 mockStatic (FileUtils .class );
144154 when (FileUtils .readFileToString (file )).thenReturn (content );
145155 when (file .getAbsolutePath ()).thenReturn (absolutePath );
146156 when (file .lastModified ()).thenReturn (lastModified );
147157 when (file .getParent ()).thenReturn ("folder" );
158+ mockStatic (IOUtils .class );
159+ when (IOUtils .toString ((InputStream ) Mockito .anyObject (), (Charset )Mockito .anyObject ())).thenReturn (content );
160+ when (IOUtils .toString ((InputStream ) Mockito .anyObject (), (String ) Mockito .anyObject ())).thenReturn (content );
148161 whenNew (FileInputStream .class ).withArguments (file ).thenReturn (fileInputStream );
149- return file ;
150162 }
151163}
0 commit comments