- 
                Notifications
    You must be signed in to change notification settings 
- Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
  
- Loading branch information
      Showing
      4 changed files
      with
      141 additions
      and
      0 deletions.
    
  
  There are no files selected for viewing
        
          
            73 changes: 73 additions & 0 deletions
          
          73 
        
  src/main/java/uk/org/iay/incommon/mda/validate/string/AcceptStringValueValidator.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * 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 uk.org.iay.incommon.mda.validate.string; | ||
|  | ||
| import javax.annotation.Nonnull; | ||
|  | ||
| import net.shibboleth.metadata.Item; | ||
| import net.shibboleth.metadata.validate.BaseValidator; | ||
| import net.shibboleth.metadata.validate.Validator; | ||
| import net.shibboleth.utilities.java.support.annotation.constraint.NonnullAfterInit; | ||
| import net.shibboleth.utilities.java.support.component.ComponentInitializationException; | ||
|  | ||
| /** | ||
| * A <code>Validator</code> that accepts a fixed {@link String} value. | ||
| * | ||
| * This validator returns {@link net.shibboleth.metadata.validate.Validator.Action#DONE} | ||
| * if the value is matched, thus terminating any validator sequence. | ||
| */ | ||
| public class AcceptStringValueValidator extends BaseValidator implements Validator<String> { | ||
|  | ||
| /** Value to be accepted by this validator. */ | ||
| @NonnullAfterInit | ||
| private String value; | ||
|  | ||
| /** | ||
| * Returns the value. | ||
| * | ||
| * @return Returns the value. | ||
| */ | ||
| @NonnullAfterInit | ||
| public String getValue() { | ||
| return value; | ||
| } | ||
|  | ||
| /** | ||
| * Sets the value to be accepted. | ||
| * | ||
| * @param v the value to set. | ||
| */ | ||
| public void setValue(@Nonnull final String v) { | ||
| value = v; | ||
| } | ||
|  | ||
| @Override | ||
| public Action validate(@Nonnull final String e, @Nonnull final Item<?> item, @Nonnull final String stageId) { | ||
| if (e.equals(value)) { | ||
| return Action.DONE; | ||
| } else { | ||
| return Action.CONTINUE; | ||
| } | ||
| } | ||
|  | ||
| @Override | ||
| protected void doInitialize() throws ComponentInitializationException { | ||
| super.doInitialize(); | ||
|  | ||
| if (getValue() == null) { | ||
| throw new ComponentInitializationException("value to be matched can not be null"); | ||
| } | ||
| } | ||
| } | 
        
          
            18 changes: 18 additions & 0 deletions
          
          18 
        
  src/main/java/uk/org/iay/incommon/mda/validate/string/package-info.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|  | ||
| /** | ||
| * Validation classes for {@link java.lang.String} values. | ||
| */ | ||
| package uk.org.iay.incommon.mda.validate.string; | 
  
    
      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
        
          
            47 changes: 47 additions & 0 deletions
          
          47 
        
  src/test/java/uk/org/iay/incommon/mda/validate/string/AcceptStringValueValidatorTest.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
|  | ||
| package uk.org.iay.incommon.mda.validate.string; | ||
|  | ||
| import java.util.List; | ||
|  | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
|  | ||
| import net.shibboleth.metadata.ErrorStatus; | ||
| import net.shibboleth.metadata.Item; | ||
| import net.shibboleth.metadata.validate.Validator; | ||
| import net.shibboleth.metadata.validate.Validator.Action; | ||
| import uk.org.ukfederation.mda.MockItem; | ||
|  | ||
| public class AcceptStringValueValidatorTest { | ||
|  | ||
| @Test | ||
| public void validateMatch() throws Exception { | ||
| final AcceptStringValueValidator v = new AcceptStringValueValidator(); | ||
| v.setId("comp"); | ||
| v.setValue("match"); | ||
| v.initialize(); | ||
|  | ||
| final Item<String> item = new MockItem("content"); | ||
| final Validator.Action action = v.validate("match", item, "stage"); | ||
| Assert.assertEquals(action, Action.DONE); | ||
|  | ||
| final List<ErrorStatus> errs = item.getItemMetadata().get(ErrorStatus.class); | ||
| Assert.assertEquals(errs.size(), 0); | ||
| } | ||
|  | ||
| @Test | ||
| public void validateMismatch() throws Exception { | ||
| final AcceptStringValueValidator v = new AcceptStringValueValidator(); | ||
| v.setId("comp"); | ||
| v.setValue("match"); | ||
| v.initialize(); | ||
|  | ||
| final Item<String> item = new MockItem("content"); | ||
| final Validator.Action action = v.validate("no match", item, "stage"); | ||
| Assert.assertEquals(action, Action.CONTINUE); | ||
|  | ||
| final List<ErrorStatus> errs = item.getItemMetadata().get(ErrorStatus.class); | ||
| Assert.assertEquals(errs.size(), 0); | ||
| } | ||
|  | ||
| } |