-
Notifications
You must be signed in to change notification settings - Fork 40
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
6 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/main/java/com/openpojo/random/awt/BufferedImageRandomGenerator.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,85 @@ | ||
/* | ||
* Copyright (c) 2010-2018 Osman Shoukry | ||
* | ||
* 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 com.openpojo.random.awt; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
import com.openpojo.random.RandomGenerator; | ||
import com.openpojo.reflection.PojoClass; | ||
import com.openpojo.reflection.PojoField; | ||
import com.openpojo.reflection.construct.InstanceFactory; | ||
|
||
import static com.openpojo.reflection.impl.PojoClassFactory.getPojoClass; | ||
import static com.openpojo.reflection.java.load.ClassUtil.isClassLoaded; | ||
import static com.openpojo.reflection.java.load.ClassUtil.loadClass; | ||
|
||
/** | ||
* @author oshoukry | ||
*/ | ||
public class BufferedImageRandomGenerator implements RandomGenerator { | ||
private static final String TYPE = "java.awt.image.BufferedImage"; | ||
private static final Random RANDOM = new Random(System.currentTimeMillis()); | ||
private static final BufferedImageRandomGenerator INSTANCE = new BufferedImageRandomGenerator(); | ||
|
||
public static RandomGenerator getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
public Collection<Class<?>> getTypes() { | ||
List<Class<?>> types = new ArrayList<Class<?>>(); | ||
if (isClassLoaded(TYPE)) { | ||
types.add(loadClass(TYPE)); | ||
} | ||
return types; | ||
} | ||
|
||
public Object doGenerate(Class<?> type) { | ||
int width = RANDOM.nextInt(10) + 1; | ||
int height = RANDOM.nextInt(10) + 1; | ||
int imageType = getRandomImageType(); | ||
return InstanceFactory.getInstance(getPojoClass(loadClass(TYPE)), width, height, imageType); | ||
} | ||
|
||
private int getRandomImageType() { | ||
//public static final int TYPE | ||
List<Integer> availableTypes = new ArrayList<Integer>(); | ||
|
||
PojoClass bufferedImagePojoClass = getPojoClass(loadClass(TYPE)); | ||
|
||
for (PojoField field : bufferedImagePojoClass.getPojoFields()) { | ||
if(isPublicStaticFinalIntNamedTypeAndNotTypeCustom(field)) | ||
availableTypes.add((Integer) field.get(null)); | ||
} | ||
return availableTypes.get(RANDOM.nextInt(availableTypes.size())); | ||
} | ||
|
||
private boolean isPublicStaticFinalIntNamedTypeAndNotTypeCustom(PojoField field) { | ||
return field.isPublic() | ||
&& field.isStatic() | ||
&& field.isFinal() | ||
&& field.getType().equals(int.class) | ||
&& field.getName().startsWith("TYPE_") | ||
&& !field.getName().equals("TYPE_CUSTOM"); | ||
} | ||
|
||
private BufferedImageRandomGenerator() {} | ||
} |
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
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,48 @@ | ||
/* | ||
* Copyright (c) 2010-2018 Osman Shoukry | ||
* | ||
* 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 com.openpojo.issues.issue113; | ||
|
||
import com.openpojo.issues.issue113.sample.AClassWithBufferedImage; | ||
import com.openpojo.validation.Validator; | ||
import com.openpojo.validation.ValidatorBuilder; | ||
import com.openpojo.validation.rule.impl.GetterMustExistRule; | ||
import com.openpojo.validation.rule.impl.SetterMustExistRule; | ||
import com.openpojo.validation.test.impl.GetterTester; | ||
import com.openpojo.validation.test.impl.SetterTester; | ||
import org.junit.Test; | ||
|
||
import static com.openpojo.reflection.impl.PojoClassFactory.getPojoClass; | ||
|
||
/** | ||
* @author oshoukry | ||
*/ | ||
public class IssueTest { | ||
|
||
@Test | ||
public void testGetterAndSetter() { | ||
Validator validator = ValidatorBuilder | ||
.create() | ||
.with(new GetterMustExistRule()) | ||
.with(new GetterTester()) | ||
.with(new SetterMustExistRule()) | ||
.with(new SetterTester()) | ||
.build(); | ||
validator.validate(getPojoClass(AClassWithBufferedImage.class)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/java/com/openpojo/issues/issue113/sample/AClassWithBufferedImage.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,36 @@ | ||
/* | ||
* Copyright (c) 2010-2018 Osman Shoukry | ||
* | ||
* 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 com.openpojo.issues.issue113.sample; | ||
|
||
import java.awt.image.BufferedImage; | ||
|
||
/** | ||
* @author oshoukry | ||
*/ | ||
public class AClassWithBufferedImage { | ||
public BufferedImage getBufferedImage() { | ||
return bufferedImage; | ||
} | ||
|
||
public void setBufferedImage(BufferedImage bufferedImage) { | ||
this.bufferedImage = bufferedImage; | ||
} | ||
|
||
private BufferedImage bufferedImage; | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/java/com/openpojo/random/awt/BufferedImageRandomGeneratorTest.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,42 @@ | ||
/* | ||
* Copyright (c) 2010-2018 Osman Shoukry | ||
* | ||
* 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 com.openpojo.random.awt; | ||
|
||
import com.openpojo.random.RandomGenerator; | ||
import com.openpojo.random.generator.AbstractGeneratorTest; | ||
import com.openpojo.reflection.PojoClass; | ||
import com.openpojo.reflection.impl.PojoClassFactory; | ||
|
||
/** | ||
* @author oshoukry | ||
*/ | ||
public class BufferedImageRandomGeneratorTest extends AbstractGeneratorTest { | ||
|
||
protected PojoClass getPojoClass() { | ||
return PojoClassFactory.getPojoClass(BufferedImageRandomGenerator.class); | ||
} | ||
|
||
protected String getTypeName() { | ||
return "java.awt.image.BufferedImage"; | ||
} | ||
|
||
protected RandomGenerator getRandomGenerator() { | ||
return BufferedImageRandomGenerator.getInstance(); | ||
} | ||
} |
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