2015-12-04 16:22:21 +01:00
/ *
The MIT License ( MIT )
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Copyright ( c ) 2015 javafx . ninja < info @javafx.ninja >
Permission is hereby granted , free of charge , to any person obtaining a copy
of this software and associated documentation files ( the " Software " ) , to deal
in the Software without restriction , including without limitation the rights
to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
copies of the Software , and to permit persons to whom the Software is
furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE .
* /
package ninja.javafx.smartcsv.validation ;
import com.typesafe.config.Config ;
import org.junit.Before ;
import org.junit.Test ;
import org.junit.runner.RunWith ;
import org.junit.runners.Parameterized ;
2015-12-17 20:44:07 +01:00
import java.util.Arrays ;
2015-12-04 16:22:21 +01:00
import java.util.Collection ;
2015-12-17 21:15:37 +01:00
import java.util.Collections ;
2015-12-17 20:44:07 +01:00
import java.util.List ;
2015-12-04 16:22:21 +01:00
import static java.util.Arrays.asList ;
2015-12-17 21:15:37 +01:00
import static java.util.Collections.singletonList ;
2015-12-04 16:22:21 +01:00
import static ninja.javafx.smartcsv.validation.ConfigMock.headerSectionConfig ;
import static org.hamcrest.Matchers.is ;
import static org.junit.Assert.assertThat ;
2015-12-17 20:44:07 +01:00
import static org.junit.Assert.assertTrue ;
2015-12-04 16:22:21 +01:00
/ * *
* unit test for header validator
* /
@RunWith ( Parameterized . class )
public class HeaderValidationTest {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// parameters
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private Config config ;
private Boolean expectedResult ;
2015-12-17 21:15:37 +01:00
private List < ValidationMessage > expectedErrors ;
2015-12-04 16:22:21 +01:00
private String [ ] headerNames ;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// subject under test
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private Validator sut ;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// parameterized constructor
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public HeaderValidationTest ( String [ ] configHeaderNames ,
String [ ] headerNames ,
Boolean expectedResult ,
2015-12-17 21:15:37 +01:00
List < ValidationMessage > expectedErrors ) {
2015-12-04 16:22:21 +01:00
this . config = headerSectionConfig ( configHeaderNames ) ;
this . headerNames = headerNames ;
this . expectedResult = expectedResult ;
2015-12-17 20:44:07 +01:00
this . expectedErrors = expectedErrors ;
2015-12-04 16:22:21 +01:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// init
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Before
public void initialize ( ) {
sut = new Validator ( config ) ;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// tests
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Test
public void validation ( ) {
// execution
2015-12-07 22:41:59 +01:00
ValidationError result = sut . isHeaderValid ( headerNames ) ;
2015-12-04 16:22:21 +01:00
// assertion
2015-12-07 22:41:59 +01:00
assertThat ( result = = null , is ( expectedResult ) ) ;
2015-12-04 16:22:21 +01:00
if ( ! expectedResult ) {
2015-12-17 20:44:07 +01:00
assertTrue ( result . getMessages ( ) . containsAll ( expectedErrors ) ) ;
2015-12-04 16:22:21 +01:00
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// parameters for tests
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Parameterized.Parameters
public static Collection validationConfigurations ( ) {
return asList ( new Object [ ] [ ] {
{ new String [ ] { } , new String [ ] { } , true , null } ,
{ new String [ ] { " a " } , new String [ ] { " a " } , true , null } ,
2015-12-17 21:15:37 +01:00
{ new String [ ] { " a " } , new String [ ] { " b " } , false , singletonList ( new ValidationMessage ( " header number 0 does not match \" a \" should be \" b \" " ) ) } ,
{ new String [ ] { " a " } , new String [ ] { " a " , " b " } , false , singletonList ( new ValidationMessage ( " number of headers is not correct! there are 2 but there should be 1 " ) ) } ,
{ new String [ ] { " a " , " b " } , new String [ ] { " b " , " a " } , false , asList ( new ValidationMessage ( " header number 0 does not match \" a \" should be \" b \" " ) , new ValidationMessage ( " header number 1 does not match \" b \" should be \" a \" " ) ) }
2015-12-04 16:22:21 +01:00
} ) ;
}
}