Skip to content

Commit

Permalink
Improved checks for #74
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Jan 19, 2020
1 parent 7b14aad commit c346c73
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/helger/jcodemodel/JCodeModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
import com.helger.jcodemodel.meta.CodeModelBuildingException;
import com.helger.jcodemodel.meta.ErrorTypeFound;
import com.helger.jcodemodel.meta.JCodeModelJavaxLangModelAdapter;
import com.helger.jcodemodel.util.FSName;
import com.helger.jcodemodel.util.EFileSystemConvention;
import com.helger.jcodemodel.util.FSName;
import com.helger.jcodemodel.util.IFileSystemConvention;
import com.helger.jcodemodel.util.JCFilenameHelper;
import com.helger.jcodemodel.util.JCSecureLoader;
Expand Down Expand Up @@ -373,7 +373,7 @@ public boolean containsResourceDir (@Nullable final String sAbsolutePath)
return false;
// 1. unify name
final String sCleanPath = _unifyPath (sAbsolutePath);
// 2. check existance
// 2. check existence
return m_aResourceDirs.containsKey (_createFSName (sCleanPath));
}

Expand Down
21 changes: 14 additions & 7 deletions src/main/java/com/helger/jcodemodel/JPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,25 @@ public JDefinedClass _class (final int nMods,
{
final FSName aKey = _createFSName (sClassName);

// Class name unique case sensitive?
// Is the class name unique in this package?
JDefinedClass aDC = m_aClasses.get (aKey);
if (aDC != null)
throw new JClassAlreadyExistsException (aDC);

// Okay, the class name is unique inside this package
// Check if a resource with the same name already exists
final JResourceDir aRD = m_aOwner.resourceDir (m_sName.replace (SEPARATOR, JResourceDir.SEPARATOR));
if (aRD.hasResourceFile (sClassName + ".java"))
throw new JResourceAlreadyExistsException (aRD.fullChildName (sClassName + ".java"));
final String sResDirName = m_sName.replace (SEPARATOR, JResourceDir.SEPARATOR);
final JResourceDir aRD = m_aOwner.resourceDir (sResDirName);

// XXX problems caught in the NC constructor
// Check if a resource file with the same name already exists
final String sClassFilename = sClassName + ".java";
if (aRD.hasResourceFile (sClassFilename))
throw new JResourceAlreadyExistsException (aRD.fullChildName (sClassFilename));

// CHeck if a sub-directory with the same name already exists (mind the "."
// in filename - don't convert to '/' :D)
if (m_aOwner.containsResourceDir (aRD.fullChildName (sClassFilename)))
throw new JResourceAlreadyExistsException (aRD.fullChildName (sClassFilename));

// Create a new class
aDC = new JDefinedClass (this, nMods, sClassName, eClassType);
m_aClasses.put (aKey, aDC);

Expand Down
18 changes: 16 additions & 2 deletions src/main/java/com/helger/jcodemodel/JResourceDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ private FSName _createFSName (@Nonnull final String sName)
return FSName.createCaseInsensitive (sName);
}

@Nonnull
private JPackage _getMatchingPackage ()
{
return owner ()._package (m_sName.replace (SEPARATOR, JPackage.SEPARATOR));
}

/**
* Adds a new resource file to this package.
*
Expand Down Expand Up @@ -199,9 +205,8 @@ public <T extends AbstractJResourceFile> T addResourceFile (@Nonnull final T aRe
// Check if a Java class with the same name already exists
if (JCStringHelper.endsWithCaseInsensitive (sName, ".java"))
{
final JPackage aPackage = owner ()._package (m_sName.replace (SEPARATOR, JPackage.SEPARATOR));
// Cut trailing ".java"
final JDefinedClass aDC = aPackage._getClass (sName.substring (0, sName.length () - 5));
final JDefinedClass aDC = _getMatchingPackage ()._getClass (sName.substring (0, sName.length () - 5));
if (aDC != null)
throw new JClassAlreadyExistsException (aDC);
}
Expand Down Expand Up @@ -262,6 +267,15 @@ public JResourceDir subDir (@Nonnull final String sSubDirName) throws JCodeModel
if (hasResourceFile (sSubDirName))
throw new JResourceAlreadyExistsException (fullChildName (sSubDirName));

// Check if a Java class with the same name already exists
if (JCStringHelper.endsWithCaseInsensitive (sSubDirName, ".java"))
{
// Cut trailing ".java"
final JDefinedClass aDC = _getMatchingPackage ()._getClass (sSubDirName.substring (0, sSubDirName.length () - 5));
if (aDC != null)
throw new JClassAlreadyExistsException (aDC);
}

return owner ().resourceDir (isUnnamed () ? sSubDirName : m_sName + SEPARATOR + sSubDirName);
}

Expand Down
114 changes: 104 additions & 10 deletions src/test/java/com/helger/jcodemodel/JResourceDirTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.helger.jcodemodel;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -61,14 +62,15 @@ public void testDirectVsSubDir () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ();
final JResourceDir rd = cm.resourceDir ("a/b");
assertNotNull (rd);
final JResourceDir rd2 = cm.resourceDir ("a").subDir ("b");
assertSame (rd, rd2);
final JResourceDir rd3 = cm.resourceDir ("a").subDir ("b/c");
assertSame (rd2, rd3.parent ());
}

@Test
public void testFilenameFilenameCollisionLinux () throws JCodeModelException
public void testCollisionFilenameFilenameLinux () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.LINUX);
cm.resourceDir ("my").addResourceFile (JTextFile.createFully ("File1", StandardCharsets.UTF_8, "bla"));
Expand All @@ -87,7 +89,7 @@ public void testFilenameFilenameCollisionLinux () throws JCodeModelException
}

@Test
public void testFilenameFilenameCollisionWindows () throws JCodeModelException
public void testCollisionFilenameFilenameWindows () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.WINDOWS);
cm.resourceDir ("my").addResourceFile (JTextFile.createFully ("File1", StandardCharsets.UTF_8, "bla"));
Expand All @@ -114,7 +116,7 @@ public void testFilenameFilenameCollisionWindows () throws JCodeModelException
}

@Test
public void testFilenameDirNameCollisionLinux () throws JCodeModelException
public void testCollisionFilenameDirNameLinux () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.LINUX);
cm.resourceDir ("my").addResourceFile (JTextFile.createFully ("name", StandardCharsets.UTF_8, "bla"));
Expand All @@ -133,7 +135,7 @@ public void testFilenameDirNameCollisionLinux () throws JCodeModelException
}

@Test
public void testFilenameDirNameCollisionWindows () throws JCodeModelException
public void testCollisionFilenameDirNameWindows () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.WINDOWS);
cm.resourceDir ("my").addResourceFile (JTextFile.createFully ("name", StandardCharsets.UTF_8, "bla"));
Expand All @@ -160,7 +162,7 @@ public void testFilenameDirNameCollisionWindows () throws JCodeModelException
}

@Test
public void testFilenameClassNameCollisionLinux () throws JCodeModelException
public void testCollisionFilenameClassNameLinux () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.LINUX);
cm.resourceDir ("my").addResourceFile (JTextFile.createFully ("Name.java", StandardCharsets.UTF_8, "bla"));
Expand All @@ -179,7 +181,7 @@ public void testFilenameClassNameCollisionLinux () throws JCodeModelException
}

@Test
public void testFilenameClassNameCollisionWindows () throws JCodeModelException
public void testCollisionFilenameClassNameWindows () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.WINDOWS);
cm.resourceDir ("my").addResourceFile (JTextFile.createFully ("Name.java", StandardCharsets.UTF_8, "bla"));
Expand All @@ -206,7 +208,7 @@ public void testFilenameClassNameCollisionWindows () throws JCodeModelException
}

@Test
public void testClassNameFilenameCollisionLinux () throws JCodeModelException
public void testCollisionClassNameFilenameLinux () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.LINUX);
cm._package ("my")._class (JMod.PUBLIC, "Name");
Expand All @@ -225,7 +227,7 @@ public void testClassNameFilenameCollisionLinux () throws JCodeModelException
}

@Test
public void testClassNameFileameCollisionWindows () throws JCodeModelException
public void testCollisionClassNameFilenameWindows () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.WINDOWS);
cm._package ("my")._class (JMod.PUBLIC, "Name");
Expand All @@ -252,7 +254,53 @@ public void testClassNameFileameCollisionWindows () throws JCodeModelException
}

@Test
public void testDirNameFilenameCollisionLinux () throws JCodeModelException
public void testCollisionClassNameDirNameLinux () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.LINUX);
cm._package ("my")._class (JMod.PUBLIC, "Name");
try
{
// this should fail
cm.resourceDir ("my").subDir ("Name.java");
fail ();
}
catch (final JClassAlreadyExistsException ex)
{
// expected
}
// Adding a different case is totally fine
cm.resourceDir ("my").subDir ("NAme.jaVA");
}

@Test
public void testCollisionClassNameDirNameWindows () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.WINDOWS);
cm._package ("my")._class (JMod.PUBLIC, "Name");
try
{
// this should fail
cm.resourceDir ("my").subDir ("Name.java");
fail ();
}
catch (final JClassAlreadyExistsException ex)
{
// expected
}
try
{
// Adding a different case doesn't help either
cm.resourceDir ("my").subDir ("NAme.jaVA");
fail ();
}
catch (final JClassAlreadyExistsException ex)
{
// expected
}
}

@Test
public void testCollisionDirNameFilenameLinux () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.LINUX);
cm.resourceDir ("my").subDir ("name");
Expand All @@ -271,7 +319,7 @@ public void testDirNameFilenameCollisionLinux () throws JCodeModelException
}

@Test
public void testDirNameFilenameCollisionWindows () throws JCodeModelException
public void testCollisionDirNameFilenameWindows () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.WINDOWS);
cm.resourceDir ("my").subDir ("name");
Expand All @@ -296,4 +344,50 @@ public void testDirNameFilenameCollisionWindows () throws JCodeModelException
// expected
}
}

@Test
public void testCollisionDirNameClassNameLinux () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.LINUX);
cm.resourceDir ("my").subDir ("Name.java");
try
{
// should fail
cm._package ("my")._class (JMod.PUBLIC, "Name");
fail ();
}
catch (final JResourceAlreadyExistsException ex)
{
// expected
}
// Different case should work
cm._package ("my")._class (JMod.PUBLIC, "name");
}

@Test
public void testCollisionDirNameClassNameWindows () throws JCodeModelException
{
final JCodeModel cm = new JCodeModel ().setFileSystemConvention (EFileSystemConvention.WINDOWS);
cm.resourceDir ("my").subDir ("Name.java");
try
{
// should fail
cm._package ("my")._class (JMod.PUBLIC, "Name");
fail ();
}
catch (final JResourceAlreadyExistsException ex)
{
// expected
}
try
{
// Different case doesn't help either
cm._package ("my")._class (JMod.PUBLIC, "name");
fail ();
}
catch (final JResourceAlreadyExistsException ex)
{
// expected
}
}
}

0 comments on commit c346c73

Please sign in to comment.