001/* 002 * Copyright (C) Christian Schulte, 2012-22 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions 007 * are met: 008 * 009 * o Redistributions of source code must retain the above copyright 010 * notice, this list of conditions and the following disclaimer. 011 * 012 * o Redistributions in binary form must reproduce the above copyright 013 * notice, this list of conditions and the following disclaimer in 014 * the documentation and/or other materials provided with the 015 * distribution. 016 * 017 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 018 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 019 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 020 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, 021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 027 * 028 * $JOMC: ModelContextFactoryTest.java 4613 2012-09-22 10:07:08Z schulte $ 029 * 030 */ 031package org.jomc.modlet.test; 032 033import org.jomc.modlet.ModelContextFactory; 034import org.jomc.modlet.ModelContextFactoryError; 035import org.jomc.modlet.test.support.ClassCastExceptionModelContextFactory; 036import org.jomc.modlet.test.support.IllegalAccessExceptionModelContextFactory; 037import org.jomc.modlet.test.support.InstantiationExceptionModelContextFactory; 038import org.junit.Test; 039import static org.junit.Assert.assertEquals; 040import static org.junit.Assert.assertNotNull; 041import static org.junit.Assert.fail; 042 043/** 044 * Test cases for class {@code org.jomc.modlet.ModelContextFactory}. 045 * 046 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0 047 * @version $JOMC: ModelContextFactoryTest.java 4613 2012-09-22 10:07:08Z schulte $ 048 */ 049public class ModelContextFactoryTest 050{ 051 052 /** Constant for the name of the default {@code ModelContextFactory} implementation. */ 053 private static final String DEFAULT_MODEL_CONTEXT_FACTORY_CLASS_NAME = "org.jomc.modlet.DefaultModelContextFactory"; 054 055 /** Constant for the name of the system property controlling {@code ModelContextFactory} implementations. */ 056 private static final String MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY = 057 "org.jomc.modlet.ModelContextFactory"; 058 059 /** Creates a new {@code ModelContextFactoryTest} instance. */ 060 public ModelContextFactoryTest() 061 { 062 super(); 063 } 064 065 @Test 066 public final void testModelContextFactoryDefaultInstance() throws Exception 067 { 068 assertNotNull( ModelContextFactory.newInstance() ); 069 assertEquals( DEFAULT_MODEL_CONTEXT_FACTORY_CLASS_NAME, 070 ModelContextFactory.newInstance().getClass().getName() ); 071 072 assertNotNull( ModelContextFactory.newInstance( DEFAULT_MODEL_CONTEXT_FACTORY_CLASS_NAME ) ); 073 074 assertEquals( 075 DEFAULT_MODEL_CONTEXT_FACTORY_CLASS_NAME, 076 ModelContextFactory.newInstance( DEFAULT_MODEL_CONTEXT_FACTORY_CLASS_NAME ).getClass().getName() ); 077 078 } 079 080 @Test 081 public final void testModelContextFactoryClassNotFound() throws Exception 082 { 083 try 084 { 085 System.setProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY, "DOES_NOT_EXIST" ); 086 ModelContextFactory.newInstance(); 087 fail( "Expected 'ModelContextFactoryError' not thrown." ); 088 } 089 catch ( final ModelContextFactoryError e ) 090 { 091 assertNotNull( e.getMessage() ); 092 System.out.println( e.toString() ); 093 } 094 finally 095 { 096 System.clearProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY ); 097 } 098 099 try 100 { 101 ModelContextFactory.newInstance( "DOES_NOT_EXIST" ); 102 fail( "Expected 'ModelContextFactoryError' not thrown." ); 103 } 104 catch ( final ModelContextFactoryError e ) 105 { 106 assertNotNull( e.getMessage() ); 107 System.out.println( e.toString() ); 108 } 109 } 110 111 @Test 112 public final void testModelContextFactoryIllegalClass() throws Exception 113 { 114 try 115 { 116 System.setProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY, 117 ClassCastExceptionModelContextFactory.class.getName() ); 118 119 ModelContextFactory.newInstance(); 120 fail( "Expected 'ModelContextFactoryError' not thrown." ); 121 } 122 catch ( final ModelContextFactoryError e ) 123 { 124 assertNotNull( e.getMessage() ); 125 System.out.println( e.toString() ); 126 } 127 finally 128 { 129 System.clearProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY ); 130 } 131 132 try 133 { 134 ModelContextFactory.newInstance( ClassCastExceptionModelContextFactory.class.getName() ); 135 fail( "Expected 'ModelContextFactoryError' not thrown." ); 136 } 137 catch ( final ModelContextFactoryError e ) 138 { 139 assertNotNull( e.getMessage() ); 140 System.out.println( e.toString() ); 141 } 142 } 143 144 @Test 145 public final void testModelContextFactoryInstantiationException() throws Exception 146 { 147 try 148 { 149 System.setProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY, 150 InstantiationExceptionModelContextFactory.class.getName() ); 151 152 ModelContextFactory.newInstance(); 153 fail( "Expected 'ModelContextFactoryError' not thrown." ); 154 } 155 catch ( final ModelContextFactoryError e ) 156 { 157 assertNotNull( e.getMessage() ); 158 System.out.println( e.toString() ); 159 } 160 finally 161 { 162 System.clearProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY ); 163 } 164 165 try 166 { 167 ModelContextFactory.newInstance( InstantiationExceptionModelContextFactory.class.getName() ); 168 fail( "Expected 'ModelContextFactoryError' not thrown." ); 169 } 170 catch ( final ModelContextFactoryError e ) 171 { 172 assertNotNull( e.getMessage() ); 173 System.out.println( e.toString() ); 174 } 175 } 176 177 @Test 178 public final void testModelContextFactoryIllegalAccessException() throws Exception 179 { 180 try 181 { 182 System.setProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY, 183 IllegalAccessExceptionModelContextFactory.class.getName() ); 184 185 ModelContextFactory.newInstance(); 186 fail( "Expected 'ModelContextFactoryError' not thrown." ); 187 } 188 catch ( final ModelContextFactoryError e ) 189 { 190 assertNotNull( e.getMessage() ); 191 System.out.println( e.toString() ); 192 } 193 finally 194 { 195 System.clearProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY ); 196 } 197 198 try 199 { 200 ModelContextFactory.newInstance( IllegalAccessExceptionModelContextFactory.class.getName() ); 201 fail( "Expected 'ModelContextFactoryError' not thrown." ); 202 } 203 catch ( final ModelContextFactoryError e ) 204 { 205 assertNotNull( e.getMessage() ); 206 System.out.println( e.toString() ); 207 } 208 } 209 210 @Test 211 public final void testModelContextNotNull() throws Exception 212 { 213 assertNotNull( ModelContextFactory.newInstance().newModelContext() ); 214 assertNotNull( ModelContextFactory.newInstance().newModelContext( null ) ); 215 assertNotNull( ModelContextFactory.newInstance().newModelContext( this.getClass().getClassLoader() ) ); 216 } 217 218}