001 /** 002 * Copyright 2005-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krms.api.repository; 017 018 import org.junit.Assert; 019 import org.junit.Test; 020 import org.kuali.rice.krms.api.repository.reference.ReferenceObjectBinding; 021 import javax.xml.bind.JAXBContext; 022 import javax.xml.bind.Marshaller; 023 import javax.xml.bind.Unmarshaller; 024 import java.io.StringReader; 025 import java.io.StringWriter; 026 027 /** 028 * @author Kuali Rice Team (rice.collab@kuali.org) 029 * 030 */ 031 public final class ReferenceObjectBindingGenTest { 032 033 private final static String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" 034 + "<referenceObjectBinding xmlns=\"http://rice.kuali.org/krms/v2_0\">\n" 035 + " <krmsDiscriminatorType>KRMS_DISCRIMINATOR_TYPE</krmsDiscriminatorType>\n" 036 + " <krmsObjectId>KRMS_OBJECT_ID</krmsObjectId>\n" 037 + " <namespace>NAMESPACE</namespace>\n" 038 + " <referenceDiscriminatorType>REFERENCE_DISCRIMINATOR_TYPE</referenceDiscriminatorType>\n" 039 + " <referenceObjectId>REFERENCE_OBJECT_ID</referenceObjectId>\n" 040 + " <id>ID</id>\n" 041 + " <active>false</active>\n" 042 + "</referenceObjectBinding>"; 043 private final static boolean ACTIVE = false; 044 private final static String COLLECTION_NAME = "COLLECTION_NAME"; 045 private final static String ID = "ID"; 046 private final static String KRMS_DISCRIMINATOR_TYPE = "KRMS_DISCRIMINATOR_TYPE"; 047 private final static String KRMS_OBJECT_ID = "KRMS_OBJECT_ID"; 048 private final static String NAMESPACE = "NAMESPACE"; 049 private final static String REFERENCE_DISCRIMINATOR_TYPE = "REFERENCE_DISCRIMINATOR_TYPE"; 050 private final static String REFERENCE_OBJECT_ID = "REFERENCE_OBJECT_ID"; 051 private final static Long VERSION_NUMBER = 0L; 052 053 @Test(expected = java.lang.IllegalArgumentException.class) 054 public void test_ReferenceObjectBinding_Builder_create_fail_all_null() { 055 ReferenceObjectBinding.Builder.create(null, null, null, null, null); 056 } 057 058 @Test 059 public void test_ReferenceObjectBinding_set_validation_id_success_null() { 060 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 061 builder.setId(null); 062 } 063 064 @Test(expected = java.lang.IllegalArgumentException.class) 065 public void test_ReferenceObjectBinding_set_validation_id_fail_empty() { 066 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 067 builder.setId(""); 068 } 069 070 @Test(expected = java.lang.IllegalArgumentException.class) 071 public void test_ReferenceObjectBinding_set_validation_id_fail_whitespace() { 072 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 073 builder.setId(" "); 074 } 075 076 @Test(expected = java.lang.IllegalArgumentException.class) 077 public void test_ReferenceObjectBinding_set_validation_krmsDiscriminatorType_fail_null() { 078 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 079 builder.setKrmsDiscriminatorType(null); 080 } 081 082 @Test(expected = java.lang.IllegalArgumentException.class) 083 public void test_ReferenceObjectBinding_set_validation_krmsDiscriminatorType_fail_empty() { 084 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 085 builder.setKrmsDiscriminatorType(""); 086 } 087 088 @Test(expected = java.lang.IllegalArgumentException.class) 089 public void test_ReferenceObjectBinding_set_validation_krmsDiscriminatorType_fail_whitespace() { 090 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 091 builder.setKrmsDiscriminatorType(" "); 092 } 093 094 @Test(expected = java.lang.IllegalArgumentException.class) 095 public void test_ReferenceObjectBinding_set_validation_krmsObjectId_fail_null() { 096 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 097 builder.setKrmsObjectId(null); 098 } 099 100 @Test(expected = java.lang.IllegalArgumentException.class) 101 public void test_ReferenceObjectBinding_set_validation_krmsObjectId_fail_empty() { 102 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 103 builder.setKrmsObjectId(""); 104 } 105 106 @Test(expected = java.lang.IllegalArgumentException.class) 107 public void test_ReferenceObjectBinding_set_validation_krmsObjectId_fail_whitespace() { 108 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 109 builder.setKrmsObjectId(" "); 110 } 111 112 @Test(expected = java.lang.IllegalArgumentException.class) 113 public void test_ReferenceObjectBinding_set_validation_namespace_fail_null() { 114 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 115 builder.setNamespace(null); 116 } 117 118 @Test(expected = java.lang.IllegalArgumentException.class) 119 public void test_ReferenceObjectBinding_set_validation_namespace_fail_empty() { 120 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 121 builder.setNamespace(""); 122 } 123 124 @Test(expected = java.lang.IllegalArgumentException.class) 125 public void test_ReferenceObjectBinding_set_validation_namespace_fail_whitespace() { 126 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 127 builder.setNamespace(" "); 128 } 129 130 @Test(expected = java.lang.IllegalArgumentException.class) 131 public void test_ReferenceObjectBinding_set_validation_referenceDiscriminatorType_fail_null() { 132 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 133 builder.setReferenceDiscriminatorType(null); 134 } 135 136 @Test(expected = java.lang.IllegalArgumentException.class) 137 public void test_ReferenceObjectBinding_set_validation_referenceDiscriminatorType_fail_empty() { 138 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 139 builder.setReferenceDiscriminatorType(""); 140 } 141 142 @Test(expected = java.lang.IllegalArgumentException.class) 143 public void test_ReferenceObjectBinding_set_validation_referenceDiscriminatorType_fail_whitespace() { 144 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 145 builder.setReferenceDiscriminatorType(" "); 146 } 147 148 @Test(expected = java.lang.IllegalArgumentException.class) 149 public void test_ReferenceObjectBinding_set_validation_referenceObjectId_fail_null() { 150 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 151 builder.setReferenceObjectId(null); 152 } 153 154 @Test(expected = java.lang.IllegalArgumentException.class) 155 public void test_ReferenceObjectBinding_set_validation_referenceObjectId_fail_empty() { 156 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 157 builder.setReferenceObjectId(""); 158 } 159 160 @Test(expected = java.lang.IllegalArgumentException.class) 161 public void test_ReferenceObjectBinding_set_validation_referenceObjectId_fail_whitespace() { 162 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 163 builder.setReferenceObjectId(" "); 164 } 165 166 @Test 167 public void test_ReferenceObjectBinding_Builder_create() { 168 ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 169 } 170 171 @Test 172 public void test_ReferenceObjectBinding_Builder_create_and_build() { 173 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 174 builder.build(); 175 } 176 177 @Test 178 public void test_ReferenceObjectBinding_xml_marshaling() 179 throws Exception 180 { 181 ReferenceObjectBinding referenceObjectBinding = buildFullReferenceObjectBinding(); 182 183 assertXmlMarshaling(referenceObjectBinding, XML); 184 } 185 186 public void assertXmlMarshaling(Object referenceObjectBinding, String expectedXml) 187 throws Exception 188 { 189 JAXBContext jc = JAXBContext.newInstance(ReferenceObjectBinding.class); 190 191 Marshaller marshaller = jc.createMarshaller(); 192 StringWriter stringWriter = new StringWriter(); 193 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 194 // marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", new CustomNamespacePrefixMapper()); 195 marshaller.marshal(referenceObjectBinding, stringWriter); 196 String xml = stringWriter.toString(); 197 198 // System.out.println(xml); // run test, paste xml output into XML, comment out this line. 199 200 Unmarshaller unmarshaller = jc.createUnmarshaller(); 201 Object actual = unmarshaller.unmarshal(new StringReader(xml)); 202 Object expected = unmarshaller.unmarshal(new StringReader(expectedXml)); 203 Assert.assertEquals(expected, actual); 204 } 205 206 public static ReferenceObjectBinding buildFullReferenceObjectBinding() { 207 ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(KRMS_DISCRIMINATOR_TYPE, KRMS_OBJECT_ID, NAMESPACE, REFERENCE_DISCRIMINATOR_TYPE, REFERENCE_OBJECT_ID); 208 builder.setId(ID); 209 ReferenceObjectBinding referenceObjectBinding = builder.build(); 210 return referenceObjectBinding; 211 } 212 213 }